-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (58 loc) · 1.22 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
package main
import (
"fmt"
"io"
"os"
"strconv"
"strings"
"github.com/devries/advent_of_code_2022/utils"
"github.com/spf13/pflag"
)
const inputfile = "../inputs/day18.txt"
func main() {
pflag.Parse()
f, err := os.Open(inputfile)
utils.Check(err, "error opening input")
defer f.Close()
r := solve(f)
fmt.Println(r)
}
func solve(r io.Reader) int {
lines := utils.ReadLines(r)
grid := make(map[Point]bool)
for _, ln := range lines {
nums := strings.Split(ln, ",")
x, err := strconv.Atoi(nums[0])
utils.Check(err, "Error converting %s", ln)
y, err := strconv.Atoi(nums[1])
utils.Check(err, "Error converting %s", ln)
z, err := strconv.Atoi(nums[2])
utils.Check(err, "Error converting %s", ln)
grid[Point{x, y, z}] = true
}
totalSides := len(grid) * 6
for k := range grid {
// Subtract two for any adjacency in + directions
if grid[k.Add(Xhat)] {
totalSides -= 2
}
if grid[k.Add(Yhat)] {
totalSides -= 2
}
if grid[k.Add(Zhat)] {
totalSides -= 2
}
}
return totalSides
}
type Point struct {
X int
Y int
Z int
}
var Xhat = Point{1, 0, 0}
var Yhat = Point{0, 1, 0}
var Zhat = Point{0, 0, 1}
func (a Point) Add(b Point) Point {
return Point{a.X + b.X, a.Y + b.Y, a.Z + b.Z}
}