-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution_2020_16.swift
121 lines (104 loc) · 3.57 KB
/
Solution_2020_16.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// Solution_2020_16.swift
//
//
// Created by Ivan Chalov on 16.12.20.
//
import Foundation
import Algorithms
import Prelude
private struct Rules {
private var rules = [String: [Bool]]()
private var allIntervals: [Bool] = Array(repeating: false, count: 1000)
var fields: Set<String> {
Set(rules.keys)
}
init(input: String) {
for line in input.split(whereSeparator: \.isNewline) {
let ruleParts = line.split(separator: ":")
ruleParts[1].dropFirst()
.components(separatedBy: " or ")
.map { range in
range
.split(separator: "-")
.compactMap(String.init >>> Int.init)
|> { $0[0]...$0[1] }
}
.forEach { range in
range.forEach {
allIntervals[$0] = true
rules[
String(ruleParts[0]),
default: Array(repeating: false, count: 1000)
][$0] = true
}
}
}
}
func isValid(number: Int) -> Bool {
allIntervals[number]
}
func isValid(ticket: Ticket) -> Bool {
ticket.allSatisfy(isValid)
}
func validRules(for number: Int) -> Set<String> {
rules.filter { $0.value[number] }.map(\.key) |> Set.init
}
}
private typealias Ticket = [Int]
private extension Ticket {
init(string: String) {
self = string
.split(separator: ",")
.compactMap(String.init >>> Int.init)
}
}
struct Solution_2020_16: Solution {
var input: Input
func run() throws {
let input = try self.input.get()
.components(separatedBy: "\n\n")
let rules = Rules(input: input[0])
let myTicket = input[1]
.split(whereSeparator: \.isNewline)[1]
|> String.init >>> Ticket.init(string:)
let tickets = input[2]
.split(whereSeparator: \.isNewline)
.map(String.init)
.dropFirst()
.map(Ticket.init(string:))
// ------- Part 1 -------
let part1: Int = tickets
.flatMap { ticket in ticket.filter { !rules.isValid(number: $0) } }
.sum()
print(part1)
// ------- Part 2 -------
let validTickets = tickets.filter { rules.isValid(ticket: $0) }
let possibleFields = validTickets.map { $0.map { rules.validRules(for: $0) } }
var intersections = possibleFields[0].indices.map {
possibleFields[column: $0].scan(uncurry(Set.intersection))
}
var remainingFields = rules.fields
var resultPositions = Array(repeating: "", count: intersections.count)
while !remainingFields.isEmpty {
intersections.indexed().filter { $0.element.count == 1 }.forEach {
let field = $0.element.first!
resultPositions[$0.index] = field
intersections.indices.forEach { intersections[$0].remove(field) }
remainingFields.remove(field)
}
}
let departureFieldPositions = resultPositions
.indexed()
.filter { $0.element.hasPrefix("departure") }
.map(\.index)
let part2 = myTicket.indexed()
.filter { departureFieldPositions.contains($0.index) }
.map(\.element)
.product()
print(part2)
// ------- Test -------
assert(part1 == 20060, "WA")
assert(part2 == 2843534243843, "WA")
}
}