Nice to have a really simple one for a change, both my day 1 and 2 solutions worked on their very first attempts.
I rewrote the code to combine the two though, since the implementations were almost identical for both solutions, and also to replace the recursion with a search list instead.
C#
int[] heights = new int[0];
(int, int) size = (0, 0);
public void Input(IEnumerable<string> lines)
{
size = (lines.First().Length, lines.Count());
heights = string.Concat(lines).Select(c => int.Parse(c.ToString())).ToArray();
}
int trails = 0, trailheads = 0;
public void PreCalc()
{
for (int y = 0; y < size.Item2; ++y)
for (int x = 0; x < size.Item1; ++x)
if (heights[y * size.Item1 + x] == 0)
{
var unique = new HashSet<(int, int)>();
trails += CountTrails((x, y), unique);
trailheads += unique.Count;
}
}
public void Part1()
{
Console.WriteLine($"Trailheads: {trailheads}");
}
public void Part2()
{
Console.WriteLine($"Trails: {trails}");
}
int CountTrails((int, int) from, HashSet<(int,int)> unique)
{
int found = 0;
List<(int,int)> toSearch = new List<(int, int)>();
toSearch.Add(from);
while (toSearch.Any())
{
var cur = toSearch.First();
toSearch.RemoveAt(0);
int height = heights[cur.Item2 * size.Item1 + cur.Item1];
for (int y = -1; y <= 1; ++y)
for (int x = -1; x <= 1; ++x)
{
if ((y != 0 && x != 0) || (y == 0 && x == 0))
continue;
var newAt = (cur.Item1 + x, cur.Item2 + y);
if (newAt.Item1 < 0 || newAt.Item1 >= size.Item1 || newAt.Item2 < 0 || newAt.Item2 >= size.Item2)
continue;
int newHeight = heights[newAt.Item2 * size.Item1 + newAt.Item1];
if (newHeight - height != 1)
continue;
if (newHeight == 9)
{
unique.Add(newAt);
found++;
continue;
}
toSearch.Add(newAt);
}
}
return found;
}
And now we get into the days where caching really is king. My first attempt didn’t go so well, I tried to handle the full list result as one cache step, instead of individually caching the result of calculating each stone per step.
I think my original attempt is still calculating at home, but I finished up this much better version on the trip to work.
All hail public transport.
C#
List<long> stones = new List<long>(); public void Input(IEnumerable<string> lines) { stones = string.Concat(lines).Split(' ').Select(v => long.Parse(v)).ToList(); } public void Part1() { var expanded = TryExpand(stones, 25); Console.WriteLine($"Stones: {expanded}"); } public void Part2() { var expanded = TryExpand(stones, 75); Console.WriteLine($"Stones: {expanded}"); } public long TryExpand(IEnumerable<long> stones, int steps) { if (steps == 0) return stones.Count(); return stones.Select(s => TryExpand(s, steps)).Sum(); } Dictionary<(long, int), long> cache = new Dictionary<(long, int), long>(); public long TryExpand(long stone, int steps) { var key = (stone, steps); if (cache.ContainsKey(key)) return cache[key]; var result = TryExpand(Blink(stone), steps - 1); cache[key] = result; return result; } public IEnumerable<long> Blink(long stone) { if (stone == 0) { yield return 1; yield break; } var str = stone.ToString(); if (str.Length % 2 == 0) { yield return long.Parse(str[..(str.Length / 2)]); yield return long.Parse(str[(str.Length / 2)..]); yield break; } yield return stone * 2024; }