-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDay23.cs
More file actions
235 lines (193 loc) · 9.07 KB
/
Day23.cs
File metadata and controls
235 lines (193 loc) · 9.07 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using AdventOfCode.CSharp.Common;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace AdventOfCode.CSharp.Y2021.Solvers;
public class Day23 : ISolver
{
const byte AmphipodA = 0;
const byte AmphipodB = 1;
const byte AmphipodC = 2;
const byte AmphipodD = 3;
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
static byte CharToAmphipodType(byte c)
{
return (byte)(c - 'A');
}
const int InputRowWidth = 14;
var slotA1 = CharToAmphipodType(input[InputRowWidth * 2 + 3]);
var slotB1 = CharToAmphipodType(input[InputRowWidth * 2 + 5]);
var slotC1 = CharToAmphipodType(input[InputRowWidth * 2 + 7]);
var slotD1 = CharToAmphipodType(input[InputRowWidth * 2 + 9]);
var slotA2 = CharToAmphipodType(input[InputRowWidth * 3 + 3]);
var slotB2 = CharToAmphipodType(input[InputRowWidth * 3 + 5]);
var slotC2 = CharToAmphipodType(input[InputRowWidth * 3 + 7]);
var slotD2 = CharToAmphipodType(input[InputRowWidth * 3 + 9]);
// Part 1 is simulated by assuming the bottom two slots are already filled with the correct amphipods
ulong part1InitialState =
CreateSlot(AmphipodA, slotA1, slotA2, AmphipodA, AmphipodA) |
((uint)CreateSlot(AmphipodB, slotB1, slotB2, AmphipodB, AmphipodB) << 8) |
((uint)CreateSlot(AmphipodC, slotC1, slotC2, AmphipodC, AmphipodC) << 16) |
((uint)CreateSlot(AmphipodD, slotD1, slotD2, AmphipodD, AmphipodD) << 24);
ulong part2InitialState =
CreateSlot(AmphipodA, slotA1, AmphipodD, AmphipodD, slotA2) |
((uint)CreateSlot(AmphipodB, slotB1, AmphipodC, AmphipodB, slotB2) << 8) |
((uint)CreateSlot(AmphipodC, slotC1, AmphipodB, AmphipodA, slotC2) << 16) |
((uint)CreateSlot(AmphipodD, slotD1, AmphipodA, AmphipodC, slotD2) << 24);
var part1 = Solve(part1InitialState);
var part2 = Solve(part2InitialState);
solution.SubmitPart1(part1);
solution.SubmitPart2(part2);
}
private static uint Solve(ulong initialState)
{
var minimumCost = MinimumCost(initialState);
var seen = new HashSet<ulong>(8192);
var pq = new PriorityQueue<ulong, uint>(8192);
pq.Enqueue(initialState, minimumCost * 16 + 16);
while (pq.TryDequeue(out var state, out var distance))
{
if (seen.Contains(state))
continue;
seen.Add(state);
var slots = (uint)(state & 0xFFFFFFFFU);
var topRow = (uint)(state >> 32);
// Try see if any amphipods in the top row can go straight to their slot
if (TryMoveAmphipodFromTopRowToSlot(slots, topRow, out var newState))
{
// If any amphipods moved into their slot, then there is no point considering further moves as it is an
// optimal decision to make
pq.Enqueue(newState, distance - 1);
continue;
}
var isFinalState = true;
for (var amph = AmphipodA; amph <= AmphipodD; amph++)
{
if (!CanMoveToSlot(amph, slots))
{
isFinalState = false;
var newSlots = PopFromSlot(amph, slots, out var newAmphipod);
if (CanMoveToSlot(newAmphipod, newSlots) && IsPathFromSlotToSlotClear(topRow, amph, newAmphipod))
{
pq.Enqueue((ulong)topRow << 32 | newSlots, distance - 1);
break;
}
var moveCost = 16 * GetMoveCost(newAmphipod);
// We know that an amphipod can't stop directly outside it's spot, so we have already added moveCost * 2
// when determining the minimum distance, so we subtract it here to counteract that.
var newDistanceStart = (uint)(distance + (amph == newAmphipod ? -moveCost * 2 : 0));
// Try move left
var newDistance = newDistanceStart;
for (var i = amph + 1; i >= 0 && ((topRow & (0xFU << (4 * i))) == 0); i--)
{
if (i < (newAmphipod + 2))
newDistance += (i != amph + 1 && i != newAmphipod + 1 && i > 0 ? 4U : 2U) * moveCost;
var newTopRow = topRow | ((8U + newAmphipod) << (4 * i));
pq.Enqueue((ulong)newTopRow << 32 | newSlots, newDistance);
}
// Try move right
newDistance = newDistanceStart;
for (var i = amph + 2; i < 7 && ((topRow & (0xFU << (4 * i))) == 0); i++)
{
if (i > (newAmphipod + 1))
newDistance += (i != amph + 2 && i != newAmphipod + 2 && i < 6 ? 4U : 2U) * moveCost;
var newTopRow = topRow | ((8U + newAmphipod) << (4 * i));
pq.Enqueue((ulong)newTopRow << 32 | newSlots, newDistance);
}
}
}
if (isFinalState)
return distance / 16;
}
return 0;
}
private static byte CreateSlot(byte expectedAmphipod, byte a1, byte a2, byte a3, byte a4)
{
a1 = (byte)((a1 + 4 - expectedAmphipod) & 3);
a2 = (byte)((a2 + 4 - expectedAmphipod) & 3);
a3 = (byte)((a3 + 4 - expectedAmphipod) & 3);
a4 = (byte)((a4 + 4 - expectedAmphipod) & 3);
return (byte)(a1 | (a2 << 2) | (a3 << 4) | (a4 << 6));
}
private static uint MinimumCost(ulong state)
{
var totalCost = 0;
var slots = (uint)(state & 0xFFFFFFFFU);
for (byte expectedAmphipod = 0; expectedAmphipod < 4; expectedAmphipod++)
{
var slot = (byte)(slots & 0xFFU);
for (var j = 0; j < 4; j++)
{
if (slot == 0)
break;
var amphipod = (byte)((slot + expectedAmphipod) & 3);
var distanceBetweenSlots =
amphipod == expectedAmphipod
? 2 // We must move twice even if the slot is the same
: Math.Abs(amphipod - expectedAmphipod) * 2;
// Cost to move incorrect amphipod to space above its correct slot
totalCost += (j + 1 + distanceBetweenSlots) * (int)GetMoveCost(amphipod);
// Cost to move amphipod from above its slot into this position
totalCost += (j + 1) * (int)GetMoveCost(expectedAmphipod);
slot >>= 2;
}
slots >>= 8;
}
return (uint)totalCost;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool CanMoveToSlot(byte amphipod, uint slots) => (slots & (0xFFU << (8 * amphipod))) == 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint PopFromSlot(byte amphipod, uint slots, out byte newAmphipod)
{
var slotStart = 8 * amphipod;
var slot = (slots >> slotStart) & 0xFFU;
newAmphipod = (byte)((slot + amphipod) & 3);
var slotMask = 0xFFU << slotStart;
return ((slot >> 2) << slotStart) | (slots & ~slotMask);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static uint GetMoveCost(byte amph) => (uint)(((1UL | (10UL << 16) | (100UL << 32) | (1000UL << 48)) >> (amph * 16)) & 0xFFFF);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static uint GetTopRowMask(int length, int start) => ((1U << (4 * length)) - 1) << (4 * start);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static bool IsPathFromTopToSlotClear(uint topRow, int start, byte slot)
{
var leftOfSlot = slot + 1;
var diff = start - leftOfSlot;
var pathMask = diff <= 0 ? GetTopRowMask(-diff, start + 1) : GetTopRowMask(diff - 1, leftOfSlot + 1);
return (pathMask & topRow) == 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static bool IsPathFromSlotToSlotClear(uint topRow, byte slot1, byte slot2)
{
var diff = slot1 - slot2;
var pathMask = diff < 0 ? GetTopRowMask(-diff, slot1 + 2) : GetTopRowMask(diff, slot2 + 2);
return (pathMask & topRow) == 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool TryMoveAmphipodFromTopRowToSlot(uint slots, uint topRow, out ulong newState)
{
var t = topRow;
var rowIndex = 0;
while (t != 0)
{
var cell = t & 0xF;
if (cell != 0)
{
var amph = (byte)(cell & 3);
if (CanMoveToSlot(amph, slots) && IsPathFromTopToSlotClear(topRow, rowIndex, amph))
{
var newTopRow = topRow ^ (cell << (rowIndex * 4));
newState = ((ulong)newTopRow) << 32 | slots;
return true;
}
}
t >>= 4;
rowIndex++;
}
newState = default;
return false;
}
}