-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day19.cs
81 lines (67 loc) · 2.42 KB
/
Day19.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Day19 {
public static int Solution1() => Solution((rules, line) => rules[0].Contains(line));
public static int Solution2() => Solution((rules, line) => {
int Count(int rule) {
int count = 0;
while (true) {
bool matched = false;
foreach (string start in rules[rule]) {
if (line.StartsWith(start)) {
line = line.Substring(start.Length);
count++;
matched = true;
break;
}
}
if (!matched) {
return count;
}
}
}
int count42 = Count(42);
if (count42 < 2) {
return false;
}
int count31 = Count(31);
return count31 >= 1 && count42 > count31 && line.Length == 0;
});
static int Solution(Func<Dictionary<int, HashSet<string>>, string, bool> counter) {
string[] input = File.ReadAllText("input19.txt").Split(new[] { "\n\n" }, StringSplitOptions.None);
Dictionary<int, int[][]> rules = new Dictionary<int, int[][]>();
foreach (string[] rule in Array.ConvertAll(input[0].Split('\n'), rule => rule.Split(new[] { ": " }, StringSplitOptions.None))) {
rules[int.Parse(rule[0])] = rule[1] == "\"a\"" ? new[] { new int[] { -'a' } } : rule[1] == "\"b\"" ? new[] { new int[] { -'b' } } : Array.ConvertAll(rule[1].Split(new[] { " | " }, StringSplitOptions.None), result => Array.ConvertAll(result.Split(' '), int.Parse));
}
Dictionary<int, HashSet<string>> ruleResults = new Dictionary<int, HashSet<string>>();
HashSet<string> GetResult(int rule) {
if (!ruleResults.TryGetValue(rule, out HashSet<string> result)) {
result = new HashSet<string>();
foreach (int[] option in rules[rule]) {
result.UnionWith(Array.ConvertAll(option, x => x < 0 ? new HashSet<string> { new string((char)-x, 1) } : GetResult(x)).Aggregate(Combine));
}
ruleResults[rule] = result;
}
return result;
}
HashSet<string> Combine(HashSet<string> first, HashSet<string> second) {
HashSet<string> cartesian = new HashSet<string>();
foreach (string a in first) {
foreach (string b in second) {
cartesian.Add(a + b);
}
}
return cartesian;
}
HashSet<string> validWords = GetResult(0);
int count = 0;
foreach (string line in input[1].Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)) {
if (counter(ruleResults, line)) {
count++;
}
}
return count;
}
}