advent-of-code/2025/day01/day01.go
C4 Patino 7062b47026
Some checks are pending
gofmt / format (push) Waiting to run
feat: completed day 1 for advent of code 2025
2025-12-03 21:54:59 -06:00

93 lines
1.3 KiB
Go
Executable file

package day01
import (
"bufio"
"fmt"
"os"
"strings"
)
func Part1(lines []string) int {
pos := 50
count := 0
for _, line := range lines {
line = strings.TrimSpace(line)
if len(line) < 2 {
continue
}
dir := line[0]
var dist int
_, err := fmt.Sscanf(line[1:], "%d", &dist)
if err != nil {
continue
}
switch dir {
case 'L':
pos = (pos - dist + 100) % 100
case 'R':
pos = (pos + dist) % 100
}
if pos == 0 {
count++
}
}
return count
}
func Part2(lines []string) int {
pos := 50
count := 0
for _, line := range lines {
line = strings.TrimSpace(line)
if len(line) < 2 {
continue
}
dir := line[0]
var dist int
_, err := fmt.Sscanf(line[1:], "%d", &dist)
if err != nil {
continue
}
step := 0
if dir == 'L' {
step = -1
} else if dir == 'R' {
step = 1
}
for i := 0; i < dist; i++ {
pos = (pos + step + 100) % 100
if pos == 0 {
count++
}
}
}
return count
}
func Run(filename string) (any, any) {
file, err := os.Open(filename)
if err != nil {
panic(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
var lines []string
for scanner.Scan() {
line := scanner.Text()
lines = append(lines, line)
}
if err := scanner.Err(); err != nil {
panic(err)
}
part1 := Part1(lines)
part2 := Part2(lines)
return part1, part2
}