This commit is contained in:
2023-12-08 20:32:01 -05:00
parent 8c4a120138
commit 4cc5d64f3a
4 changed files with 114 additions and 0 deletions

View File

@@ -55,6 +55,8 @@
<None Remove="problems\aoc2023\day7\input.txt" /> <None Remove="problems\aoc2023\day7\input.txt" />
<None Remove="problems\aoc2023\day7\sample.txt" /> <None Remove="problems\aoc2023\day7\sample.txt" />
<None Remove="problems\aoc2023\day7\sara.txt" /> <None Remove="problems\aoc2023\day7\sara.txt" />
<None Remove="problems\aoc2023\day8\input.txt" />
<None Remove="problems\aoc2023\day8\sample.txt" />
</ItemGroup> </ItemGroup>

View File

@@ -0,0 +1,74 @@
using AdventOfCode.Runner.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode.Problems.AOC2023.Day8;
[ProblemInfo(2023, 8, "Haunted Wasteland")]
internal class HauntedWasteland : Problem<int, long>
{
private string _path = string.Empty;
private Dictionary<string, (string left, string right)> _nodes = [];
public override void LoadInput()
{
var data = ReadInputLines();
_path = data[0];
for (int i = 2; i < data.Length; i++)
{
var curLine = data[i].Split('=');
var node = curLine[0].TrimEnd();
var branches = curLine[1][2..^1].Split(", ");
_nodes.Add(node, (branches[0], branches[1]));
}
}
public override void CalculatePart1()
{
var curPos = "AAA";
var i = 0;
var steps = 0;
do
{
curPos = _path[i] switch
{
'L' => _nodes[curPos].left,
'R' => _nodes[curPos].right,
_ => throw new Exception("Something went horribly wrong")
};
i = (i + 1) % _path.Length;
steps++;
} while (curPos != "ZZZ");
Part1 = steps;
}
public override void CalculatePart2()
{
var curPos = _nodes.Keys.Where(n => n[^1] == 'A').ToArray();
var len = new long[curPos.Length];
var i = 0;
do
{
for (int j = 0; j < curPos.Length; j++)
{
if (curPos[j][^1] == 'Z')
continue;
len[j]++;
curPos[j] = _path[i] switch
{
'L' => _nodes[curPos[j]].left,
'R' => _nodes[curPos[j]].right,
_ => throw new Exception("Something went horribly wrong")
};
}
i = (i + 1) % _path.Length;
} while (curPos.Any(n => n[^1] != 'Z'));
Part2 = len.LCM();
}
}

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Numerics;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -15,4 +16,15 @@ public static class Extensions
} }
return values; return values;
} }
public static T LCM<T>(this IEnumerable<T> values) where T : INumber<T>
{
var a = values.First();
values = values.Skip(1);
foreach (var item in values)
{
a = a.LCM(item);
}
return a;
}
} }

View File

@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode.Runner;
public static class ExtraMath
{
public static T GCF<T>(this T a, T b) where T : INumber<T>
{
while (!b.Equals(T.Zero))
{
var t = b;
b = a % b;
a = t;
}
return a;
}
public static T LCM<T>(this T a, T b) where T: INumber<T>
{
return (a / GCF(a, b)) * b;
}
}