-
Notifications
You must be signed in to change notification settings - Fork 0
/
day18.go
113 lines (92 loc) · 2.09 KB
/
day18.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//! [Day 18: RAM Run](https://adventofcode.com/2024/day/18)
package main
import (
"bufio"
"fmt"
"os"
)
type Point struct {
x, y int
}
type QueueItem struct {
pos Point
steps uint
}
func readInput(filename string) []Point {
file, err := os.Open(filename)
if err != nil {
panic(err)
}
defer file.Close()
var points []Point
scanner := bufio.NewScanner(file)
for scanner.Scan() {
var x, y int
fmt.Sscanf(scanner.Text(), "%d,%d", &x, &y)
points = append(points, Point{x, y})
}
return points
}
func findPath(memory [][]bool, memSize int) uint {
start := Point{0, 0}
end := Point{memSize - 1, memSize - 1}
if memory[start.y][start.x] || memory[end.y][end.x] {
return 0
}
seen := make([][]bool, len(memory))
for i := range seen {
seen[i] = make([]bool, len(memory[0]))
}
dirs := []Point{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
queue := []QueueItem{{start, 0}}
seen[start.y][start.x] = true
for len(queue) > 0 {
pos := queue[0]
queue = queue[1:]
if pos.pos == end {
return pos.steps
}
for _, dir := range dirs {
new_pos := Point{pos.pos.x + dir.x, pos.pos.y + dir.y}
if new_pos.x >= 0 && new_pos.x < memSize && new_pos.y >= 0 && new_pos.y < memSize &&
!memory[new_pos.y][new_pos.x] && !seen[new_pos.y][new_pos.x] {
seen[new_pos.y][new_pos.x] = true
queue = append(queue, QueueItem{new_pos, pos.steps + 1})
}
}
}
return 0
}
func main() {
inputFile := "input.txt"
if len(os.Args) >= 2 {
inputFile = os.Args[1]
}
byte_positions := readInput(inputFile)
memSize := 71
numCorruptions := 1024
// Part 1
memory := make([][]bool, memSize)
for i := range memory {
memory[i] = make([]bool, memSize)
}
for i := 0; i < numCorruptions && i < len(byte_positions); i++ {
p := byte_positions[i]
memory[p.y][p.x] = true
}
steps := findPath(memory, memSize)
fmt.Println(steps)
// Part 2
memory = make([][]bool, memSize)
for i := range memory {
memory[i] = make([]bool, memSize)
}
for _, p := range byte_positions {
memory[p.y][p.x] = true
steps := findPath(memory, memSize)
if steps == 0 {
fmt.Printf("%d,%d\n", p.x, p.y)
break
}
}
}