-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (62 loc) · 1.34 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
package main
import (
"fmt"
"strings"
"github.com/devkvlt/aoc"
)
var lines = aoc.Lines("input")
func area(x, y []int, boundary int) int {
// https://en.wikipedia.org/wiki/Shoelace_formula
n := len(x)
area := 0
for i := 1; i < n-1; i++ {
area += x[i] * (y[i-1] - y[i+1])
}
area += x[0] * (y[n-1] - y[1])
area += x[n-1] * (y[n-2] - y[0])
area /= 2
area = max(area, -area)
// https://en.wikipedia.org/wiki/Pick%27s_theorem
interior := area - boundary/2 + 1
return interior + boundary
}
func parse1(fields []string) (byte, int) {
dir := fields[0][0]
steps := aoc.Atoi(fields[1])
return dir, steps
}
func parse2(fields []string) (byte, int) {
m := map[byte]byte{'0': 'R', '1': 'D', '2': 'L', '3': 'U'}
dir := m[fields[2][7]]
steps := aoc.Hextoi(fields[2][2:7])
return dir, steps
}
func solveWith(parseFn func([]string) (byte, int)) {
currX := 0
currY := 0
x := []int{currX}
y := []int{currY}
boundary := 0
for _, line := range lines {
fields := strings.Fields(line)
dir, steps := parseFn(fields)
switch dir {
case 'R':
currX += steps
case 'D':
currY += steps
case 'L':
currX -= steps
case 'U':
currY -= steps
}
x = append(x, currX)
y = append(y, currY)
boundary += steps
}
fmt.Println(area(x, y, boundary))
}
func main() {
solveWith(parse1) // 41019
solveWith(parse2) // 96116995735219
}