From a1219929760974daa7b297cca5b6dec28d41e288 Mon Sep 17 00:00:00 2001 From: Parnic Date: Fri, 23 Dec 2022 13:31:50 -0600 Subject: [PATCH] Day 23 solution Part 2 takes 7 seconds on my macbook, which I'd like to improve, but this gets the answer. --- advent-of-code-2022.csproj | 2 + inputs/23.txt | 79 +++++++++++++++++++++-- inputs/23a.txt | 7 ++ inputs/23b.txt | 6 ++ src/23.cs | 129 +++++++++++++++++++++++++++++++++++++ src/Util/Vec2.cs | 16 +++++ 6 files changed, 234 insertions(+), 5 deletions(-) create mode 100644 inputs/23a.txt create mode 100644 inputs/23b.txt create mode 100644 src/23.cs diff --git a/advent-of-code-2022.csproj b/advent-of-code-2022.csproj index c124c02..3dd7e3a 100644 --- a/advent-of-code-2022.csproj +++ b/advent-of-code-2022.csproj @@ -91,6 +91,8 @@ + + diff --git a/inputs/23.txt b/inputs/23.txt index 3b25139..638ace4 100644 --- a/inputs/23.txt +++ b/inputs/23.txt @@ -1,5 +1,74 @@ -############# -#...........# -###A#D#B#C### - #B#C#D#A# - ######### \ No newline at end of file +.##.#.#####..##.#.#...#.##....#.######.##.#.#.##..#.#.##.#.######.#.###.## +.#.#..#####....###.####.###..##.....#####..#.#####.##...#.##.#.##....####. +........##..#.###.#.#..#..#.######..#.##.#.#####.#..##...#.#.##...###.#... +.#..#....#.......#..#...##.#.##.#..##..#.###.######.#.#.#####....#...####. +##......##.####...##.###....##.#.####..###...#..#.#...#..#.##..###.#.####. +#...#..##.######.#...##...#.#........##...#.###..#.##..##.##...#######.##. +#....#.#..###...####.....#..##.##......#.....###.##.....#.####..##..#..#.. +...##.#.##.#.#...##....#.#.###.###.##..#.##.###...#..#..##..#....#.##.##.# +#....#########.####..#....##.###.##.....###.#..##...##.#####.###..##.....# +###.######.###.#####..####.#....#.###..#..###.#.####.###..#.##...##.#..#.. +.##....#..##..#....######.##..###.#.#.#...###...#.###.##.#....#.##.#.####. +.......##...##..#..####.##........##...##.#.###.#####.####.##...####.#.... +###.#......#..#.#.#.####...#####.#.####.#.####....#.....#.######....##.##. +###...#.##.####..#..##.#.#.#...#.###..##..##.###....##.....##..#.##..#.#.. +#########.#.####..##.#.#...##.##....####..#####..#....#.#...######.###.... +#.##....###..####.#..#####.#..##.###....#.#.###..##.#....###.#.#.#.#..##.. +##.###....###..##..###..#.#..##.#.##.#..#.......#.##.######.#...#...#.#### +##.......#.....####...#...#.##.#..##.##........####..#.....#...##.#...#.## +.##..#.#....##.....##.##.#.#.###...##...#..####...#.#.#..######.#...##.... +.#....##.#.#.#..#.#.#....#....###.##.###....#.....###.#...#.##..#..####..# +..##...##..###..#####.########.##....#.#...##.##..#.#.##...##..#...##.##.# +#.#...##....#...###....#...#.##.#.##.######.##...#####...##.##...#######.. +.#.#.######.##..#.####.#.##.##.##.#...####......##.....##...####..##.####. +.###.###..##.....#####...####..##...##.##.##..##...#..#.#..##.....###.#.## +#.#.####.##.#.###.###.#..#.#....#######.###......#.##.....######.....##... +##..#.#.#..#....##.....#.###.#..##.....##############.#.#.####.##..#.##.## +#.....#.#..#.##..##..##..##...###....###..##.##...##.##....#..#######..##. +##.##..###.#..###.##.####.#.#.........##.#......#.###.#..##..#...##..#.##. +..#.###.##.####.####.#..#.###...#.#.###.#.##..#....#.#.#...#....###....### +#..#...##.#....###.#.###..#...###.##.#.######......#.##.#..#..#.###.###### +#.#...#.##..#....###....#.#.#.....########.#..##...#.#..############.##### +..####.#....#####.#.#.#....#..#.##....#..#..###.####.#....##.##...#.#.##.# +.##...#.#..#.###..#...#..#.#.#....#..#.###.##.#.####.#.###..#..#.....##..# +###..#.##...#.###.###...##..#.####....#.####......#..###...#...#.#....#.## +...#.######...#.########..######...##.....###.####.#.###...#.###.###.#.... +##.#####...###.##.#.####..#...#.#####.##.....#....####.#####.####.###..#.# +....###...#.#..#.##.#...#....#.#.#..##########.....#..##..##..##.##.##.#.. +#....#..##.#...###..####.#......########.#.......###..#...##.#.#..#.....#. +....#.......###.##.##.###.#.####.#...##.#.#..#.#.#.#.....##.#.#...#.#.#.## +###.#.##..##..####.#####.#.##.###..#.#######.##...#.#####.##.##.###..#...# +####.#.#..#..##.##..#.#.##.#.###.#...#.#..##.######.#.###.#..#..####..#..# +#......#.###.#.##...##..##...#.#..#.#.###...#..##..##.###.#.#..#.####.#..# +.###.#.#.....#.#####.....#.#..#...#...####...#.####....#..#.#...#.#.#..### +##....######.#.....##.....#.###...#..###.#.##...##..#.###.....#..#..#.#... +##..#.#.##....#.#...##.##.##.##.#.#.##..#.#..#..##.##..##..#.##..#....##.# +#.##.#.#.###...######...##.#.#.##.#.#..#...#.###..##.##.#....#..#..#.#...# +..##...#....#.##.##..##...##.......#..#####...#.#...###..#.#.##...#..#.... +#.#.....##.##.#.#.#...##.#....#...#.#.##..#.####.#.#.##..#.##.##.###....## +..#.##..#.#..##.#..##....###.##...#.##...#.#....##.###.#.#.##...##.##..#.# +.######.###........#.#.....#.#.......#.##.########.#...#.#..##.##.##..#### +#..##.....##....###.###.#.#..##..##..#.#.#.##.#.....##.###.#.##.##.#.#...# +.......###..##.##.####.#.#####.#..#...#####...#...#..#.##.####..##.##..##. +##....#####.##....#..###.####.###.###..#.#.#..#.#.##..#..###.#########.#.. +#.###..###...#.#..#.#...#.#.##.#.######.####..#..##..##.#......###.#..#... +.##..#####..#.#....###.###.##.....##..#.##....#..##.#...####...##.#....... +##.####..##...##.#...######.###.###.#.###..####.####.##..###.##.##..###### +##.#.####.#.#..#.#.###.......###......####.#.#.##.#...##..##..##.....#.#.# +.###....####.#...#.#......#####.##.##.#...###..##...##...#####....##.#..## +#..###..##....#...##.###..##.##.##########..#..##...#....##....#.......##. +###.###..#.#..#....##.#......#.###.#.#.#.#.##...#..#..##......###.##..#.#. +....##......#......#....#.##.####.####.##.###.#.##.#..#.#.#..........#.#.# +#...####...#.###..##....##..##.#####.###.#...#..#.....##.#.###.##.#...#.## +.###..##.#.###.#....#..#.###.######.##.#####..#..#.#..##.#..######.##....# +.#....###..#.#.###....##.#...###.#.#...##.#.##.##.#.#.##.#.#...#.#........ +..#.##...#....##.#..#.#.#.......#..#......#..#..#..#.#.##....#.#.#...###.. +##.#.#..##.##.#....#.#...........###....#..##.##.######.##.##..####.#..... +..##...###....###.#####..###......###.#..##...##....##.#.#.###....#..##..# +.#....#.#.#.###..##.##.#....#.##.##....#.#.##..###..##...###.#...#....##.. +#...#...####...####...#.#.#.#####....####..#..##.###.#.#....##...#.##..##. +###..#####..#.#.#...#.#.#...#...###.####.###..#...###.####.####..##.#..#.. +#...#####...##..#.###.#.#.#.#..####..##......#####....#####.####.####...## +..........#..#..#..##.#.##...##...#..#.#...#####.....#####..#...##.##.#..# +.#.##.###...#.##.#....###.#.##.##.#.####.#..##.#..#..#.#.##........#.##### +.###########.##.###.#..#.###....#......#.##.....####.####..##.....#####.#. \ No newline at end of file diff --git a/inputs/23a.txt b/inputs/23a.txt new file mode 100644 index 0000000..a24b553 --- /dev/null +++ b/inputs/23a.txt @@ -0,0 +1,7 @@ +....#.. +..###.# +#...#.# +.#...## +#.###.. +##.#.## +.#..#.. \ No newline at end of file diff --git a/inputs/23b.txt b/inputs/23b.txt new file mode 100644 index 0000000..6ad574d --- /dev/null +++ b/inputs/23b.txt @@ -0,0 +1,6 @@ +..... +..##. +..#.. +..... +..##. +..... \ No newline at end of file diff --git a/src/23.cs b/src/23.cs new file mode 100644 index 0000000..5d23622 --- /dev/null +++ b/src/23.cs @@ -0,0 +1,129 @@ +using System.Collections.Immutable; +using aoc2022.Util; + +namespace aoc2022; + +internal class Day23 : Day +{ + private readonly HashSet elves = new(); + + private static readonly ivec2[][] dirOrder = + { + new[]{ivec2.UP, ivec2.UP + ivec2.RIGHT, ivec2.UP + ivec2.LEFT}, + new[]{ivec2.DOWN, ivec2.DOWN + ivec2.RIGHT, ivec2.DOWN + ivec2.LEFT}, + new[]{ivec2.LEFT, ivec2.UP + ivec2.LEFT, ivec2.DOWN + ivec2.LEFT}, + new[]{ivec2.RIGHT, ivec2.UP + ivec2.RIGHT, ivec2.DOWN + ivec2.RIGHT}, + }; + + internal override void Parse() + { + var lines = Parsing.ReadAllLines("23").ToImmutableList(); + for (int row = 0; row < lines.Count; row++) + { + var line = lines[row]; + for (int col = 0; col < line.Length; col++) + { + if (line[col] == '#') + { + elves.Add(new ivec2(col, row)); + } + } + } + } + + private static List<(ivec2 curr, ivec2 proposed)> ConsiderMove(ICollection positions, int orderStart) + { + List<(ivec2 curr, ivec2 proposed)> considerations = new(positions.Count); + + foreach (var elfPos in positions) + { + if (!elfPos.GetNeighbors().Any(positions.Contains)) + { + continue; + } + + for (int i = 0; i < dirOrder.Length; i++) + { + var dirs = dirOrder[(i + orderStart) % dirOrder.Length]; + if (!dirs.Any(d => positions.Contains(d + elfPos))) + { + considerations.Add((elfPos, elfPos + dirs[0])); + break; + } + } + } + + return considerations; + } + + private static int PlayOut(ICollection positions, int? maxRounds = null) + { + int round; + for (round = 0; (maxRounds == null || round < maxRounds); round++) + { + var considerations = ConsiderMove(positions, round); + if (!considerations.Any()) + { + break; + } + + for (int i = 0; i < considerations.Count; i++) + { + bool bDuped = false; + for (int j = i + 1; j < considerations.Count; j++) + { + if (considerations[i].proposed == considerations[j].proposed) + { + bDuped = true; + considerations.RemoveAt(j); + j--; + } + } + + if (bDuped) + { + considerations.RemoveAt(i); + i--; + } + } + + foreach (var move in considerations) + { + positions.Remove(move.curr); + positions.Add(move.proposed); + } + } + + return round + 1; + } + + private static long GetEmptySpots(ICollection positions) + { + var minY = positions.MinBy(p => p.y).y; + var minX = positions.MinBy(p => p.x).x; + var maxY = positions.MaxBy(p => p.y).y; + var maxX = positions.MaxBy(p => p.x).x; + + var height = (maxY - minY) + 1; + var width = (maxX - minX) + 1; + var area = height * width; + var emptyGround = area - positions.Count; + + return emptyGround; + } + + internal override string Part1() + { + HashSet positions = new(elves); + PlayOut(positions, 10); + var emptyGround = GetEmptySpots(positions); + return $"Empty ground spaces after 10 rounds: <+white>{emptyGround}"; + } + + internal override string Part2() + { + HashSet positions = new(elves); + var round = PlayOut(positions); + return $"Rounds to settle: <+white>{round}"; + } +} diff --git a/src/Util/Vec2.cs b/src/Util/Vec2.cs index b8641d2..83fc11d 100644 --- a/src/Util/Vec2.cs +++ b/src/Util/Vec2.cs @@ -58,6 +58,22 @@ public struct ivec2 : IEquatable, IComparable, IComparable return this + dir; } + public IEnumerable GetOrthogonalNeighbors() + { + foreach (var dir in FOURWAY) + { + yield return this + dir; + } + } + + public IEnumerable GetNeighbors() + { + foreach (var dir in EIGHTWAY) + { + yield return this + dir; + } + } + public long this[long i] => (i == 0) ? x : y; public static ivec2 operator +(ivec2 v) => v;