Created
December 11, 2016 22:06
-
-
Save markjenkins/56a991a9fe3d308c0e796535853e55da to your computer and use it in GitHub Desktop.
Solutions to http://adventofcode.com/2016/day/3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# functional solution to http://adventofcode.com/2016/day/3 part 1 | |
# Mark Jenkins <mark@markjenkins.ca> | |
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 <mark@markjenkins.ca> | |
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) ) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment