-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspring.go
152 lines (120 loc) · 3.59 KB
/
spring.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package day12
import (
"fmt"
"github.com/user3690/advent-of-code-in-go/util"
"log"
"strconv"
"strings"
)
func BothParts() {
var (
groups, extendedGroups []int
lines []string
number int
arrangementSum, arrangementSumPart2, arrangements uint
cache = make(map[string]uint)
err error
)
lines, err = util.ReadFileInLines("./2023/day12/input.txt")
if err != nil {
log.Fatal(err)
}
// part 1
for _, line := range lines {
splitLine := strings.Split(line, " ")
splitGroups := strings.Split(splitLine[1], ",")
groups = make([]int, 0)
for _, group := range splitGroups {
number, err = strconv.Atoi(group)
if err != nil {
log.Fatal(err)
}
groups = append(groups, number)
}
fmt.Println("found groups: ", groups)
arrangements = calculateArrangements(&cache, splitLine[0], groups, 0)
fmt.Println("found arrangements: ", arrangements)
arrangementSum += arrangements
}
fmt.Println("sum of all arrangements: ", arrangementSum)
// part 2
for _, line := range lines {
splitLine := strings.Split(line, " ")
splitGroups := strings.Split(splitLine[1], ",")
groups = make([]int, 0)
for _, group := range splitGroups {
number, err = strconv.Atoi(group)
if err != nil {
log.Fatal(err)
}
groups = append(groups, number)
}
var i = 0
extendedGroups = make([]int, 0)
for i < 5 {
extendedGroups = append(extendedGroups, groups...)
i++
}
extendedRecord := strings.Join(
[]string{
splitLine[0],
splitLine[0],
splitLine[0],
splitLine[0],
splitLine[0],
},
"?",
)
arrangements = calculateArrangements(&cache, extendedRecord, extendedGroups, 0)
arrangementSumPart2 += arrangements
}
fmt.Println("sum of all arrangements part 2: ", arrangementSumPart2)
}
func calculateArrangements(cache *map[string]uint, record string, groups []int, curGroupLength int) (arrangements uint) {
key := buildCacheKey(record, groups, curGroupLength)
if value, exists := (*cache)[key]; exists {
return value
}
if len(record) == 0 {
// case we reached the end and found nothing, we can assume we only have 1 valid arrangement
if len(groups) == 0 && curGroupLength == 0 {
return 1
}
// case we found exactly one valid group at the end of the string
if len(groups) == 1 && groups[0] == curGroupLength {
return 1
}
return 0
}
char := record[0]
switch char {
case '#':
arrangements += calculateArrangements(cache, record[1:], groups, curGroupLength+1)
case '.':
if curGroupLength == 0 {
arrangements += calculateArrangements(cache, record[1:], groups, 0)
} else if len(groups) > 0 && curGroupLength == groups[0] {
arrangements += calculateArrangements(cache, record[1:], groups[1:], 0)
}
case '?':
// we assume the ? is a #
arrangements += calculateArrangements(cache, record[1:], groups, curGroupLength+1)
// we assume the ? is a .
if curGroupLength == 0 {
arrangements += calculateArrangements(cache, record[1:], groups, 0)
} else if len(groups) > 0 && curGroupLength == groups[0] {
arrangements += calculateArrangements(cache, record[1:], groups[1:], 0)
}
}
(*cache)[key] = arrangements
return arrangements
}
func buildCacheKey(record string, groups []int, curGroupLength int) string {
var key = make([]string, len(groups)+2)
key[0] = record
for i, group := range groups {
key[i+1] = strconv.Itoa(group)
}
key[len(key)-1] = strconv.Itoa(curGroupLength)
return strings.Join(key, ",")
}