Skip to content

2015 - Day 19 - Medicine for Rudolph #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 2015/Sources/AdventOfCode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ let allChallenges: [any AdventDay] = [
Day16(),
Day17(),
Day18(),
Day19(),
]

@main
Expand Down
33 changes: 33 additions & 0 deletions 2015/Sources/Day19.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Foundation

struct Day19: AdventDay {
let data: String

private var replacements: [String: [String]] {
data.split(separator: /\n\n/)[0]
.matches(of: /^(?'source'[A-Za-z]{1}[a-z]{,1}) => (?'replacement'[A-Za-z]+)$/.anchorsMatchLineEndings())
.map {
(String($0.output.source), String($0.output.replacement))
}
.reduce(into: [String:[String]]()) { result, mapping in
result[mapping.0, default: []].append(mapping.1)
}
}
private var molecole: String {
String(data.split(separator: /\n\n/)[1])
}

func part1() -> Int {
let all = replacements.flatMap { key, values in
values.flatMap { value in
molecole.matches(of: try! Regex(key)).map { match in
var molecole = molecole
molecole.replaceSubrange(match.range, with: value)
return molecole
}
}
}.uniqued()

return Set(all).count
}
}
17 changes: 17 additions & 0 deletions 2015/Tests/Day19Tests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Testing

@testable import AoC2015

struct Day19Tests {
private let testData = """
H => HO
H => OH
O => HH

HOH
""".replacing(/( )+/.anchorsMatchLineEndings(), with: "")

@Test func testPart1() {
#expect(Day19(data: testData).part1() == 4)
}
}