Browse Source

Solved part 2

Piotr Czajkowski 2 years ago
parent
commit
414c0eded1
1 changed files with 21 additions and 0 deletions
  1. 21 0
      02/code.go

+ 21 - 0
02/code.go

@@ -67,6 +67,26 @@ func part1(moves []move) int {
 	return position * depth
 }
 
+func part2(input []move) int {
+	position := 0
+	depth := 0
+	aim := 0
+
+	for _, move := range input {
+		switch move.direction {
+		case "forward":
+			position += move.steps
+			depth += aim * move.steps
+		case "down":
+			aim += move.steps
+		case "up":
+			aim -= move.steps
+		}
+	}
+
+	return position * depth
+}
+
 func main() {
 	if len(os.Args) < 2 {
 		log.Fatal("No input file specified")
@@ -74,4 +94,5 @@ func main() {
 
 	moves := readInput(os.Args[1])
 	fmt.Println("Part 1:", part1(moves))
+	fmt.Println("Part 2:", part2(moves))
 }