diff --git a/day-22/benchmark b/day-22/benchmark new file mode 100644 index 0000000..728f20a --- /dev/null +++ b/day-22/benchmark @@ -0,0 +1,15 @@ +goos: linux +goarch: amd64 +pkg: github.com/onyx-and-iris/aoc2024/day-22 +cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz +BenchmarkSolve-12 1 2054969661 ns/op +BenchmarkSolve-12 1 1820308129 ns/op +BenchmarkSolve-12 1 1853098610 ns/op +BenchmarkSolve-12 1 1825725443 ns/op +BenchmarkSolve-12 1 1817395243 ns/op +BenchmarkSolve-12 1 1823292443 ns/op +BenchmarkSolve-12 1 1825067743 ns/op +BenchmarkSolve-12 1 1827243915 ns/op +BenchmarkSolve-12 1 1821714708 ns/op +BenchmarkSolve-12 1 1825390608 ns/op +ok github.com/onyx-and-iris/aoc2024/day-22 18.538s diff --git a/day-22/cmd/cli/main.go b/day-22/cmd/cli/main.go new file mode 100644 index 0000000..28f204d --- /dev/null +++ b/day-22/cmd/cli/main.go @@ -0,0 +1,41 @@ +/******************************************************************************** + Advent of Code 2024 - day-22 +********************************************************************************/ + +package main + +import ( + "embed" + "flag" + "fmt" + "slices" + + log "github.com/sirupsen/logrus" + + problems "github.com/onyx-and-iris/aoc2024/day-22" +) + +//go:embed testdata +var files embed.FS + +func main() { + filename := flag.String("f", "input.txt", "input file") + loglevel := flag.Int("l", int(log.InfoLevel), "log level") + flag.Parse() + + if slices.Contains(log.AllLevels, log.Level(*loglevel)) { + log.SetLevel(log.Level(*loglevel)) + } + + data, err := files.ReadFile(fmt.Sprintf("testdata/%s", *filename)) + if err != nil { + log.Fatal(err) + } + + one, two, err := problems.Solve(data) + if err != nil { + log.Fatal(err) + } + + fmt.Printf("solution one: %d\nsolution two: %d\n", one, two) +} diff --git a/day-22/go.mod b/day-22/go.mod new file mode 100644 index 0000000..06575ad --- /dev/null +++ b/day-22/go.mod @@ -0,0 +1,7 @@ +module github.com/onyx-and-iris/aoc2024/day-22 + +go 1.23.3 + +require github.com/sirupsen/logrus v1.9.3 + +require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect diff --git a/day-22/go.sum b/day-22/go.sum new file mode 100644 index 0000000..21f9bfb --- /dev/null +++ b/day-22/go.sum @@ -0,0 +1,15 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/day-22/internal/one/benchmark b/day-22/internal/one/benchmark new file mode 100644 index 0000000..2c173d8 --- /dev/null +++ b/day-22/internal/one/benchmark @@ -0,0 +1,6 @@ +goos: linux +goarch: amd64 +pkg: github.com/onyx-and-iris/aoc2024/day-22/internal/one +cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz +BenchmarkSolve-12 1000000000 0.03026 ns/op +ok github.com/onyx-and-iris/aoc2024/day-22/internal/one 0.219s diff --git a/day-22/internal/one/solve.go b/day-22/internal/one/solve.go new file mode 100644 index 0000000..ad1a43b --- /dev/null +++ b/day-22/internal/one/solve.go @@ -0,0 +1,29 @@ +package one + +import ( + "bytes" + + "github.com/onyx-and-iris/aoc2024/day-22/internal/randomiser" +) + +const numIterations = 2000 + +func Solve(buf []byte) (int, error) { + r := bytes.NewReader(buf) + secrets, err := parseLines(r) + if err != nil { + return 0, err + } + + var sum int + for _, secret := range secrets { + randomiser := randomiser.New(secret) + + for range numIterations { + randomiser.Randomise() + } + sum += randomiser.Secret() + } + + return sum, nil +} diff --git a/day-22/internal/one/solve_internal_test.go b/day-22/internal/one/solve_internal_test.go new file mode 100644 index 0000000..e5fd7ea --- /dev/null +++ b/day-22/internal/one/solve_internal_test.go @@ -0,0 +1,15 @@ +package one + +import ( + _ "embed" + "os" + "testing" +) + +//go:embed testdata/input.txt +var data []byte + +func BenchmarkSolve(b *testing.B) { + os.Stdout, _ = os.Open(os.DevNull) + Solve(data) +} diff --git a/day-22/internal/one/util.go b/day-22/internal/one/util.go new file mode 100644 index 0000000..b3d7400 --- /dev/null +++ b/day-22/internal/one/util.go @@ -0,0 +1,31 @@ +package one + +import ( + "bufio" + "io" + "strconv" + "strings" +) + +func parseLines(r io.Reader) ([]int, error) { + secrets := []int{} + + scanner := bufio.NewScanner(r) + for scanner.Scan() { + secrets = append(secrets, mustConv(strings.TrimSpace(scanner.Text()))) + } + + if err := scanner.Err(); err != nil { + return nil, err + } + + return secrets, nil +} + +func mustConv(s string) int { + n, err := strconv.Atoi(s) + if err != nil { + panic(err) + } + return n +} diff --git a/day-22/internal/randomiser/randomiser.go b/day-22/internal/randomiser/randomiser.go new file mode 100644 index 0000000..96d97cf --- /dev/null +++ b/day-22/internal/randomiser/randomiser.go @@ -0,0 +1,43 @@ +package randomiser + +type Randomiser struct { + secret int +} + +func New(secret int) *Randomiser { + return &Randomiser{secret: secret} +} + +func (r *Randomiser) multiplyBy(n int) int { + return r.secret * n +} + +func (r *Randomiser) divideBy(n int) int { + return r.secret / n +} + +func (r *Randomiser) mix(a int) { + r.secret ^= a +} + +func (r *Randomiser) prune() { + r.secret %= 16777216 +} + +func (r *Randomiser) Randomise() { + res := r.multiplyBy(64) + r.mix(res) + r.prune() + + res = r.divideBy(32) + r.mix(res) + r.prune() + + res = r.multiplyBy(2048) + r.mix(res) + r.prune() +} + +func (r *Randomiser) Secret() int { + return r.secret +} diff --git a/day-22/internal/randomiser/randomiser_internal_test.go b/day-22/internal/randomiser/randomiser_internal_test.go new file mode 100644 index 0000000..6690237 --- /dev/null +++ b/day-22/internal/randomiser/randomiser_internal_test.go @@ -0,0 +1,19 @@ +package randomiser + +import "testing" + +func TestRandomiserMix(t *testing.T) { + r := New(42) + r.mix(15) + if r.secret != 37 { + t.Errorf("r.secret: got = %d want = 3", r.secret) + } +} + +func TestRandomiserPrune(t *testing.T) { + r := New(10e7) + r.prune() + if r.secret != 16113920 { + t.Errorf("r.secret: got = %d want = 16113920", r.secret) + } +} diff --git a/day-22/internal/two/benchmark b/day-22/internal/two/benchmark new file mode 100644 index 0000000..bdea7c6 --- /dev/null +++ b/day-22/internal/two/benchmark @@ -0,0 +1,6 @@ +goos: linux +goarch: amd64 +pkg: github.com/onyx-and-iris/aoc2024/day-22/internal/two +cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz +BenchmarkSolve-12 1 2064547562 ns/op +ok github.com/onyx-and-iris/aoc2024/day-22/internal/two 2.091s diff --git a/day-22/internal/two/cache.go b/day-22/internal/two/cache.go new file mode 100644 index 0000000..f6a6542 --- /dev/null +++ b/day-22/internal/two/cache.go @@ -0,0 +1,21 @@ +package two + +import "sync" + +type sequenceCache struct { + mu sync.RWMutex + data map[sequence]int +} + +func newSequenceCache() sequenceCache { + return sequenceCache{ + data: make(map[sequence]int), + } +} + +func (sc *sequenceCache) upsert(seq sequence, value int) { + sc.mu.Lock() + defer sc.mu.Unlock() + + sc.data[seq] += value +} diff --git a/day-22/internal/two/sequence.go b/day-22/internal/two/sequence.go new file mode 100644 index 0000000..632f7d0 --- /dev/null +++ b/day-22/internal/two/sequence.go @@ -0,0 +1,7 @@ +package two + +type sequence [4]int + +func (s *sequence) shift(next int) { + s[0], s[1], s[2], s[3] = s[1], s[2], s[3], next +} diff --git a/day-22/internal/two/solve.go b/day-22/internal/two/solve.go new file mode 100644 index 0000000..829b09a --- /dev/null +++ b/day-22/internal/two/solve.go @@ -0,0 +1,56 @@ +package two + +import ( + "bytes" + "math" + + "github.com/onyx-and-iris/aoc2024/day-22/internal/randomiser" +) + +const numIterations = 2000 + +func Solve(buf []byte) (int, error) { + r := bytes.NewReader(buf) + secrets, err := parseLines(r) + if err != nil { + return 0, err + } + + conc := len(secrets) + done := make(chan bool) + + sequences := newSequenceCache() + for _, secret := range secrets { + go func() { + seen := make(map[sequence]struct{}) + sequence := sequence{math.MaxInt, math.MaxInt, math.MaxInt, math.MaxInt} + + randomiser := randomiser.New(secret) + for range numIterations { + prev := lastDigit(randomiser.Secret()) + randomiser.Randomise() + + sequence.shift(prev - lastDigit(randomiser.Secret())) + + _, ok := seen[sequence] + if !ok { + seen[sequence] = struct{}{} + sequences.upsert(sequence, lastDigit(randomiser.Secret())) + } + } + done <- true + }() + } + + for range conc { + <-done + } + + max := 0 + for _, sum := range sequences.data { + if sum > max { + max = sum + } + } + return max, nil +} diff --git a/day-22/internal/two/solve_internal_test.go b/day-22/internal/two/solve_internal_test.go new file mode 100644 index 0000000..ecb05d5 --- /dev/null +++ b/day-22/internal/two/solve_internal_test.go @@ -0,0 +1,15 @@ +package two + +import ( + _ "embed" + "os" + "testing" +) + +//go:embed testdata/input.txt +var data []byte + +func BenchmarkSolve(b *testing.B) { + os.Stdout, _ = os.Open(os.DevNull) + Solve(data) +} diff --git a/day-22/internal/two/util.go b/day-22/internal/two/util.go new file mode 100644 index 0000000..7407209 --- /dev/null +++ b/day-22/internal/two/util.go @@ -0,0 +1,35 @@ +package two + +import ( + "bufio" + "io" + "strconv" + "strings" +) + +func parseLines(r io.Reader) ([]int, error) { + secrets := []int{} + + scanner := bufio.NewScanner(r) + for scanner.Scan() { + secrets = append(secrets, mustConv(strings.TrimSpace(scanner.Text()))) + } + + if err := scanner.Err(); err != nil { + return nil, err + } + + return secrets, nil +} + +func mustConv(s string) int { + n, err := strconv.Atoi(s) + if err != nil { + panic(err) + } + return n +} + +func lastDigit(n int) int { + return n % 10 +} diff --git a/day-22/makefile b/day-22/makefile new file mode 100644 index 0000000..1fc6528 --- /dev/null +++ b/day-22/makefile @@ -0,0 +1,30 @@ +program = day-22 + +GO = go +SRC_DIR := src +BIN_DIR := bin + +EXE := $(BIN_DIR)/$(program) + +.DEFAULT_GOAL := build + +.PHONY: fmt vet build bench clean +fmt: + $(GO) fmt ./... + +vet: fmt + $(GO) vet ./... + +build: vet | $(BIN_DIR) + $(GO) build -o $(EXE) ./$(SRC_DIR) + +bench: + $(GO) test ./internal/one/ -bench=. > internal/one/benchmark + $(GO) test ./internal/two/ -bench=. > internal/two/benchmark + $(GO) test . -count=10 -bench=. > benchmark + +$(BIN_DIR): + @mkdir -p $@ + +clean: + @rm -rv $(BIN_DIR) diff --git a/day-22/solve.go b/day-22/solve.go new file mode 100644 index 0000000..1e78fca --- /dev/null +++ b/day-22/solve.go @@ -0,0 +1,20 @@ +package daytwentytwo + +import ( + "github.com/onyx-and-iris/aoc2024/day-22/internal/one" + "github.com/onyx-and-iris/aoc2024/day-22/internal/two" +) + +func Solve(buf []byte) (int, int, error) { + answerOne, err := one.Solve(buf) + if err != nil { + return 0, 0, err + } + + answerTwo, err := two.Solve(buf) + if err != nil { + return 0, 0, err + } + + return answerOne, answerTwo, nil +} diff --git a/day-22/solve_internal_test.go b/day-22/solve_internal_test.go new file mode 100644 index 0000000..b8bfd4b --- /dev/null +++ b/day-22/solve_internal_test.go @@ -0,0 +1,15 @@ +package daytwentytwo + +import ( + _ "embed" + "os" + "testing" +) + +//go:embed testdata/input.txt +var data []byte + +func BenchmarkSolve(b *testing.B) { + os.Stdout, _ = os.Open(os.DevNull) + Solve(data) +}