question: --- Day 16: Reindeer Maze ---
  
  It's time again for the Reindeer Olympics! This year, the big event is the Reindeer Maze, where the Reindeer compete for the lowest score.
  
  You and The Historians arrive to search for the Chief right as the event is about to start. It wouldn't hurt to watch a little, right?
  
  The Reindeer start on the Start Tile (marked S) facing East and need to reach the End Tile (marked E). They can move forward one tile at a time (increasing their score by 1 point), but never into a wall (#). They can also rotate clockwise or counterclockwise 90 degrees at a time (increasing their score by 1000 points).
  
  To figure out the best place to sit, you start by grabbing a map (your puzzle input) from a nearby kiosk. For example:
  
  ###############
  #.......#....E#
  #.#.###.#.###.#
  #.....#.#...#.#
  #.###.#####.#.#
  #.#.#.......#.#
  #.#.#####.###.#
  #...........#.#
  ###.#.#####.#.#
  #...#.....#.#.#
  #.#.#.###.#.#.#
  #.....#...#.#.#
  #.###.#.#.#.#.#
  #S..#.....#...#
  ###############
  There are many paths through this maze, but taking any of the best paths would incur a score of only 7036. This can be achieved by taking a total of 36 steps forward and turning 90 degrees a total of 7 times:
  
  
  ###############
  #.......#....E#
  #.#.###.#.###^#
  #.....#.#...#^#
  #.###.#####.#^#
  #.#.#.......#^#
  #.#.#####.###^#
  #..>>>>>>>>v#^#
  ###^#.#####v#^#
  #>>^#.....#v#^#
  #^#.#.###.#v#^#
  #^....#...#v#^#
  #^###.#.#.#v#^#
  #S..#.....#>>^#
  ###############
  Here's a second example:
  
  #################
  #...#...#...#..E#
  #.#.#.#.#.#.#.#.#
  #.#.#.#...#...#.#
  #.#.#.#.###.#.#.#
  #...#.#.#.....#.#
  #.#.#.#.#.#####.#
  #.#...#.#.#.....#
  #.#.#####.#.###.#
  #.#.#.......#...#
  #.#.###.#####.###
  #.#.#...#.....#.#
  #.#.#.#####.###.#
  #.#.#.........#.#
  #.#.#.#########.#
  #S#.............#
  #################
  In this maze, the best paths cost 11048 points; following one such path would look like this:
  
  #################
  #...#...#...#..E#
  #.#.#.#.#.#.#.#^#
  #.#.#.#...#...#^#
  #.#.#.#.###.#.#^#
  #>>v#.#.#.....#^#
  #^#v#.#.#.#####^#
  #^#v..#.#.#>>>>^#
  #^#v#####.#^###.#
  #^#v#..>>>>^#...#
  #^#v###^#####.###
  #^#v#>>^#.....#.#
  #^#v#^#####.###.#
  #^#v#^........#.#
  #^#v#^#########.#
  #S#>>^..........#
  #################
  Note that the path shown above includes one 90 degree turn as the very first move, rotating the Reindeer from facing East to facing North.
  
  Analyze your map carefully. What is the lowest score a Reindeer could possibly get?
solution: # Python heap implementation
  from heapq import heappush, heappop
  
  with open("./day_16.in") as fin:
      grid = fin.read().strip().split("\n")
  
  n = len(grid)
  for i in range(n):
      for j in range(n):
          if grid[i][j] == "S":
              start = (i, j)
          elif grid[i][j] == "E":
              end = (i, j)
  
  dd = [[0, 1], [1, 0], [0, -1], [-1, 0]]
  
  # DIjkstra's
  q = [(0, 0, *start)]
  seen = set()
  while len(q) > 0:
      top = heappop(q)
      cost, d, i, j = top
      if (d, i, j) in seen:
          continue
      seen.add((d, i, j))
  
      if grid[i][j] == "#":
          continue
  
      if grid[i][j] == "E":
          print(cost)
          break
      
      ii = i + dd[d][0]
      jj = j + dd[d][1]
  
      for nbr in [(cost + 1, d, ii, jj),
                  (cost + 1000, (d + 1) % 4, i, j),
                  (cost + 1000, (d + 3) % 4, i, j)]:
          if nbr[1:] in seen:
              continue
          heappush(q, nbr)
  