-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.go
92 lines (83 loc) · 1.7 KB
/
day11.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"math"
"os"
"strconv"
"strings"
)
func main() {
file, err := os.ReadFile("day11/day11.txt")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
stonesString := strings.Split(string(file), " ")
stones := make(map[int]int)
for _, char := range stonesString {
number, err := strconv.Atoi(char)
if err != nil {
fmt.Printf("Error converting number: %v\n", err)
continue
}
stones[number]++
}
fmt.Println("Part 1 - Number of stones:", blink(stones, 25))
fmt.Println("Part 2 - Number of stones:", blink(stones, 75))
}
func blink(stones map[int]int, blinks int) int {
for i := 0; i < blinks; i++ {
stones = arrange(stones)
//fmt.Printf("Loop %v: %v\n", i+1, countStones(stones))
}
return countStones(stones)
}
func arrange(stones map[int]int) map[int]int {
stonesNew := make(map[int]int)
for num, qty := range stones {
stonesNew[num] = qty
}
for num, qty := range stones {
switch {
case num == 0:
stonesNew[1] += qty
stonesNew[0] -= qty
case isEven(num):
left, right := splitNumber(num)
stonesNew[right] += qty
stonesNew[left] += qty
stonesNew[num] -= qty
default:
stonesNew[num] -= qty
num *= 2024
stonesNew[num] += qty
}
}
return stonesNew
}
func countStones(stones map[int]int) int {
var count int
for _, qty := range stones {
count += qty
}
return count
}
func isEven(n int) bool {
digits := 1
for n >= 10 {
n /= 10
digits++
}
return digits%2 == 0
}
func splitNumber(n int) (left, right int) {
digits := 1
for temp := n; temp >= 10; temp /= 10 {
digits++
}
midpoint := (digits + 1) / 2
divisor := int(math.Pow10(digits - midpoint))
left = n / divisor
right = n % divisor
return left, right
}