-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday02.go
More file actions
84 lines (71 loc) · 1.99 KB
/
day02.go
File metadata and controls
84 lines (71 loc) · 1.99 KB
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
package main
import (
"AdventOfCode-go/advent2021/utils"
"fmt"
"strconv"
"strings"
)
func calcDepthAndPositionUsingAim(movementCommands []string) int {
/*
down X increases your aim by X units.
up X decreases your aim by X units.
forward X does two things:
It increases your horizontal position by X units.
It increases your depth by your aim multiplied by X.
*/
var currentPosition, currentDepth, currentAim, moveAmount int
var moveCommand []string
for i := 0; i < len(movementCommands); i++ {
moveCommand = strings.Split(movementCommands[i], " ")
moveAmount, _ = strconv.Atoi(moveCommand[1])
switch moveCommand[0] {
case "forward":
currentPosition += moveAmount
currentDepth += currentAim * moveAmount
case "down":
currentAim += moveAmount
case "up":
currentAim -= moveAmount
}
}
return currentPosition * currentDepth
}
func calcDepthAndPosition(movementCommands []string) int {
/*
forward X increases the horizontal position by X units.
down X increases the depth by X units.
up X decreases the depth by X units.
*/
var currentPosition, currentDepth, moveAmount int
var moveCommand []string
for i := 0; i < len(movementCommands); i++ {
moveCommand = strings.Split(movementCommands[i], " ")
moveAmount, _ = strconv.Atoi(moveCommand[1])
switch moveCommand[0] {
case "forward":
currentPosition += moveAmount
case "down":
currentDepth += moveAmount
case "up":
currentDepth -= moveAmount
}
}
return currentPosition * currentDepth
}
func solveDay(filename string, part byte, debug bool) int {
puzzleInput, _ := utils.ReadFile(filename)
if part == 'a' {
return calcDepthAndPosition(puzzleInput)
} else {
return calcDepthAndPositionUsingAim(puzzleInput)
}
}
// Main routine
func main() {
filenamePtr, execPart, debug := utils.CatchUserInput()
if execPart == 'z' {
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
} else {
fmt.Printf("Result is: %d\n", solveDay(filenamePtr, execPart, debug))
}
}