-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
183 lines (155 loc) · 3.89 KB
/
main.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
// -*- compile-command: "go run main.go ../example1.txt ../input.txt"; -*-
package main
import (
"flag"
"fmt"
"log"
"strings"
. "github.com/gmlewis/advent-of-code-2021/enum"
"github.com/gmlewis/advent-of-code-2021/must"
)
var logf = log.Printf
var printf = fmt.Printf
func main() {
flag.Parse()
Each(flag.Args(), process)
}
func process(filename string) {
logf("Processing %v ...", filename)
buf := must.ReadFile(filename)
puz := parsePuzzle(buf)
for i := 0; i < totalRocks; i++ {
puz.dropRock(i)
// if i < 10 {
// fmt.Printf("%v\n\n", puz)
// }
}
printf("Solution: %v\n", puz.height)
}
const (
startXOffset = 2
startYOffset = 4
chamberWidth = 7
totalRocks = 2022
)
type keyT [2]int
type puzT struct {
height int
gasIndex int
gas []rune
grid map[keyT]rune
}
// type rockFunc func(p *puzT, startX int)
type RenderRock func(p *puzT) bool
type rockFunc func(x, y int, render bool) RenderRock
type rockDef []keyT
var (
rockFuncs = []rockFunc{rock1, rock2, rock3, rock4, rock5}
rock1def = []keyT{{0, 0}, {1, 0}, {2, 0}, {3, 0}}
rock2def = []keyT{{1, 0}, {0, 1}, {1, 1}, {2, 1}, {1, 2}}
rock3def = []keyT{{0, 0}, {1, 0}, {2, 0}, {2, 1}, {2, 2}}
rock4def = []keyT{{0, 0}, {0, 1}, {0, 2}, {0, 3}}
rock5def = []keyT{{0, 0}, {0, 1}, {1, 0}, {1, 1}}
rockSizes = []keyT{{4, 1}, {3, 3}, {3, 3}, {1, 4}, {2, 2}}
)
func (p *puzT) checkRock(x, y int, rockdef rockDef, render bool) bool {
for _, k := range rockdef {
if r := p.grid[keyT{x + k[0], y + k[1]}]; r != 0 {
return false // collision
}
}
if render {
for _, k := range rockdef {
p.grid[keyT{x + k[0], y + k[1]}] = '#'
}
}
return true
}
func rock1(x, y int, render bool) RenderRock {
return func(p *puzT) bool { return p.checkRock(x, y, rock1def, render) }
}
func rock2(x, y int, render bool) RenderRock {
return func(p *puzT) bool { return p.checkRock(x, y, rock2def, render) }
}
func rock3(x, y int, render bool) RenderRock {
return func(p *puzT) bool { return p.checkRock(x, y, rock3def, render) }
}
func rock4(x, y int, render bool) RenderRock {
return func(p *puzT) bool { return p.checkRock(x, y, rock4def, render) }
}
func rock5(x, y int, render bool) RenderRock {
return func(p *puzT) bool { return p.checkRock(x, y, rock5def, render) }
}
func (p *puzT) dropSpecificRock(rockSize keyT, fn rockFunc) {
x, y := startXOffset, p.height+3
// fmt.Printf("rock starts at (%v,%v)\n", x, y)
for {
dx := p.getGasDx(1)
if x+dx >= 0 && x+dx+rockSize[0] <= chamberWidth && fn(x+dx, y, false)(p) {
x += dx
// dir := "right"
// if dx < 0 {
// dir = "left"
// }
// fmt.Printf("rock moves %v to x=%v (y=%v)\n", dir, x, y)
}
if y == 0 || !fn(x, y-1, false)(p) {
break
}
y--
// fmt.Printf("rock (x=%v) drops to y=%v\n", x, y)
}
// fmt.Printf("rock comes to rest at (%v,%v)\n", x, y)
if !fn(x, y, true)(p) {
log.Fatalf("Programming error: dropSpecificRock: fn(%v,%v,true)=false, puz:\n%v", x, y, p)
}
newHeight := y + rockSize[1]
if newHeight > p.height {
p.height = newHeight
}
}
func (p *puzT) dropRock(n int) {
rockNum := n % 5
p.dropSpecificRock(rockSizes[rockNum], rockFuncs[rockNum])
}
func (p *puzT) getGasDx(n int) int {
var dx int
for i := 0; i < n; i++ {
r := p.gas[p.gasIndex]
p.gasIndex = (p.gasIndex + 1) % len(p.gas)
switch r {
case '>':
// fmt.Printf("gas blows right\n")
dx++
case '<':
// fmt.Printf("gas blows left\n")
dx--
default:
log.Fatalf("Bad gas: '%c'", r)
}
}
return dx
}
func parsePuzzle(buf string) *puzT {
return &puzT{
gas: []rune(strings.TrimSpace(buf)),
grid: map[keyT]rune{},
}
}
func (p *puzT) String() string {
var lines []string
for y := p.height - 1; y >= 0; y-- {
line := "|"
for x := 0; x < chamberWidth; x++ {
r := p.grid[keyT{x, y}]
if r == 0 {
r = '.'
}
line += string(r)
}
line += "|"
lines = append(lines, line)
}
lines = append(lines, "+-------+")
return strings.Join(lines, "\n")
}