day 6 part 2
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Ben Harris 2021-12-06 01:00:14 -05:00
parent 93e61904ac
commit 08e67ecace
2 changed files with 20 additions and 27 deletions

View File

@ -9,6 +9,7 @@ public class DayTests
[DataRow(typeof(Day03), "3009600", "6940518")] [DataRow(typeof(Day03), "3009600", "6940518")]
[DataRow(typeof(Day04), "8580", "9576")] [DataRow(typeof(Day04), "8580", "9576")]
[DataRow(typeof(Day05), "7318", "19939")] [DataRow(typeof(Day05), "7318", "19939")]
[DataRow(typeof(Day06), "362740", "1644874076764")]
public void CheckAllDays(Type dayType, string part1, string part2) public void CheckAllDays(Type dayType, string part1, string part2)
{ {
var s = Stopwatch.StartNew(); var s = Stopwatch.StartNew();
@ -44,7 +45,7 @@ public class DayTests
[DataRow(typeof(Day03), "198", "230")] [DataRow(typeof(Day03), "198", "230")]
[DataRow(typeof(Day04), "4512", "1924")] [DataRow(typeof(Day04), "4512", "1924")]
[DataRow(typeof(Day05), "5", "12")] [DataRow(typeof(Day05), "5", "12")]
[DataRow(typeof(Day06), "5934", "")] [DataRow(typeof(Day06), "5934", "26984457539")]
public void CheckTestInputs(Type dayType, string part1, string part2) public void CheckTestInputs(Type dayType, string part1, string part2)
{ {
Day.UseTestInput = true; Day.UseTestInput = true;

View File

@ -5,39 +5,31 @@
/// </summary> /// </summary>
public sealed class Day06 : Day public sealed class Day06 : Day
{ {
private readonly List<int> _fishes; private readonly long _p1, _p2;
public Day06() : base(6, "Lanternfish") public Day06() : base(6, "Lanternfish")
{ {
//UseTestInput = true; var fishes = Input.First().Split(',').Select(long.Parse).ToList();
_fishes = Input.First().Split(',').Select(int.Parse).ToList(); Dictionary<long, long> counts = new();
}
private static List<int> DayStep(List<int> state)
{
List<int> result = 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) var breeders = counts[0];
{ for (var j = 0; j < 8; j++)
case 0: counts[j] = counts[j + 1];
result.Add(6); counts[6] += breeders;
result.Add(8); counts[8] = breeders;
break;
default: if (i == 79) _p1 = counts.Values.Sum();
result.Add(fish - 1);
break;
}
} }
return result; _p2 = counts.Values.Sum();
} }
public override string Part1() public override string Part1() => $"{_p1}";
{
var fishes = Enumerable.Range(0, 80).Aggregate(_fishes, (current, _) => DayStep(current));
return $"{fishes.Count}";
}
public override string Part2() => ""; public override string Part2() => $"{_p2}";
} }