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
73 changes: 73 additions & 0 deletions docs/day18.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
url: "https://adventofcode.com/2024/day/18"
---

## Day 18: RAM Run

You and The Historians look a lot more pixelated than you remember. You're inside a computer at the North Pole!

Just as you're about to check out your surroundings, a program runs up to you. "This region of memory isn't safe! The User misunderstood what a pushdown automaton is and their algorithm is pushing whole bytes down on top of us! Run!"

The algorithm is fast - it's going to cause a byte to fall into your memory space once every nanosecond! Fortunately, you're faster, and by quickly scanning the algorithm, you create a list of which bytes will fall (your puzzle input) in the order they'll land in your memory space.

Your memory space is a two-dimensional grid with coordinates that range from `0` to `70` both horizontally and vertically. However, for the sake of example, suppose you're on a smaller grid with coordinates that range from `0` to `6` and the following list of incoming byte positions:

```txt
5,4
4,2
4,5
3,0
2,1
6,3
2,4
1,5
0,6
3,3
2,6
5,1
1,2
5,5
2,5
6,5
1,4
0,4
6,4
1,1
6,1
1,0
0,5
1,6
2,0
```

Each byte position is given as an `X,Y` coordinate, where `X` is the distance from the left edge of your memory space and `Y` is the distance from the top edge of your memory space.

You and The Historians are currently in the top left corner of the memory space (at `0,0`) and need to reach the exit in the bottom right corner (at `70,70` in your memory space, but at `6,6` in this example). You'll need to simulate the falling bytes to plan out where it will be safe to run; for now, simulate just the first few bytes falling into your memory space.

As bytes fall into your memory space, they make that coordinate corrupted. Corrupted memory coordinates cannot be entered by you or The Historians, so you'll need to plan your route carefully. You also cannot leave the boundaries of the memory space; your only hope is to reach the exit.

In the above example, if you were to draw the memory space after the first `12` bytes have fallen (using `.` for safe and `#` for corrupted), it would look like this:

```txt
...#...
..#..#.
....#..
...#..#
..#..#.
.#..#..
#.#....
```

You can take steps up, down, left, or right. After just 12 bytes have corrupted locations in your memory space, the shortest path from the top left corner to the exit would take `22` steps. Here (marked with `O`) is one such path:

```txt
OO.#OOO
.O#OO#O
.OOO#OO
...#OO#
..#OO#.
.#.O#..
#.#OOOO
```

Simulate the first kilobyte (`1024` bytes) falling onto your memory space. Afterward, what is the minimum number of steps needed to reach the exit?
123 changes: 123 additions & 0 deletions src/day18/day18.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package day18

import (
"slices"
"strconv"
"strings"
)

const MAX_THREADS_PER_LOOP = 32

type point struct {
x, y int
}

type maze struct {
walls map[point]bool
start point
end point
width int
height int
}

func (m maze) valid(p point) bool {
return p.x >= 0 && p.y >= 0 && p.x < m.width && p.y < m.height
}

type thread struct {
p point
moves uint
}

func Solve(input string, size int, square int) uint {
m := maze{width: square, height: square, start: point{0, 0}, end: point{x: square - 1, y: square - 1}, walls: make(map[point]bool)}
blocks := parseInput(input)
for i := 0; i < size; i++ {
m.walls[blocks[i]] = true
}
return solve(m)
}

func parseInput(input string) []point {
result := make([]point, 0)
for _, line := range strings.Split(input, "\n") {
d := strings.Split(line, ",")
if len(d) != 2 {
continue
}

x, _ := strconv.Atoi(d[0])
y, _ := strconv.Atoi(d[1])
result = append(result, point{x: x, y: y})
}
return result
}

func solve(m maze) uint {
seenSpaces := make(map[point]uint)
seenSpaces[m.start] = 1
threads := []thread{{p: m.start}}
coldStorage := make([]thread, 0)
var solution uint = 0
loops := 0
var threadCount uint
for {
nextThreads := make([]thread, 0)
threadCount += uint(len(threads))
for i, t := range threads {
if i >= MAX_THREADS_PER_LOOP {
coldStorage = append(coldStorage, t)
continue
} else if t.p == m.end {
if solution == 0 || t.moves < solution {
solution = t.moves
}
} else {
nextThreads = append(calcNextMoves(m, t, seenSpaces), nextThreads...)
}
}

if len(nextThreads) == 0 {
if len(coldStorage) > 0 {
x := MAX_THREADS_PER_LOOP
if len(coldStorage) < x {
x = len(coldStorage)
}
nextThreads = coldStorage[:x]
coldStorage = slices.Delete(coldStorage, 0, x)
} else {
if solution == 0 {
panic("No solution with no more spaces to check")
}
return solution
}
}
for i, t := range nextThreads {
if solution > 0 && t.moves >= solution {
nextThreads = slices.Delete(nextThreads, i, i)
}
if seenSpaces[t.p] == 0 || t.moves < seenSpaces[t.p] {
seenSpaces[t.p] = t.moves
}
}
threads = nextThreads
loops++
}
}

func calcNextMoves(m maze, curr thread, seenSpaces map[point]uint) []thread {
nextMoves := make([]thread, 0)
possibilities := []thread{{p: point{x: curr.p.x + 1, y: curr.p.y}, moves: curr.moves},
{p: point{x: curr.p.x, y: curr.p.y + 1}, moves: curr.moves},
{p: point{x: curr.p.x - 1, y: curr.p.y}, moves: curr.moves},
{p: point{x: curr.p.x, y: curr.p.y - 1}, moves: curr.moves}}
for _, t := range possibilities {
if m.valid(t.p) && !m.walls[t.p] {
t.moves++
if seenSpaces[t.p] == 0 || t.moves < seenSpaces[t.p] {
nextMoves = append(nextMoves, t)
}
}
}
return nextMoves
}
Loading
Loading