-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.jl
39 lines (35 loc) · 1.33 KB
/
day3.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
using DelimitedFiles
diagnostics = readdlm("day3.txt", String)
gamma = epsilon = ""
for i in 1:length(diagnostics[1])
count_1s = count(x->x[i] == '1', diagnostics)
count_0s = length(diagnostics) - count_1s
global gamma = gamma * (count_1s >= count_0s ? '1' : '0')
global epsilon = epsilon * (count_1s < count_0s ? '1' : '0')
end
@show parse(Int, gamma, base=2), parse(Int, epsilon, base=2)
@show parse(Int, gamma, base=2) * parse(Int, epsilon, base=2)
while length(diagnostics) > 1
for i in 1:length(diagnostics[1])
count_1s = count(x->x[i] == '1', diagnostics)
count_0s = length(diagnostics) - count_1s
keep = count_1s >= count_0s ? '1' : '0'
global diagnostics = filter(x->(x[i] == keep), diagnostics)
end
break
end
@assert length(diagnostics) == 1
@show oxygen = parse(Int, diagnostics[1], base=2)
diagnostics = readdlm("day3.txt", String)
while length(diagnostics) > 1
for i in 1:length(diagnostics[1])
count_1s = count(x->x[i] == '1', diagnostics)
count_0s = length(diagnostics) - count_1s
keep = count_1s < count_0s ? '1' : '0'
global diagnostics = filter(x->(x[i] == keep), diagnostics)
if length(diagnostics) == 1 break end
end
end
@assert length(diagnostics) == 1
@show scrubber = parse(Int, diagnostics[1], base=2)
@show oxygen * scrubber