Skip to content

Instantly share code, notes, and snippets.

@Vertygo
Created December 14, 2021 11:21
Show Gist options
  • Save Vertygo/b6fda90e53a969406f7f4315c648cef9 to your computer and use it in GitHub Desktop.
Save Vertygo/b6fda90e53a969406f7f4315c648cef9 to your computer and use it in GitHub Desktop.
public void Main()
{
var board = new Board();
board.Parse(input);
for (int i = 0; i < 40; i++)
{
$"Step {i}".Dump();
board.Step();
}
board.CharCounter.Dump();
board.Combinations.Dump();
long max = board.CharCounter.Max(c=>c.Value);
long min = board.CharCounter.Min(c => c.Value);
$"result: {max} - {min} = {max - min}".Dump();
}
public class Board
{
public Dictionary<string, long> Combinations = new Dictionary<string, long>();
public Dictionary<string, char> Rules = new Dictionary<string, char>();
public Dictionary<char, long> CharCounter = new Dictionary<char, long>();
public void Parse(string input)
{
var manual = input.Split("\r\n\r\n").ToArray();
Rules = manual[1].Split("\r\n").ToDictionary(k => k.Split(" -> ")[0], v => v.Split(" -> ")[1].First());
var template = manual[0];
for(int i = 0; i<template.Count()-1;i++)
{
string key = string.Join("", $"{template[i]}{template[i+1]}");
if(!Combinations.TryAdd(key, 1))
{
Combinations[key] += 1;
}
}
foreach(char c in template)
{
if (!CharCounter.TryAdd(c, 1))
{
CharCounter[c] += 1;
}
}
}
internal void Step()
{
var dic = Combinations.Where(c => c.Value>0).ToDictionary(k => k.Key, v=>v.Value);
foreach(var key in dic.Keys)
{
long count = dic[key];
Combinations[key] -= count;
var middle = Rules[key];
var l = $"{key[0]}{middle}";
if (!Combinations.TryAdd(l, count))
{
Combinations[l] += count;
}
var r = $"{middle}{key[1]}";
if (!Combinations.TryAdd(r, count))
{
Combinations[r] += count;
}
if (!CharCounter.TryAdd(middle, count))
{
CharCounter[middle] += count;
}
}
}
}
//NCNBCHB
string inputTest = @"NNCB
CH -> B
HH -> N
CB -> H
NH -> C
HB -> C
HC -> B
HN -> C
NN -> C
BH -> H
NC -> B
NB -> B
BN -> B
BB -> N
BC -> B
CC -> N
CN -> C";
string input = @"OOBFPNOPBHKCCVHOBCSO
NS -> H
NN -> P
FF -> O
HF -> C
KN -> V
PO -> B
PS -> B
FB -> N
ON -> F
OK -> F
OO -> K
KS -> F
FN -> F
KC -> H
NC -> N
NB -> C
KH -> S
SV -> B
BC -> S
KB -> B
SC -> S
KP -> H
FS -> K
NK -> K
OC -> H
NH -> C
PH -> F
OS -> V
BB -> C
CC -> F
CF -> H
CP -> V
VB -> N
VC -> F
PK -> V
NV -> N
FO -> S
CK -> O
BH -> K
PN -> B
PP -> S
NF -> B
SF -> K
VF -> H
HS -> F
NP -> F
SH -> V
SK -> K
PC -> V
BO -> H
HN -> P
BK -> O
BP -> S
OP -> N
SP -> N
KK -> C
HB -> H
OF -> C
VH -> C
HO -> N
FK -> V
NO -> H
KF -> S
KO -> V
PF -> K
HV -> C
SO -> F
SS -> F
VN -> K
HH -> B
OB -> S
CH -> B
FH -> B
CO -> V
HK -> F
VK -> K
CN -> V
SB -> K
PV -> O
PB -> F
VV -> P
CS -> N
CB -> C
BS -> F
HC -> B
SN -> P
VP -> P
OV -> P
BV -> P
FC -> N
KV -> S
CV -> F
BN -> S
BF -> C
OH -> F
VO -> B
FP -> S
FV -> V
VS -> N
HP -> B";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment