Skip to content

Commit

Permalink
Day 18
Browse files Browse the repository at this point in the history
  • Loading branch information
sec committed Dec 18, 2023
1 parent 5a7c772 commit 6a863d1
Show file tree
Hide file tree
Showing 3 changed files with 760 additions and 7 deletions.
77 changes: 70 additions & 7 deletions aoc2023/Code/Day18.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,77 @@
namespace aoc2023.Code;
namespace aoc2023.Code;

internal class Day18 : BaseDay
{
protected override object Part1()
{
return 0;
}
record DigPlan(char D, int S, string Color);

record XY(long X, long Y);

protected override object Part2()
class Lagoon
{
return 0;
readonly List<DigPlan> _plan;
readonly List<XY> _edges;

public Lagoon(IEnumerable<string[]> digPlan)
{
_plan = [];
_edges = [];

foreach (var plan in digPlan)
{
_plan.Add(new DigPlan(plan[0][0], int.Parse(plan[1]), plan[2]));
}
}

internal Lagoon Trench(bool fix)
{
var edge = new XY(0, 0);
_edges.Add(edge);

foreach (var plan in _plan)
{
var length = plan.S;
var direction = plan.D;

if (fix)
{
length = int.Parse(plan.Color[2..7], System.Globalization.NumberStyles.HexNumber);
direction = "RDLU"[plan.Color[^2] - '0'];
}

edge = direction switch
{
'R' => new(edge.X + length, edge.Y),
'L' => new(edge.X - length, edge.Y),
'U' => new(edge.X, edge.Y - length),
'D' => new(edge.X, edge.Y + length),
_ => throw new NotImplementedException()
};

_edges.Add(edge);
}

return this;
}

internal long Shoelace()
{
var s = 0L;
var p = 0L;

for (int i = 0; i < _edges.Count - 1; i++)
{
var a = _edges[i];
var b = _edges[i + 1];

s += (a.X * b.Y - b.X * a.Y);
p += Math.Abs(a.X - b.X + a.Y - b.Y);
}

return (s + p) / 2 + 1;
}
}

protected override object Part1() => new Lagoon(ReadAllLinesSplit(" ", true)).Trench(false).Shoelace();

protected override object Part2() => new Lagoon(ReadAllLinesSplit(" ", true)).Trench(true).Shoelace();
}
Loading

0 comments on commit 6a863d1

Please sign in to comment.