-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21.cs
More file actions
142 lines (124 loc) · 3.88 KB
/
21.cs
File metadata and controls
142 lines (124 loc) · 3.88 KB
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
List<long?> values = new();
List<Operation?> operations = new();
int root = 0, humn = 0;
{
Dictionary<string, int> keys = new();
int ResolveKey(string name)
{
if (keys.TryGetValue(name, out var key)) return key;
key = keys.Count;
keys.Add(name, key);
values.Add(null);
operations.Add(null);
return key;
}
while (Console.ReadLine()?.Split() is { } line)
{
var name = line[0][..^1];
var key = ResolveKey(name);
root = name == "root" ? key : root;
humn = name == "humn" ? key : humn;
switch (line)
{
case [_, var c]:
values[key] = long.Parse(c);
break;
case [_, var ls, var op, var rs]:
{
var l = ResolveKey(ls);
var r = ResolveKey(rs);
operations[key] = new Operation(l, r, op[0]);
break;
}
}
}
}
var (rootLeft, rootRight, _) = operations[root]!;
var input = values.ToList();
Shout(root, input);
Console.WriteLine($"Root: {input[root]} (left: {input[rootLeft]}, right: {input[rootRight]})");
// Very ad-hoc process to find the solution for part 2, could be generalized more.
Console.WriteLine($"{values.Count(s => !s.HasValue)} unsolved initially");
var originals = values.ToList();
values[humn] = null;
operations[root] = operations[root]! with {Op = '='};
Shout(root, values);
Console.WriteLine($"{values.Count(s => !s.HasValue)} unsolved with humn unknown");
{
HashSet<int> rootLeftDeps = new(), rootRightDeps = new();
DiscoverDependencies(rootLeft, rootLeftDeps);
DiscoverDependencies(rootRight, rootRightDeps);
long TryEvaluate(long x)
{
foreach (var dep in rootLeftDeps)
{
values[dep] = null;
}
values[humn] = x;
return Shout(rootLeft, values)!.Value;
}
// Turns out right side is fully evaluated.
long target = values[rootRight]!.Value;
long zero = TryEvaluate(0),
positive = TryEvaluate(10);
Console.WriteLine($"Results around zero: {zero}, {positive}");
var inputDirection = zero < positive ? 1 : -1;
int searchDirection = zero > target ? -inputDirection : inputDirection;
Console.WriteLine($"Input search direction: {searchDirection}");
long attempt = 0, result;
do
{
result = TryEvaluate(attempt);
attempt += 1000000;
}
while (Math.Sign(result - target) == searchDirection) ;
searchDirection *= -1;
while (result != target)
{
attempt += searchDirection;
result = TryEvaluate(attempt);
}
// There can be multiple solutions, find the lowest one.
for (var i = 0; i < 1000; ++i)
{
attempt = attempt - i;
originals[humn] = attempt;
var check = Shout(root, originals.ToList());
if (check == 0)
{
Console.WriteLine($"Found input: {attempt}");
}
}
}
long? Shout(int key, List<long?> vals)
{
var n = vals[key];
if (n.HasValue || operations[key] == null) return n;
if (operations[key] is var (left, right, op))
{
var l = Shout(left, vals);
var r = Shout(right, vals);
if (!l.HasValue || !r.HasValue) n = null;
else n = op switch
{
'+' => checked(l.Value + r.Value),
'-' => checked(l.Value - r.Value),
'*' => checked(l.Value * r.Value),
'/' => l.Value / r.Value,
_ => l.Value.CompareTo(r.Value)
};
}
vals[key] = n;
return n;
}
void DiscoverDependencies(int key, HashSet<int> dependencies)
{
if (values[key].HasValue) return;
dependencies.Add(key);
if (operations[key] is var (left, right, _))
{
DiscoverDependencies(left, dependencies);
DiscoverDependencies(right, dependencies);
}
}
internal sealed record Operation(int Left, int Right, char Op);