Advertisement
petrlos

aoc 2024/6

Dec 6th, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. #Advent of Code 2024: Day 6
  2. from icecream import ic
  3. from collections import defaultdict
  4. from datetime import datetime
  5. time_start = datetime.now()
  6.  
  7. def tuple_sum(a,b):
  8.     return tuple([x + y for x, y in zip(a,b)])
  9.  
  10. def create_grid(lines):
  11.     grid = dict()
  12.     guard = (0,0)
  13.     for r, line in enumerate(lines):
  14.         for c, char in enumerate(line):
  15.             if char == "^":
  16.                 guard = (r,c)
  17.                 grid[(r,c)] = "."
  18.             else:
  19.                 grid[(r,c)] = char
  20.     return grid, guard
  21.  
  22. def step_or_turn(guard, direction):
  23.     dr, dc = directions[direction]
  24.     r, c = guard
  25.     new_position = ((r + dr, c + dc))
  26.     if new_position in grid.keys():
  27.         if grid[new_position] != "#":
  28.             guard = new_position
  29.         else:
  30.             direction = (direction + 1) % 4
  31.     else:
  32.         guard = -1
  33.     return guard, direction
  34.  
  35. def loop_exists(start, in_directions):
  36.     result = set()
  37.     start = (8,2)
  38.     in_directions = {3}
  39.     for in_direction in in_directions:
  40.         if start == (8,2) and in_direction == 3:
  41.             print("got u")
  42.         guard = start
  43.         obsticle = tuple_sum(start, directions[in_direction])
  44.         grid[obsticle] = "#"
  45.         direction = (in_direction + 1) % 4
  46.         visited = defaultdict(set)
  47.         while guard in grid.keys():
  48.             guard, direction = step_or_turn(guard, direction)
  49.             if direction == in_direction and guard == start:
  50.                 ic(obsticle, direction, start)
  51.                 result.add(obsticle)
  52.                 break
  53.             if guard != -1:
  54.                 visited[guard].add(direction)
  55.         grid[obsticle] = "."
  56.     return result
  57.  
  58. #MAIN
  59. with open("test.txt") as file:
  60.     lines = file.read().splitlines()
  61.  
  62. grid, guard = create_grid(lines)
  63. directions = [(-1, 0), (0, 1), (1, 0), (0, -1)]  # ULDR
  64. direction = 0
  65. path = defaultdict(set) # key = coord, value = set of visited directions
  66. path[guard].add(0) #first point is also visited
  67.  
  68. while guard in grid.keys():
  69.     guard, direction = step_or_turn(guard, direction)
  70.     if guard != -1:
  71.         path[guard].add(direction)
  72. print("Part 1:", len(path.keys()))
  73.  
  74. loop_points = set()
  75. for start, in_dirs in path.items():
  76.     loop_points |= loop_exists(start, in_dirs)
  77.  
  78. print(loop_points)
  79. print(len(loop_points))
  80.  
  81.  
  82. print("Runtime:", datetime.now() - time_start)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement