From 5a31176884bca492ecdc19de574bf5142d6e450c Mon Sep 17 00:00:00 2001 From: Daniel Aberger Date: Thu, 2 Dec 2021 13:22:34 +0100 Subject: [PATCH] 2nd: refactor --- pkg/two/two.go | 46 +++++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/pkg/two/two.go b/pkg/two/two.go index 1605f4e..5366ada 100644 --- a/pkg/two/two.go +++ b/pkg/two/two.go @@ -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 {