-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
186 lines (152 loc) · 4.25 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"flag"
"fmt"
"strconv"
"strings"
"github.com/itsluketwist/advent-of-code-2023/utils"
)
const Day = 12
func main() {
part := flag.Int("part", 0, "Which parts to try?")
try := flag.Int("try", 0, "Whether to try the real input?")
flag.Parse()
fmt.Println("Running day", Day, "( part:", *part, ", try:", *try, ")")
exampleOne, _ := utils.ReadFileToArray(Day, "example1", false)
exampleTwo, _ := utils.ReadFileToArray(Day, "example1", false)
input, _ := utils.ReadFileToArray(Day, "input", false)
if *part == 0 || *part == 1 {
solutionOneExample := SolvePartOne(exampleOne)
fmt.Println("Solution to part 1 (example):", solutionOneExample)
if *try == 1 {
SolutionOneInput := SolvePartOne(input)
fmt.Println("Solution to part 1 (input):", SolutionOneInput)
}
}
if *part == 0 || *part == 2 {
SolutionTwoExample := SolvePartTwo(exampleTwo)
fmt.Println("Solution to part 2 (example):", SolutionTwoExample)
if *try == 1 {
SolutionTwoInput := SolvePartTwo(input)
fmt.Println("Solution to part 2 (input):", SolutionTwoInput)
}
}
}
type SpringRecord struct {
record string
damagedGroups []int
unknownIndexes []int
missing int
}
func parseData(data []string, repeat int) []SpringRecord {
var springs []SpringRecord
for _, line := range data {
split := strings.Split(line, " ")
record := split[0]
numbers := split[1]
for i := 0; i < repeat; i++ {
record += "?"
record += split[0]
numbers += ","
numbers += split[1]
}
record += "."
var damagedGroups []int
damagedTotal := 0
strNums := strings.Split(numbers, ",")
for _, strNum := range strNums {
num, _ := strconv.Atoi(strNum)
damagedGroups = append(damagedGroups, num)
damagedTotal += num
}
damagedKnown := 0
var unknownIndexes []int
for i, spr := range record {
if spr == '#' {
damagedKnown++
} else if spr == '?' {
unknownIndexes = append(unknownIndexes, i)
}
}
springs = append(springs, SpringRecord{
record: record,
damagedGroups: damagedGroups,
unknownIndexes: unknownIndexes,
missing: damagedTotal - damagedKnown,
})
}
return springs
}
type State struct {
position int // string index to consider from
matches int // count of currently matched spring groups
current int // current length of group being processed
}
func Permutations(sta State, rec SpringRecord, cache map[State]int) int {
if num, ok := cache[sta]; ok {
return num // use cached result
}
if sta.position == len(rec.record)-1 {
// either have already matched all groups
// or finishing to match the last group
if (sta.matches == len(rec.damagedGroups) && sta.current == 0) ||
(sta.matches == len(rec.damagedGroups)-1 && sta.current == rec.damagedGroups[len(rec.damagedGroups)-1]) {
cache[sta] = 1
return 1
} else {
cache[sta] = 0
return 0
}
}
hashResult := 0
if rec.record[sta.position] == '#' || rec.record[sta.position] == '?' {
if sta.current == 0 {
// start new group
if sta.matches == len(rec.damagedGroups) {
hashResult = 0
} else {
hashResult = Permutations(State{sta.position + 1, sta.matches, 1}, rec, cache)
}
} else {
// continue current group
if sta.current+1 > rec.damagedGroups[sta.matches] {
hashResult = 0
} else {
hashResult = Permutations(State{sta.position + 1, sta.matches, sta.current + 1}, rec, cache)
}
}
}
dotResult := 0
if rec.record[sta.position] == '.' || rec.record[sta.position] == '?' {
if sta.current == 0 {
// no group, just continue
dotResult = Permutations(State{sta.position + 1, sta.matches, 0}, rec, cache)
} else {
// end of a group
if sta.current == rec.damagedGroups[sta.matches] {
dotResult = Permutations(State{sta.position + 1, sta.matches + 1, 0}, rec, cache)
} else {
dotResult = 0
}
}
}
// cache and return the result
result := dotResult + hashResult
cache[sta] = result
return result
}
func Solve(data []string, repeat int) int {
parsed := parseData(data, repeat)
total := 0
for _, record := range parsed {
blankCache := make(map[State]int)
total += Permutations(State{0, 0, 0}, record, blankCache)
}
return total
}
func SolvePartOne(data []string) int {
return Solve(data, 0)
}
func SolvePartTwo(data []string) int {
return Solve(data, 4)
}