Skip to content
{{ message }}

Instantly share code, notes, and snippets.

# markjenkins/d3p1.py

Created Dec 11, 2016
 #!/usr/bin/env python3 # functional solution to http://adventofcode.com/2016/day/3 part 1 # Mark Jenkins from sys import stdin def possible_triangle(args): assert(len(args)==3) sum_args = sum(args) return all( (sum_args - n) > n for n in args ) print( sum( 1 for line in stdin if possible_triangle(tuple(int(s) for s in line.split()) ) ) # sum ) # print
 #!/usr/bin/env python3 # solution to http://adventofcode.com/2016/day/3 part 2 which illustrates # the use of zip() to perform column iteration # Mark Jenkins def possible_triangle(args): assert(len(args)==3) sum_args = sum(args) return all( (sum_args - n) > n for n in args ) def stdin_lines_until_eof(): try: while True: yield input() except EOFError: pass def next_n(iteration, n): result_tuple = tuple( next(iteration) for i in range(n) ) if len(result_tuple) != n: raise StopIteration() else: return result_tuple def n_at_a_time(iteration, n): while True: result = next_n(iteration, n) yield result def parsed_three_at_a_time_triangles_stdin(): return n_at_a_time( ( tuple(int(p) for p in line.split()) for line in stdin_lines_until_eof()), 3) print( sum( 1 for three_tri in parsed_three_at_a_time_triangles_stdin() for tri in zip(*three_tri) if possible_triangle(tri) ) )
to join this conversation on GitHub. Already have an account? Sign in to comment