-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.go
72 lines (59 loc) · 1.11 KB
/
day02.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
package main
import (
"mathutil"
"os"
"panic"
"readers"
"strconv"
"strings"
)
func Day02Part1() int {
file, err := os.Open("assets/day02.txt")
panic.Check(err)
lines, err := readers.ReadStrings(file)
panic.Check(err)
p := mathutil.Point{X: 0, Y: 0}
for _, s := range lines {
instructions := strings.Split(s, " ")
direction := instructions[0]
length, err := strconv.Atoi(instructions[1])
panic.Check(err)
switch direction {
case "forward":
p.X += length
break
case "down":
p.Y += length
break
case "up":
p.Y -= length
}
}
return p.X * p.Y
}
func Day02Part2() int {
file, err := os.Open("assets/day02.txt")
panic.Check(err)
lines, err := readers.ReadStrings(file)
panic.Check(err)
p := mathutil.Point{X: 0, Y: 0}
aim := 0
for _, s := range lines {
instructions := strings.Split(s, " ")
direction := instructions[0]
length, err := strconv.Atoi(instructions[1])
panic.Check(err)
switch direction {
case "forward":
p.X += length
p.Y += aim * length
break
case "down":
aim += length
break
case "up":
aim -= length
}
}
return p.X * p.Y
}