Day 14 minor cleanup
This commit is contained in:
parent
e3323cb305
commit
c260de0841
70
Day14.cs
70
Day14.cs
|
|
@ -1,35 +1,33 @@
|
||||||
#define Day14
|
#define Day14
|
||||||
#if Day14
|
#if Day14
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using AoC2021;
|
|
||||||
var text = File.ReadAllText("day14/input").Split("\n\n");
|
var text = File.ReadAllText("day14/input").Split("\n\n");
|
||||||
var startOne = text[0].Select(x => x).ToList();
|
var start = text[0].Select(x => x).ToList();
|
||||||
var startTwo = startOne.ToList();
|
|
||||||
|
|
||||||
var reg = new Regex(@"(?<a>\w)(?<b>\w) -> (?<res>\w)");
|
var reg = new Regex(@"(?<a>\w)(?<b>\w) -> (?<res>\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);
|
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++) {
|
for (var i = 0; i < 10; i++) {
|
||||||
// var next = new List<char>();
|
var next = new List<char>();
|
||||||
// for (int pos = 1; pos < startOne.Count; pos++) {
|
for (int pos = 1; pos < start.Count; pos++) {
|
||||||
// var res = rules[new Pair(startOne[pos - 1], startOne[pos])];
|
var res = rules[new Pair(start[pos - 1], start[pos])];
|
||||||
// next.Add(startOne[pos - 1]);
|
next.Add(start[pos - 1]);
|
||||||
// next.Add(res);
|
next.Add(res);
|
||||||
// }
|
}
|
||||||
// next.Add(startOne.Last());
|
next.Add(start.Last());
|
||||||
// startOne = next;
|
start = 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<char>(){r.a, r.b}).Distinct().ToDictionary(x => x, _ => 0L);
|
|
||||||
int maxDepth = 40;
|
|
||||||
|
|
||||||
var finalCount = new Dictionary<char, long>();
|
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<char, long>();
|
||||||
var save = new Dictionary<AsKey, Dictionary<char, long>>();
|
var save = new Dictionary<AsKey, Dictionary<char, long>>();
|
||||||
|
|
||||||
Dictionary<char, long> Bla(Pair cur, int depth) {
|
Dictionary<char, long> Process(Pair cur, int depth) {
|
||||||
var curKey = new AsKey(cur, depth);
|
var curKey = new AsKey(cur, depth);
|
||||||
if (save.ContainsKey(curKey)) {
|
if (save.ContainsKey(curKey)) {
|
||||||
return save[curKey];
|
return save[curKey];
|
||||||
|
|
@ -39,8 +37,8 @@ Dictionary<char, long> Bla(Pair cur, int depth) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var c = rules[cur];
|
var c = rules[cur];
|
||||||
var first = Bla(new Pair(cur.a, c), depth - 1);
|
var first = Process(new Pair(cur.a, c), depth - 1);
|
||||||
var second = Bla(new Pair(c, cur.b), depth - 1);
|
var second = Process(new Pair(c, cur.b), depth - 1);
|
||||||
|
|
||||||
var merged = Merge(first, second);
|
var merged = Merge(first, second);
|
||||||
save[curKey] = merged.ToDictionary(x => x.Key, x => x.Value);
|
save[curKey] = merged.ToDictionary(x => x.Key, x => x.Value);
|
||||||
|
|
@ -49,28 +47,18 @@ Dictionary<char, long> Bla(Pair cur, int depth) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Dictionary<char, long> Merge(Dictionary<char, long> a, Dictionary<char, long> b) {
|
Dictionary<char, long> Merge(Dictionary<char, long> a, Dictionary<char, long> b) {
|
||||||
var merged = new Dictionary<char, long>();
|
return new List<Dictionary<char, long>>(){a, b}.SelectMany(x => x)
|
||||||
foreach (var k in a.Keys) {
|
.ToLookup(pair => pair.Key, pair => pair.Value)
|
||||||
merged.Add(k, a[k]);
|
.ToDictionary(group => group.Key, group => group.Sum());
|
||||||
}
|
|
||||||
foreach (var k in b.Keys) {
|
|
||||||
if (merged.ContainsKey(k)) {
|
|
||||||
merged[k] += b[k];
|
|
||||||
} else {
|
|
||||||
merged[k] = b[k];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return merged;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int pos = 1; pos < startTwo.Count; pos++) {
|
for (int pos = 1; pos < start.Count; pos++) {
|
||||||
var countBla = Bla(new Pair(startTwo[pos - 1], startTwo[pos]), maxDepth);
|
var countBla = Process(new Pair(start[pos - 1], start[pos]), maxDepth);
|
||||||
finalCount = Merge(finalCount, countBla);
|
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 AsKey(Pair p, int depth);
|
||||||
record Rule(Pair p, char res);
|
record Rule(Pair p, char res);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue