AoC2021/src/day2/day2.go
2021-12-02 21:34:10 +01:00

59 lines
1.1 KiB
Go

package main
import (
"AOC2021/src/helper"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
args := os.Args[1:]
input, err := helper.GetInput(args[0])
if err != nil {
fmt.Println(err)
}
//part1(input)
part2(input)
}
func part2(input []string) {
forward := 0
depth := 0
aim := 0
for _, line := range input {
seperatedLine := strings.Fields(line)
movement := seperatedLine[0]
distance, _ := strconv.Atoi(seperatedLine[1])
switch movement {
case "forward":
forward += distance
depth += aim * distance
case "down":
aim += distance
case "up":
aim -= distance
}
}
fmt.Printf("With a forward of %d and depth of %d I get the value %d \r\n", forward, depth, (forward * depth))
}
func part1(input []string) {
forward := 0
depth := 0
for _, line := range input {
seperatedLine := strings.Fields(line)
movement := seperatedLine[0]
distance, _ := strconv.Atoi(seperatedLine[1])
switch movement {
case "forward":
forward += distance
case "down":
depth += distance
case "up":
depth -= distance
}
}
fmt.Printf("With a forward of %d and depth of %d I get the value %d \r\n", forward, depth, (forward * depth))
}