From 40053ef992f3bcb7bdb8faaf11e57151f4ac206a Mon Sep 17 00:00:00 2001 From: Max Nuding Date: Tue, 14 Dec 2021 20:13:34 +0100 Subject: [PATCH] Switched from string fiddling to actual character pairs --- Sources/14/14.swift | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/Sources/14/14.swift b/Sources/14/14.swift index 9b6a502..6009355 100644 --- a/Sources/14/14.swift +++ b/Sources/14/14.swift @@ -8,17 +8,19 @@ import Foundation import Runner -struct Replacement { - let from: String - let to: String +struct Pair: Hashable { + let first: String.Element + let second: String.Element - init(line: String) { - let r = line.components(separatedBy: " -> ") - from = r.first! - to = r.last! + init(_ str: String) { + first = str.first! + second = str.last! } - func replace(pair: String) -> String.Element? { pair == from ? to.first : nil } + init(first: String.Element, second: String.Element) { + self.first = first + self.second = second + } } class Day14: Runnable { @@ -37,18 +39,17 @@ class Day14: Runnable { let replacements = parts.last! .components(separatedBy: .newlines) .map {$0.components(separatedBy: " -> ")} - .reduce(into: [String:String](), { p, c in - p[c.first!] = c.last! + .reduce(into: [Pair:String.Element](), { + $0[Pair($1.first!)] = $1.last!.first! }) - var pairCounts = [String:Int]() + var pairCounts = [Pair:Int]() for i in startingPolymer.indices { let next = startingPolymer.index(i, offsetBy: 1) guard next != startingPolymer.endIndex else { continue } - let pair = "\(startingPolymer[i])\(startingPolymer[next])" - pairCounts[pair, default: 0] += 1 + pairCounts[Pair(first: startingPolymer[i], second: startingPolymer[next]), default: 0] += 1 } var charCount = startingPolymer.reduce(into: [String.Element:Int](), { $0[$1, default: 0] += 1 @@ -56,15 +57,14 @@ class Day14: Runnable { for step in 1...40 { let tmpPairCounts = pairCounts + for pairCount in tmpPairCounts { let pair = pairCount.key let replacement = replacements[pair]! pairCounts[pair]! -= pairCount.value - let firstCharacter = pair.first! - let secondCharacter = pair.last! - pairCounts["\(firstCharacter)\(replacement)", default: 0] += pairCount.value - pairCounts["\(replacement)\(secondCharacter)", default: 0] += pairCount.value - charCount[replacement.first!, default: 0] += pairCount.value + pairCounts[Pair(first: pair.first, second: replacement), default: 0] += pairCount.value + pairCounts[Pair(first: replacement, second: pair.second), default: 0] += pairCount.value + charCount[replacement, default: 0] += pairCount.value } if step == 10 { printResult(charCount: charCount) @@ -74,8 +74,10 @@ class Day14: Runnable { } func printResult(charCount: [String.Element:Int]) { - let maxC = charCount.max(by: {$0.value < $1.value })! - let minC = charCount.min(by: {$0.value < $1.value })! + // Sort instead of min/max so we need to only go through the dict once + let sorted = charCount.sorted(by: {$0.value > $1.value }) + let maxC = sorted.first! + let minC = sorted.last! print(maxC.value - minC.value) } }