-
Notifications
You must be signed in to change notification settings - Fork 0
/
02.go
52 lines (47 loc) · 1005 Bytes
/
02.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
package main
import (
"fmt"
"log"
"github.com/jdrst/adventofgo/util"
)
func main() {
fmt.Printf("First part: %v\n", partOne(util.ReadFile("input.txt")))
fmt.Printf("Second part: %v\n", partTwo(util.ReadFile("input.txt")))
}
func partOne(file util.File) int {
lines := file.AsLines()
hPos, depth := 0, 0
for _, l := range lines {
command := l.SubSplitWith(" ")
switch command[0] {
case "forward":
hPos += command[1].AsInt()
case "down":
depth += command[1].AsInt()
case "up":
depth -= command[1].AsInt()
default:
log.Fatalf("can't")
}
}
return hPos * depth
}
func partTwo(file util.File) int {
lines := file.AsLines()
hPos, depth, aim := 0, 0, 0
for _, l := range lines {
command := l.SubSplitWith(" ")
switch command[0] {
case "forward":
hPos += command[1].AsInt()
depth += aim * command[1].AsInt()
case "down":
aim += command[1].AsInt()
case "up":
aim -= command[1].AsInt()
default:
log.Fatalf("can't")
}
}
return hPos * depth
}