From 2cc95c6e8e749dabb3a272919f77e1b01578a1f7 Mon Sep 17 00:00:00 2001 From: Parnic Date: Sun, 5 Dec 2021 23:55:15 -0600 Subject: [PATCH] Day 6 solution Took me a while to come to the fast solution, but I got there. --- advent-of-code-2021.csproj | 3 ++ inputs/06.txt | 1 + src/06.cs | 81 ++++++++++++++++++++++++++++++++++++++ src/main.cs | 1 + 4 files changed, 86 insertions(+) create mode 100644 inputs/06.txt create mode 100644 src/06.cs diff --git a/advent-of-code-2021.csproj b/advent-of-code-2021.csproj index 380676c..781fedd 100644 --- a/advent-of-code-2021.csproj +++ b/advent-of-code-2021.csproj @@ -30,6 +30,9 @@ PreserveNewest + + PreserveNewest + diff --git a/inputs/06.txt b/inputs/06.txt new file mode 100644 index 0000000..9946edb --- /dev/null +++ b/inputs/06.txt @@ -0,0 +1 @@ +4,1,4,1,3,3,1,4,3,3,2,1,1,3,5,1,3,5,2,5,1,5,5,1,3,2,5,3,1,3,4,2,3,2,3,3,2,1,5,4,1,1,1,2,1,4,4,4,2,1,2,1,5,1,5,1,2,1,4,4,5,3,3,4,1,4,4,2,1,4,4,3,5,2,5,4,1,5,1,1,1,4,5,3,4,3,4,2,2,2,2,4,5,3,5,2,4,2,3,4,1,4,4,1,4,5,3,4,2,2,2,4,3,3,3,3,4,2,1,2,5,5,3,2,3,5,5,5,4,4,5,5,4,3,4,1,5,1,3,4,4,1,3,1,3,1,1,2,4,5,3,1,2,4,3,3,5,4,4,5,4,1,3,1,1,4,4,4,4,3,4,3,1,4,5,1,2,4,3,5,1,1,2,1,1,5,4,2,1,5,4,5,2,4,4,1,5,2,2,5,3,3,2,3,1,5,5,5,4,3,1,1,5,1,4,5,2,1,3,1,2,4,4,1,1,2,5,3,1,5,2,4,5,1,2,3,1,2,2,1,2,2,1,4,1,3,4,2,1,1,5,4,1,5,4,4,3,1,3,3,1,1,3,3,4,2,3,4,2,3,1,4,1,5,3,1,1,5,3,2,3,5,1,3,1,1,3,5,1,5,1,1,3,1,1,1,1,3,3,1 \ No newline at end of file diff --git a/src/06.cs b/src/06.cs new file mode 100644 index 0000000..946286a --- /dev/null +++ b/src/06.cs @@ -0,0 +1,81 @@ +using System.Diagnostics; + +namespace aoc2021 +{ + internal class Day06 + { + [DebuggerDisplay("{State}")] + struct Fish + { + public int State; + } + + internal static void Go() + { + Logger.Log("Day 6"); + Logger.Log("-----"); + var input = File.ReadAllText("inputs/06.txt"); + List fish = new(); + foreach (var state in input.Split(',')) + { + fish.Add(new Fish() { State = Convert.ToInt32(state) }); + } + Part1(fish); + Part2(fish); + Logger.Log(""); + } + + private static void Part1(IEnumerable fish) + { + using var t = new Timer(); + + var list = fish.ToList(); + // brute force method (my initial solution) + for (int day = 0; day < 80; day++) + { + for (int i = list.Count - 1; i >= 0; i--) + { + Fish f = list[i]; + if (f.State == 0) + { + list.Add(new Fish() { State = 8 }); + f.State = 7; + } + + f.State--; + list[i] = f; + } + } + +#pragma warning disable CA1829 // Use Length/Count property instead of Count() when available - Count is of type int, list might be longer than that + Logger.Log($"part1: #fish={list.LongCount()}"); +#pragma warning restore CA1829 // Use Length/Count property instead of Count() when available + } + + private static void Part2(IEnumerable fish) + { + using var t = new Timer(); + + // fast method (when brute force threatened to blow RAM and take way too long) + Dictionary fishAtState = new(); + for (int i = 0; i <= 8; i++) + { + fishAtState[i] = fish.Count(x => x.State == i); + } + + for (int day = 0; day < 256; day++) + { + var adders = fishAtState[0]; + for (int i = 0; i < 8; i++) + { + fishAtState[i] = fishAtState[i + 1]; + } + + fishAtState[6] += adders; + fishAtState[8] = adders; + } + + Logger.Log($"part2: #fish={fishAtState.Values.Sum()}"); + } + } +} diff --git a/src/main.cs b/src/main.cs index 2c38f15..f493dc4 100644 --- a/src/main.cs +++ b/src/main.cs @@ -1,3 +1,4 @@ aoc2021.Day02.Go(); aoc2021.Day03.Go(); aoc2021.Day05.Go(); +aoc2021.Day06.Go();