d23p1
This commit is contained in:
43
day23/d23p1.swift
Normal file
43
day23/d23p1.swift
Normal file
@@ -0,0 +1,43 @@
|
||||
import Foundation
|
||||
|
||||
typealias Network = [String: Set<String>]
|
||||
|
||||
func readInput(_ filePath: String) throws -> Network {
|
||||
var map: Network = [:]
|
||||
try String(contentsOfFile: filePath, encoding: .ascii)
|
||||
.split(separator: "\n").map { $0.split(separator: "-") }
|
||||
.map { (String($0[0]), String($0[1])) }.forEach { m1, m2 in
|
||||
map[m1, default: []].insert(m2)
|
||||
map[m2, default: []].insert(m1)
|
||||
}
|
||||
return map
|
||||
}
|
||||
|
||||
struct Triple : Hashable, CustomStringConvertible {
|
||||
let (m1, m2, m3): (String, String, String)
|
||||
var description: String { return "\(m1)-\(m2)-\(m3)" }
|
||||
var hasT: Bool {
|
||||
return m1.first == "t" || m2.first == "t" || m3.first == "t"
|
||||
}
|
||||
init (_ m1: String, _ m2: String, _ m3: String) {
|
||||
let ms = [m1, m2, m3].sorted()
|
||||
(self.m1, self.m2, self.m3) = (ms[0], ms[1], ms[2])
|
||||
}
|
||||
}
|
||||
|
||||
func checkTriples(_ network: Network) -> Set<Triple> {
|
||||
var triples: Set<Triple> = []
|
||||
for (m1, m1conns) in network {
|
||||
for m2 in m1conns {
|
||||
for m3 in network[m2]! {
|
||||
if network[m3]!.contains(m1) {
|
||||
triples.insert(Triple(m1, m2, m3))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return triples
|
||||
}
|
||||
|
||||
let conns = try readInput(CommandLine.arguments[1])
|
||||
print(checkTriples(conns).filter { $0.hasT }.count)
|
3380
day23/input.txt
Normal file
3380
day23/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
32
day23/test.txt
Normal file
32
day23/test.txt
Normal file
@@ -0,0 +1,32 @@
|
||||
kh-tc
|
||||
qp-kh
|
||||
de-cg
|
||||
ka-co
|
||||
yn-aq
|
||||
qp-ub
|
||||
cg-tb
|
||||
vc-aq
|
||||
tb-ka
|
||||
wh-tc
|
||||
yn-cg
|
||||
kh-ub
|
||||
ta-co
|
||||
de-co
|
||||
tc-td
|
||||
tb-wq
|
||||
wh-td
|
||||
ta-ka
|
||||
td-qp
|
||||
aq-cg
|
||||
wq-ub
|
||||
ub-vc
|
||||
de-ta
|
||||
wq-aq
|
||||
wq-vc
|
||||
wh-yn
|
||||
ka-de
|
||||
kh-ta
|
||||
co-tc
|
||||
wh-qp
|
||||
tb-vc
|
||||
td-yn
|
Reference in New Issue
Block a user