Solutions to Advent of Code 2017 https://adventofcode.com/2017/
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.

19.py 1.1KB

1234567891011121314151617181920212223242526272829303132333435
  1. import operator
  2. def junction_route(maze, pos, original_heading):
  3. headings = [(-1, 0), (1, 0)] if original_heading[0] == 0 else [(0, -1), (0, 1)]
  4. for heading in headings:
  5. new_pos = tuple(map(operator.add, pos, heading))
  6. try:
  7. cell = maze[new_pos]
  8. if cell.isalpha() or cell in ['|', '-', '+']:
  9. return heading
  10. except (IndexError, KeyError): # Out of bounds
  11. pass
  12. with open('data/19.txt') as file:
  13. lines = file.readlines()
  14. pos, heading, letters, cell = (lines[0].index('|'), 0), (0, 1), '', '|'
  15. maze = dict(((x, y), v) for y, line in enumerate(lines) for x, v in enumerate(line))
  16. # Steps doesn't include the starting step, but does include an extra step at the end
  17. # where we step into whitespace, so balances out.
  18. steps = 0
  19. while cell != ' ':
  20. pos = tuple(map(operator.add, pos, heading))
  21. cell = maze[pos]
  22. steps += 1
  23. if cell == '+':
  24. heading = junction_route(maze, pos, heading)
  25. elif cell.isalpha():
  26. letters += cell
  27. print(f'Part one: {letters}')
  28. print(f'Part two: {steps}')