Solved first day
This commit is contained in:
3
go.mod
Normal file
3
go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module git.gulaschkanister.de/julian.dnr/AoC-2025
|
||||||
|
|
||||||
|
go 1.25.5
|
||||||
4654
inputs/day_1.txt
Normal file
4654
inputs/day_1.txt
Normal file
File diff suppressed because it is too large
Load Diff
46
src/days/benchmark_test.go
Normal file
46
src/days/benchmark_test.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package days
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"runtime"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// Changing into root dir of project
|
||||||
|
_, filename, _, _ := runtime.Caller(0)
|
||||||
|
dir := path.Join(path.Dir(filename), "..", "..")
|
||||||
|
err := os.Chdir(dir)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkDays(b *testing.B) {
|
||||||
|
benchmarks := []struct {
|
||||||
|
name string
|
||||||
|
lfn func() []string
|
||||||
|
fn func([]string) int
|
||||||
|
expected int
|
||||||
|
}{
|
||||||
|
{"Day 1", Load_Day_1, Day01, 1150},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, bm := range benchmarks {
|
||||||
|
// Loading data for the current day
|
||||||
|
data := bm.lfn()
|
||||||
|
|
||||||
|
// Just for good measures
|
||||||
|
b.ResetTimer()
|
||||||
|
|
||||||
|
b.Run(bm.name, func(b *testing.B) {
|
||||||
|
for b.Loop() {
|
||||||
|
res := bm.fn(data)
|
||||||
|
if res != bm.expected {
|
||||||
|
b.Fatalf("invalid result: got %d, want %d", res, bm.expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
72
src/days/day_1.go
Normal file
72
src/days/day_1.go
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
package days
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// numbers from 0 to 99 in order
|
||||||
|
// instructions contain order to turn L(eft) or R(ight)
|
||||||
|
// instructions contain the distance to turn
|
||||||
|
// left reduces the number
|
||||||
|
// right increases the number
|
||||||
|
//
|
||||||
|
// L => 76
|
||||||
|
// R => 82
|
||||||
|
//
|
||||||
|
// dial starts at 50 by default
|
||||||
|
//
|
||||||
|
// examples:
|
||||||
|
// 11 -> R8 => 19 -> L19 => 0
|
||||||
|
|
||||||
|
func Load_Day_1() []string {
|
||||||
|
var lines []string
|
||||||
|
|
||||||
|
file, err := os.Open("inputs/day_1.txt")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to open file: %s", err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
lines = append(lines, line)
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines
|
||||||
|
}
|
||||||
|
|
||||||
|
func wrap(n int) int {
|
||||||
|
min := 0
|
||||||
|
max := 99
|
||||||
|
rangeSize := max - min + 1
|
||||||
|
|
||||||
|
return ((n-min)%rangeSize+rangeSize)%rangeSize + min
|
||||||
|
}
|
||||||
|
|
||||||
|
func Day01(instructions []string) int {
|
||||||
|
c := 0
|
||||||
|
position := 50
|
||||||
|
for _, i := range instructions {
|
||||||
|
len, err := strconv.Atoi(i[1:])
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to convert string to int: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
if i[0] == 76 {
|
||||||
|
position = wrap(position - len)
|
||||||
|
} else {
|
||||||
|
position = wrap(position + len)
|
||||||
|
}
|
||||||
|
|
||||||
|
if position == 0 {
|
||||||
|
c++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
15
src/main.go
Normal file
15
src/main.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.gulaschkanister.de/julian.dnr/AoC-2025/src/days"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
data := days.Load_Day_1()
|
||||||
|
|
||||||
|
res := days.Day01(data)
|
||||||
|
|
||||||
|
fmt.Println(res)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user