-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay15.swift
70 lines (52 loc) · 1.18 KB
/
Day15.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
import Tools
final class Day15Solver: DaySolver {
let dayNumber: Int = 15
struct Input {
let a: Int
let b: Int
}
func solvePart1(withInput input: Input) -> Int {
var a = input.a
var b = input.b
var counter = 0
for _ in 0 ..< 40_000_000 {
a = (a * 16807) % 2147483647
b = (b * 48271) % 2147483647
if (a & 0xFFFF) == (b & 0xFFFF) {
counter += 1
}
}
return counter
}
func solvePart2(withInput input: Input) -> Int {
var a = input.a
var b = input.b
var counter = 0
var aStack: [UInt16] = []
var bStack: [UInt16] = []
aStack.reserveCapacity(10_000_000)
bStack.reserveCapacity(5_000_000)
var comparePointer = 0
while comparePointer < 5_000_000 {
a = (a * 16807) % 2147483647
b = (b * 48271) % 2147483647
if a % 4 == 0 {
aStack.append(UInt16(a & 0xFFFF))
}
if b % 8 == 0 {
bStack.append(UInt16(b & 0xFFFF))
}
if comparePointer < aStack.count, comparePointer < bStack.count {
if aStack[comparePointer] == bStack[comparePointer] {
counter += 1
}
comparePointer += 1
}
}
return counter
}
func parseInput(rawString: String) -> Input {
return .init(a: 512, b: 191)
}
}