Much better solution
This commit is contained in:
parent
653b8d55dd
commit
3926ea400b
|
@ -6,31 +6,29 @@ proc parse(input: Lines): (string, Table[(char, char), char]) =
|
||||||
var c = input[i].split(" -> ")
|
var c = input[i].split(" -> ")
|
||||||
result[1][(c[0][0], c[0][1])] = c[1][0]
|
result[1][(c[0][0], c[0][1])] = c[1][0]
|
||||||
|
|
||||||
proc stepPolymer(polymer: string, combis: Table[(char, char), char]): string =
|
proc steps(input: Lines, steps: uint): uint64 =
|
||||||
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): uint64 =
|
|
||||||
var (polymer, combis) = input.parse
|
var (polymer, combis) = input.parse
|
||||||
for i in 1..10:
|
var pCounter = initTable[(char, char), uint64]()
|
||||||
polymer = polymer.stepPolymer combis
|
var cCounter = initTable[char, uint64]()
|
||||||
var pps = polymer.toCountTable.values.toSeq
|
for k in combis.keys:
|
||||||
uint64(max(pps) - min(pps))
|
pCounter[k] = 0
|
||||||
|
cCounter[k[0]] = 0
|
||||||
proc p2(input: Lines): uint64 =
|
cCounter[k[1]] = 0
|
||||||
var (polymer, combis) = input.parse
|
inc cCounter[polymer[0]]
|
||||||
for i in 1..40:
|
for i in 1..<polymer.len:
|
||||||
echo &"Step {i}"
|
inc pCounter[(polymer[i-1], polymer[i])]
|
||||||
polymer = polymer.stepPolymer combis
|
inc cCounter[polymer[i]]
|
||||||
var t = initTable[char, uint64]()
|
for i in 1..steps:
|
||||||
for c in polymer:
|
var newPCounter = pCounter
|
||||||
inc t[c]
|
for t, n in pCounter.pairs:
|
||||||
var pps = t.values.toSeq
|
let (l, r) = t
|
||||||
|
var twixt = combis[(l, r)]
|
||||||
|
newPCounter[(l, r)] -= n
|
||||||
|
newPCounter[(l, twixt)] += n
|
||||||
|
newPCounter[(twixt, r)] += n
|
||||||
|
cCounter[twixt] += n
|
||||||
|
pCounter = newPCounter
|
||||||
|
var pps = cCounter.values.toSeq
|
||||||
max(pps) - min(pps)
|
max(pps) - min(pps)
|
||||||
|
|
||||||
const input = """
|
const input = """
|
||||||
|
@ -53,4 +51,4 @@ BC -> B
|
||||||
CC -> N
|
CC -> N
|
||||||
CN -> C
|
CN -> C
|
||||||
""".strip().split('\n').mapIt(it.strip)
|
""".strip().split('\n').mapIt(it.strip)
|
||||||
doDayX 14, n => n.loadInput, p1, p2, (input, 1588'u64, 2188189693529'u64)
|
doDayX 14, n => n.loadInput, (i) => steps(i, 10), (i) => steps(i, 40), (input, 1588'u64, 2188189693529'u64)
|
||||||
|
|
Loading…
Reference in a new issue