-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (113 loc) · 2.21 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
package main
import (
"fmt"
"slices"
"strconv"
"strings"
"utils"
)
type Brick struct {
min, max []int
grounded bool
}
type Bricks []Brick
func parseCoord(s string) []int {
numStrings := strings.Split(s, ",")
nums := make([]int, 0)
for _, str := range numStrings {
n, _ := strconv.Atoi(str)
nums = append(nums, n)
}
return nums
}
func parseBricks() Bricks {
lines := utils.ReadInput("input.txt", "\n")
bricks := make([]Brick, len(lines))
for i, line := range lines {
s := strings.Split(line, "~")
min, max := parseCoord(s[0]), parseCoord(s[1])
if min[2] == 1 {
bricks[i] = Brick{min, max, true}
} else {
bricks[i] = Brick{min, max, false}
}
}
return bricks
}
func (b Brick) fall() Brick {
b.min[2]--
b.max[2]--
if b.min[2] == 1 {
b.grounded = true
}
return b
}
func (a Brick) supportedBy(b Brick) bool {
return !(a.min[0] > b.max[0] ||
a.max[0] < b.min[0] ||
a.min[1] > b.max[1] ||
a.max[1] < b.min[1] ||
a.min[2]-1 > b.max[2] ||
a.max[2]-1 < b.min[2])
}
func supersetOf(a, b []int) bool {
for _, n := range b {
if !slices.Contains(a, n) {
return false
}
}
return true
}
func solve() {
bricks := parseBricks()
supportedBy := map[int][]int{}
falling := true
for falling {
falling = false
for i, brick := range bricks {
if !brick.grounded {
falling = true
canFall := true
for j, block := range bricks {
if i != j {
if brick.supportedBy(block) {
canFall = false
if block.grounded {
brick.grounded = true
bricks[i] = brick
supportedBy[i] = append(supportedBy[i], j)
}
}
}
}
if canFall {
bricks[i] = brick.fall()
}
}
}
}
part1, part2 := 0, 0
for i := range bricks {
fallingBricks := []int{i}
chainReaction := true
for chainReaction {
chainReaction = false
for j, supports := range supportedBy {
if !slices.Contains(fallingBricks, j) && supersetOf(fallingBricks, supports) {
fallingBricks = append(fallingBricks, j)
chainReaction = true
}
}
}
if len(fallingBricks) == 1 {
part1++
} else {
part2 += len(fallingBricks) - 1
}
}
fmt.Println("Part 1:", part1)
fmt.Println("Part 2:", part2)
}
func main() {
utils.TimeFunction(solve)
}