generated from mazharenko/aoc-agent-template-multipleyears
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
27be90c
commit 7f190e5
Showing
6 changed files
with
329 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System.Numerics; | ||
|
||
namespace aoc.Common.Points; | ||
|
||
public static class V | ||
{ | ||
public static V<T> Create<T>(T x, T y) where T : INumber<T> | ||
{ | ||
return new V<T>(x, y); | ||
} | ||
public static V<T> Dir<T>(this V<T> p) where T : INumber<T> | ||
{ | ||
return Create(T.CreateChecked(T.Sign(p.X)), T.CreateChecked(T.Sign(p.Y))); | ||
} | ||
|
||
public static V<T> RotateCw<T>(this V<T> v) where T : INumber<T> | ||
=> Create(v.Y, -v.X); | ||
|
||
public static V<T> RotateCcw<T>(this V<T> v) where T : INumber<T> | ||
=> Create(-v.Y, v.X); | ||
} | ||
|
||
public static class Directions | ||
{ | ||
public static V<int> Down() => new(1, 0); | ||
|
||
public static V<int> Up() => new(-1, 0); | ||
|
||
public static V<int> Right() => new(0, 1); | ||
|
||
public static V<int> Left() => new(0, -1); | ||
} | ||
|
||
public record struct V<T>(T X, T Y) where T : INumber<T> | ||
{ | ||
public V((T X, T Y) tuple) : this(tuple.X, tuple.Y) | ||
{ | ||
} | ||
|
||
public static V<T> operator +(V<T> v1, V<T> v2) | ||
{ | ||
return new V<T>(v1.X + v2.X, v1.Y + v2.Y); | ||
} | ||
|
||
public static V<T> operator -(V<T> v1, V<T> v2) | ||
{ | ||
return new V<T>(v1.X - v2.X, v1.Y - v2.Y); | ||
} | ||
|
||
public static V<T> operator +(V<T> v1, V<int> v2) | ||
{ | ||
return new V<T>(v1.X + T.CreateChecked(v2.X), v1.Y + T.CreateChecked(v2.Y)); | ||
} | ||
|
||
public static V<T> operator -(V<T> v1, V<int> v2) | ||
{ | ||
return new V<T>(v1.X - T.CreateChecked(v2.X), v1.Y - T.CreateChecked(v2.Y)); | ||
} | ||
|
||
public static V<T> operator *(V<T> v1, T n) | ||
{ | ||
return new V<T>(v1.X * n, v1.Y * n); | ||
} | ||
|
||
public static V<T> operator *(V<T> v1, int n) | ||
{ | ||
return new V<T>(v1.X * T.CreateChecked(n), v1.Y * T.CreateChecked(n)); | ||
} | ||
|
||
public static V<T> Zero { get; } = new(T.Zero, T.Zero); | ||
public static V<T> One { get; } = new(T.One, T.One); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,96 @@ | ||
using aoc.Common.Maps; | ||
using aoc.Common.Points; | ||
using MoreLinq; | ||
using V = aoc.Common.Points.V<int>; | ||
|
||
namespace aoc.Year2024.Day06; | ||
|
||
internal partial class Day06 | ||
{ | ||
internal partial class Part1 | ||
{ | ||
public string Solve(string input) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
|
||
internal partial class Part2 | ||
{ | ||
public string Solve(string input) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
internal partial class Part1 | ||
{ | ||
private readonly Example example = new( | ||
""" | ||
....#..... | ||
.........# | ||
.......... | ||
..#....... | ||
.......#.. | ||
.......... | ||
.#..^..... | ||
........#. | ||
#......... | ||
......#... | ||
""", 41); | ||
|
||
public int Solve(char[,] input) | ||
{ | ||
var (guardPosition, _) = input.AsEnumerable().Single(x => x.element == '^'); | ||
|
||
return WalkUntilOutside(input, guardPosition, Directions.Up()) | ||
.Select(x => x.pos) | ||
.Distinct() | ||
.Count(); | ||
} | ||
|
||
|
||
public char[,] Parse(string input) | ||
{ | ||
return Character.In('.', '#', '^').Map().Parse(input); | ||
} | ||
} | ||
|
||
internal partial class Part2 | ||
{ | ||
private readonly Example example = new( | ||
""" | ||
....#..... | ||
.........# | ||
.......... | ||
..#....... | ||
.......#.. | ||
.......... | ||
.#..^..... | ||
........#. | ||
#......... | ||
......#... | ||
""", 6); | ||
|
||
|
||
public int Solve(char[,] input) | ||
{ | ||
var (guardPosition, _) = input.AsEnumerable().Single(x => x.element == '^'); | ||
var originalVisited = | ||
WalkUntilOutside(input, guardPosition, Directions.Up()) | ||
.Select(x => x.pos).Distinct(); | ||
return originalVisited.Count(obstacleCandidate => | ||
{ | ||
var copy = input.Map((p, cell) => p == obstacleCandidate ? '#' : cell); | ||
return | ||
WalkUntilOutside(copy, guardPosition, Directions.Up()) | ||
// enumerable returned will sometimes be infinite and looped. | ||
// as soon as we discover a duplicate, we go continue to the next obstacle candidate. | ||
// Duplicates is documented to be deferred and not populate the whole sequence. | ||
.Duplicates().Any(); | ||
}); | ||
} | ||
|
||
public char[,] Parse(string input) | ||
{ | ||
return Character.In('.', '#', '^').Map().Parse(input); | ||
} | ||
} | ||
|
||
private static IEnumerable<(V pos, V dir)> WalkUntilOutside(char[,] map, V pos, V dir) | ||
{ | ||
while (true) | ||
{ | ||
yield return (pos, dir); | ||
if (!map.TryAt(pos + dir, out var nextCell)) break; | ||
if (nextCell is '#') | ||
dir = dir.RotateCw(); | ||
else | ||
pos += dir; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.