-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution_2018_01.swift
53 lines (39 loc) · 1.32 KB
/
Solution_2018_01.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
import Foundation
struct Solution_2018_01: Solution {
var input: Input
func run() throws {
let frequencies = try input.get()
.components(separatedBy: .whitespacesAndNewlines)
.compactMap({ Int($0) })
// ------- Part 1 -------
let part1 = frequencies.reduce(0, +)
print(part1)
// ------- Part 2 -------
var current = 0
var index = 0
var visited = Set<Int>()
while visited.insert(current).inserted {
current += frequencies[index]
index = (index + 1) % frequencies.count
}
let part2 = current
print(part2)
/*
Originally my solution was to construct an infinite lazy sequence : D
let repeatedFrequencies = sequence(first: (0, frequencies[0]), next: { (idx, _) in
let next = (idx + 1) % frequencies.count
return (next, frequencies[next])
}).lazy.map({$0.1})
var current = 0
var visited = Set<Int>([current])
_ = repeatedFrequencies.first(where: { change in
current += change
return !visited.insert(current).inserted
})
let part2 = current
*/
// ------- Test -------
assert(part1 == 425, "WA")
assert(part2 == 57538, "WA")
}
}