diff --git a/exercises/2025/03-lobby/README.md b/exercises/2025/03-lobby/README.md new file mode 100644 index 00000000..8db10aae --- /dev/null +++ b/exercises/2025/03-lobby/README.md @@ -0,0 +1,29 @@ +# [Day 3: Lobby](https://adventofcode.com/2025/day/3) + + + +## Go + +```text +< section intentionally left blank > +``` + +## Python + +```text +< section intentionally left blank > +``` + +## 2025 Run Times + +![2025 exercise run-time graphs](../run-times.png) diff --git a/exercises/2025/03-lobby/go/exercise.go b/exercises/2025/03-lobby/go/exercise.go new file mode 100644 index 00000000..e797b7f1 --- /dev/null +++ b/exercises/2025/03-lobby/go/exercise.go @@ -0,0 +1,97 @@ +package exercises + +import ( + "strings" + + "github.com/asphaltbuffet/advent-of-code/internal/common" +) + +// Exercise for Advent of Code 2025 day 3. +type Exercise struct { + common.BaseExercise +} + +// One returns the answer to the first part of the exercise. +func (e Exercise) One(instr string) (any, error) { + var sum int + for bank := range strings.Lines(instr) { + sum += Largest(strings.Trim(bank, "\n")) + // sum += LongLargest(strings.Trim(bank, "\n"), 2) + } + + return sum, nil +} + +// Two returns the answer to the second part of the exercise. +func (e Exercise) Two(instr string) (any, error) { + var sum int + for bank := range strings.Lines(instr) { + sum += LongLargest(strings.Trim(bank, "\n"), 12) + } + + return sum, nil +} + +// Largest creates the max 2-digit integer from consecutive integers in a string. +// It assumes that every character in the string is '0' -> '9'. +func Largest(b string) int { + var l int + var r int + + for i := 0; i < len(b)-1; i++ { + n := int(b[i] - '0') + if n > l { + l = n + r = -1 + } else if n > r { + r = n + } + } + + //set r to last value if unset + n := int(b[len(b)-1] - '0') + if r < n { + r = n + } + + return l*10 + r +} + +func LongLargest(b string, digits int) int { + bank := make([]int, len(b)) + + for i, r := range b { + bank[i] = int(r - '0') + } + // fmt.Println("bank: ", bank) + + out := 0 + remaining := digits + + for i := 0; remaining > 0; { + window := len(b) - i - remaining + 1 + // fmt.Printf("i=%d w=%d r= %d out=%d\n", i, window, remaining, out) + + m, idx := MaxInRange(bank[i : i+window]) + out = out*10 + m + + i += idx + 1 + remaining-- + } + + return out +} + +func MaxInRange(b []int) (int, int) { + idx := 0 + m := 0 + + for i, n := range b { + if n > m { + m = n + idx = i + } + } + + return m, idx +} diff --git a/exercises/2025/03-lobby/info.json b/exercises/2025/03-lobby/info.json new file mode 100644 index 00000000..eae9cee0 --- /dev/null +++ b/exercises/2025/03-lobby/info.json @@ -0,0 +1,60 @@ +{ + "id": "2025-03", + "title": "Lobby", + "year": 2025, + "day": 3, + "url": "https://adventofcode.com/2025/day/3", + "data": { + "inputFile": "input.txt", + "testCases": { + "one": [ + { + "input": "987654321111111", + "expected": "98" + }, + { + "input": "811111111111119", + "expected": "89" + }, + { + "input": "234234234234278", + "expected": "78" + }, + { + "input": "818181911112111", + "expected": "92" + }, + { + "input": "987654321111111\n811111111111119\n234234234234278\n818181911112111", + "expected": "357" + } + ], + "two": [ + { + "input": "987654321111111", + "expected": "987654321111" + }, + { + "input": "811111111111119", + "expected": "811111111119" + }, + { + "input": "234234234234278", + "expected": "434234234278" + }, + { + "input": "818181911112111", + "expected": "888911112111" + }, + { + "input": "987654321111111\n811111111111119\n234234234234278\n818181911112111", + "expected": "3121910778619" + } + ] + }, + "answers": { + "a": "17087", + "b": "" + } + } +} diff --git a/exercises/2025/README.md b/exercises/2025/README.md index 2ce97fa1..eb91bbcd 100644 --- a/exercises/2025/README.md +++ b/exercises/2025/README.md @@ -8,7 +8,7 @@ | -------------------------------- | :---: | --------- | | [Day 1: Secret Entrance][rm1] | ⭐⭐ | [][go1] | | [Day 2: Gift Shop][rm2] | ⭐⭐ | [][go2] | -| Day 3 | ☆☆ | | +| [Day 3: Lobby][rm3] | ☆☆ | [Go][go3] | | Day 4 | ☆☆ | | | Day 5 | ☆☆ | | | Day 6 | ☆☆ | | @@ -49,3 +49,5 @@ Day -Part 1- -Part 2- [go1]: 01-secretEntrance/go [rm2]: 02-giftShop/README.md [go2]: 02-giftShop/go +[rm3]: 03-lobby/README.md +[go3]: 03-lobby/go diff --git a/go.mod b/go.mod index 7b14a57f..a0e177f3 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/asphaltbuffet/advent-of-code -go 1.23 +go 1.25.4 require github.com/stretchr/testify v1.10.0