-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15.jl
47 lines (41 loc) · 1.11 KB
/
day15.jl
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
module Day15
using AdventOfCode2017
function day15(input::String = readInput(joinpath(@__DIR__, "..", "data", "day15.txt")))
start = parse.(Int, map(x -> x[end], split.(split(rstrip(input), "\n"))))
return [part1!(copy(start)), part2!(start)]
end
function part1!(numbers::Vector{Int})
s = 0
for i = 1:40_000_000
numbers[1] = mod(numbers[1] * 16807, 2147483647)
numbers[2] = mod(numbers[2] * 48271, 2147483647)
if match16(numbers)
s += 1
end
end
return s
end
function part2!(numbers::Vector{Int})
s = 0
for i = 1:5_000_000
while true
numbers[1] = mod(numbers[1] * 16807, 2147483647)
mod(numbers[1], 4) == 0 && break
end
while true
numbers[2] = mod(numbers[2] * 48271, 2147483647)
mod(numbers[2], 8) == 0 && break
end
if match16(numbers)
s += 1
end
end
return s
end
function match16(numbers::Vector{Int})
for i = 0:15
mod(numbers[1] >> i, 2) != mod(numbers[2] >> i, 2) && return false
end
return true
end
end # module