generated from devries/aoc_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.go
186 lines (159 loc) · 4.02 KB
/
solution.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package day21p2
import (
"fmt"
"io"
"aoc/utils"
)
func Solve(r io.Reader) any {
return solveSteps(r, 26501365)
}
// 3456789
// 2#6789 +2
// 1#56789 +2
// 0#456789 +2
// 123456789
// 2#456789
// 3456789
func solveSteps(r io.Reader, steps int) uint64 {
lines := utils.ReadLines(r)
garden := Maze{
positions: make(map[utils.Point]rune),
bfs: utils.NewBFS[utils.Point](),
steps: 0, // start without a step limit to get available squares
width: len(lines[0]),
height: len(lines),
}
fmt.Printf("%#v\n", garden)
for j, ln := range lines {
for i, r := range ln {
p := utils.Point{X: i, Y: j}
garden.positions[p] = r
if r == 'S' {
garden.start = p
}
}
}
// for j := 0; j < garden.width; j += 2 {
// for i := 0; i < garden.height; i += 2 {
// fmt.Printf("%c", garden.positions[utils.Point{X: i, Y: j}])
// }
// fmt.Println()
// }
// fmt.Println()
// for j := 1; j < garden.width; j += 2 {
// for i := 1; i < garden.height; i += 2 {
// fmt.Printf("%c", garden.positions[utils.Point{X: i, Y: j}])
// }
// fmt.Println()
// }
// fmt.Println()
// Get number of even and odd squares
_, err := garden.bfs.Run(garden)
if err != utils.BFSNotFound {
panic("this should end when points are exhausted")
}
var even uint64
var odd uint64
for _, d := range garden.bfs.Distance {
if d%2 == 0 {
even++
} else {
odd++
}
}
var count uint64
// How many maps fit within the total steps from S?
fmt.Println("start:", garden.start, "width:", garden.width, "height", garden.height)
fmt.Println("even:", even, "odd:", odd)
mapNumber := (steps - garden.start.X) / garden.width
additionalStepsDirect := steps - garden.start.X - mapNumber*garden.width
additionalStepsDiag := steps - garden.start.X + garden.start.Y - (mapNumber-1)*garden.width
fmt.Println("Maps:", mapNumber, "Extra:", additionalStepsDirect, "Extra diag:", additionalStepsDiag)
evenmaps := 1
oddmaps := 0
for i := 1; i <= mapNumber; i++ {
amt := i / 2
if i%2 == 0 {
evenmaps += 8 * amt
} else {
oddmaps += 8*amt + 4
}
}
fmt.Println(evenmaps, oddmaps, even*uint64(evenmaps)+odd*uint64(oddmaps))
// sp := garden.GetInitial()
// for j := 0; j < garden.width; j++ {
// for i := 0; i < garden.height; i++ {
// pos := utils.Point{X: i, Y: j}
// if pos == sp {
// fmt.Printf("S")
// continue
// }
// if garden.positions[pos] == '.' || garden.positions[pos] == 'S' {
// if stp, ok := garden.bfs.Distance[pos]; ok {
// stp = garden.bfs.Distance[pos]
// expected := uint64(sp.Add(pos.Scale(-1)).Manhattan())
// if expected != stp {
// fmt.Printf("%d", stp-expected)
// } else {
// fmt.Printf(" ")
// }
// } else {
// fmt.Printf("X")
// }
// } else {
// fmt.Printf("#")
// }
// }
// fmt.Println()
// }
// fmt.Println()
// var count uint64
// for _, d := range garden.bfs.Distance {
// if d%2 == 0 {
// count++
// }
// }
return count
}
type Maze struct {
positions map[utils.Point]rune
bfs *utils.BreadthFirstSearch[utils.Point]
steps int
width int
height int
start utils.Point
}
func (m Maze) GetInitial() utils.Point {
return m.start
}
func (m Maze) GetNeighbors(pos utils.Point) []utils.Point {
ret := []utils.Point{}
// if m.bfs.Distance[pos] < uint64(m.steps) {
// for _, dir := range utils.Directions {
// np := pos.Add(dir)
// npmapped := utils.Point{X: np.X % m.width, Y: np.Y % m.height}
// if npmapped.X < 0 {
// npmapped.X += m.width
// }
// if npmapped.Y < 0 {
// npmapped.Y += m.height
// }
// if m.positions[npmapped] == '.' || m.positions[npmapped] == 'S' {
// ret = append(ret, np)
// }
// }
// }
// Let's get distance to every point in map
if m.steps == 0 || m.bfs.Distance[pos] < uint64(m.steps) {
for _, dir := range utils.Directions {
np := pos.Add(dir)
if m.positions[np] == '.' || m.positions[np] == 'S' {
ret = append(ret, np)
}
}
}
return ret
}
func (m Maze) IsFinal(pos utils.Point) bool {
return false
}