Skip to content

Commit a83584e

Browse files
committed
day19
1 parent 1fa10c4 commit a83584e

File tree

7 files changed

+224
-149
lines changed

7 files changed

+224
-149
lines changed

.swiftpm/xcode/xcshareddata/xcschemes/AdventOfCode2020-Package.xcscheme

+14
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,20 @@
720720
ReferencedContainer = "container:">
721721
</BuildableReference>
722722
</BuildActionEntry>
723+
<BuildActionEntry
724+
buildForTesting = "YES"
725+
buildForRunning = "YES"
726+
buildForProfiling = "YES"
727+
buildForArchiving = "YES"
728+
buildForAnalyzing = "YES">
729+
<BuildableReference
730+
BuildableIdentifier = "primary"
731+
BlueprintIdentifier = "AdventOfCode2020_Day9"
732+
BuildableName = "AdventOfCode2020_Day9"
733+
BlueprintName = "AdventOfCode2020_Day9"
734+
ReferencedContainer = "container:">
735+
</BuildableReference>
736+
</BuildActionEntry>
723737
</BuildActionEntries>
724738
</BuildAction>
725739
<TestAction

README.md

+19-4
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
|[Day 16: Ticket Translation](https://adventofcode.com/2020/day/16)|⭐️|⭐️|
3131
|[Day 17: Conway Cubes](https://adventofcode.com/2020/day/17)|⭐️|⭐️|
3232
|[Day 18: Operation Order](https://adventofcode.com/2020/day/18)|⭐️|⭐️|
33-
|[Day 19: Monster Messages](https://adventofcode.com/2020/day/19)|⭐️|🌵|
34-
|[Day 20: ](https://adventofcode.com/2020/day/20)|🌵||
35-
|[Day 21: ](https://adventofcode.com/2020/day/21)|🌵||
33+
|[Day 19: Monster Messages](https://adventofcode.com/2020/day/19)|⭐️|⭐️|
34+
|[Day 20: Jurassic Jigsaw](https://adventofcode.com/2020/day/20)|🌵||
35+
|[Day 21: Allergen Assessment ](https://adventofcode.com/2020/day/21)|🌵||
3636
|[Day 22: ](https://adventofcode.com/2020/day/22)|🌵||
3737
|[Day 23: ](https://adventofcode.com/2020/day/23)|🌵||
3838
|[Day 24: ](https://adventofcode.com/2020/day/24)|🌵||
@@ -907,4 +907,19 @@ print("Solution part one : ", count)
907907

908908
```
909909

910-
```
910+
## Day19
911+
This is the winning regex. See the strategically positioned curly brackets ;) It results from RegexA{x,} plus RegexA{y} plus RegexB{y} phewwww!!
912+
913+
<p align="center">
914+
<img src="/images/regex.png" width="600" title="regex"></img>
915+
</p>
916+
917+
Well to be fair that only one of the winning regexes because they are dynamically created. I iterate changing the values in the curly braces of course (just saying for completeness, even I know nobody would bother to check :)
918+
919+
Quite a tough one, but regexes are phenomenal, I never realised they had so much power!
920+
If I paste this regex in Lea Verou web page regex playground it works!
921+
https://projects.verou.me/regexplained/
922+
And they are so fast! How do they accomplish this? 🤔
923+
924+
925+
https://projects.verou.me/regexplained/

Sources/Day19/README.md

+90
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,93 @@ aabbbbbaabbbaaaaaabbbbbababaaaaabbaaabba
139139
```
140140
After updating rules 8 and 11, how many messages completely match rule 0?
141141

142+
Your puzzle answer was 184.
143+
144+
## Part Two
145+
146+
As you look over the list of messages, you realize your matching rules aren't quite right. To fix them, completely replace rules 8: 42 and 11: 42 31 with the following:
147+
```
148+
8: 42 | 42 8
149+
11: 42 31 | 42 11 31
150+
```
151+
This small change has a big impact: now, the rules do contain loops, and the list of messages they could hypothetically match is infinite. You'll need to determine how these changes affect which messages are valid.
152+
153+
Fortunately, many of the rules are unaffected by this change; it might help to start by looking at which rules always match the same set of values and how those rules (especially rules 42 and 31) are used by the new versions of rules 8 and 11.
154+
155+
(Remember, you only need to handle the rules you have; building a solution that could handle any hypothetical combination of rules would be significantly more difficult.)
156+
157+
For example:
158+
```
159+
42: 9 14 | 10 1
160+
9: 14 27 | 1 26
161+
10: 23 14 | 28 1
162+
1: "a"
163+
11: 42 31
164+
5: 1 14 | 15 1
165+
19: 14 1 | 14 14
166+
12: 24 14 | 19 1
167+
16: 15 1 | 14 14
168+
31: 14 17 | 1 13
169+
6: 14 14 | 1 14
170+
2: 1 24 | 14 4
171+
0: 8 11
172+
13: 14 3 | 1 12
173+
15: 1 | 14
174+
17: 14 2 | 1 7
175+
23: 25 1 | 22 14
176+
28: 16 1
177+
4: 1 1
178+
20: 14 14 | 1 15
179+
3: 5 14 | 16 1
180+
27: 1 6 | 14 18
181+
14: "b"
182+
21: 14 1 | 1 14
183+
25: 1 1 | 1 14
184+
22: 14 14
185+
8: 42
186+
26: 14 22 | 1 20
187+
18: 15 15
188+
7: 14 5 | 1 21
189+
24: 14 1
190+
191+
abbbbbabbbaaaababbaabbbbabababbbabbbbbbabaaaa
192+
bbabbbbaabaabba
193+
babbbbaabbbbbabbbbbbaabaaabaaa
194+
aaabbbbbbaaaabaababaabababbabaaabbababababaaa
195+
bbbbbbbaaaabbbbaaabbabaaa
196+
bbbababbbbaaaaaaaabbababaaababaabab
197+
ababaaaaaabaaab
198+
ababaaaaabbbaba
199+
baabbaaaabbaaaababbaababb
200+
abbbbabbbbaaaababbbbbbaaaababb
201+
aaaaabbaabaaaaababaa
202+
aaaabbaaaabbaaa
203+
aaaabbaabbaaaaaaabbbabbbaaabbaabaaa
204+
babaaabbbaaabaababbaabababaaab
205+
aabbbbbaabbbaaaaaabbbbbababaaaaabbaaabba
206+
```
207+
Without updating rules 8 and 11, these rules only match three messages: `bbabbbbaabaabba`, `ababaaaaaabaaab`, and `ababaaaaabbbaba`.
208+
209+
However, after updating rules 8 and 11, a total of 12 messages match:
210+
```
211+
bbabbbbaabaabba
212+
babbbbaabbbbbabbbbbbaabaaabaaa
213+
aaabbbbbbaaaabaababaabababbabaaabbababababaaa
214+
bbbbbbbaaaabbbbaaabbabaaa
215+
bbbababbbbaaaaaaaabbababaaababaabab
216+
ababaaaaaabaaab
217+
ababaaaaabbbaba
218+
baabbaaaabbaaaababbaababb
219+
abbbbabbbbaaaababbbbbbaaaababb
220+
aaaaabbaabaaaaababaa
221+
aaaabbaabbaaaaaaabbbabbbaaabbaabaaa
222+
aabbbbbaabbbaaaaaabbbbbababaaaaabbaaabba
223+
```
224+
After updating rules 8 and 11, how many messages completely match rule 0?
225+
226+
Your puzzle answer was 389.
227+
228+
Both parts of this puzzle are complete! They provide two gold stars: **
229+
230+
231+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
42: 9 14 | 10 1
2+
9: 14 27 | 1 26
3+
10: 23 14 | 28 1
4+
1: "a"
5+
11: 42 31 | 42 11 31
6+
5: 1 14 | 15 1
7+
19: 14 1 | 14 14
8+
12: 24 14 | 19 1
9+
16: 15 1 | 14 14
10+
31: 14 17 | 1 13
11+
6: 14 14 | 1 14
12+
2: 1 24 | 14 4
13+
0: 8 11
14+
13: 14 3 | 1 12
15+
15: 1 | 14
16+
17: 14 2 | 1 7
17+
23: 25 1 | 22 14
18+
28: 16 1
19+
4: 1 1
20+
20: 14 14 | 1 15
21+
3: 5 14 | 16 1
22+
27: 1 6 | 14 18
23+
14: "b"
24+
21: 14 1 | 1 14
25+
25: 1 1 | 1 14
26+
22: 14 14
27+
8: 42 | 42 8
28+
26: 14 22 | 1 20
29+
18: 15 15
30+
7: 14 5 | 1 21
31+
24: 14 1
32+
33+
abbbbbabbbaaaababbaabbbbabababbbabbbbbbabaaaa
34+
bbabbbbaabaabba
35+
babbbbaabbbbbabbbbbbaabaaabaaa
36+
aaabbbbbbaaaabaababaabababbabaaabbababababaaa
37+
bbbbbbbaaaabbbbaaabbabaaa
38+
bbbababbbbaaaaaaaabbababaaababaabab
39+
ababaaaaaabaaab
40+
ababaaaaabbbaba
41+
baabbaaaabbaaaababbaababb
42+
abbbbabbbbaaaababbbbbbaaaababb
43+
aaaaabbaabaaaaababaa
44+
aaaabbaaaabbaaa
45+
aaaabbaabbaaaaaaabbbabbbaaabbaabaaa
46+
babaaabbbaaabaababbaabababaaab
47+
aabbbbbaabbbaaaaaabbbbbababaaaaabbaaabba

Sources/Day19/main.swift

+54-68
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ struct Day19: ParsableCommand {
88
//Declare optional argument. Drag the input file to terminal!
99
@Option(name: [.short, .customLong("inputFile")], help: "Specify the path to the input file.")
1010
var inputFile : String = ""
11-
11+
1212
func run() throws {
1313
var input: [Array<String>.SubSequence] = []
1414
if !inputFile.isEmpty {
1515
let url = URL(fileURLWithPath: inputFile)
1616
guard let inputFile = try? String(contentsOf: url).lines else {fatalError()}
1717
input = inputFile.split {$0 == "" }
1818
} else {
19-
print("Running Day17 Challenge with input from the website\n")
20-
guard let url = Bundle.module.url(forResource: "input2", withExtension: "txt") else { fatalError()}
19+
print("Running Day19 Challenge with input from the website\n")
20+
guard let url = Bundle.module.url(forResource: "input", withExtension: "txt") else { fatalError()}
2121
guard let inputFile = try? String(contentsOf: url).lines else {fatalError()}
2222
input = inputFile.split {$0 == "" }
2323
}
24-
print(input)
24+
//print(input)
2525

2626
let rules = input[0]
2727
let messages = input[1]
@@ -32,91 +32,33 @@ struct Day19: ParsableCommand {
3232
let key = Int(regex[0])!
3333
let expr = " " + regex[1] + " "
3434
rulesDict[key] = expr
35-
print("key, expr", key, expr)
3635
} else if let regex = rule.getCapturedGroupsFrom(regexPattern: "(\\d+):(.+)") {
3736
let key = Int(regex[0])!
3837
var expr = regex[1]
39-
expr = ("(" + expr + " )")
40-
print("key, expr", key, expr)
38+
expr = ("(" + expr + " )")//.replacingOccurrences(of: " ", with: "")
4139
rulesDict[key] = expr
4240
}
4341
}
44-
var resolvedDict: [Int: Int] = [:]
42+
4543
func createRegex(from ruleZero: String) -> String {
4644
let arrayRules = ruleZero.split(separator: " ").map {String($0)}
4745
var regexPattern = arrayRules
4846
for (idx,rule) in arrayRules.enumerated() {
4947
if rule == "(" || rule == ")" || rule == "a" || rule == "b" || rule == "|" {continue}
5048
print("rules",rule)
51-
let keyRule = Int(String(rule))!
52-
print("keyRule",keyRule)
53-
if let itemToResolve = rulesDict[keyRule] {
49+
if let itemToResolve = rulesDict[Int(String(rule))!] {
5450
print(itemToResolve)
55-
if itemToResolve == " a " || itemToResolve == " b " {
56-
regexPattern[idx] = itemToResolve
57-
continue
58-
} else if resolvedDict[keyRule, default: 0] < 200 {
59-
print("resolvedDict[\(keyRule), default: 0] ", resolvedDict[keyRule, default: 0] )
60-
resolvedDict[keyRule, default: 0] += 1
61-
regexPattern[idx] = itemToResolve
62-
} else {
63-
print("skipped ------ ", keyRule)
64-
}
51+
regexPattern[idx] = itemToResolve
6552
}
6653
}
6754
return regexPattern.joined(separator: " ")
6855
}
6956

7057
var regexPattern = (rulesDict[0] ?? "")
71-
//while !regexPattern.allSatisfy({"ab()| ".contains($0)}) {
72-
for i in 0..<5{
73-
regexPattern = createRegex(from: regexPattern)
74-
75-
regexPattern = createRegex(from: regexPattern)
76-
regexPattern = createRegex(from: regexPattern)
77-
regexPattern = createRegex(from: regexPattern)
78-
79-
regexPattern = createRegex(from: regexPattern)
80-
81-
regexPattern = createRegex(from: regexPattern)
82-
regexPattern = createRegex(from: regexPattern)
83-
84-
regexPattern = createRegex(from: regexPattern)
85-
regexPattern = createRegex(from: regexPattern)
86-
87-
regexPattern = createRegex(from: regexPattern)
88-
regexPattern = createRegex(from: regexPattern)
89-
regexPattern = createRegex(from: regexPattern)
90-
91-
regexPattern = createRegex(from: regexPattern)
92-
93-
regexPattern = createRegex(from: regexPattern)
94-
regexPattern = createRegex(from: regexPattern)
95-
regexPattern = createRegex(from: regexPattern)
96-
regexPattern = createRegex(from: regexPattern)
97-
regexPattern = createRegex(from: regexPattern)
98-
99-
regexPattern = createRegex(from: regexPattern)
100-
101-
regexPattern = createRegex(from: regexPattern)
102-
regexPattern = createRegex(from: regexPattern)
103-
104-
regexPattern = createRegex(from: regexPattern)
105-
regexPattern = createRegex(from: regexPattern)
106-
107-
regexPattern = createRegex(from: regexPattern)
108-
regexPattern = createRegex(from: regexPattern)
109-
110-
regexPattern = createRegex(from: regexPattern)
111-
112-
regexPattern = createRegex(from: regexPattern)
113-
114-
regexPattern = createRegex(from: regexPattern)
58+
while !regexPattern.allSatisfy({"ab()| ".contains($0)}) {
59+
regexPattern = createRegex(from: regexPattern)
11560
}
116-
//}
11761
print(regexPattern)
118-
119-
12062
regexPattern = "^" + regexPattern.replacingOccurrences(of:" ", with: "") + "$"
12163
guard let regex = try? NSRegularExpression(pattern: regexPattern) else { fatalError("invalid regex expression \n") }
12264
var count = 0
@@ -126,6 +68,50 @@ regexPattern = createRegex(from: regexPattern)
12668
}
12769
print("Solution part one : ", count)
12870

71+
regexPattern = (rulesDict[42] ?? "")
72+
print(regexPattern)
73+
while !regexPattern.allSatisfy({"ab()| ".contains($0)}) {
74+
regexPattern = createRegex(from: regexPattern)
75+
}
76+
let regexPattern42First = regexPattern + "{1,}"
77+
let regexPattern42Second = regexPattern + "{x}"
78+
79+
regexPattern = (rulesDict[31] ?? "")
80+
while !regexPattern.allSatisfy({"ab()| ".contains($0)}) {
81+
regexPattern = createRegex(from: regexPattern)
82+
}
83+
let regexPattern31 = regexPattern + "{x}"
84+
85+
regexPattern = regexPattern42First + regexPattern42Second + regexPattern31
86+
print(regexPattern)
87+
let regexPatternX = "^" + regexPattern.replacingOccurrences(of:" ", with: "") + "$"
88+
regexPattern = regexPatternX.replacingOccurrences(of:"x", with: "1")
89+
print(regexPattern)
90+
91+
guard let regexPartTwo = try? NSRegularExpression(pattern: regexPattern) else { fatalError("invalid regex expression \n") }
92+
count = 0
93+
for message in messages {
94+
let range = NSRange(location: 0, length: message.utf16.count)
95+
if regexPartTwo.firstMatch(in: message, options: [], range: range) != nil {
96+
count += 1
97+
continue
98+
}
99+
else {
100+
for i in 2...6 {
101+
//print("alternative regex!! ===== \n\n")
102+
let regexPattern2 = regexPatternX.replacingOccurrences(of:"x", with: String(i))
103+
guard let regex2 = try? NSRegularExpression(pattern: regexPattern2) else { fatalError("invalid regex expression \n") }
104+
if regex2.firstMatch(in: message, options: [], range: range) != nil {
105+
count += 1
106+
continue
107+
}
108+
}
109+
}
110+
}
111+
112+
113+
print("Solution part two : ", count)
114+
129115
}
130116
}
131117

0 commit comments

Comments
 (0)