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\13a.txt" />
<EmbeddedResource Include="inputs\14.txt" /> <EmbeddedResource Include="inputs\14.txt" />
<None Remove="inputs\15.txt" /> <None Remove="inputs\15.txt" />
<EmbeddedResource Include="inputs\14a.txt" />
<EmbeddedResource Include="inputs\15.txt" /> <EmbeddedResource Include="inputs\15.txt" />
<None Remove="inputs\16.txt" /> <None Remove="inputs\16.txt" />
<EmbeddedResource Include="inputs\16.txt" /> <EmbeddedResource Include="inputs\16.txt" />

View File

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