-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.go
116 lines (89 loc) · 2.39 KB
/
day12.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
package day12
import (
"math"
"regexp"
"strconv"
"strings"
"github.com/augustoccesar/adventofcode/utils"
)
type Day12 struct{}
func (d *Day12) InputFileName() string { return "input" }
func (d *Day12) PartOne(input string) string {
commands := strings.Split(input, "\n")
tracker := map[string]int{"NS": 0, "WE": 0}
facingIdx := 0 // To be used to query direction
for _, command := range commands {
cmd, value := parseCommand(command)
if cmd == "F" {
facing := directions[facingIdx]
moveInDirection(tracker, facing, value)
}
if cmd == "R" || cmd == "L" {
if cmd == "L" {
value = 360 - value // Equivalent of the left rotation to the right
}
facingIdx = (facingIdx + (value / 90)) % 4
}
if utils.SliceContains(directions, cmd) {
moveInDirection(tracker, cmd, value)
}
}
ns := int(math.Abs(float64(tracker["NS"])))
we := int(math.Abs(float64(tracker["WE"])))
return strconv.Itoa(ns + we)
}
func (d *Day12) PartTwo(input string) string {
commands := strings.Split(input, "\n")
waypoint := map[string]int{"NS": 1, "WE": 10}
ship := map[string]int{"NS": 0, "WE": 0}
for _, command := range commands {
cmd, value := parseCommand(command)
if cmd == "F" {
ship["NS"] += waypoint["NS"] * value
ship["WE"] += waypoint["WE"] * value
}
if cmd == "R" || cmd == "L" {
fValue := utils.DegreeToRadians(value)
if cmd == "R" {
fValue = -fValue
}
sin := int(math.Sin(fValue))
cos := int(math.Cos(fValue))
x := waypoint["WE"]
y := waypoint["NS"]
waypoint["WE"] = x*cos - y*sin
waypoint["NS"] = x*sin + y*cos
}
if utils.SliceContains(directions, cmd) {
moveInDirection(waypoint, cmd, value)
}
}
ns := int(math.Abs(float64(ship["NS"])))
we := int(math.Abs(float64(ship["WE"])))
return strconv.Itoa(ns + we)
}
var commandPattern = regexp.MustCompile(`(\w)(\d+)`)
var directions = []string{"E", "S", "W", "N"}
// command, value
func parseCommand(rawCommand string) (string, int) {
match := commandPattern.FindAllStringSubmatch(rawCommand, -1)[0]
cmd := match[1]
value := utils.Atoi(match[2])
return cmd, value
}
func moveInDirection(movementTracker map[string]int, direction string, amount int) {
switch direction {
case "E":
movementTracker["WE"] += amount
break
case "W":
movementTracker["WE"] -= amount
break
case "N":
movementTracker["NS"] += amount
break
case "S":
movementTracker["NS"] -= amount
break
}
}