Day 11. part 1.
This commit is contained in:
1 parent
2fdf520e2b
commit
e8e29e2701
6 files changed
+84
No files matched your search
@@ -0,0 +1 @@
|
||||
64554 35 906 6 6960985 5755 975820 0
|
||||
@@ -0,0 +1 @@
|
||||
125 17
|
||||
@@ -0,0 +1,57 @@
|
||||
package day_11
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// task:https://adventofcode.com/2024/day/11
|
||||
func SolveBasic(input string) int {
|
||||
stones := load(input)
|
||||
|
||||
for i := 0; i < 25; i++ {
|
||||
stones = blink(stones)
|
||||
}
|
||||
|
||||
return len(stones)
|
||||
}
|
||||
|
||||
// task:https://adventofcode.com/2024/day/10#part2
|
||||
func SolveComplex(input string) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func blink(before []uint64) []uint64 {
|
||||
after := make([]uint64, 0, len(before))
|
||||
for _, stone := range before {
|
||||
if stone == 0 {
|
||||
after = append(after, 1)
|
||||
} else if val := fromUint(stone); len(val)%2 == 0 {
|
||||
after = append(after, parseUint(val[:len(val)/2]))
|
||||
after = append(after, parseUint(val[len(val)/2:]))
|
||||
} else {
|
||||
after = append(after, stone*2024)
|
||||
}
|
||||
}
|
||||
return after
|
||||
}
|
||||
|
||||
func fromUint(stone uint64) string {
|
||||
return strconv.FormatUint(stone, 10)
|
||||
}
|
||||
|
||||
func load(input string) []uint64 {
|
||||
stones := []uint64{}
|
||||
for _, substring := range strings.Split(input, " ") {
|
||||
stones = append(stones, parseUint(substring))
|
||||
}
|
||||
return stones
|
||||
}
|
||||
|
||||
func parseUint(substring string) uint64 {
|
||||
if val, err := strconv.ParseUint(substring, 10, 64); err == nil {
|
||||
return val
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package day_11_test
|
||||
|
||||
import (
|
||||
"advent_of_code_2024/day_11"
|
||||
_ "embed"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//go:embed example_data.txt
|
||||
var ExampleData string
|
||||
|
||||
func TestBasicSolutionExample(t *testing.T) {
|
||||
result := day_11.SolveBasic(ExampleData)
|
||||
if result != 55312 {
|
||||
t.Fatalf("Expected 55312 received %d", result)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user