-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05.go
194 lines (184 loc) · 3.44 KB
/
day05.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
187
188
189
190
191
192
193
194
package main
import (
"aoc2021/util"
"fmt"
"regexp"
"strconv"
"time"
)
func main() {
lines := util.GetLines("day05\\05.in")
start := time.Now()
day05a(lines)
duration := time.Since(start)
day05b(lines)
duration2 := time.Since(start)
fmt.Printf("p1: %s, p2: %s\n", duration, duration2-duration)
}
// Pair is a struct with x and y
type Pair struct {
x, y int
}
func mustAtoi(line []byte) int {
i, err := strconv.Atoi(string(line))
if err != nil {
panic(err)
}
return i
}
func parsePairs(line string) (Pair, Pair) {
var start, end Pair
var num []byte // accumulator
var state int // parser state: 0: x1 1: y1 2: x2 3: y2
for i := 0; i < len(line); i++ {
switch line[i] {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
num = append(num, line[i])
case ' ':
start.y = mustAtoi(num)
num = []byte{}
i += 3 // jump past '-> '
state = 2
case ',':
if state == 0 {
start.x = mustAtoi(num)
num = []byte{}
state = 1
}
if state == 2 {
end.x = mustAtoi(num)
num = []byte{}
state = 3
}
}
}
end.y = mustAtoi(num)
return start, end
}
var (
vents map[Pair]int
re = regexp.MustCompile("^(?P<x1>[0-9]+),(?P<y1>[0-9]+)+ -> (?P<x2>[0-9]+),(?P<y2>[0-9]+)$")
)
func day05a(lines []string) {
vents = map[Pair]int{}
counter := 0
for _, line := range lines {
var p1, p2 Pair
p1, p2 = parsePairs(line)
x1 := p1.x
x2 := p2.x
y1 := p1.y
y2 := p2.y
// no diags
if x1 == x2 || y1 == y2 {
var xs []int
var ys []int
if x2 < x1 {
x1, x2 = x2, x1
}
if y2 < y1 {
y1, y2 = y2, y1
}
for i := x1; i <= x2; i++ {
xs = append(xs, i)
}
for i := y1; i <= y2; i++ {
ys = append(ys, i)
}
for _, x := range xs {
for _, y := range ys {
p := Pair{x, y}
_, ok := vents[p]
if !ok {
vents[p] = 1
} else {
vents[p]++
}
}
}
}
}
for _, value := range vents {
if value > 1 {
counter++
}
}
fmt.Printf("Solution for part A: %d\n", counter)
}
func day05b(lines []string) {
vents = map[Pair]int{}
counter := 0
for _, line := range lines {
m := re.FindAllStringSubmatch(line, -1)
x1, _ := strconv.Atoi(m[0][1])
y1, _ := strconv.Atoi(m[0][2])
x2, _ := strconv.Atoi(m[0][3])
y2, _ := strconv.Atoi(m[0][4])
// no diags
if x1 == x2 || y1 == y2 {
var xs []int
var ys []int
if x2 < x1 {
x1, x2 = x2, x1
}
if y2 < y1 {
y1, y2 = y2, y1
}
for i := x1; i <= x2; i++ {
xs = append(xs, i)
}
for i := y1; i <= y2; i++ {
ys = append(ys, i)
}
for _, x := range xs {
for _, y := range ys {
p := Pair{x, y}
_, ok := vents[p]
if !ok {
vents[p] = 1
} else {
vents[p]++
}
}
}
}
// special diags
dx := x2 - x1
xstep := 1
dy := y2 - y1
ystep := 1
if dx < 0 {
dx *= -1
xstep *= -1
}
if dy < 0 {
dy *= -1
ystep *= -1
}
if dx == dy {
pairs := []Pair{}
x := x1
y := y1
for dx >= 0 {
pairs = append(pairs, Pair{x, y})
x += xstep
y += ystep
dx--
}
for _, p := range pairs {
_, ok := vents[p]
if !ok {
vents[p] = 1
} else {
vents[p]++
}
}
}
}
for _, value := range vents {
if value > 1 {
counter++
}
}
fmt.Printf("Solution for part B: %d\n", counter)
}