-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay22.swift
138 lines (104 loc) · 3.06 KB
/
Day22.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import BigNumber
import Collections
import Foundation
import Tools
final class Day22Solver: DaySolver {
let dayNumber: Int = 22
struct Input {
let steps: [Step]
}
enum Step {
case reverse
case cut(n: Int)
case increment(n: Int)
}
private func reverse(deck: [Int]) -> [Int] {
deck.reversed()
}
private func cut(deck: [Int], n: Int) -> [Int] {
if n >= 0 {
Array(deck[n ..< deck.count] + deck[0 ..< n])
} else {
Array(deck[deck.count + n ..< deck.count] + deck[0 ..< deck.count + n])
}
}
private func increment(deck: [Int], n: Int) -> [Int] {
var dequeDeck = Deque(deck)
var newDeck: [Int] = Array(repeating: 0, count: deck.count)
var index = 0
while dequeDeck.isNotEmpty {
newDeck[index] = dequeDeck.removeFirst()
index = (index + n) % deck.count
}
return newDeck
}
func solvePart1(withInput input: Input) -> Int {
var deck: [Int] = []
for i in 0 ... 10006 {
deck.append(i)
}
for step in input.steps {
switch step {
case .reverse:
deck = reverse(deck: deck)
case .cut(let n):
deck = cut(deck: deck, n: n)
case .increment(let n):
deck = increment(deck: deck, n: n)
}
// print("\(step): \(deck)")
}
return deck.firstIndex(of: 2019)!
}
func solvePart2(withInput input: Input) -> String {
// source: https://www.reddit.com/r/adventofcode/comments/ee0rqi/2019_day_22_solutions/fbnifwk/
func modPower(_ x: BInt, _ y: BInt, _ m: BInt) -> BInt {
if y == 0 {
return 1
}
var p = modPower(x, y / 2, m) % m
p = (p * p) % m
return (y % 2 == 0) ? p : (x * p) % m
}
func modInverse(_ a: BInt, _ m: BInt) -> BInt {
modPower(a, m - 2, m)
}
let deckSize = BInt(119315717514047)
let loopSize = BInt(101741582076661)
let X = BInt(2020)
func apply(steps: [Step], index originalIndex: BInt) -> BInt {
var index = originalIndex
for step in input.steps.reversed() {
switch step {
case .reverse:
index = deckSize - index - 1
case .cut(let n):
index = (index + deckSize + BInt(n)) % deckSize
case .increment(let n):
index = modInverse(BInt(n), deckSize) * index % deckSize
}
}
return index
}
let Y = apply(steps: input.steps, index: 2020)
let Z = apply(steps: input.steps, index: Y)
let A: BInt = (Y - Z) * modInverse(X - Y, deckSize) % deckSize
let B: BInt = (Y - A * X) % deckSize
return String((modPower(A, loopSize, deckSize) * X + (modPower(A, loopSize, deckSize) - 1) * modInverse(A - 1, deckSize) * B) % deckSize)
}
func parseInput(rawString: String) -> Input {
var invalidNumberCharacterSet = NSCharacterSet.decimalDigits
invalidNumberCharacterSet.insert("-")
invalidNumberCharacterSet = invalidNumberCharacterSet.inverted
let steps: [Step] = rawString.allLines().map { line in
if line.starts(with: "deal with increment") {
Step.increment(n: Int(line.trimmingCharacters(in: invalidNumberCharacterSet))!)
} else if line.starts(with: "cut") {
Step.cut(n: Int(line.trimmingCharacters(in: invalidNumberCharacterSet))!)
} else {
Step.reverse
}
}
return .init(steps: steps)
}
}