diff --git a/aoc2021.test/DayTests.cs b/aoc2021.test/DayTests.cs index c57eead..b5cad43 100644 --- a/aoc2021.test/DayTests.cs +++ b/aoc2021.test/DayTests.cs @@ -9,6 +9,7 @@ public class DayTests [DataRow(typeof(Day03), "3009600", "6940518")] [DataRow(typeof(Day04), "8580", "9576")] [DataRow(typeof(Day05), "7318", "19939")] + [DataRow(typeof(Day06), "362740", "1644874076764")] public void CheckAllDays(Type dayType, string part1, string part2) { var s = Stopwatch.StartNew(); @@ -44,7 +45,7 @@ public class DayTests [DataRow(typeof(Day03), "198", "230")] [DataRow(typeof(Day04), "4512", "1924")] [DataRow(typeof(Day05), "5", "12")] - [DataRow(typeof(Day06), "5934", "")] + [DataRow(typeof(Day06), "5934", "26984457539")] public void CheckTestInputs(Type dayType, string part1, string part2) { Day.UseTestInput = true; diff --git a/aoc2021/Day06.cs b/aoc2021/Day06.cs index 5783a44..60687c5 100644 --- a/aoc2021/Day06.cs +++ b/aoc2021/Day06.cs @@ -5,39 +5,31 @@ /// public sealed class Day06 : Day { - private readonly List _fishes; + private readonly long _p1, _p2; + public Day06() : base(6, "Lanternfish") { - //UseTestInput = true; - _fishes = Input.First().Split(',').Select(int.Parse).ToList(); - } - - private static List DayStep(List state) - { - List result = new(); + var fishes = Input.First().Split(',').Select(long.Parse).ToList(); + Dictionary counts = new(); - foreach (var fish in state) + for (var i = 0; i <= 8; i++) + counts[i] = fishes.Count(x => x == i); + + for (var i = 0; i < 256; i++) { - switch (fish) - { - case 0: - result.Add(6); - result.Add(8); - break; - default: - result.Add(fish - 1); - break; - } + var breeders = counts[0]; + for (var j = 0; j < 8; j++) + counts[j] = counts[j + 1]; + counts[6] += breeders; + counts[8] = breeders; + + if (i == 79) _p1 = counts.Values.Sum(); } - return result; + _p2 = counts.Values.Sum(); } - public override string Part1() - { - var fishes = Enumerable.Range(0, 80).Aggregate(_fishes, (current, _) => DayStep(current)); - return $"{fishes.Count}"; - } + public override string Part1() => $"{_p1}"; - public override string Part2() => ""; + public override string Part2() => $"{_p2}"; }