Day 11. part 1.
This commit is contained in:
parent
2fdf520e2b
commit
e8e29e2701
3
data.go
3
data.go
|
@ -33,3 +33,6 @@ var Day09Data string
|
||||||
|
|
||||||
//go:embed day_10/data.txt
|
//go:embed day_10/data.txt
|
||||||
var Day10Data string
|
var Day10Data string
|
||||||
|
|
||||||
|
//go:embed day_11/data.txt
|
||||||
|
var Day11Data string
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
5
main.go
5
main.go
|
@ -11,6 +11,7 @@ import (
|
||||||
"advent_of_code_2024/day_08"
|
"advent_of_code_2024/day_08"
|
||||||
"advent_of_code_2024/day_09"
|
"advent_of_code_2024/day_09"
|
||||||
"advent_of_code_2024/day_10"
|
"advent_of_code_2024/day_10"
|
||||||
|
"advent_of_code_2024/day_11"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
@ -60,6 +61,10 @@ func main() {
|
||||||
fmt.Printf("Day 10. Basic: %d\n", day_10.SolveBasic(Day10Data)) //
|
fmt.Printf("Day 10. Basic: %d\n", day_10.SolveBasic(Day10Data)) //
|
||||||
case "10-complex":
|
case "10-complex":
|
||||||
fmt.Printf("Day 10. Complex: %d\n", day_10.SolveComplex(Day10Data)) //
|
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)) //
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue