Day 14 solution

This runs slower than I'd like, but I wanted to check my initial solution submitted so I can iterate on it later.
This commit is contained in:
2022-12-14 17:42:23 -06:00
parent ba6ad54329
commit b1d57f4fd0
4 changed files with 281 additions and 102 deletions

View File

@ -63,6 +63,7 @@
<EmbeddedResource Include="inputs\13a.txt" />
<EmbeddedResource Include="inputs\14.txt" />
<None Remove="inputs\15.txt" />
<EmbeddedResource Include="inputs\14a.txt" />
<EmbeddedResource Include="inputs\15.txt" />
<None Remove="inputs\16.txt" />
<EmbeddedResource Include="inputs\16.txt" />

View File

@ -1,102 +1,115 @@
OKSBBKHFBPVNOBKHBPCO
CB -> P
VH -> S
CF -> P
OV -> B
CH -> N
PB -> F
KF -> O
BC -> K
FB -> F
SN -> F
FV -> B
PN -> K
SF -> V
FN -> F
SS -> K
VP -> F
VB -> B
OS -> N
HP -> O
NF -> S
SK -> H
OO -> S
PF -> C
CC -> P
BP -> F
OB -> C
CS -> N
BV -> F
VV -> B
HO -> F
KN -> P
VC -> K
KK -> N
BO -> V
NH -> O
HC -> S
SB -> F
NN -> V
OF -> V
FK -> S
OP -> S
NS -> C
HV -> O
PC -> C
FO -> H
OH -> F
BF -> S
SO -> O
HB -> P
NK -> H
NV -> C
NB -> B
FF -> B
BH -> C
SV -> B
BK -> K
NO -> C
VN -> P
FC -> B
PH -> V
HH -> C
VO -> O
SP -> P
VK -> N
CP -> H
SC -> C
KV -> H
CO -> C
OK -> V
ON -> C
KS -> S
NP -> O
CK -> C
BS -> F
VS -> B
KH -> O
KC -> C
KB -> N
OC -> F
PP -> S
HK -> H
BN -> S
KO -> K
NC -> B
PK -> K
CV -> H
PO -> O
BB -> C
HS -> F
SH -> K
CN -> S
HN -> S
KP -> O
FP -> H
HF -> F
PS -> B
FH -> K
PV -> O
FS -> N
VF -> V
504,62 -> 504,58 -> 504,62 -> 506,62 -> 506,54 -> 506,62 -> 508,62 -> 508,55 -> 508,62
514,69 -> 519,69
534,120 -> 534,110 -> 534,120 -> 536,120 -> 536,115 -> 536,120 -> 538,120 -> 538,117 -> 538,120 -> 540,120 -> 540,119 -> 540,120 -> 542,120 -> 542,116 -> 542,120
510,67 -> 515,67
489,36 -> 489,29 -> 489,36 -> 491,36 -> 491,34 -> 491,36 -> 493,36 -> 493,26 -> 493,36 -> 495,36 -> 495,35 -> 495,36 -> 497,36 -> 497,29 -> 497,36 -> 499,36 -> 499,31 -> 499,36
534,120 -> 534,110 -> 534,120 -> 536,120 -> 536,115 -> 536,120 -> 538,120 -> 538,117 -> 538,120 -> 540,120 -> 540,119 -> 540,120 -> 542,120 -> 542,116 -> 542,120
515,104 -> 520,104
514,86 -> 514,87 -> 530,87 -> 530,86
489,36 -> 489,29 -> 489,36 -> 491,36 -> 491,34 -> 491,36 -> 493,36 -> 493,26 -> 493,36 -> 495,36 -> 495,35 -> 495,36 -> 497,36 -> 497,29 -> 497,36 -> 499,36 -> 499,31 -> 499,36
534,120 -> 534,110 -> 534,120 -> 536,120 -> 536,115 -> 536,120 -> 538,120 -> 538,117 -> 538,120 -> 540,120 -> 540,119 -> 540,120 -> 542,120 -> 542,116 -> 542,120
489,36 -> 489,29 -> 489,36 -> 491,36 -> 491,34 -> 491,36 -> 493,36 -> 493,26 -> 493,36 -> 495,36 -> 495,35 -> 495,36 -> 497,36 -> 497,29 -> 497,36 -> 499,36 -> 499,31 -> 499,36
510,90 -> 510,92 -> 502,92 -> 502,97 -> 516,97 -> 516,92 -> 515,92 -> 515,90
504,62 -> 504,58 -> 504,62 -> 506,62 -> 506,54 -> 506,62 -> 508,62 -> 508,55 -> 508,62
534,120 -> 534,110 -> 534,120 -> 536,120 -> 536,115 -> 536,120 -> 538,120 -> 538,117 -> 538,120 -> 540,120 -> 540,119 -> 540,120 -> 542,120 -> 542,116 -> 542,120
497,71 -> 502,71
521,84 -> 521,82 -> 521,84 -> 523,84 -> 523,82 -> 523,84 -> 525,84 -> 525,77 -> 525,84
534,120 -> 534,110 -> 534,120 -> 536,120 -> 536,115 -> 536,120 -> 538,120 -> 538,117 -> 538,120 -> 540,120 -> 540,119 -> 540,120 -> 542,120 -> 542,116 -> 542,120
568,158 -> 573,158
497,13 -> 497,16 -> 496,16 -> 496,23 -> 511,23 -> 511,16 -> 503,16 -> 503,13
556,152 -> 561,152
497,13 -> 497,16 -> 496,16 -> 496,23 -> 511,23 -> 511,16 -> 503,16 -> 503,13
548,127 -> 553,127
524,107 -> 538,107 -> 538,106
544,125 -> 549,125
497,39 -> 497,43 -> 493,43 -> 493,49 -> 506,49 -> 506,43 -> 501,43 -> 501,39
553,138 -> 553,141 -> 546,141 -> 546,149 -> 558,149 -> 558,141 -> 557,141 -> 557,138
489,36 -> 489,29 -> 489,36 -> 491,36 -> 491,34 -> 491,36 -> 493,36 -> 493,26 -> 493,36 -> 495,36 -> 495,35 -> 495,36 -> 497,36 -> 497,29 -> 497,36 -> 499,36 -> 499,31 -> 499,36
553,138 -> 553,141 -> 546,141 -> 546,149 -> 558,149 -> 558,141 -> 557,141 -> 557,138
534,127 -> 539,127
521,84 -> 521,82 -> 521,84 -> 523,84 -> 523,82 -> 523,84 -> 525,84 -> 525,77 -> 525,84
537,125 -> 542,125
521,84 -> 521,82 -> 521,84 -> 523,84 -> 523,82 -> 523,84 -> 525,84 -> 525,77 -> 525,84
521,84 -> 521,82 -> 521,84 -> 523,84 -> 523,82 -> 523,84 -> 525,84 -> 525,77 -> 525,84
553,138 -> 553,141 -> 546,141 -> 546,149 -> 558,149 -> 558,141 -> 557,141 -> 557,138
489,36 -> 489,29 -> 489,36 -> 491,36 -> 491,34 -> 491,36 -> 493,36 -> 493,26 -> 493,36 -> 495,36 -> 495,35 -> 495,36 -> 497,36 -> 497,29 -> 497,36 -> 499,36 -> 499,31 -> 499,36
497,39 -> 497,43 -> 493,43 -> 493,49 -> 506,49 -> 506,43 -> 501,43 -> 501,39
514,86 -> 514,87 -> 530,87 -> 530,86
534,120 -> 534,110 -> 534,120 -> 536,120 -> 536,115 -> 536,120 -> 538,120 -> 538,117 -> 538,120 -> 540,120 -> 540,119 -> 540,120 -> 542,120 -> 542,116 -> 542,120
518,71 -> 523,71
551,131 -> 556,131
489,36 -> 489,29 -> 489,36 -> 491,36 -> 491,34 -> 491,36 -> 493,36 -> 493,26 -> 493,36 -> 495,36 -> 495,35 -> 495,36 -> 497,36 -> 497,29 -> 497,36 -> 499,36 -> 499,31 -> 499,36
489,36 -> 489,29 -> 489,36 -> 491,36 -> 491,34 -> 491,36 -> 493,36 -> 493,26 -> 493,36 -> 495,36 -> 495,35 -> 495,36 -> 497,36 -> 497,29 -> 497,36 -> 499,36 -> 499,31 -> 499,36
534,120 -> 534,110 -> 534,120 -> 536,120 -> 536,115 -> 536,120 -> 538,120 -> 538,117 -> 538,120 -> 540,120 -> 540,119 -> 540,120 -> 542,120 -> 542,116 -> 542,120
489,36 -> 489,29 -> 489,36 -> 491,36 -> 491,34 -> 491,36 -> 493,36 -> 493,26 -> 493,36 -> 495,36 -> 495,35 -> 495,36 -> 497,36 -> 497,29 -> 497,36 -> 499,36 -> 499,31 -> 499,36
522,104 -> 527,104
556,135 -> 561,135 -> 561,134
560,154 -> 565,154
564,156 -> 569,156
510,90 -> 510,92 -> 502,92 -> 502,97 -> 516,97 -> 516,92 -> 515,92 -> 515,90
510,90 -> 510,92 -> 502,92 -> 502,97 -> 516,97 -> 516,92 -> 515,92 -> 515,90
508,104 -> 513,104
541,127 -> 546,127
507,69 -> 512,69
521,84 -> 521,82 -> 521,84 -> 523,84 -> 523,82 -> 523,84 -> 525,84 -> 525,77 -> 525,84
521,84 -> 521,82 -> 521,84 -> 523,84 -> 523,82 -> 523,84 -> 525,84 -> 525,77 -> 525,84
497,39 -> 497,43 -> 493,43 -> 493,49 -> 506,49 -> 506,43 -> 501,43 -> 501,39
534,120 -> 534,110 -> 534,120 -> 536,120 -> 536,115 -> 536,120 -> 538,120 -> 538,117 -> 538,120 -> 540,120 -> 540,119 -> 540,120 -> 542,120 -> 542,116 -> 542,120
511,102 -> 516,102
504,62 -> 504,58 -> 504,62 -> 506,62 -> 506,54 -> 506,62 -> 508,62 -> 508,55 -> 508,62
553,138 -> 553,141 -> 546,141 -> 546,149 -> 558,149 -> 558,141 -> 557,141 -> 557,138
534,120 -> 534,110 -> 534,120 -> 536,120 -> 536,115 -> 536,120 -> 538,120 -> 538,117 -> 538,120 -> 540,120 -> 540,119 -> 540,120 -> 542,120 -> 542,116 -> 542,120
497,13 -> 497,16 -> 496,16 -> 496,23 -> 511,23 -> 511,16 -> 503,16 -> 503,13
504,62 -> 504,58 -> 504,62 -> 506,62 -> 506,54 -> 506,62 -> 508,62 -> 508,55 -> 508,62
489,36 -> 489,29 -> 489,36 -> 491,36 -> 491,34 -> 491,36 -> 493,36 -> 493,26 -> 493,36 -> 495,36 -> 495,35 -> 495,36 -> 497,36 -> 497,29 -> 497,36 -> 499,36 -> 499,31 -> 499,36
497,39 -> 497,43 -> 493,43 -> 493,49 -> 506,49 -> 506,43 -> 501,43 -> 501,39
554,158 -> 559,158
489,36 -> 489,29 -> 489,36 -> 491,36 -> 491,34 -> 491,36 -> 493,36 -> 493,26 -> 493,36 -> 495,36 -> 495,35 -> 495,36 -> 497,36 -> 497,29 -> 497,36 -> 499,36 -> 499,31 -> 499,36
506,65 -> 511,65
497,13 -> 497,16 -> 496,16 -> 496,23 -> 511,23 -> 511,16 -> 503,16 -> 503,13
550,156 -> 555,156
521,84 -> 521,82 -> 521,84 -> 523,84 -> 523,82 -> 523,84 -> 525,84 -> 525,77 -> 525,84
504,62 -> 504,58 -> 504,62 -> 506,62 -> 506,54 -> 506,62 -> 508,62 -> 508,55 -> 508,62
489,36 -> 489,29 -> 489,36 -> 491,36 -> 491,34 -> 491,36 -> 493,36 -> 493,26 -> 493,36 -> 495,36 -> 495,35 -> 495,36 -> 497,36 -> 497,29 -> 497,36 -> 499,36 -> 499,31 -> 499,36
553,138 -> 553,141 -> 546,141 -> 546,149 -> 558,149 -> 558,141 -> 557,141 -> 557,138
497,13 -> 497,16 -> 496,16 -> 496,23 -> 511,23 -> 511,16 -> 503,16 -> 503,13
561,158 -> 566,158
497,39 -> 497,43 -> 493,43 -> 493,49 -> 506,49 -> 506,43 -> 501,43 -> 501,39
540,123 -> 545,123
489,36 -> 489,29 -> 489,36 -> 491,36 -> 491,34 -> 491,36 -> 493,36 -> 493,26 -> 493,36 -> 495,36 -> 495,35 -> 495,36 -> 497,36 -> 497,29 -> 497,36 -> 499,36 -> 499,31 -> 499,36
553,138 -> 553,141 -> 546,141 -> 546,149 -> 558,149 -> 558,141 -> 557,141 -> 557,138
510,90 -> 510,92 -> 502,92 -> 502,97 -> 516,97 -> 516,92 -> 515,92 -> 515,90
514,86 -> 514,87 -> 530,87 -> 530,86
489,36 -> 489,29 -> 489,36 -> 491,36 -> 491,34 -> 491,36 -> 493,36 -> 493,26 -> 493,36 -> 495,36 -> 495,35 -> 495,36 -> 497,36 -> 497,29 -> 497,36 -> 499,36 -> 499,31 -> 499,36
534,120 -> 534,110 -> 534,120 -> 536,120 -> 536,115 -> 536,120 -> 538,120 -> 538,117 -> 538,120 -> 540,120 -> 540,119 -> 540,120 -> 542,120 -> 542,116 -> 542,120
500,69 -> 505,69
514,100 -> 519,100
518,102 -> 523,102
504,62 -> 504,58 -> 504,62 -> 506,62 -> 506,54 -> 506,62 -> 508,62 -> 508,55 -> 508,62
534,120 -> 534,110 -> 534,120 -> 536,120 -> 536,115 -> 536,120 -> 538,120 -> 538,117 -> 538,120 -> 540,120 -> 540,119 -> 540,120 -> 542,120 -> 542,116 -> 542,120
489,36 -> 489,29 -> 489,36 -> 491,36 -> 491,34 -> 491,36 -> 493,36 -> 493,26 -> 493,36 -> 495,36 -> 495,35 -> 495,36 -> 497,36 -> 497,29 -> 497,36 -> 499,36 -> 499,31 -> 499,36
504,62 -> 504,58 -> 504,62 -> 506,62 -> 506,54 -> 506,62 -> 508,62 -> 508,55 -> 508,62
534,120 -> 534,110 -> 534,120 -> 536,120 -> 536,115 -> 536,120 -> 538,120 -> 538,117 -> 538,120 -> 540,120 -> 540,119 -> 540,120 -> 542,120 -> 542,116 -> 542,120
553,154 -> 558,154
503,67 -> 508,67
489,36 -> 489,29 -> 489,36 -> 491,36 -> 491,34 -> 491,36 -> 493,36 -> 493,26 -> 493,36 -> 495,36 -> 495,35 -> 495,36 -> 497,36 -> 497,29 -> 497,36 -> 499,36 -> 499,31 -> 499,36
510,90 -> 510,92 -> 502,92 -> 502,97 -> 516,97 -> 516,92 -> 515,92 -> 515,90
504,62 -> 504,58 -> 504,62 -> 506,62 -> 506,54 -> 506,62 -> 508,62 -> 508,55 -> 508,62
556,135 -> 561,135 -> 561,134
489,36 -> 489,29 -> 489,36 -> 491,36 -> 491,34 -> 491,36 -> 493,36 -> 493,26 -> 493,36 -> 495,36 -> 495,35 -> 495,36 -> 497,36 -> 497,29 -> 497,36 -> 499,36 -> 499,31 -> 499,36
497,13 -> 497,16 -> 496,16 -> 496,23 -> 511,23 -> 511,16 -> 503,16 -> 503,13
557,156 -> 562,156
553,138 -> 553,141 -> 546,141 -> 546,149 -> 558,149 -> 558,141 -> 557,141 -> 557,138
510,90 -> 510,92 -> 502,92 -> 502,97 -> 516,97 -> 516,92 -> 515,92 -> 515,90
521,84 -> 521,82 -> 521,84 -> 523,84 -> 523,82 -> 523,84 -> 525,84 -> 525,77 -> 525,84
511,71 -> 516,71
489,36 -> 489,29 -> 489,36 -> 491,36 -> 491,34 -> 491,36 -> 493,36 -> 493,26 -> 493,36 -> 495,36 -> 495,35 -> 495,36 -> 497,36 -> 497,29 -> 497,36 -> 499,36 -> 499,31 -> 499,36
534,120 -> 534,110 -> 534,120 -> 536,120 -> 536,115 -> 536,120 -> 538,120 -> 538,117 -> 538,120 -> 540,120 -> 540,119 -> 540,120 -> 542,120 -> 542,116 -> 542,120
534,120 -> 534,110 -> 534,120 -> 536,120 -> 536,115 -> 536,120 -> 538,120 -> 538,117 -> 538,120 -> 540,120 -> 540,119 -> 540,120 -> 542,120 -> 542,116 -> 542,120
497,39 -> 497,43 -> 493,43 -> 493,49 -> 506,49 -> 506,43 -> 501,43 -> 501,39
524,107 -> 538,107 -> 538,106
547,158 -> 552,158
497,13 -> 497,16 -> 496,16 -> 496,23 -> 511,23 -> 511,16 -> 503,16 -> 503,13
510,90 -> 510,92 -> 502,92 -> 502,97 -> 516,97 -> 516,92 -> 515,92 -> 515,90
497,39 -> 497,43 -> 493,43 -> 493,49 -> 506,49 -> 506,43 -> 501,43 -> 501,39
504,71 -> 509,71

