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, " ")
switch command[0] {
case "forward":
i, _ := strconv.Atoi(command[1])
forward(i, alt)
x, _ := strconv.Atoi(command[1])
horizontalPosition += x
if alt {
depth += aim * x
}
case "up":
i, _ := strconv.Atoi(command[1])
up(i, alt)
x, _ := strconv.Atoi(command[1])
if !alt {
depth -= x
} else {
aim -= x
}
case "down":
i, _ := strconv.Atoi(command[1])
down(i, alt)
x, _ := strconv.Atoi(command[1])
if !alt {
depth += x
} else {
aim += x
}
}
}
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 {
file, err := os.Open("./pkg/two/input.txt")
if err != nil {