Advent of code 2015 solutions https://adventofcode.com/2015/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

18.py 893B

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/python3
  2. SIZE = 100
  3. PART_TWO = True
  4. to_bool = lambda char: char == '#'
  5. get_range = lambda x: range(-1 if x > 0 else 0, 2 if x < SIZE - 1 else 1)
  6. def step_one(lights, x, y, state):
  7. n = sum(lights[y + i][x + j] for i in get_range(y) for j in get_range(x) if i != 0 or j != 0)
  8. return n == 3 or (n == 2 and state)
  9. def with_broken_lights(lights):
  10. if PART_TWO:
  11. for y, x in zip([0, 0, SIZE-1, SIZE-1], [0, SIZE-1] * 2):
  12. lights[y][x] = True
  13. return lights
  14. def step(lights):
  15. return with_broken_lights([[step_one(lights, x, y, cell) for x, cell in enumerate(line)]
  16. for y, line in enumerate(lights)])
  17. with open('18.txt', 'r') as f:
  18. lights = with_broken_lights([list(map(to_bool, line)) for line in f.read().splitlines()])
  19. for _ in range(100):
  20. lights = step(lights)
  21. print(sum(sum(line) for line in lights))