From 578058890352101ac1b0bc1192e048b0158a6e92 Mon Sep 17 00:00:00 2001 From: Max Nuding Date: Tue, 14 Dec 2021 20:31:09 +0100 Subject: [PATCH] cosmetic fixes --- Sources/14/14.swift | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Sources/14/14.swift b/Sources/14/14.swift index 6009355..61b22ab 100644 --- a/Sources/14/14.swift +++ b/Sources/14/14.swift @@ -44,21 +44,20 @@ class Day14: Runnable { }) var pairCounts = [Pair:Int]() + var charCount = [String.Element:Int](minimumCapacity: 26) for i in startingPolymer.indices { + let firstCharacter = startingPolymer[i] + charCount[firstCharacter, default: 0] += 1 let next = startingPolymer.index(i, offsetBy: 1) guard next != startingPolymer.endIndex else { continue } - pairCounts[Pair(first: startingPolymer[i], second: startingPolymer[next]), default: 0] += 1 + pairCounts[Pair(first: firstCharacter, second: startingPolymer[next]), default: 0] += 1 } - var charCount = startingPolymer.reduce(into: [String.Element:Int](), { - $0[$1, default: 0] += 1 - }) for step in 1...40 { let tmpPairCounts = pairCounts - - for pairCount in tmpPairCounts { + tmpPairCounts.forEach { pairCount in let pair = pairCount.key let replacement = replacements[pair]! pairCounts[pair]! -= pairCount.value