2nd: remove redundancy

This commit is contained in:
Daniel Aberger
2021-12-02 12:56:44 +01:00
parent d33f37336a
commit 4e18054987
+12 -22
View File
@@ -9,46 +9,36 @@ import (
"strings" "strings"
) )
var depth int = 0 var depth int
var horizontalPosition int = 0 var horizontalPosition int
var aim int = 0 var aim int
func Run() { func Run() {
commands := readInput() commands := readInput()
for _, v := range commands { fmt.Printf("1st final position: %d\n", solve(commands, false))
command := strings.Split(v, " ") fmt.Printf("2nd final position: %d\n", solve(commands, true))
switch command[0] { }
case "forward":
i, _ := strconv.Atoi(command[1])
forward(i, false)
case "up":
i, _ := strconv.Atoi(command[1])
up(i, false)
case "down":
i, _ := strconv.Atoi(command[1])
down(i, false)
}
}
fmt.Printf("1st final position: %d\n", horizontalPosition*depth)
func solve(commands []string, alt bool) int {
depth = 0 depth = 0
horizontalPosition = 0 horizontalPosition = 0
aim = 0
for _, v := range commands { for _, v := range commands {
command := strings.Split(v, " ") command := strings.Split(v, " ")
switch command[0] { switch command[0] {
case "forward": case "forward":
i, _ := strconv.Atoi(command[1]) i, _ := strconv.Atoi(command[1])
forward(i, true) forward(i, alt)
case "up": case "up":
i, _ := strconv.Atoi(command[1]) i, _ := strconv.Atoi(command[1])
up(i, true) up(i, alt)
case "down": case "down":
i, _ := strconv.Atoi(command[1]) i, _ := strconv.Atoi(command[1])
down(i, true) down(i, alt)
} }
} }
fmt.Printf("2nd final position: %d\n", horizontalPosition*depth) return horizontalPosition * depth
} }
func up(d int, alt bool) { func up(d int, alt bool) {