Day 14 (ugly)
This commit is contained in:
parent
d109e882cb
commit
e3323cb305
2
Day13.cs
2
Day13.cs
|
|
@ -1,4 +1,4 @@
|
|||
#define Day13
|
||||
// #define Day13
|
||||
#if Day13
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,78 @@
|
|||
#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 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);
|
||||
|
||||
// for (var i = 0; i < 10; i++) {
|
||||
// var next = new List<char>();
|
||||
// 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<char>(){r.a, r.b}).Distinct().ToDictionary(x => x, _ => 0L);
|
||||
int maxDepth = 40;
|
||||
|
||||
var finalCount = new Dictionary<char, long>();
|
||||
var save = new Dictionary<AsKey, Dictionary<char, long>>();
|
||||
|
||||
Dictionary<char, long> Bla(Pair cur, int depth) {
|
||||
var curKey = new AsKey(cur, depth);
|
||||
if (save.ContainsKey(curKey)) {
|
||||
return save[curKey];
|
||||
}
|
||||
if (depth <= 0) {
|
||||
return new Dictionary<char, long>() { {cur.a, 1} };
|
||||
}
|
||||
|
||||
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 merged = Merge(first, second);
|
||||
save[curKey] = merged.ToDictionary(x => x.Key, x => x.Value);
|
||||
|
||||
return merged;
|
||||
}
|
||||
|
||||
Dictionary<char, long> Merge(Dictionary<char, long> a, Dictionary<char, long> b) {
|
||||
var merged = new Dictionary<char, long>();
|
||||
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;
|
||||
}
|
||||
|
||||
for (int pos = 1; pos < startTwo.Count; pos++) {
|
||||
var countBla = Bla(new Pair(startTwo[pos - 1], startTwo[pos]), maxDepth);
|
||||
finalCount = Merge(finalCount, countBla);
|
||||
}
|
||||
finalCount[startTwo.Last()]++;
|
||||
|
||||
Console.WriteLine(finalCount.Values.Max() - finalCount.Values.Min());
|
||||
|
||||
record AsKey(Pair p, int depth);
|
||||
record Rule(Pair p, char res);
|
||||
record Pair(char a, char b);
|
||||
#endif
|
||||
|
|
@ -0,0 +1,102 @@
|
|||
PSVVKKCNBPNBBHNSFKBO
|
||||
|
||||
CF -> H
|
||||
PP -> H
|
||||
SP -> V
|
||||
NO -> C
|
||||
SF -> F
|
||||
FS -> H
|
||||
OF -> P
|
||||
PN -> B
|
||||
SH -> V
|
||||
BO -> K
|
||||
ON -> V
|
||||
VP -> S
|
||||
HN -> B
|
||||
PS -> P
|
||||
FV -> H
|
||||
NC -> N
|
||||
FN -> S
|
||||
PF -> F
|
||||
BF -> F
|
||||
NB -> O
|
||||
HS -> C
|
||||
SC -> V
|
||||
PC -> K
|
||||
KF -> K
|
||||
HC -> C
|
||||
OK -> H
|
||||
KS -> P
|
||||
VF -> C
|
||||
NV -> S
|
||||
KK -> F
|
||||
HV -> H
|
||||
SV -> V
|
||||
KC -> N
|
||||
HF -> P
|
||||
SN -> F
|
||||
VS -> P
|
||||
VN -> F
|
||||
VH -> C
|
||||
OB -> K
|
||||
VV -> O
|
||||
VC -> O
|
||||
KP -> V
|
||||
OP -> C
|
||||
HO -> S
|
||||
NP -> K
|
||||
HB -> C
|
||||
CS -> S
|
||||
OO -> S
|
||||
CV -> K
|
||||
BS -> F
|
||||
BH -> P
|
||||
HP -> P
|
||||
PK -> B
|
||||
BB -> H
|
||||
PV -> N
|
||||
VO -> P
|
||||
SS -> B
|
||||
CC -> F
|
||||
BC -> V
|
||||
FF -> S
|
||||
HK -> V
|
||||
OH -> N
|
||||
BV -> C
|
||||
CP -> F
|
||||
KN -> K
|
||||
NN -> S
|
||||
FB -> F
|
||||
PH -> O
|
||||
FH -> N
|
||||
FK -> P
|
||||
CK -> V
|
||||
CN -> S
|
||||
BP -> K
|
||||
CH -> F
|
||||
FP -> K
|
||||
HH -> N
|
||||
NF -> C
|
||||
VB -> B
|
||||
FO -> N
|
||||
PB -> C
|
||||
KH -> K
|
||||
PO -> K
|
||||
OV -> F
|
||||
NH -> H
|
||||
KV -> B
|
||||
OS -> K
|
||||
OC -> K
|
||||
FC -> H
|
||||
SO -> H
|
||||
KO -> P
|
||||
NS -> F
|
||||
CB -> C
|
||||
CO -> F
|
||||
KB -> V
|
||||
BK -> K
|
||||
NK -> O
|
||||
SK -> C
|
||||
SB -> B
|
||||
VK -> O
|
||||
BN -> H
|
||||
Loading…
Reference in New Issue