-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc2024_day_19.swift
97 lines (77 loc) · 2.17 KB
/
aoc2024_day_19.swift
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
import Foundation
import UIKit
import HeapModule
import OrderedCollections
class ViewController: UIViewController {
let input =
"""
"""
let testInput =
"""
r, wr, b, g, bwu, rb, gb, br
brwrr
bggr
gbbr
rrbgbr
ubwu
bwurrg
brgr
bbrgwb
"""
override func viewDidLoad() {
super.viewDidLoad()
parse()
part1()
part2()
}
var strings: [String] = []
func parse() {
strings = testInput.split(separator: "\n", omittingEmptySubsequences: true)
// strings = input.split(separator: "\n", omittingEmptySubsequences: true)
.map({ String($0) })
possiblePatterns = strings[0].split(separator: ", ").map({ String($0) })
designs = strings.dropFirst().map({ String($0) })
}
var possiblePatterns: [String] = []
var designs: [String] = []
func exploreDesign(design: String) -> UInt128 {
var indexDict = OrderedDictionary<UInt128, UInt128>()
indexDict[0] = 1
while let key = indexDict.keys.min(),
let value = indexDict[key] {
if key == design.count {
return value
}
let nextIndecies: [UInt128]
let remaining = design.suffix(design.count - Int(key))
let exist = possiblePatterns.filter({ remaining.hasPrefix($0) })
nextIndecies = exist.map({ key + UInt128($0.count) })
for nextIndex in nextIndecies {
if let count = indexDict[nextIndex] {
indexDict[nextIndex] = count + value
} else {
indexDict[nextIndex] = value
}
}
indexDict[key] = nil
}
return 0
}
func part1() {
var uniqueCount = 0
var allCount: UInt128 = 0
for (_, design) in designs.enumerated() {
let count = exploreDesign(design: design)
if count > 0 {
uniqueCount += 1
allCount += count
print(design, count)
}
}
print(uniqueCount)
// part 2
print(allCount)
}
func part2() {
}
}