Lukas Angerer пре 2 година
родитељ
комит
a8f1f1cd1d
9 измењених фајлова са 350 додато и 0 уклоњено
  1. 6 0
      AdventOfCode23.sln
  2. 26 0
      Day3/Day3.csproj
  3. 16 0
      Day3/NumberToken.cs
  4. 69 0
      Day3/Program.cs
  5. 46 0
      Day3/SchematicParser.cs
  6. 12 0
      Day3/SymbolToken.cs
  7. 25 0
      Day3/Token.cs
  8. 140 0
      Day3/inputs/input-lar.txt
  9. 10 0
      Day3/inputs/sample1.txt

+ 6 - 0
AdventOfCode23.sln

@@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day1", "Day1\Day1.csproj",
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day2", "Day2\Day2.csproj", "{1FB701F9-05FA-4D1D-97E5-9355C683E3D0}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day3", "Day3\Day3.csproj", "{62336B74-EB88-4F21-9EBA-6FC5499155A2}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -24,5 +26,9 @@ Global
 		{1FB701F9-05FA-4D1D-97E5-9355C683E3D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{1FB701F9-05FA-4D1D-97E5-9355C683E3D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{1FB701F9-05FA-4D1D-97E5-9355C683E3D0}.Release|Any CPU.Build.0 = Release|Any CPU
+		{62336B74-EB88-4F21-9EBA-6FC5499155A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{62336B74-EB88-4F21-9EBA-6FC5499155A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{62336B74-EB88-4F21-9EBA-6FC5499155A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{62336B74-EB88-4F21-9EBA-6FC5499155A2}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 EndGlobal

+ 26 - 0
Day3/Day3.csproj

@@ -0,0 +1,26 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>net8.0</TargetFramework>
+    <ImplicitUsings>enable</ImplicitUsings>
+    <Nullable>enable</Nullable>
+    <RootNamespace>Day2</RootNamespace>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <None Update="inputs\lan-input.txt">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </None>
+    <None Update="inputs\sample-input.txt">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </None>
+    <None Update="inputs\sample-input2.txt">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </None>
+    <None Update="inputs\lar-input.txt">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </None>
+  </ItemGroup>
+
+</Project>

+ 16 - 0
Day3/NumberToken.cs

@@ -0,0 +1,16 @@
+using System.Text.RegularExpressions;
+
+namespace Day3;
+
+public class NumberToken : Token
+{
+    public int Length { get; }
+    public int Value { get; }
+    
+    public NumberToken(int col, int row, string value)
+        : base(new Point2D(col, row))
+    {
+        Length = value.Length;
+        Value = int.Parse(value);
+    }
+}

+ 69 - 0
Day3/Program.cs

@@ -0,0 +1,69 @@
+using Day3;
+
+if (args.Length < 1)
+{
+    Console.WriteLine("Requires 1 args: inputFileName");
+    return -1;
+}
+
+var inputFile = args[0];
+var parser = new SchematicParser();
+
+var set = new HashSet<Point2D>();
+var tokens = parser.Parse(inputFile).ToList();
+foreach (var token in tokens)
+{
+    if (token is SymbolToken sym)
+    {
+        foreach (var p in sym.Position.SurroundingAndSelf())
+        {
+            set.Add(p);
+        }
+    }
+}
+
+Console.WriteLine();
+
+for (var y = 0; y < parser.Height; y++)
+{
+    for (var x = 0; x < parser.Width; x++)
+    {
+        if (set.Contains(new Point2D(x, y)))
+        {
+            Console.Write("X");
+        }
+        else
+        {
+            Console.Write("-");
+        }
+    }
+    Console.WriteLine("|");
+}
+
+var sum = 0;
+foreach (var token in tokens)
+{
+    if (token is NumberToken num)
+    {
+        sum += IsTouchingSymbol(set, num);
+    }
+}
+
+Console.WriteLine();
+Console.WriteLine($"Sum: {sum}");
+
+return 0;
+
+int IsTouchingSymbol(ISet<Point2D> set, NumberToken num)
+{
+    for (var w = 0; w < num.Length; w++)
+    {
+        var p = num.Position with { X = num.Position.X + w };
+        if (set.Contains(p))
+        {
+            return num.Value;
+        }
+    }
+
+    return 0;
+}

+ 46 - 0
Day3/SchematicParser.cs

@@ -0,0 +1,46 @@
+using System.Text.RegularExpressions;
+
+namespace Day3;
+
+public partial class SchematicParser
+{
+    [GeneratedRegex(@"\d+|[^\d.]+")]
+    private partial Regex TokenExtractor();
+    
+    public int Width { get; private set; }
+    public int Height { get; private set; }
+    
+    public IEnumerable<Token> Parse(string inputFile)
+    {
+        using var reader = File.OpenText(inputFile);
+        var row = 0;
+        while (!reader.EndOfStream)
+        {
+            var line = reader.ReadLine()!;
+            if (Width == 0)
+            {
+                Width = line.Length;
+            }
+            foreach (Match match in TokenExtractor().Matches(line))
+            {
+                if (match.Value[0] >= '0' && match.Value[0] <= '9')
+                {
+                    yield return new NumberToken(match.Index, row, match.Value);
+                }
+                else
+                {
+                    if (match.Length > 1)
+                    {
+                        throw new Exception($"Unexpected symbol token: {match.Value}");
+                    }
+
+                    yield return new SymbolToken(match.Index, row, match.Value);
+                }
+            }
+
+            row++;
+        }
+
+        Height = row;
+    }
+}

+ 12 - 0
Day3/SymbolToken.cs

@@ -0,0 +1,12 @@
+namespace Day3;
+
+public class SymbolToken : Token
+{
+    public string Symbol { get; }
+    
+    public SymbolToken(int col, int row, string symbol)
+        : base(new Point2D(col, row))
+    {
+        Symbol = symbol;
+    }
+}

+ 25 - 0
Day3/Token.cs

@@ -0,0 +1,25 @@
+namespace Day3;
+
+public record Point2D(int X, int Y)
+{
+    public IEnumerable<Point2D> SurroundingAndSelf()
+    {
+        for (var y = -1; y < 2; y++)
+        {
+            for (var x = -1; x < 2; x++)
+            {
+                yield return new Point2D(X + x, Y + y);
+            }
+        }
+    }
+}
+
+public abstract class Token
+{
+    public Point2D Position { get; }
+
+    protected Token(Point2D position)
+    {
+        Position = position;
+    }
+}

+ 140 - 0
Day3/inputs/input-lar.txt

@@ -0,0 +1,140 @@
+...........441.................367................296........................................567..47.....45.................947.............
+...606..........888.....................508..........*892................+..=138.381..967...............*....%......926...........218.......
+....*......116..*..............747............-....................777..460..........*.......549......127...595.......*..290........*.968...
+..902..........425................+..........730..........#...........*.....196.............-.......................512.*....@...994..%.....
+..........................924.....................%...*...139..............*............/......458......................10..155.............
+...318.......825.......+.....*201................793.143.......522.%...568.....&.........558....*.......583....792..........................
+....*................971............448....653..............79..@..792....*.105....115.........200.....+.........*.............*........380.
+.850....269................775*........*......@.......%................921........#........................812....793....22.460.305.........
+..........&..414@...824........129.....816......*..560...421......955/................141......336........*.................................
+.........................59....................201......$...............@651.........*...........+..937...732...549-....284....544..........
+...........................*..178.125*808....................919..............246....400.53......................................$.+........
+.....=.....964.238..116.722.....*............624..............*...........209...............312....670.....953.....#....265.710.....414.....
+...966.152*.........*........375....*........*.........663....639............................&....*.........*.....424......*....%...........
+..................-..68.206......812.601......874..651...+..........857*686.........*.....33......825......497...............402............
+862*766......@...545.......*4............706.....................................887.395....=.731......47.........................942.949...
+..........900..........390........5-........*345.....................724.......%..........................+........778..761........%........
+..............887..985...*.......................141...................$.......64......&...334.278..896...572.....*.....*.....804...........
+......184....*..........596...$.....................*.-764....494....$....54............35....*................900...750..$.....*...........
+.....#.....322......979.....447............542.61..52.................880..........................*718............-.......926..659.........
+.......91.......793....*.@.............215*............@.........585......+....126..............102.......207..&....762.............&703....
+.....................751..87..$........................117.......*.......778...*.....363...=157......351.....*..577.........................
+..483=...374*823.............512...835.551.....333..........978........#.......954.....*.............*.....140..........634.....357......473
+......................................*.........#...223.....*.........79...............275..........24..........310............+....390=....
+755.255.#....751....#...342....51..........507.....*.....764....220..........909../.............................../.........................
+.....&...320..=..293.....*.....@......204.....*.956.............*......485....#...866..765..973..668......476.............772.....463.479...
+..........................53.............*.............+410...667.-103..&..%...................*....*......*..995#.........-..75.......*....
+.......289.......995..............33...714.......366........................556.......518......95.....................836.....*......699....
+......./............/...31.........................&...............&....................*...........220.814............$....658.............
+.....=...10...............*.....+852.....%98...........838......652..........11.125..488......799...*.....*.................................
+...470......102......889.250..................209..118..#...........729........*..................159..799..417.140....762......588.629.....
+........@...*...844....*........900...507....*......#...............=......*...........878.....................*......*.....539*............
+...756.769.426..*......94.........#......*....997...........#793........115.685..........*.....973.869.................12............596....
+.....=..........2......................698........416...$.........302...............530...315...@...@.......-.....605.........542.....*.....
+.711....373........................358...............&..820..........*......#243...=........................148...../...$........*...581....
+...........$...727.......314...57..*.............*..................762......................524..346$...................581.....120........
+......=568..........@844.&.....*...257.485......593.....*................................30..*.........740.....709*.........................
+................................75......*............194.675.420*282.....967..552.............850..................789..=.....-........223..
+..910.....$.............562..=..........56..............................-....*......194..................................425...789.....*....
+...-......618.716#...........233.............................828...........301......................190....#120....................360.232..
+.......................587..........766.812............178...-........929...................../.......*...........707.949....465..$.........
+..........191...=....................-...................................*378............766..456......605...............*....*........593..
+.481...#.%......267.....729.................619......116*164....................505....+....*.....531...................498...47...789*.....
+.....625............579*.......914.997=.....*...............................456*....632..133......*......751...%..631.......................
+..........=415.481.........979.............318.......=.............942.855...................512...985..*....865./..........................
+.................*...........@..................436-..542.........*.....*..$631........108.....%........619........194..............825..329
+..333.............624......................=.................795-..818.431.......702...*..............................=........+....%.......
+.../......................312.....666.......785.198......719....................&.......837........838..551.175..........368....557.........
+.....581.........986......*........+................268.....*...445............................*..*.......-...*.......99*...............=...
+.................@....793..............868......935....*..415.....*.430....253....@...........678..............420............%.46#..679....
+........................*....70*239......*............824.......499....*...*...965...696..532............405.........90....306..............
+.........................34...........152...216$.381.................373..22......../........*.............&...........-.........976........
+.................132.........448....................$.............*..........585.............321...............+...66....741.......*........
+.......194........&..669.......*................*..............460.465.54.......+........763.......738......444..........*....791..776......
+...978.....839......*........808...669.......864.30...................../...........439..*......*.....*...........441.....278...=...........
+.......971*.....*.....................*......................................43....*....778..329.711.136....................................
+............792.182..21............81..545...398&...........................*.....763....................181............*506....736..141.933
+........282*..........*......=............................41*982......237..166.............678..718=.656..*...........87.........*..........
+...................684........5......220*697.......259............347....*.............*.....*.........*.832.............388.....192........
+..395........=............838......................+.....693....#.....784...469*334.....870.464......572........875.....*...............152.
+......362.....693.........+...859......183..479...........*......969.......................................811..&....171.........284...=....
+.....*.............../...................*.........606..187..........644..........401.=984...98....760....*..............54.................
+...505.............806....249..........327....821.....@.................*....211..*.........*........*.422.................*......*902.863..
+............................*...................*..........219..........571....*...284...272......477.........938.....312...981..7..........
+.688...=....................544...766...........346..........................923.............................*........+............923......
+......779..=281...419..............+.................308....51...................836.........537../....931....97.........................952
+..................*......312...834.....................*........../.............*....83.262....*...748...............873.....46.4...........
+......355.......728.........*....%....................56..........752..296-....386.....*......53...........................-...*........@...
+....-.................483/..192.........714*248.................-.........................621.........*............926.....244.........867..
+...878...........#641...........................832.72.........877.......=..793............@........676.696.........*.......................
+.............853.............&614.......#699......*.......*29..........900.....*718..%369....#.$919........$....*....791.381......624.......
+................*255..........................758.604...........985.......................811................955.745.......*................
+...........9............276.393.76............$.............952......................947............385...*.............620....875..........
+...........*..755...210...#..*..*................/............/.........774..=............315..........$.750...........................@.381
+........975..*...../........168..472.....662.....308..............996........685..................77.........424..188...............639.....
+..............173.........#..............*...........740.............*372...........................*...............*.......................
+........................991.........*769..651.36.+12...*......861&............897.....749..859....271...96..........248.667.................
+.............%..............518..624...........*.......475.........183*.......%...............*...........-.....658.......*..+..............
+.29..........25...688..346.....*.........720.29....162.................502....................414...616........*........52..698..........453
+........286%.........-....*....359.........*......*........................881.........634..........+....=.....932..519...........765.......
+.................&........280....................282..209...519...........................*............970...........*.....@.....&.....*....
+.....448...216..204.190........108*868...*408......../.......*..=............824.........344..................29.....254..139.......811.656.
+.310..................*................................$..449....159...731.....*...154................107.......*279........................
+...*...194...........307.817.........................314....................208.....*................*.......+.......................871*...
+254...@............$.......@..#17..............284.........757......................921..#479..933.414....308......@154............*.....222
+..................817.................555.389....*............@.151...........................&................143..............348.448.....
+....20......321..........303............*.....873.....%..899...................226*.....*830.....................=........@.................
+....@....*...*......698...*.............363........189......*...192.&................890...............@814...............830......300..48..
+.......907..207.......*...284.......24+....................346.+....867.....808..43......397....363................254.........178......*...
+.................$................$............772...............+..........-...........*.......-...............#...%..................246..
+......332....855.250..........*....692.....968...............212..297...........223.....725.............419.....213...316.822...............
+..290....&..*.........108.....577....................133.87...=..............$...+.............&.........................*..............917.
+.......@.....701.......*..476....................806../..*.........216.5+.834......174..........597...............617......222..............
+...-...618.........992.47...*............497.............428.....@.*..............*.........276........521.../.......*......*........234....
+.465...............%........438.%627.727......................949...258.....927....365........*.....%..@...93.......186..535..........-.....
+...........557..........................*66......456.....+........*........+............539..95..790............220..........$...........95.
+117..........*.......869...........................@..943..133.249.364..............806*................=...342...*.......817....667........
+.............490.641*...........273.786..507.................@...............244......................71....*......717...........*.......871
+...236.70...............$.........*.*....%....................................*..386=...503..................778.=.........419...7..........
+.......*....$905..660...287....654..634........934.............344..........322............*............702......745..........*.............
+........950.........*............................#..906..118..*.....553.694.....191.....682.........412*.........................801........
+.............186....175...607................330....@.......*..931.*....*......@...............618............14........851&.567*...../.....
+............/...........+........156.449.334*....*....888..951.....559...144.........%.................................................16...
+....835..................96..751..*...........409.365....*...........................736........................310$......583...............
+......$........47...744........*.68.........*..........634..-..589........250..370...........644.....693...13*.............*...&............
+..470.....775...=..+.....&..234..........538.597...$.......391...........................179*.................524.......232..79.............
+....*.....=.............72..........................493..............=...............$.............................828...........583.*......
+..207...........312..........867..806......224..123...............616......76.......777./200..232..149.48...716..@...=..........&.....7.....
+......48.................277..........755=....*..........735.................*.................@....-...*..*....590..........+......-.......
+........*787.....82........*.....208...........359......................766.11.............%..........31...60..............731...928........
+...................@.......265..........333.........................781.@...................991...#............93...........................
+.........................................+...........................*..........................848....691.97...*..........539..............
+.507..238.........384......#.......&.....................987......@..395.844*......949*608.460......29...*.*.....955........*.........#734..
+........*./910..........607.........411.................*........102........................=...169...*....74........846..382.533/..........
+.....221........................742.......%...267.491.357...544......+...846.973....................66........440...........................
+..............-................/....833.103...$....*..............369........*.......519.....4.........-........*................809.942....
+.833..48..523..281.................&...................................442.958......*....951..-.........856.=..............49...&...../.....
+.......*.....*.......275.......259.....$.........-.175...662.697.307................169....*................33.....955.....*......+......68.
+...388.......231.994....@.........*.934........889.*.........*.............................668....*878......................353.560.....*...
+....@................%.........806..................708...221.......................498.....................79..385...................112...
+451.....555&........463.........................%.........................80*494...$.........976..487.......*.....=....801........781.......
+...*.99.......766@.........751.........174..760..169....479*368.382..281.............&........*.....*......179........=..........=.......51.
+.670..&..565.......577.$......+...=583........-......=............*....=......173#....732....9...221............*979....*....=..............
+...............551..&..235..............204*....@...65.......735..664................................966.918$........247.945..237...........
+.....920....42..*............844&.364@......823.801...........*.............&.995...344...688..178............560....................658*680
+.271...#........339.142.................921.................219..748......727.*.....*............@...........*.................131..........
+............./......#......260.....%851...*......................*.............910...535................657..996.%499..625........*.........
+....@363......785...........*.............795........31.........213.443....................................*...........+........244.....=806
+.........390.........320...553...84@.................*..........................45....547.........376.......206.............................
+54...................*................131..........*..32...............469........=..*.....665......&.........................427......349..
+..................193......894.....*.....*442...589.....................&............124...=.............405.........636......*...#.........
+..............................-.950.457.......................................-...................39.875...*.....230*......191..349.........
+.../.........863..........334...........90..199........184....................421.....979...666..+....*.....145.......#.....................
+...288..............568..#....501........*.....................$.....677.+.............*......*....959..............302..=875..821@.....531.
+...................*.........*....625..621.....689........405..247..#.....351..659..162....942............349.304...........................
+.......851.........742...&...478.....*.........#....523.........................=................$.......*.........589....758......371......
+...$........745........593...........181...835......*......794....=...502..381@...................973...77..433.....=....&........#.........
+.932........*................-.............%..........147........316..*..........%..........................*................323............
+.........283...+......100...486....4.............52..*...............977.......472.*....262................298......=894..........128.93....
+.............953.......*........................*....719....$......................473.....=...523......-.......723..................*......
+....................571.......................720..........269...........885.............................902...........80...738..........975

+ 10 - 0
Day3/inputs/sample1.txt

@@ -0,0 +1,10 @@
+467..114..
+...*......
+..35..633.
+......#...
+617*......
+.....+.58.
+..592.....
+......755.
+...$.*....
+.664.598..