-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (50 loc) · 1.06 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
package main
import (
"fmt"
"strconv"
"strings"
"utils"
)
func parseLine(s string, part1 bool) (rune, int) {
line := strings.Split(s, " ")
if part1 {
n, _ := strconv.Atoi(line[1])
return rune(line[0][0]), n
}
dist, _ := strconv.ParseInt(line[2][2:7], 16, 32)
return rune(line[2][7]), int(dist)
}
type Point struct{ x, y int }
func move(p Point, dir rune, dist int) Point {
switch dir {
case 'R', '0':
p.x += dist
case 'D', '1':
p.y += dist
case 'L', '2':
p.x -= dist
case 'U', '3':
p.y -= dist
}
return p
}
func findArea(lines []string, part1 bool) int {
curr := Point{0, 0}
shoelace, boundary := 0, 0
for _, line := range lines {
dir, dist := parseLine(line, part1)
new := move(curr, dir, dist)
boundary += dist
shoelace += curr.x*new.y - curr.y*new.x // Shoelace formula
curr = new
}
return (shoelace+boundary)>>1 + 1 // Pick's formula
}
func solve() {
lines := utils.ReadInput("input.txt", "\n")
fmt.Println("Part 1:", findArea(lines, true))
fmt.Println("Part 2:", findArea(lines, false))
}
func main() {
utils.TimeFunction(solve)
}