-
Notifications
You must be signed in to change notification settings - Fork 0
/
11.swift
70 lines (65 loc) · 2.22 KB
/
11.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
import Foundation
class Monkey {
var items: [UInt64] = [], destinations: [Int] = []
var op: [String] = []
var inspected: UInt64 = 0, div: UInt64
var relief: Bool = true
init(_ items: [UInt64], _ dest: [Int], _ op: [String], _ div: UInt64, _ relief: Bool) {
self.items = items
self.destinations = dest
self.op = op
self.div = div
self.relief = relief
}
func inspect(_ ms: inout [Monkey]) {
for item in items {
let arg: UInt64 = op[7] == "old" ? item : UInt64(op[7])!
var level: UInt64 = op[6] == "+" ? item + arg : item * arg
if relief { level /= 3 }
level %= lcm
let to = level % self.div == 0 ? destinations[0] : destinations[1]
ms[to].items.append(level)
}
inspected += UInt64(items.count)
items = []
}
}
var lcm: UInt64
var monkeys: [Monkey] = [], monkeys2: [Monkey] = []
while let line = readLine() {
if line.hasPrefix("Monkey ") {
let lines = [readLine(), readLine(), readLine(), readLine(), readLine()]
let items = lines[0]!.numbers
let dest = [Int(lines[3]!.numbers[0]), Int(lines[4]!.numbers[0])]
let op = lines[1]!.components(separatedBy: [" "])
let div = lines[2]!.numbers[0]
monkeys.append(Monkey(items, dest, op, div, true))
monkeys2.append(Monkey(items, dest, op, div, false))
}
}
lcm = monkeys.map {$0.div}.reduce(1, *)
for _ in 0..<20 {
for m in monkeys { m.inspect(&monkeys) }
}
for _ in 0..<10000 {
for m in monkeys2 { m.inspect(&monkeys2) }
}
monkeys.sort { $1.inspected < $0.inspected }
monkeys2.sort { $1.inspected < $0.inspected }
print(monkeys[0].inspected * monkeys[1].inspected, monkeys2[0].inspected * monkeys2[1].inspected)
extension String {
var numbers: [UInt64] {
guard let regex = try? NSRegularExpression(
pattern: "\\d+",
options: [.caseInsensitive]) else {
return []
}
let matches = regex.matches(in: self, options: [],
range: NSMakeRange(0, self.count))
return matches.map { match in
String(self[Range(match.range, in: self)!])
}.map {
UInt64($0)!
}
}
}