diff --git a/docs/day11.md b/docs/day11.md new file mode 100644 index 0000000..9b5cede --- /dev/null +++ b/docs/day11.md @@ -0,0 +1,62 @@ +--- +url: "https://adventofcode.com/2024/day/11" +--- + +# Day 11: Plutonian Pebbles + +The ancient civilization on Pluto was known for its ability to manipulate spacetime, and while The Historians explore their infinite corridors, you've noticed a strange set of physics-defying stones. + +At first glance, they seem like normal stones: they're arranged in a perfectly straight line, and each stone has a number engraved on it. + +The strange part is that every time you blink, the stones change. + +Sometimes, the number engraved on a stone changes. Other times, a stone might split in two, causing all the other stones to shift over a bit to make room in their perfectly straight line. + +As you observe them for a while, you find that the stones have a consistent behavior. Every time you blink, the stones each simultaneously change according to the first applicable rule in this list: + +* If the stone is engraved with the number `0`, it is replaced by a stone engraved with the number `1`. +* If the stone is engraved with a number that has an even number of digits, it is replaced by two stones. The left half of the digits are engraved on the new left stone, and the right half of the digits are engraved on the new right stone. (The new numbers don't keep extra leading zeroes: 1000 would become stones `10` and `0`.) +* If none of the other rules apply, the stone is replaced by a new stone; the old stone's number multiplied by 2024 is engraved on the new stone. + +No matter how the stones change, their order is preserved, and they stay on their perfectly straight line. + +How will the stones evolve if you keep blinking at them? You take a note of the number engraved on each stone in the line (your puzzle input). + +If you have an arrangement of five stones engraved with the numbers `0 1 10 99 999` and you blink once, the stones transform as follows: + +* The first stone, `0`, becomes a stone marked `1`. +* The second stone, `1`, is multiplied by 2024 to become `2024`. +* The third stone, `10`, is split into a stone marked `1` followed by a stone marked `0`. +* The fourth stone, `99`, is split into two stones marked `9`. +* The fifth stone, `999`, is replaced by a stone marked `2021976`. + +So, after blinking once, your five stones would become an arrangement of seven stones engraved with the numbers `1 2024 1 0 9 9 2021976`. + +Here is a longer example: + +```txt +`Initial arrangement: +125 17 + +After 1 blink: +253000 1 7 + +After 2 blinks: +253 0 2024 14168 + +After 3 blinks: +512072 1 20 24 28676032 + +After 4 blinks: +512 72 2024 2 0 2 4 2867 6032 + +After 5 blinks: +1036288 7 2 20 24 4048 1 4048 8096 28 67 60 32 + +After 6 blinks: +2097446912 14168 4048 2 0 2 4 40 48 2024 40 48 80 96 2 8 6 7 6 0 3 2 +``` + +In this example, after blinking six times, you would have `22` stones. After blinking 25 times, you would have `55312` stones! + +Consider the arrangement of stones in front of you. How many stones will you have after blinking 25 times? \ No newline at end of file diff --git a/src/day11/day11.go b/src/day11/day11.go new file mode 100644 index 0000000..2725fac --- /dev/null +++ b/src/day11/day11.go @@ -0,0 +1,99 @@ +package day11 + +import ( + "slices" + "strings" +) + +const ZERO_RUNE = '0' +const BLINKS = 25 + +var MULTIPLE = []uint{4, 2, 0, 2} + +type stone []uint + +func Solve(input string) uint { + initial := parseInput(input) + return solve(initial, BLINKS) +} + +func parseInput(input string) []stone { + result := make([]stone, 0) + for i, num := range strings.Split(input, " ") { + result = slices.Insert(result, i, make(stone, 0)) + for _, chr := range num { + result[i] = append(result[i], runeToUInt(chr)) + } + slices.Reverse(result[i]) + } + return result +} + +func runeToUInt(r rune) uint { + return uint(r - ZERO_RUNE) +} + +func solve(input []stone, blinks uint) uint { + curr := input + for range blinks { + curr = blink(curr) + } + return uint(len(curr)) +} + +func blink(input []stone) []stone { + result := make([]stone, 0) + for _, stn := range input { + if isZero(stn) { + result = append(result, stone{1}) + } else if isEvenDigits(stn) { + splt := split(stn) + result = append(result, splt[0], splt[1]) + } else { + result = append(result, multiply(stn)) + } + } + return result +} + +func isZero(s stone) bool { + return len(s) == 1 && s[0] == 0 +} + +func isEvenDigits(s stone) bool { + return len(s)%2 == 0 +} + +func split(input stone) [2]stone { + digits := len(input) + var result [2]stone + result[0] = zeroTrim(input[digits/2 : digits]) + result[1] = zeroTrim(input[0 : digits/2]) + return result +} + +func zeroTrim(input stone) stone { + lastDigit := 0 + for i, d := range input { + if d != 0 { + lastDigit = i + } + } + return input[0 : lastDigit+1] +} + +// @see{https://en.wikipedia.org/wiki/Multiplication_algorithm#Example} +func multiply(input stone) stone { + result := make(stone, len(input)+len(MULTIPLE)) + var carry uint + for i := range input { + carry = 0 + for a := range MULTIPLE { + result[a+i] += carry + MULTIPLE[a]*input[i] + carry = result[a+i] / 10 + result[a+i] = result[a+i] % 10 + } + result[i+len(MULTIPLE)] = carry + } + return zeroTrim(result) +} diff --git a/src/day11/day11_test.go b/src/day11/day11_test.go new file mode 100644 index 0000000..b96120a --- /dev/null +++ b/src/day11/day11_test.go @@ -0,0 +1,13 @@ +package day11 + +import ( + "testing" +) + +func TestSample(t *testing.T) { + input := `125 17` + result := Solve(input) + if result != 55312 { + t.Errorf("Calculated solution was not expected") + } +}