-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (56 loc) · 1.66 KB
/
main.go
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
package main
import (
"fmt"
"strconv"
aoc "github.com/shraddhaag/aoc/library"
)
func main() {
input := aoc.FetchSliceOfIntsInString(aoc.ReadFileLineByLine("input.txt")[0])
fmt.Println("answer for part 1: ", getStoneCountAfterBlinking(input, 25))
fmt.Println("answer for part 2: ", getStoneCountAfterBlinking(input, 75))
}
func getStonesAfterBlink(stone int) []int {
resultStones := []int{}
switch {
case stone == 0:
resultStones = append(resultStones, 1)
case len(strconv.Itoa(stone))%2 == 0:
s1, s2 := splitStone(stone)
resultStones = append(resultStones, s1, s2)
default:
resultStones = append(resultStones, stone*2024)
}
return resultStones
}
func splitStone(stone int) (int, int) {
stoneString := strconv.Itoa(stone)
stone1, stone2 := stoneString[:len(stoneString)/2], stoneString[len(stoneString)/2:]
return aoc.FetchNumFromStringIgnoringNonNumeric(stone1), aoc.FetchNumFromStringIgnoringNonNumeric(stone2)
}
func getCountAfterXBlinks(stone int, cache map[int][]int, blinkCount int) int {
if _, ok := cache[stone]; ok {
if cache[stone][blinkCount-1] != 0 {
return cache[stone][blinkCount-1]
}
} else {
cache[stone] = make([]int, 75)
}
if blinkCount == 1 {
cache[stone][blinkCount-1] = len(getStonesAfterBlink(stone))
return len(getStonesAfterBlink(stone))
}
sum := 0
for _, stone := range getStonesAfterBlink(stone) {
sum += getCountAfterXBlinks(stone, cache, blinkCount-1)
}
cache[stone][blinkCount-1] = sum
return sum
}
func getStoneCountAfterBlinking(input []int, timesBlink int) int {
sum := 0
cache := make(map[int][]int)
for _, stone := range input {
sum += getCountAfterXBlinks(stone, cache, timesBlink)
}
return sum
}