-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
105 lines (88 loc) · 2.52 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
// This operation is being repeated for every digit
static long MysteriousOperation(int input, long z, (int, int) parameters)
{
long x = z % 26;
if (parameters.Item1 < 0)
{
z /= 26;
}
// This is the if we are trying to avoid for negavit first parameter
if (x != input - parameters.Item1)
{
z *= 26;
z += input + parameters.Item2;
}
return z;
}
// This is the MONAD that the submarine would actually run
static bool Monad(string input, (int, int)[] parameters)
{
if (input.Contains('0'))
{
return false;
}
var position = 0;
int GetNext()
{
return input[position++] - '0';
}
long z = 0;
for (int i = 0; i < parameters.Length; ++i)
{
z = MysteriousOperation(GetNext(), z, parameters[i]);
}
return z == 0;
}
static long? FindModelNumberRecursive(long z, (int, int)[] parameters, int index, long acc, bool highestNumber)
{
if (index == parameters.Length)
{
return z == 0 ? acc : null;
}
// Based on highest/lowest flag, either run for 1..9 or 9..1
for (int i = highestNumber ? 9 : 1; (highestNumber && i > 0) || (!highestNumber && i <= 9); i += (highestNumber ? -1 : 1))
{
var p = parameters[index];
// When z can go down, let's make sure it does
// When first parameters is < 0, z is divided so let's make sure we don't hit the if() that multiplies z later
if (p.Item1 > 0 || z % 26 == i - p.Item1)
{
var newZ = MysteriousOperation(i, z, p);
var result = FindModelNumberRecursive(newZ, parameters, index + 1, acc * 10 + i, highestNumber);
if (result != null)
{
// This is an extra check but why not..
if (!Monad(result.ToString()!, parameters))
{
throw new Exception("???");
}
return result;
}
}
}
return null;
}
static long? FindModelNumber(bool highestNumber)
{
// This comes from the input
var monadParameters = new[]
{
(15, 13),
(10, 16),
(12, 2),
(10, 8),
(14, 11),
(-11, 6),
(10, 12),
(-16, 2),
(-9, 2),
(11, 15),
(-8, 1),
(-8, 10),
(-10, 14),
(-9, 10),
};
return FindModelNumberRecursive(0, monadParameters, 0, 0, highestNumber);
}
Console.WriteLine("Part 1: " + FindModelNumber(true));
Console.WriteLine("Part 2: " + FindModelNumber(false));