diff --git a/data.go b/data.go index 9624483..cd3b73a 100644 --- a/data.go +++ b/data.go @@ -33,3 +33,6 @@ var Day09Data string //go:embed day_10/data.txt var Day10Data string + +//go:embed day_11/data.txt +var Day11Data string diff --git a/day_11/data.txt b/day_11/data.txt new file mode 100644 index 0000000..917d18a --- /dev/null +++ b/day_11/data.txt @@ -0,0 +1 @@ +64554 35 906 6 6960985 5755 975820 0 \ No newline at end of file diff --git a/day_11/example_data.txt b/day_11/example_data.txt new file mode 100644 index 0000000..528f9d5 --- /dev/null +++ b/day_11/example_data.txt @@ -0,0 +1 @@ +125 17 \ No newline at end of file diff --git a/day_11/solution.go b/day_11/solution.go new file mode 100644 index 0000000..f4b7b65 --- /dev/null +++ b/day_11/solution.go @@ -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) + } +} diff --git a/day_11/solution_test.go b/day_11/solution_test.go new file mode 100644 index 0000000..85fd8bf --- /dev/null +++ b/day_11/solution_test.go @@ -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) + } +} diff --git a/main.go b/main.go index e18bd02..13e55b1 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "advent_of_code_2024/day_08" "advent_of_code_2024/day_09" "advent_of_code_2024/day_10" + "advent_of_code_2024/day_11" _ "embed" "fmt" "os" @@ -60,6 +61,10 @@ func main() { fmt.Printf("Day 10. Basic: %d\n", day_10.SolveBasic(Day10Data)) // case "10-complex": fmt.Printf("Day 10. Complex: %d\n", day_10.SolveComplex(Day10Data)) // + case "11-basic": + fmt.Printf("Day 11. Basic: %d\n", day_11.SolveBasic(Day11Data)) // + case "11-complex": + fmt.Printf("Day 11. Complex: %d\n", day_11.SolveComplex(Day11Data)) // } }