-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (54 loc) · 1.14 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
package main
import (
"fmt"
"io"
"os"
"github.com/devries/advent_of_code_2021/utils"
)
func main() {
f, err := os.Open("../inputs/day22.txt")
utils.Check(err, "error opening input file")
defer f.Close()
r := solve(f)
fmt.Println(r)
}
func solve(r io.Reader) int {
lines := utils.ReadLines(r)
grid := parseInput(lines)
total := 0
for _, v := range grid {
if v {
total++
}
}
return total
}
type Point struct {
X int
Y int
Z int
}
func parseInput(lines []string) map[Point]bool {
grid := make(map[Point]bool)
for _, line := range lines {
var low Point
var high Point
var command string
_, err := fmt.Sscanf(line, "%s x=%d..%d,y=%d..%d,z=%d..%d", &command, &low.X, &high.X, &low.Y, &high.Y, &low.Z, &high.Z)
utils.Check(err, "error parsing line")
if low.X >= -50 && high.X <= 50 && low.Y >= -50 && high.Y <= 50 && low.Z >= -50 && high.Z <= 50 {
for k := low.Z; k <= high.Z; k++ {
for j := low.Y; j <= high.Y; j++ {
for i := low.X; i <= high.X; i++ {
if command == "on" {
grid[Point{i, j, k}] = true
} else {
grid[Point{i, j, k}] = false
}
}
}
}
}
}
return grid
}