From c260de0841aab5a639400a84932cebec225c7a27 Mon Sep 17 00:00:00 2001 From: Stefan Forstenlechner Date: Tue, 14 Dec 2021 23:05:07 +0100 Subject: [PATCH] Day 14 minor cleanup --- Day14.cs | 70 +++++++++++++++++++++++--------------------------------- 1 file changed, 29 insertions(+), 41 deletions(-) diff --git a/Day14.cs b/Day14.cs index a5f27ca..612babb 100644 --- a/Day14.cs +++ b/Day14.cs @@ -1,35 +1,33 @@ #define Day14 #if Day14 using System.Text.RegularExpressions; -using AoC2021; var text = File.ReadAllText("day14/input").Split("\n\n"); -var startOne = text[0].Select(x => x).ToList(); -var startTwo = startOne.ToList(); +var start = text[0].Select(x => x).ToList(); var reg = new Regex(@"(?\w)(?\w) -> (?\w)"); var rules = reg.Matches(text[1]).Select(m => new Rule(new Pair(m.Groups["a"].Value[0], m.Groups["b"].Value[0]), m.Groups["res"].Value[0])).ToDictionary(x => x.p, x=> x.res); -// for (var i = 0; i < 10; i++) { -// var next = new List(); -// for (int pos = 1; pos < startOne.Count; pos++) { -// var res = rules[new Pair(startOne[pos - 1], startOne[pos])]; -// next.Add(startOne[pos - 1]); -// next.Add(res); -// } -// next.Add(startOne.Last()); -// startOne = next; -// } -// -// var charCountPartOne = startOne.GroupBy(s => s).ToDictionary(g => g.Key, g => g.LongCount()); -// Console.WriteLine(charCountPartOne.Values.Max() - charCountPartOne.Values.Min()); -// -// var charCountPartTwo = rules.Keys.SelectMany(r => new List(){r.a, r.b}).Distinct().ToDictionary(x => x, _ => 0L); -int maxDepth = 40; +for (var i = 0; i < 10; i++) { + var next = new List(); + for (int pos = 1; pos < start.Count; pos++) { + var res = rules[new Pair(start[pos - 1], start[pos])]; + next.Add(start[pos - 1]); + next.Add(res); + } + next.Add(start.Last()); + start = next; +} -var finalCount = new Dictionary(); +var charCountPartOne = start.GroupBy(s => s).ToDictionary(g => g.Key, g => g.LongCount()); +Console.WriteLine(charCountPartOne.Values.Max() - charCountPartOne.Values.Min()); + +// Part two +start = text[0].Select(x => x).ToList(); +int maxDepth = 40; +var charCountPartTwo = new Dictionary(); var save = new Dictionary>(); -Dictionary Bla(Pair cur, int depth) { +Dictionary Process(Pair cur, int depth) { var curKey = new AsKey(cur, depth); if (save.ContainsKey(curKey)) { return save[curKey]; @@ -39,8 +37,8 @@ Dictionary Bla(Pair cur, int depth) { } var c = rules[cur]; - var first = Bla(new Pair(cur.a, c), depth - 1); - var second = Bla(new Pair(c, cur.b), depth - 1); + var first = Process(new Pair(cur.a, c), depth - 1); + var second = Process(new Pair(c, cur.b), depth - 1); var merged = Merge(first, second); save[curKey] = merged.ToDictionary(x => x.Key, x => x.Value); @@ -49,28 +47,18 @@ Dictionary Bla(Pair cur, int depth) { } Dictionary Merge(Dictionary a, Dictionary b) { - var merged = new Dictionary(); - foreach (var k in a.Keys) { - merged.Add(k, a[k]); - } - foreach (var k in b.Keys) { - if (merged.ContainsKey(k)) { - merged[k] += b[k]; - } else { - merged[k] = b[k]; - } - } - - return merged; + return new List>(){a, b}.SelectMany(x => x) + .ToLookup(pair => pair.Key, pair => pair.Value) + .ToDictionary(group => group.Key, group => group.Sum()); } -for (int pos = 1; pos < startTwo.Count; pos++) { - var countBla = Bla(new Pair(startTwo[pos - 1], startTwo[pos]), maxDepth); - finalCount = Merge(finalCount, countBla); +for (int pos = 1; pos < start.Count; pos++) { + var countBla = Process(new Pair(start[pos - 1], start[pos]), maxDepth); + charCountPartTwo = Merge(charCountPartTwo, countBla); } -finalCount[startTwo.Last()]++; +charCountPartTwo[start.Last()]++; -Console.WriteLine(finalCount.Values.Max() - finalCount.Values.Min()); +Console.WriteLine(charCountPartTwo.Values.Max() - charCountPartTwo.Values.Min()); record AsKey(Pair p, int depth); record Rule(Pair p, char res);