feat: add main code

This commit is contained in:
2025-11-24 21:35:22 +01:00
parent 19ea2cd1f6
commit c56c2aa449

65
2021/day02/main.go Normal file
View File

@@ -0,0 +1,65 @@
package main
import (
"fmt"
"log"
"os"
"strconv"
"strings"
)
func parseInput(file string) []string {
content, err := os.ReadFile(file)
if err != nil {
log.Fatalf("Failed to read input file: %v", err)
}
return strings.Split(string(content), "\n")
}
func PartOne(data []string) int {
horizontal := 0
depth := 0
for _, line := range data {
parts := strings.Split(line, " ")
direction := parts[0]
amount, _ := strconv.Atoi(parts[1])
switch direction {
case "forward":
horizontal += amount
case "down":
depth += amount
case "up":
depth -= amount
}
}
return horizontal * depth
}
func PartTwo(data []string) int {
horizontal := 0
depth := 0
aim := 0
for _, line := range data {
parts := strings.Split(line, " ")
direction := parts[0]
amount, _ := strconv.Atoi(parts[1])
switch direction {
case "forward":
horizontal += amount
depth += aim * amount
case "down":
aim += amount
case "up":
aim -= amount
}
}
return horizontal * depth
}
func main() {
data := parseInput("input.txt")
fmt.Println("Part 1:", PartOne(data))
fmt.Println("Part 2:", PartTwo(data))
}