day 23 part 1
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Ben Harris 2021-11-14 14:11:01 -05:00
parent 7e4899ae2e
commit a8e60e3da2
2 changed files with 72 additions and 3 deletions

View File

@ -29,7 +29,7 @@ public class DayTests
//[DataRow(typeof(Day20), "21599955909991", "")]
[DataRow(typeof(Day21), "2436", "dhfng,pgblcd,xhkdc,ghlzj,dstct,nqbnmzx,ntggc,znrzgs")]
[DataRow(typeof(Day22), "32856", "33805")]
//[DataRow(typeof(Day23), "", "")]
[DataRow(typeof(Day23), "36542897", "")]
//[DataRow(typeof(Day24), "", "")]
//[DataRow(typeof(Day25), "", "")]
public void CheckAllDays(Type dayType, string part1, string part2)

View File

@ -5,11 +5,80 @@ namespace aoc2020;
/// </summary>
public sealed class Day23 : Day
{
private readonly Dictionary<long, long> cups = new();
private readonly ImmutableList<long> initialCups;
private readonly long[] move;
private long current;
public Day23() : base(23, "Crab Cups")
{
initialCups = Input.First().Select(c => long.Parse(c.ToString())).ToImmutableList();
current = initialCups.First();
move = new long[3];
}
public override string Part1() => "";
private void DoMoves(int turns)
{
for (var turn = 0; turn < turns; turn++)
{
var dest = current - 1;
if (dest == 0) dest = cups.Count;
public override string Part2() => "";
for (var i = 0; i <= 2; i++)
{
var id = cups[current];
var removedNext = cups[id];
cups.Remove(id);
cups[current] = removedNext;
move[i] = id;
}
while (move.Contains(dest))
{
dest--;
if (dest == 0) dest = cups.Count + 3;
}
for (var i = 0; i <= 2; i++)
{
var id = cups[dest];
cups[dest] = move[i];
cups.Add(move[i], id);
dest = cups[dest];
}
current = cups[current];
}
}
public override string Part1()
{
for (var i = 0; i < initialCups.Count; i++)
cups[initialCups[i]] = initialCups[(i + 1) % initialCups.Count];
DoMoves(100);
current = 1;
var result = new StringBuilder();
while (cups[current] != 1)
{
result.Append(cups[current]);
current = cups[current];
}
return result.ToString();
}
public override string Part2()
{
cups[initialCups.Last()] = 10;
for (var i = 10; i < 1_000_000; i++)
cups.Add(i, i + 1);
cups[1_000_000] = initialCups.First();
DoMoves(10_000_000);
return $"{cups[1] * cups[cups[1]]}";
}
}