Day 14 initial solution

Part 1 brute force, part 2 solved "correctly" (I think, anyway). This day runs slower than I would expect, given how little part 2 is doing.
This commit is contained in:
2021-12-14 12:01:35 -06:00
parent 43be612236
commit bd6038c837
4 changed files with 216 additions and 1 deletions

View File

@ -57,6 +57,9 @@
<None Update="inputs\13.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="inputs\14.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

102
inputs/14.txt Normal file
View File

@ -0,0 +1,102 @@
OKSBBKHFBPVNOBKHBPCO
CB -> P
VH -> S
CF -> P
OV -> B
CH -> N
PB -> F
KF -> O
BC -> K
FB -> F
SN -> F
FV -> B
PN -> K
SF -> V
FN -> F
SS -> K
VP -> F
VB -> B
OS -> N
HP -> O
NF -> S
SK -> H
OO -> S
PF -> C
CC -> P
BP -> F
OB -> C
CS -> N
BV -> F
VV -> B
HO -> F
KN -> P
VC -> K
KK -> N
BO -> V
NH -> O
HC -> S
SB -> F
NN -> V
OF -> V
FK -> S
OP -> S
NS -> C
HV -> O
PC -> C
FO -> H
OH -> F
BF -> S
SO -> O
HB -> P
NK -> H
NV -> C
NB -> B
FF -> B
BH -> C
SV -> B
BK -> K
NO -> C
VN -> P
FC -> B
PH -> V
HH -> C
VO -> O
SP -> P
VK -> N
CP -> H
SC -> C
KV -> H
CO -> C
OK -> V
ON -> C
KS -> S
NP -> O
CK -> C
BS -> F
VS -> B
KH -> O
KC -> C
KB -> N
OC -> F
PP -> S
HK -> H
BN -> S
KO -> K
NC -> B
PK -> K
CV -> H
PO -> O
BB -> C
HS -> F
SH -> K
CN -> S
HN -> S
KP -> O
FP -> H
HF -> F
PS -> B
FH -> K
PV -> O
FS -> N
VF -> V

109
src/14.cs Normal file
View File

@ -0,0 +1,109 @@
using System.Text;
namespace aoc2021;
internal class Day14 : Day
{
internal override void Go()
{
var lines = Util.ReadAllLines("inputs/14.txt");
string template = string.Empty;
Dictionary<string, char> rules = new();
foreach (var line in lines)
{
if (string.IsNullOrEmpty(template))
{
template = line;
}
else if (!string.IsNullOrEmpty(line))
{
var pair = line.Split(" -> ");
rules.Add(pair[0], pair[1][0]);
}
}
Part1(template, rules);
Part2(template, rules);
}
private static void Part1(string template, Dictionary<string, char> rules)
{
using var t = new Timer();
string curr = template;
for (int i = 0; i < 10; i++)
{
StringBuilder sb = new();
for (int j = 0; j < curr.Length - 1; j++)
{
sb.Append(curr[j]);
sb.Append(rules[curr[j..(j + 2)]]);
}
sb.Append(curr[curr.Length - 1]);
curr = sb.ToString();
}
Dictionary<char, int> frequency = new();
foreach (var c in curr)
{
if (!frequency.ContainsKey(c))
{
frequency.Add(c, 0);
}
frequency[c]++;
}
var least = frequency.MinBy(x => x.Value);
var most = frequency.MaxBy(x => x.Value);
Logger.Log($"part1: <blue>{most.Value - least.Value}<r>");
}
private static void Part2(string template, Dictionary<string, char> rules)
{
using var t = new Timer();
Dictionary<string, long> pairs = new();
Dictionary<char, long> frequencies = new();
foreach (var pair in rules)
{
pairs.Add(pair.Key, 0);
if (!frequencies.ContainsKey(pair.Key[0]))
{
frequencies.Add(pair.Key[0], 0);
}
if (!frequencies.ContainsKey(pair.Key[1]))
{
frequencies.Add(pair.Key[1], 0);
}
}
for (int i = 0; i < template.Length - 1; i++)
{
var pair = template[i..(i + 2)];
pairs[pair]++;
frequencies[template[i]]++;
}
frequencies[template[^1]]++;
for (int round = 0; round < 40; round++)
{
foreach (var pair in pairs.Where(x => x.Value > 0).ToList())
{
pairs[pair.Key] -= pair.Value;
frequencies[rules[pair.Key]] += pair.Value;
var np1 = $"{pair.Key[0]}{rules[pair.Key]}";
var np2 = $"{rules[pair.Key]}{pair.Key[1]}";
pairs[np1] += pair.Value;
pairs[np2] += pair.Value;
}
}
var least = frequencies.MinBy(x => x.Value);
var most = frequencies.MaxBy(x => x.Value);
Logger.Log($"part2: <blue>{most.Value - least.Value}<r>");
}
}

View File

@ -31,7 +31,8 @@ else
"10" => new Day10(),
"11" => new Day11(),
"12" => new Day12(),
_ => new Day13(),
"13" => new Day13(),
_ => new Day14(),
};
day.Go();
}