14a (bad version)

This commit is contained in:
Max Nuding 2021-12-14 17:24:15 +01:00
parent 365e67056b
commit ae1b714614
Signed by: phlaym
GPG Key ID: A06651BAB6777237
5 changed files with 212 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) ]
)
]
)

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

@ -0,0 +1,74 @@
//
// File.swift
//
//
// Created by Max Nuding on 14.12.21.
//
import Foundation
import Runner
struct Replacement {
let from: String
let to: String
init(line: String) {
let r = line.components(separatedBy: " -> ")
from = r.first!
to = r.last!
}
func replace(pair: String) -> String.Element? { pair == from ? to.first : nil }
}
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")
var startingPolymer = parts.first!
let replacements = parts.last!
.components(separatedBy: .newlines)
.map(Replacement.init)
for step in 1...10 {
var inserts = [(String.Index, Character)]()
for i in startingPolymer.indices {
let next = startingPolymer.index(i, offsetBy: 1)
guard next != startingPolymer.endIndex else {
continue
}
var pair = startingPolymer[i]/* + startingPolymer[next]*/
let p = "\(startingPolymer[i])\(startingPolymer[next])"
for replacement in replacements {
if let c = replacement.replace(pair: p) {
inserts.append((next, c))
break
}
}
}
var tmp = startingPolymer
var count = 0
for r in inserts {
let idx = tmp.index(r.0, offsetBy: count)
tmp.insert(r.1, at: idx)
count += 1
}
startingPolymer = tmp
}
let charCounts = startingPolymer.reduce(into: [String.Element: Int](), {
$0[$1, default: 0] += 1
})
let maxC = charCounts.max(by: {$0.value < $1.value })!
let minC = charCounts.min(by: {$0.value < $1.value })!
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