-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
153 lines (124 loc) · 3.8 KB
/
Program.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
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
using System.Data;
using AdventOfCode.Common;
using Range = AdventOfCode.Common.Range;
var lines = Resources.GetInputFileLines();
var workflows = lines
.Where(l => !l.StartsWith('{'))
.Select(Workflow.FromDefinition)
.ToDictionary(w => w.Name);
var parts = lines
.Where(l => l.StartsWith('{'))
.Select(Part.FromDefinition)
.ToList();
//var acceptedParts = parts
// .Where(IsAccepted)
// .Select(part => 0L + part.X + part.M + part.A + part.S)
// .Sum();
//Console.WriteLine($"Part 1: {acceptedParts}");
var paths = FindPaths();
var ranges = new List<(string Type, Range Range)>();
var xRanges = new List<Range?>();
var mRanges = new List<Range?>();
var aRanges = new List<Range?>();
var sRanges = new List<Range?>();
var a = 0L;
foreach (var path in paths)
{
Console.WriteLine(string.Join(" -> ", path.Select(p => p.Workflow)));
var rules = path
.ToLookup(p => p.GetType(), p => p);
var xRange = new Range(1, 4000);
var mRange = new Range(1, 4000);
var aRange = new Range(1, 4000);
var sRange = new Range(1, 4000);
foreach (var group in rules)
{
foreach (var rule in group)
{
switch (group.Key.Name[0])
{
case 'X':
xRange = xRange?.Intersect(rule.LessThan ? new Range(1, rule.Target) : new Range(rule.Target + 1, int.MaxValue));
break;
case 'M':
mRange = mRange?.Intersect(rule.LessThan ? new Range(1, rule.Target) : new Range(rule.Target + 1, int.MaxValue));
break;
case 'A':
aRange = aRange?.Intersect(rule.LessThan ? new Range(1, rule.Target) : new Range(rule.Target + 1, int.MaxValue));
break;
case 'S':
sRange = sRange?.Intersect(rule.LessThan ? new Range(1, rule.Target) : new Range(rule.Target + 1, int.MaxValue));
break;
};
}
}
Console.WriteLine($" " + string.Join(" ,", xRange, mRange, aRange, sRange));
xRanges.Add(xRange);
mRanges.Add(mRange);
aRanges.Add(aRange);
sRanges.Add(sRange);
a += (xRange?.End ?? 0 - xRange?.Start ?? 0)
* (mRange?.End ?? 0 - mRange?.Start ?? 0)
* (aRange?.End ?? 0 - aRange?.Start ?? 0)
* (sRange?.End ?? 0 - sRange?.Start ?? 0);
Console.WriteLine();
Console.WriteLine();
}
var total = 1L;
foreach (var ranges2 in new[] { xRanges, mRanges, aRanges, sRanges })
{
var count = 0;
for (int i = 1; i <= 4000; i++)
{
if (ranges2.Any(r => r?.Contains(i) ?? false))
{
count++;
}
}
Console.WriteLine("!!! " + count);
total += count;
}
Console.WriteLine(a);
Console.WriteLine(4000L * 4000L * 4000L * 4000L - a);
Console.WriteLine(total);
List<List<Rule>> FindPathsRecursive(string workflow, List<Rule> path)
{
if (workflow == "A")
{
return [[..path]];
}
if (workflow == "R")
{
return [];
}
var result = new List<List<Rule>>();
foreach (var rule in workflows![workflow].Rules)
{
// No loops
//if (path.Any(r => r. == rule.Workflow))
//{
// continue;
//}
result.AddRange(FindPathsRecursive(rule.Workflow, [.. path, rule]));
path.Add(rule with
{
LessThan = !rule.LessThan,
Target = rule.LessThan ? rule.Target - 1 : rule.Target + 1,
Workflow = "!" + rule.Workflow,
});
}
return result;
}
List<List<Rule>> FindPaths()
{
return FindPathsRecursive("in", []);
}
//bool IsAccepted(Part part)
//{
// var workflow = "in";
// while (workflow != "A" && workflow != "R")
// {
// workflow = workflows[workflow].Evaluate(part);
// }
// return workflow == "A";
//}