2
inputs/14a.txt Normal file
View File

@ -0,0 +1,2 @@
498,4 -> 498,6 -> 496,6
503,4 -> 502,4 -> 502,9 -> 494,9

163
src/14.cs Normal file
View File

@ -0,0 +1,163 @@
namespace aoc2022;
internal class Day14 : Day
{
private record point(int x, int y);
// Parses a string in the form "x,y" into a point
private static point ParsePoint(string str)
{
var parts = str.Trim().Split(',');
if (parts.Length != 2)
{
throw new Exception($"found {parts.Length} pieces of input string, expected 2");
}
var x = int.Parse(parts[0].Trim());
var y = int.Parse(parts[1].Trim());
return new point(x, y);
}
enum cellType
{
sand,
wall,
}
private readonly Dictionary<point, cellType> grid = new();
internal override void Parse()
{
foreach (var line in Util.Parsing.ReadAllLines("14"))
{
point? lastPoint = null;
var parts = line.Split(" -> ");
foreach (var part in parts)
{
var p = ParsePoint(part);
if (lastPoint == null)
{
lastPoint = p;
continue;
}
if (p.x != lastPoint.x)
{
for (int i = lastPoint.x; i != p.x; i += Math.Sign(p.x - lastPoint.x))
{
grid[p with {x = i}] = cellType.wall;
}
}
else if (p.y != lastPoint.y)
{
for (int i = lastPoint.y; i != p.y; i += Math.Sign(p.y - lastPoint.y))
{
grid[p with {y = i}] = cellType.wall;
}
}
grid[p] = cellType.wall;
lastPoint = p;
}
}
}
internal override string Part1()
{
var g = new Dictionary<point, cellType>(grid);
int lowestY = g.MaxBy(pair => pair.Key.y).Key.y;
point dropPoint = new point(500, 0);
bool hitVoid = false;
int numDroppedSand = 0;
while (!hitVoid)
{
bool atRest = false;
point sandLoc = dropPoint with {y = dropPoint.y + 1};
while (!atRest)
{
var nextPoint = sandLoc with {y = sandLoc.y + 1};
if (g.ContainsKey(nextPoint))
{
nextPoint = nextPoint with {x = sandLoc.x - 1};
if (g.ContainsKey(nextPoint))
{
nextPoint = nextPoint with {x = sandLoc.x + 1};
if (g.ContainsKey(nextPoint))
{
atRest = true;
}
}
}
if (!atRest)
{
sandLoc = nextPoint;
}
if (nextPoint.y > lowestY)
{
hitVoid = true;
break;
}
}
if (!hitVoid)
{
numDroppedSand++;
g[sandLoc] = cellType.sand;
}
}
return $"Sand dropped before hitting the void: <+white>{numDroppedSand}";
}
internal override string Part2()
{
var g = new Dictionary<point, cellType>(grid);
int lowestY = g.MaxBy(pair => pair.Key.y).Key.y;
int floor = 2 + lowestY;
point dropPoint = new point(500, 0);
int numDroppedSand = 0;
while (true)
{
bool atRest = false;
point sandLoc = dropPoint;
while (!atRest)
{
var nextPoint = sandLoc with {y = sandLoc.y + 1};
if (nextPoint.y == floor)
{
break;
}
if (g.ContainsKey(nextPoint))
{
nextPoint = nextPoint with {x = sandLoc.x - 1};
if (g.ContainsKey(nextPoint))
{
nextPoint = nextPoint with {x = sandLoc.x + 1};
if (g.ContainsKey(nextPoint))
{
atRest = true;
}
}
}
if (!atRest)
{
sandLoc = nextPoint;
}
}
g[sandLoc] = cellType.sand;
numDroppedSand++;
if (sandLoc == dropPoint)
{
break;
}
}
return $"Sand dropped before filling the space: <+white>{numDroppedSand}";
}
}