package main import ( "log" "os" "strconv" "strings" ) func main() { inputBytes, _ := os.ReadFile("input.txt") inputs := strings.Split(string(inputBytes), "\n") log.Println("Puzzle 1:", puzzle1(inputs)) log.Println("Puzzle 2:", puzzle2(inputs)) } func puzzle1(input []string) int { // "forward X" increases the horizontal position by X units. // "down X" increases the depth by X units. // "up X" decreases the depth by X units. horizontal := 0 depth := 0 for _, instruction := range input { instructionParts := strings.Split(instruction, " ") direction := instructionParts[0] distance, _ := strconv.Atoi(instructionParts[1]) switch direction { case "forward": horizontal += distance case "down": depth += distance case "up": depth -= distance } } // Multiply horizontal position and depth together to get the final answer. return horizontal * depth } func puzzle2(input []string) int { // down X increases your aim by X units. // up X decreases your aim by X units. // forward X does two things: // - It increases your horizontal position by X units. // - It increases your depth by your aim multiplied by X. horizontal := 0 depth := 0 aim := 0 for _, instruction := range input { instructionParts := strings.Split(instruction, " ") direction := instructionParts[0] distance, _ := strconv.Atoi(instructionParts[1]) switch direction { case "down": aim += distance case "up": aim -= distance case "forward": horizontal += distance depth += aim * distance } } // Multiply horizontal position and depth together to get the final answer. return horizontal * depth }