-
Notifications
You must be signed in to change notification settings - Fork 0
/
day03.jl
36 lines (31 loc) · 1007 Bytes
/
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
diagnostics = open("diagnostics.txt") do f
hcat(map(row -> parse.(Int, split(row, "")), readlines(f))...)
end
gamma_rate = parse(Int, join(round.(Int, sum(diagnostics, dims = 2)/size(diagnostics, 2))), base = 2)
epsilon_rate = ~gamma_rate & ((1 << size(diagnostics, 1)) - 1)
println(gamma_rate*epsilon_rate)
oxygen = copy(diagnostics)
for i in 1:size(oxygen, 2)
if sum(oxygen[i,:])/size(oxygen, 2) >= 0.5
global oxygen = oxygen[:,oxygen[i, :] .== 1]
else
global oxygen = oxygen[:,oxygen[i, :] .== 0]
end
if size(oxygen, 2) == 1
break
end
end
oxygen_generator_rating = parse(Int, join(oxygen), base = 2)
co2 = copy(diagnostics)
for i in 1:size(co2, 2)
if sum(co2[i,:])/size(co2, 2) >= 0.5
global co2 = co2[:,co2[i, :] .== 0]
else
global co2 = co2[:,co2[i, :] .== 1]
end
if size(co2, 2) == 1
break
end
end
co2_scrubber_rating = parse(Int, join(co2), base = 2)
println(oxygen_generator_rating*co2_scrubber_rating)