-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdventOfCode2017-6.swift
executable file
·48 lines (39 loc) · 1.06 KB
/
AdventOfCode2017-6.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
#!/usr/bin/swift
import Foundation
let example = [0, 2, 7, 0]
let input = [2, 8, 8, 5, 4, 2, 3, 1, 5, 5, 1, 2, 15, 13, 5, 14]
var records = Set<String>()
extension Array where Element == Int {
func stringify() -> String {
return "\(self)"
}
}
func isUnique(_ input: [Int]) -> Bool {
let numRecords = records.count
records = records.union([input.stringify()])
return records.count == numRecords + 1
}
func process(_ input: [Int]) {
records = Set<String>()
var step = 0
var banks = input
var numBanks = banks.count
while (isUnique(banks)) {
step += 1
print(banks.stringify())
var toDistribute = banks.max()!
let target = banks.enumerated().first { (_, x) in x == toDistribute }!
var offset = target.offset
banks[offset] = 0
while toDistribute > 0 {
offset += 1
if offset == numBanks { offset = 0 }
banks[offset] += 1
toDistribute -= 1
}
}
print(banks.stringify())
print(step)
}
process(example)
process(input)