Skip to content

Instantly share code, notes, and snippets.

@lwiecek
Created December 19, 2015 20:35
Show Gist options
  • Save lwiecek/2de23a446033b63ae922 to your computer and use it in GitHub Desktop.
Save lwiecek/2de23a446033b63ae922 to your computer and use it in GitHub Desktop.
adventofcode - day6
import itertools
on = set()
def get_min_max(fragments):
return tuple(int(v) for v in (fragments[0] + ',' + fragments[2]).split(','))
def walk(min_x, min_y, max_x, max_y, func):
for coord in itertools.product(xrange(min_x, max_x+1), xrange(min_y, max_y+1)):
func(coord)
with open('day6_input.txt') as f:
lines = f.readlines()
for line in lines:
line = line.strip()
fragments = line.split()
if line.startswith('turn on'):
walk(*get_min_max(fragments[2:]), func=lambda c: on.add(c))
elif line.startswith('turn off'):
walk(*get_min_max(fragments[2:]), func=lambda c: on.discard(c))
elif line.startswith('toggle'):
walk(*get_min_max(fragments[1:]), func=lambda c: on.remove(c) if c in on else on.add(c))
print(len(on))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment