Skip to content

Instantly share code, notes, and snippets.

@markjenkins
Created December 11, 2016 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markjenkins/56a991a9fe3d308c0e796535853e55da to your computer and use it in GitHub Desktop.
Save markjenkins/56a991a9fe3d308c0e796535853e55da to your computer and use it in GitHub Desktop.
#!/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
) # 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 <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