Archived
2nd: refactor
This commit is contained in:
+17
-29
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user