-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd14.go
170 lines (152 loc) · 2.98 KB
/
d14.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
package main
import (
"fmt"
"strconv"
"strings"
)
type d14robot struct {
g *d14grid
pos *coords
vel *coords
}
type d14grid struct {
width int64
height int64
cycle int
robots []*d14robot
}
func d14parseBounds(bounds string) (width int64, height int64) {
parts := strings.Split(bounds, ",")
width, _ = strconv.ParseInt(parts[0], 10, 64)
height, _ = strconv.ParseInt(parts[1], 10, 64)
return
}
func d14parseRobot(line string) *d14robot {
line = strings.TrimPrefix(line, "p=")
line = strings.ReplaceAll(line, " v=", ",")
parts := strings.Split(line, ",")
var nums []int64
for _, p := range parts {
num, _ := strconv.ParseInt(p, 10, 64)
nums = append(nums, num)
}
return &d14robot{
pos: &coords{x: nums[0], y: nums[1]},
vel: &coords{x: nums[2], y: nums[3]},
}
}
func d14parseGrid(input string) *d14grid {
lines := strings.Split(input, "\n")
w, h := d14parseBounds(lines[0])
g := &d14grid{
width: w,
height: h,
}
for _, line := range lines[1:] {
r := d14parseRobot(line)
r.g = g
g.robots = append(g.robots, r)
}
return g
}
func (r *d14robot) walk(cycles int) {
r.pos.x += r.vel.x * int64(cycles) % r.g.width
r.pos.y += r.vel.y * int64(cycles) % r.g.height
if r.pos.x < 0 {
r.pos.x += r.g.width
}
if r.pos.y < 0 {
r.pos.y += r.g.height
}
if r.pos.x >= r.g.width {
r.pos.x -= r.g.width
}
if r.pos.y >= r.g.height {
r.pos.y -= r.g.height
}
}
func (g *d14grid) run(cycles int) {
for _, r := range g.robots {
r.walk(cycles)
}
g.cycle += cycles
}
func (g *d14grid) calculateSafetyFactor() int64 {
hw := g.width / 2
hh := g.height / 2
var q1, q2, q3, q4 int64
for _, r := range g.robots {
if r.pos.x < hw && r.pos.y < hh {
q1++
}
if r.pos.x > hw && r.pos.y < hh {
q2++
}
if r.pos.x < hw && r.pos.y > hh {
q3++
}
if r.pos.x > hw && r.pos.y > hh {
q4++
}
}
return q1 * q2 * q3 * q4
}
func (g *d14grid) print() (out string) {
var grid [][]bool
for y := int64(0); y < g.height; y++ {
var row []bool
for x := int64(0); x < g.width; x++ {
row = append(row, false)
}
grid = append(grid, row)
}
for _, r := range g.robots {
grid[r.pos.y][r.pos.x] = true
}
for _, row := range grid {
for _, spot := range row {
if spot {
out += "*"
} else {
out += "."
}
}
out += "\n"
}
return
}
func (g *d14grid) getTreeProbability() int {
var prob int
m := make(map[int64]bool)
for _, r := range g.robots {
if r.pos.y == 42 {
_, ok := m[r.pos.x]
if !ok {
prob++
m[r.pos.x] = true
}
}
}
return prob
}
func (*methods) D14P1(input string) string {
grid := d14parseGrid(input)
grid.run(100)
return strconv.FormatInt(grid.calculateSafetyFactor(), 10)
}
// half manual solution...
func (*methods) D14P2(input string) string {
grid := d14parseGrid(input)
var maxprob int
for {
grid.run(1)
prob := grid.getTreeProbability()
if prob >= maxprob {
maxprob = prob
fmt.Println(grid.print())
fmt.Printf("Cycle %d, prob %d\n", grid.cycle, prob)
fmt.Scanln()
}
}
return ""
}