-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay11.swift
63 lines (47 loc) · 1.29 KB
/
Day11.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
import AOCCore
import Foundation
struct Day11: Day {
let title = "Plutonian Pebbles"
var rawInput: String?
func part1() throws -> Int {
var stones = makeStones()
(0..<25).forEach { _ in
stones.blink()
}
return stones.values.sum
}
func part2() throws -> Int {
var stones = makeStones()
(0..<75).forEach { _ in
stones.blink()
}
return stones.values.sum
}
private func makeStones() -> [Int: Int] {
input().integers.reduce(into: [:]) { result, item in
result[item, default: 0] += 1
}
}
}
private extension Int {
func blinked() -> [Int] {
if self == 0 { return [1] }
else if self.digits.count.isEven {
let separator = Int(pow(10, Double(self.digits.count / 2)))
let left = self / separator
let right = self - (left * separator)
return [left, right]
}
return [self * 2024]
}
}
private extension Dictionary where Key == Int, Value == Int {
mutating
func blink() {
self = self.keys.reduce(into: [:]) { result, item in
item
.blinked()
.forEach { result[$0, default: 0] += self[item, default: 0] }
}
}
}