-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (131 loc) · 2.61 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
package main
import (
"bytes"
"fmt"
"github.com/danvolchek/AdventOfCode/lib"
"time"
)
func parse(line []byte) []bool {
line = bytes.TrimSpace(line)
return lib.Map(line, func(b byte) bool {
return b == '<'
})
}
type rock struct {
shape []Pos
height int
wind *globalWind
cave *lib.Set[Pos]
}
// 0 -> left, 1 -> right, 2 -> down
func (r *rock) valid(pos Pos) bool {
for _, off := range r.shape {
ppx := pos.x + off.x
ppy := pos.y - off.y
if ppx < 0 || ppx > 6 {
return false
}
if ppy <= -1 {
return false
}
if r.cave.Contains(Pos{x: ppx, y: ppy}) {
return false
}
}
return true
}
type globalWind struct {
pattern []bool
i int
}
func (g *globalWind) get() bool {
ret := g.pattern[g.i]
g.i = (g.i + 1) % len(g.pattern)
return ret
}
func (r *rock) Fall(highestY int) Pos {
curr := Pos{x: 2, y: highestY + 3 + r.height}
for {
dirWind := -1
if !r.wind.get() {
dirWind = 1
}
curr.x += dirWind
if ok := r.valid(curr); !ok {
curr.x -= dirWind
}
curr.y -= 1
if ok := r.valid(curr); !ok {
curr.y += 1
break
}
}
for _, off := range r.shape {
finalP := Pos{x: curr.x + off.x, y: curr.y - off.y}
r.cave.Add(finalP)
}
return curr
}
type Pos struct {
x, y int
}
func solve(lines []bool) int {
cave := &lib.Set[Pos]{}
wind := &globalWind{pattern: lines}
rocks := []*rock{
{
shape: []Pos{{x: 0}, {x: 1}, {x: 2}, {x: 3}},
height: 1,
wind: wind,
cave: cave,
},
{
shape: []Pos{{x: 1}, {x: 0, y: 1}, {x: 1, y: 1}, {x: 2, y: 1}, {x: 1, y: 2}},
height: 3,
wind: wind,
cave: cave,
},
{
shape: []Pos{{x: 2}, {x: 2, y: 1}, {x: 0, y: 2}, {x: 1, y: 2}, {x: 2, y: 2}},
height: 3,
wind: wind,
cave: cave,
},
{
shape: []Pos{{y: 0}, {y: 1}, {y: 2}, {y: 3}},
height: 4,
wind: wind,
cave: cave,
},
{
shape: []Pos{{}, {y: 1}, {x: 1}, {x: 1, y: 1}},
height: 2,
wind: wind,
cave: cave,
},
}
last := time.Now()
highest := Pos{x: 0, y: -1}
for i := 0; i < 1000000000000; i++ {
if i%1000000 == 0 {
nowNow := time.Now()
rem := nowNow.Sub(last) * (time.Duration(1000000000000-i) / 1000000)
fmt.Println(i, (100 * float64(i) / 1000000000000), nowNow.Sub(last), rem, nowNow.Add(rem))
last = nowNow
}
// assume jet cycle starts over per rock
landed := rocks[i%len(rocks)].Fall(highest.y)
if landed.y > highest.y {
highest = landed
}
}
return highest.y + 1
}
func main() {
solver := lib.Solver[[]bool, int]{
ParseF: lib.ParseBytesFunc(parse),
SolveF: solve,
}
solver.Expect(">>><<><>><<<>><>>><<<>>><<<><<<>><>><<>>", 1514285714288)
solver.Solve()
}