1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
package main
import (
"fmt"
"math"
"os"
"strconv"
"strings"
)
func FileRead(path string) string {
file, err := os.ReadFile(path)
if err != nil {
fmt.Println("Couldn't Read file: ", err)
}
return string(file)
}
func parseInput(data string) []int {
var stones []int
for _, x := range strings.Split(data, " ") {
x := strings.TrimRight(x, "\n")
stone, _ := strconv.Atoi(x)
stones = append(stones, stone)
}
return stones
}
func insert(slice []int, index int, value int) []int {
if index < 0 || index > len(slice) {
return slice // Or handle the error as you prefer
}
return append(slice[:index], append([]int{value}, slice[index:]...)...)
}
func getNumDigits(digit int) int {
cnt := 0
for digit != 0 {
digit /= 10
cnt++
}
return cnt
}
func splitNumber(num int) (int, int) {
digits := getNumDigits(num)
divisor := int(math.Pow10(digits / 2))
left := num / divisor
right := num % divisor
return left, right
}
func isEvenDigits(digit int) bool {
return getNumDigits(digit)%2 == 0
}
func processStone(cache *map[[2]int]int, stone, blinks int) int {
result := 0
if blinks == 0 {
return 1
} else {
_, exists := (*cache)[[2]int{stone, blinks}]
if !exists {
if stone == 0 {
result = processStone(cache, 1, blinks-1)
} else if isEvenDigits(stone) {
left, right := splitNumber(stone)
result += processStone(cache, left, blinks-1)
result += processStone(cache, right, blinks-1)
} else {
result = processStone(cache, stone*2024, blinks-1)
}
(*cache)[[2]int{stone, blinks}] = result
}
return (*cache)[[2]int{stone, blinks}]
}
}
func processStones(stones []int, blinks int) int {
cache := make(map[[2]int]int)
ans := 0
n := len(stones)
for j := 0; j < n; j++ {
ans += processStone(&cache, stones[j], blinks)
}
return ans
}
func solve_part_one(data string) int {
stones := parseInput(data)
ans := processStones(stones, 25)
return ans
}
func solve_part_two(data string) int {
stones := parseInput(data)
ans := processStones(stones, 75)
return ans
}
func main() {
test := FileRead("../input/day11.test")
prod := FileRead("../input/day11.prod")
fmt.Println("Part_1 test: ", solve_part_one(test))
fmt.Println("Part_1 prod: ", solve_part_one(prod))
fmt.Println("Part_2 test: ", solve_part_two(test))
fmt.Println("Part_2 prod: ", solve_part_two(prod))
}
|