2nd: refactor

This commit is contained in:
Daniel Aberger
2021-12-02 13:22:34 +01:00
parent 19f9b2999b
commit 5a31176884
+17 -29
View File
@@ -28,42 +28,30 @@ func solve(commands []string, alt bool) int {
command := strings.Split(v, " ") command := strings.Split(v, " ")
switch command[0] { switch command[0] {
case "forward": case "forward":
i, _ := strconv.Atoi(command[1]) x, _ := strconv.Atoi(command[1])
forward(i, alt) horizontalPosition += x
if alt {
depth += aim * x
}
case "up": case "up":
i, _ := strconv.Atoi(command[1]) x, _ := strconv.Atoi(command[1])
up(i, alt) if !alt {
depth -= x
} else {
aim -= x
}
case "down": case "down":
i, _ := strconv.Atoi(command[1]) x, _ := strconv.Atoi(command[1])
down(i, alt) if !alt {
depth += x
} else {
aim += x
}
} }
} }
return horizontalPosition * depth return horizontalPosition * depth
} }
func up(d int, alt bool) {
if !alt {
depth -= d
} else {
aim -= d
}
}
func down(d int, alt bool) {
if !alt {
depth += d
} else {
aim += d
}
}
func forward(d int, alt bool) {
horizontalPosition += d
if alt {
depth += aim * d
}
}
func readInput() []string { func readInput() []string {
file, err := os.Open("./pkg/two/input.txt") file, err := os.Open("./pkg/two/input.txt")
if err != nil { if err != nil {