-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17.go
105 lines (102 loc) · 2.09 KB
/
day17.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
package main
import (
"bufio"
"fmt"
"regexp"
"strconv"
)
func settled(ch byte) bool {
return ch == '#' || ch == '~'
}
func (x Aoc) Day17(scanner *bufio.Scanner) {
pat := regexp.MustCompile(`(x|y)=(\d+), (x|y)=(\d+)..(\d+)`)
mapp := make([][]byte, 2000)
maxy := int64(0)
miny := int64(9999)
for y := 0; y < 2000; y++ {
mapp[y] = make([]byte, 1000)
}
for scanner.Scan() {
g := pat.FindStringSubmatch(scanner.Text())
v, _ := strconv.ParseInt(g[2], 10, 0)
a, _ := strconv.ParseInt(g[4], 10, 0)
b, _ := strconv.ParseInt(g[5], 10, 0)
for i := a; i <= b; i++ {
if g[1] == "x" {
mapp[i][v] = '#'
if i > maxy {
maxy = i
}
if i < miny {
miny = i
}
} else {
mapp[v][i] = '#'
if v > maxy {
maxy = v
}
if v < miny {
miny = v
}
}
}
}
flows := make([]map[int64]bool, 2000)
flows[1] = make(map[int64]bool, 1)
flows[1][500] = true
deep := int64(1)
tot := int64(0)
tot2 := int64(0)
for deep <= maxy {
fmt.Printf("@Depth %d Flows: %v\n", deep, flows[deep])
dir := int64(1)
if flows[deep+1] == nil {
flows[deep+1] = make(map[int64]bool, 0)
}
for p := range flows[deep] {
if settled(mapp[deep+1][p]) {
pl := p - 1
pr := p + 1
for settled(mapp[deep+1][pl]) && mapp[deep][pl] != '#' {
pl--
}
for settled(mapp[deep+1][pr]) && mapp[deep][pr] != '#' {
pr++
}
ch := byte('~')
if !settled(mapp[deep+1][pr]) && mapp[deep][pr] != '#' {
flows[deep+1][pr] = true
ch = '|'
}
if !settled(mapp[deep+1][pl]) && mapp[deep][pl] != '#' {
flows[deep+1][pl] = true
ch = '|'
}
if ch == '~' {
delete(flows[deep], p)
dir = -1
}
for i := pl; i <= pr; i++ {
if mapp[deep][i] != '#' {
if mapp[deep][i] == 0 {
tot += 1
}
if ch == '~' && mapp[deep][i] != '~' {
tot2 += 1
}
mapp[deep][i] = ch
}
}
} else {
if mapp[deep][p] == 0 {
tot += 1
}
mapp[deep][p] = '|'
flows[deep+1][p] = true
}
}
deep = deep + dir
}
fmt.Printf("Part 1: %d\n", tot-miny+1)
fmt.Printf("Part 2: %d\n", tot2)
}