diff --git a/docs/day5.md b/docs/day5.md index 4797b8b..70b3a2e 100644 --- a/docs/day5.md +++ b/docs/day5.md @@ -1,5 +1,5 @@ --- - +url: "https://adventofcode.com/2024/day/5" --- # Day 5: Print Queue diff --git a/docs/day6.md b/docs/day6.md new file mode 100644 index 0000000..83c000e --- /dev/null +++ b/docs/day6.md @@ -0,0 +1,112 @@ +--- +url: "https://adventofcode.com/2024/day/6" +--- + +# Day 6: Guard Gallivant + +The Historians use their fancy device again, this time to whisk you all away to the North Pole prototype suit manufacturing lab... in the year 1518! It turns out that having direct access to history is very convenient for a group of historians. + +You still have to be careful of time paradoxes, and so it will be important to avoid anyone from 1518 while The Historians search for the Chief. Unfortunately, a single guard is patrolling this part of the lab. + +Maybe you can work out where the guard will go ahead of time so that The Historians can search safely? + +You start by making a map (your puzzle input) of the situation. For example: + +```txt +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#... +``` + +The map shows the current position of the guard with `^` (to indicate the guard is currently facing up from the perspective of the map). Any obstructions - crates, desks, alchemical reactors, etc. - are shown as `#`. + +Lab guards in 1518 follow a very strict patrol protocol which involves repeatedly following these steps: + +* If there is something directly in front of you, turn right 90 degrees. +* Otherwise, take a step forward. + +Following the above protocol, the guard moves up several times until she reaches an obstacle (in this case, a pile of failed suit prototypes): + +```txt +....#..... +....^....# +.......... +..#....... +.......#.. +.......... +.#........ +........#. +#......... +......#... +``` + +Because there is now an obstacle in front of the guard, she turns right before continuing straight in her new facing direction: + +```txt +....#..... +........># +.......... +..#....... +.......#.. +.......... +.#........ +........#. +#......... +......#... +``` + +Reaching another obstacle (a spool of several very long polymers), she turns right again and continues downward: + +```txt +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#......v. +........#. +#......... +......#... +``` + +This process continues for a while, but the guard eventually leaves the mapped area (after walking past a tank of universal solvent): + +```txt +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#........ +........#. +#......... +......#v.. +``` + +By predicting the guard's route, you can determine which specific positions in the lab will be in the patrol path. Including the guard's starting position, the positions visited by the guard before leaving the area are marked with an `X`: + +```txt +....#..... +....XXXXX# +....X...X. +..#.X...X. +..XXXXX#X. +..X.X.X.X. +.#XXXXXXX. +.XXXXXXX#. +#XXXXXXX.. +......#X.. +``` + +In this example, the guard will visit 41 distinct positions on your map. + +Predict the path of the guard. How many distinct positions will the guard visit before leaving the mapped area? \ No newline at end of file diff --git a/src/day5/day5.go b/src/day5/day5.go index b82eb21..7351cb5 100644 --- a/src/day5/day5.go +++ b/src/day5/day5.go @@ -1,4 +1,4 @@ -package main +package day5 import ( "slices" diff --git a/src/day5/day5_test.go b/src/day5/day5_test.go index 46259aa..3fada0e 100644 --- a/src/day5/day5_test.go +++ b/src/day5/day5_test.go @@ -1,4 +1,4 @@ -package main +package day5 import ( "testing" diff --git a/src/day6/day6.go b/src/day6/day6.go new file mode 100644 index 0000000..453d299 --- /dev/null +++ b/src/day6/day6.go @@ -0,0 +1,125 @@ +package day6 + +import ( + "slices" + "strings" +) + +type direction uint + +const ( + Up direction = iota + Right + Down + Left +) + +type room struct { + layout [][]square + guard guard +} + +type square struct { + visited bool + obstacle bool +} + +type guard struct { + direction direction + location +} + +type location struct { + x int + y int +} + +func Solve(input string) uint { + rm := parseInput(input) + return solve(rm) +} + +func parseInput(input string) room { + var rm room + for j, l := range strings.Split(input, "\n") { + rm.layout = slices.Insert(rm.layout, j, make([]square, 0)) + for i, ch := range l { + sq := square{} + if ch == '#' { + sq.obstacle = true + } else if ch != '.' { + rm.guard = guard{ + direction: parseDirection(ch), + location: location{ + x: i, + y: j, + }, + } + sq.visited = true + } + rm.layout[j] = slices.Insert(rm.layout[j], i, sq) + } + } + return rm +} + +func parseDirection(input rune) direction { + switch input { + case '>': + return Right + case '<': + return Left + case 'v': + return Down + } + return Up +} + +func solve(rm room) uint { + var moves, unique uint = 0, 1 + nextLocation := rm.guard.location + for { + moves++ + x, y := adjustLocation(rm.guard.direction) + nextLocation.x += x + nextLocation.y += y + + if offGrid(nextLocation, rm) { + break + } + + nextSquare := rm.layout[nextLocation.y][nextLocation.x] + if nextSquare.obstacle { + rm.guard.direction += 1 + if rm.guard.direction > Left { + rm.guard.direction = Up + } + nextLocation = rm.guard.location + } else { + if !nextSquare.visited { + unique++ + rm.layout[nextLocation.y][nextLocation.x].visited = true + } + rm.guard.location = nextLocation + } + } + return unique +} + +func adjustLocation(input direction) (int, int) { + switch input { + case Right: + return 1, 0 + case Left: + return -1, 0 + case Down: + return 0, 1 + case Up: + return 0, -1 + } + return 0, 0 +} + +func offGrid(loc location, rm room) bool { + return loc.x < 0 || loc.y < 0 || loc.x >= len(rm.layout) || loc.y >= len(rm.layout[0]) +} diff --git a/src/day6/day6_test.go b/src/day6/day6_test.go new file mode 100644 index 0000000..fb8bb09 --- /dev/null +++ b/src/day6/day6_test.go @@ -0,0 +1,22 @@ +package day6 + +import ( + "testing" +) + +func TestSample(t *testing.T) { + input := `....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#...` + result := Solve(input) + if result != 41 { + t.Errorf("Calculated solution was not expected") + } +}