-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
executable file
·44 lines (35 loc) · 1.71 KB
/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import Foundation
func main() throws {
let isTestMode = CommandLine.arguments.contains("test")
let input: [String] = try readInput(fromTestFile: isTestMode)
let insertionRules: [String: Character] = input[2..<input.count].reduce(into: [:], {
let split = $1.components(separatedBy: " -> ")
$0[split[0]] = split[1][0]
})
let pairs = (0..<input[0].count-1).map({ input[0][$0] + input[0][$0+1] })
print("Part 1:", getExpansionResult(for: pairs, ticks: 10, insertionRules: insertionRules))
print("Part 2:", getExpansionResult(for: pairs, ticks: 40, insertionRules: insertionRules))
}
private func getExpansionResult(for inputPairs: [String], ticks: Int, insertionRules: [String: Character]) -> Int {
var pairCounts: [String: Int] = inputPairs.counts
for _ in 0..<ticks {
var newCounts: [String: Int] = [:]
for (pair, currentPairCount) in pairCounts {
guard let insertion = insertionRules[pair] else {
newCounts[pair] = newCounts[pair, default: 0] + 1
continue
}
let firstNewPair = pair[0] + String(insertion)
let secondNewPair = String(insertion) + pair[1]
newCounts[firstNewPair] = newCounts[firstNewPair, default: 0] + currentPairCount
newCounts[secondNewPair] = newCounts[secondNewPair, default: 0] + currentPairCount
}
pairCounts = newCounts
}
var finalCounts: [Character: Int] = pairCounts.reduce(into: [:], { $0[$1.0[1]] = $0[$1.0[1], default: 0] + $1.1 })
finalCounts[inputPairs[0][0]]! += 1
let mostCommon = finalCounts.values.max()!
let leastCommon = finalCounts.values.min()!
return mostCommon - leastCommon
}
Timer.time(main)