-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
171 lines (150 loc) · 3.24 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"bufio"
"io"
"strings"
aoc "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
scanner := bufio.NewScanner(input)
var instructions []Instruction
for scanner.Scan() {
line := scanner.Text()
instructions = append(instructions, toInstruction(line))
}
sum := 0
for x := -50; x <= 50; x++ {
for y := -50; y <= 50; y++ {
for z := -50; z <= 50; z++ {
for i := len(instructions) - 1; i >= 0; i-- {
ins := instructions[i]
if !ins.isInside(x, y, z) {
continue
}
if ins.on {
sum++
}
break
}
}
}
}
return sum
}
type Cube struct {
on bool
count int
from Position
to Position
}
func newCube(from, to Position, on bool) *Cube {
c := &Cube{
on: on,
count: (to.x + 1 - from.x) * (to.y + 1 - from.y) * (to.z + 1 - from.z),
from: from,
to: to,
}
return c
}
func (c *Cube) overlap(c2 *Cube) (*Cube, bool) {
if c.from.x > c2.to.x || c.to.x < c2.from.x {
return nil, false
}
if c.from.y > c2.to.y || c.to.y < c2.from.y {
return nil, false
}
if c.from.z > c2.to.z || c.to.z < c2.from.z {
return nil, false
}
on := false
if c.on && c2.on {
on = false
} else if !c.on && !c2.on {
on = true
} else {
on = c2.on
}
return newCube(Position{
x: aoc.Max(c.from.x, c2.from.x),
y: aoc.Max(c.from.y, c2.from.y),
z: aoc.Max(c.from.z, c2.from.z),
}, Position{
x: aoc.Min(c.to.x, c2.to.x),
y: aoc.Min(c.to.y, c2.to.y),
z: aoc.Min(c.to.z, c2.to.z),
}, on), true
}
func toInstruction(s string) Instruction {
s = strings.TrimSpace(s)
spaces := aoc.NewDelimiter(s, " ")
ins := Instruction{}
if spaces.GetString(0) == "on" {
ins.on = true
}
del := aoc.NewDelimiter(spaces.GetString(1), ",")
delx := aoc.NewDelimiter(del.GetString(0)[2:], "..")
dely := aoc.NewDelimiter(del.GetString(1)[2:], "..")
delz := aoc.NewDelimiter(del.GetString(2)[2:], "..")
ins.from.x = delx.GetInt(0)
ins.to.x = delx.GetInt(1)
ins.from.y = dely.GetInt(0)
ins.to.y = dely.GetInt(1)
ins.from.z = delz.GetInt(0)
ins.to.z = delz.GetInt(1)
return ins
}
type Instruction struct {
on bool
from Position
to Position
}
func (ins Instruction) isInside(x, y, z int) bool {
return x >= ins.from.x && x <= ins.to.x &&
y >= ins.from.y && y <= ins.to.y &&
z >= ins.from.z && z <= ins.to.z
}
type Position struct {
x int
y int
z int
}
func fs2(input io.Reader) int {
scanner := bufio.NewScanner(input)
var instructions []Instruction
for scanner.Scan() {
line := scanner.Text()
instructions = append(instructions, toInstruction(line))
}
var cubes []*Cube
cubes = append(cubes, newCube(instructions[0].from, instructions[0].to, true))
for i := 1; i < len(instructions); i++ {
ins := instructions[i]
if ins.on {
cubes = append(cubes, newCube(ins.from, ins.to, true))
} else {
cubes = append(cubes, newCube(ins.from, ins.to, false))
}
}
var uniques []*Cube
for _, c := range cubes {
var tmp []*Cube
for _, c2 := range uniques {
if intersection, overlap := c2.overlap(c); overlap {
tmp = append(tmp, intersection)
}
}
if c.on {
tmp = append(tmp, c)
}
uniques = append(uniques, tmp...)
}
var total int
for _, c := range uniques {
if c.on {
total += c.count
} else {
total -= c.count
}
}
return total
}