From 942a6dd8979765e3fffcb7937342cedc5f2c583d Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Tue, 14 Dec 2021 15:40:41 -0600 Subject: [PATCH] Day 14 part 1 --- 2021/nim/day14.nim | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 2021/nim/day14.nim diff --git a/2021/nim/day14.nim b/2021/nim/day14.nim new file mode 100644 index 0000000..6570729 --- /dev/null +++ b/2021/nim/day14.nim @@ -0,0 +1,48 @@ +import ./common, std/[sequtils, algorithm, sugar, sets, strformat, strutils, tables] + +proc parse(input: Lines): (string, Table[(char, char), char]) = + result[0] = input[0] + for i in 2.. ") + result[1][(c[0][0], c[0][1])] = c[1][0] + +proc stepPolymer(polymer: string, combis: Table[(char, char), char]): string = + result.add polymer[0] + var i = 1 + while i < polymer.len: + var key = (polymer[i-1], polymer[i]) + if combis.contains key: result.add combis[key] + result.add polymer[i] + i += 1 + +proc p1(input: Lines): int = + var (polymer, combis) = input.parse + for i in 1..10: + polymer = polymer.stepPolymer combis + var pps = polymer.toCountTable.values.toSeq + max(pps) - min(pps) + +proc p2(input: Lines): int = + return 0 + +const input = """ +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 +""".strip().split('\n').mapIt(it.strip) +doDay 14, n => n.loadInput, p1, p2, (input, 1588, 0)