Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/day5.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---

url: "https://adventofcode.com/2024/day/5"
---

# Day 5: Print Queue
Expand Down
112 changes: 112 additions & 0 deletions docs/day6.md
Original file line number Diff line number Diff line change
@@ -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?
2 changes: 1 addition & 1 deletion src/day5/day5.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package day5

import (
"slices"
Expand Down
2 changes: 1 addition & 1 deletion src/day5/day5_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package day5

import (
"testing"
Expand Down
125 changes: 125 additions & 0 deletions src/day6/day6.go
Original file line number Diff line number Diff line change
@@ -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])
}
22 changes: 22 additions & 0 deletions src/day6/day6_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
Loading