feat: completed day 1 for advent of code 2025
Some checks are pending
gofmt / format (push) Waiting to run
Some checks are pending
gofmt / format (push) Waiting to run
This commit is contained in:
parent
4ca16dabaa
commit
7062b47026
4 changed files with 142 additions and 1 deletions
|
|
@ -8,9 +8,13 @@ import (
|
|||
|
||||
"cpatino.com/advent-of-code/2025/cmd/generate"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"cpatino.com/advent-of-code/2025/day01"
|
||||
)
|
||||
|
||||
var days = map[int]func(string) (any, any){}
|
||||
var days = map[int]func(string) (any, any){
|
||||
1: day01.Run,
|
||||
}
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "advent-of-code",
|
||||
|
|
|
|||
93
2025/day01/day01.go
Executable file
93
2025/day01/day01.go
Executable file
|
|
@ -0,0 +1,93 @@
|
|||
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
|
||||
}
|
||||
34
2025/day01/day01_test.go
Executable file
34
2025/day01/day01_test.go
Executable file
|
|
@ -0,0 +1,34 @@
|
|||
package day01
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type TestCase struct {
|
||||
FileName string
|
||||
Part1 any
|
||||
Part2 any
|
||||
}
|
||||
|
||||
func TestDay01(t *testing.T) {
|
||||
tests := []TestCase{
|
||||
{"test.txt", 3, 6},
|
||||
{"input.txt", 1086, 6268},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
if _, err := os.Stat(test.FileName); os.IsNotExist(err) {
|
||||
t.Fatalf("test file does not exist: %v", test.FileName)
|
||||
}
|
||||
|
||||
part1, part2 := Run(test.FileName)
|
||||
if test.Part1 != nil && part1 != test.Part1 {
|
||||
t.Fatalf("%s: unexpected part1:\nwant:\t%v\ngot:\t%v", test.FileName, test.Part1, part1)
|
||||
}
|
||||
|
||||
if test.Part2 != nil && part2 != test.Part2 {
|
||||
t.Fatalf("%s: unexpected part2:\nwant:\t%v\ngot:\t%v", test.FileName, test.Part2, part2)
|
||||
}
|
||||
}
|
||||
}
|
||||
10
2025/day01/test.txt
Executable file
10
2025/day01/test.txt
Executable file
|
|
@ -0,0 +1,10 @@
|
|||
L68
|
||||
L30
|
||||
R48
|
||||
L5
|
||||
R60
|
||||
L55
|
||||
L1
|
||||
L99
|
||||
R14
|
||||
L82
|
||||
Loading…
Add table
Add a link
Reference in a new issue