Compare commits

..

3 Commits

Author SHA1 Message Date
40053ef992
Switched from string fiddling to actual character pairs 2021-12-14 20:13:34 +01:00
dac98f99b5
14b 2021-12-14 19:50:38 +01:00
ae1b714614
14a (bad version) 2021-12-14 17:24:15 +01:00
5 changed files with 221 additions and 1 deletions

View File

@ -22,7 +22,8 @@ let package = Package(
.executable(name: "10", targets: ["10"]),
.executable(name: "11", targets: ["11"]),
.executable(name: "12", targets: ["12"]),
.executable(name: "13", targets: ["13"])
.executable(name: "13", targets: ["13"]),
.executable(name: "14", targets: ["14"])
],
dependencies: [
// Dependencies declare other packages that this package depends on.
@ -106,6 +107,10 @@ let package = Package(
.executableTarget(
name: "13",
dependencies: [ .targetItem(name: "Runner", condition: nil) ]
),
.executableTarget(
name: "14",
dependencies: [ .targetItem(name: "Runner", condition: nil) ]
)
]
)

83
Sources/14/14.swift Normal file
View File

@ -0,0 +1,83 @@
//
// File.swift
//
//
// Created by Max Nuding on 14.12.21.
//
import Foundation
import Runner
struct Pair: Hashable {
let first: String.Element
let second: String.Element
init(_ str: String) {
first = str.first!
second = str.last!
}
init(first: String.Element, second: String.Element) {
self.first = first
self.second = second
}
}
class Day14: Runnable {
let inputPath: String
required init(inputPath: String) {
self.inputPath = inputPath
}
public func run() {
let input = try! String(contentsOfFile: inputPath)
let parts = input
.trimmingCharacters(in: .newlines)
.components(separatedBy: "\n\n")
let startingPolymer = parts.first!
let replacements = parts.last!
.components(separatedBy: .newlines)
.map {$0.components(separatedBy: " -> ")}
.reduce(into: [Pair:String.Element](), {
$0[Pair($1.first!)] = $1.last!.first!
})
var pairCounts = [Pair:Int]()
for i in startingPolymer.indices {
let next = startingPolymer.index(i, offsetBy: 1)
guard next != startingPolymer.endIndex else {
continue
}
pairCounts[Pair(first: startingPolymer[i], 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 {
let pair = pairCount.key
let replacement = replacements[pair]!
pairCounts[pair]! -= 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)
}
}
printResult(charCount: charCount)
}
func printResult(charCount: [String.Element:Int]) {
// 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)
}
}

12
Sources/14/main.swift Normal file
View File

@ -0,0 +1,12 @@
//
// File.swift
//
//
// Created by Max Nuding on 14.12.21.
//
import Foundation
import Runner
//Runner(target: Day14.self, day: "14", isTest: true).run()
Runner(target: Day14.self, day: "14", isTest: false).run()

View File

@ -0,0 +1,102 @@
FNFPPNKPPHSOKFFHOFOC
VS -> B
SV -> C
PP -> N
NS -> N
BC -> N
PB -> F
BK -> P
NV -> V
KF -> C
KS -> C
PV -> N
NF -> S
PK -> F
SC -> F
KN -> K
PN -> K
OH -> F
PS -> P
FN -> O
OP -> B
FO -> C
HS -> F
VO -> C
OS -> B
PF -> V
SB -> V
KO -> O
SK -> N
KB -> F
KH -> C
CC -> B
CS -> C
OF -> C
FS -> B
FP -> H
VN -> O
NB -> N
BS -> H
PC -> H
OO -> F
BF -> O
HC -> P
BH -> S
NP -> P
FB -> C
CB -> H
BO -> C
NN -> V
SF -> N
FC -> F
KK -> C
CN -> N
BV -> F
FK -> C
CF -> F
VV -> B
VF -> S
CK -> C
OV -> P
NC -> N
SS -> F
NK -> V
HN -> O
ON -> P
FH -> O
OB -> H
SH -> H
NH -> V
FF -> B
HP -> B
PO -> P
HB -> H
CH -> N
SN -> P
HK -> P
FV -> H
SO -> O
VH -> V
BP -> V
CV -> P
KP -> K
VB -> N
HV -> K
SP -> N
HO -> P
CP -> H
VC -> N
CO -> S
BN -> H
NO -> B
HF -> O
VP -> K
KV -> H
KC -> F
HH -> C
BB -> K
VK -> P
OK -> C
OC -> C
PH -> H

View File

@ -0,0 +1,18 @@
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