-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
149 lines (123 loc) · 2.91 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
package main
import (
"fmt"
"strings"
"github.com/Alex-Waring/AoC/utils"
)
type Position struct {
x int
y int
z int
}
func sumCovered(cubes map[Position]int) int {
covered := 0
for _, c := range cubes {
covered += c
}
return covered
}
func part1(cubes map[Position]int) {
defer utils.Timer("part1")()
for pos := range cubes {
covered := 0
above := Position{pos.x, pos.y, pos.z + 1}
if _, exists := cubes[above]; exists {
covered++
}
below := Position{pos.x, pos.y, pos.z - 1}
if _, exists := cubes[below]; exists {
covered++
}
north := Position{pos.x + 1, pos.y, pos.z}
if _, exists := cubes[north]; exists {
covered++
}
south := Position{pos.x - 1, pos.y, pos.z}
if _, exists := cubes[south]; exists {
covered++
}
east := Position{pos.x, pos.y + 1, pos.z}
if _, exists := cubes[east]; exists {
covered++
}
west := Position{pos.x, pos.y - 1, pos.z}
if _, exists := cubes[west]; exists {
covered++
}
cubes[pos] = covered
}
surface_area := len(cubes)*6 - sumCovered(cubes)
fmt.Println(surface_area)
}
func part2(cubes map[Position]int) {
defer utils.Timer("part2")()
// Looking at the input, everything is happening in a 20*20*20 area
// Fill up the area with steam, staring at -1,-1,-1, keep track of the number of
// times the steam is blocked
// Some room has been give to go round the bubble
blocked := 0
q := utils.Queue[Position]{}
q.Push(Position{x: -1, y: -1, z: -1})
seen := map[Position]bool{}
for !q.IsEmpty() {
steam := q.Pop()
if _, exists := seen[steam]; exists {
continue
} else {
seen[steam] = true
}
// move up
above := Position{x: steam.x, y: steam.y, z: steam.z + 1}
if _, exists := cubes[above]; exists {
blocked++
} else if above.z < 22 {
q.Push(above)
}
below := Position{x: steam.x, y: steam.y, z: steam.z - 1}
if _, exists := cubes[below]; exists {
blocked++
} else if below.z > -1 {
q.Push(below)
}
north := Position{x: steam.x + 1, y: steam.y, z: steam.z}
if _, exists := cubes[north]; exists {
blocked++
} else if north.x < 22 {
q.Push(north)
}
south := Position{x: steam.x - 1, y: steam.y, z: steam.z}
if _, exists := cubes[south]; exists {
blocked++
} else if south.x > -1 {
q.Push(south)
}
east := Position{x: steam.x, y: steam.y + 1, z: steam.z}
if _, exists := cubes[east]; exists {
blocked++
} else if east.y < 22 {
q.Push(east)
}
west := Position{x: steam.x, y: steam.y - 1, z: steam.z}
if _, exists := cubes[west]; exists {
blocked++
} else if west.y > -1 {
q.Push(west)
}
}
fmt.Println(blocked)
}
func main() {
input := utils.ReadInput("input.txt")
cubes := map[Position]int{}
for _, line := range input {
coords := strings.Split(line, ",")
new_pos := Position{
x: utils.IntegerOf(coords[0]),
y: utils.IntegerOf(coords[1]),
z: utils.IntegerOf(coords[2]),
}
cubes[new_pos] = 0
}
part1(cubes)
part2(cubes)
}