-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday03.jl
50 lines (45 loc) · 1.29 KB
/
day03.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
48
49
50
module Day03
using AdventOfCode2021
function day03(input::String = readInput(joinpath(@__DIR__, "..", "data", "day03.txt")))
numbers = parse.(Int, split(rstrip(input)), base = 2)
gammarate = BitVector()
i = 0
while true
shifted = mod.(numbers .>> i, 2)
all(i -> i == 0, shifted) && break
if sum(shifted) > length(shifted) / 2
push!(gammarate, 1)
else
push!(gammarate, 0)
end
i += 1
end
g, e, v = 0, 0, 1
for gr in gammarate
g += gr * v
e += (1 - gr) * v
v *= 2
end
p1 = g * e
oxyco2 = [0, 0]
numbersc = [copy(numbers), copy(numbers)]
cvals = [[1, 0], [0, 1]]
for j ∈ 1:2
for k ∈ i-1:-1:0
shifted = mod.(numbersc[j] .>> k, 2)
nones = count(x -> x == 1, shifted)
nzeros = count(x -> x == 0, shifted)
if nones >= nzeros
numbersc[j] = numbersc[j][findall(i -> i == cvals[j][1], shifted)]
else
numbersc[j] = numbersc[j][findall(i -> i == cvals[j][2], shifted)]
end
if length(numbersc[j]) == 1
oxyco2[j] = numbersc[j][1]
break
end
end
end
return [p1, prod(oxyco2)]
end
end # module