You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							69 lines
						
					
					
						
							1.5 KiB
						
					
					
				
			
		
		
	
	
							69 lines
						
					
					
						
							1.5 KiB
						
					
					
				using DelimitedFiles
 | 
						|
 | 
						|
infile = size(ARGS, 1) > 0 ? ARGS[1] : "day3input.txt"
 | 
						|
println("infile = ", infile)
 | 
						|
 | 
						|
diags = readdlm(infile, ' ', String)
 | 
						|
 | 
						|
n = size(diags, 1)
 | 
						|
w = length(diags[1])
 | 
						|
 | 
						|
half_n = n / 2
 | 
						|
 | 
						|
gamma = 0
 | 
						|
epsilon = 0
 | 
						|
 | 
						|
for i in 1:w
 | 
						|
    global gamma, epsilon
 | 
						|
    is_one_at_i(s) = s[i] == '1' ? true : false
 | 
						|
    one_count = size(findall(is_one_at_i, diags), 1)
 | 
						|
    v = 2^(w-i)
 | 
						|
    if one_count > half_n
 | 
						|
        gamma += v
 | 
						|
    else
 | 
						|
        epsilon += v
 | 
						|
    end
 | 
						|
end
 | 
						|
 | 
						|
println("gamma   = ", gamma)
 | 
						|
println("epsilon = ", epsilon)
 | 
						|
println("prod    = ", gamma * epsilon)
 | 
						|
 | 
						|
o2gen = view(diags, :)
 | 
						|
co2scrub = view(diags, :)
 | 
						|
 | 
						|
o2n = n
 | 
						|
co2n = n
 | 
						|
 | 
						|
for i in 1:w
 | 
						|
    global o2gen, co2scrub, o2n, co2n
 | 
						|
    is_one_at_i(s) = s[i] == '1' ? true : false
 | 
						|
    is_zero_at_i(s) = s[i] == '0' ? true : false
 | 
						|
    o2idx_ones = findall(is_one_at_i, o2gen)
 | 
						|
    co2idx_ones = findall(is_one_at_i, co2scrub)
 | 
						|
 | 
						|
    if length(o2gen) > 1
 | 
						|
        if length(o2idx_ones) >= o2n/2
 | 
						|
            o2gen = o2gen[o2idx_ones]
 | 
						|
        else
 | 
						|
            o2gen = o2gen[findall(is_zero_at_i, o2gen)]
 | 
						|
        end
 | 
						|
        o2n = length(o2gen)
 | 
						|
    end
 | 
						|
 | 
						|
    if length(co2scrub) > 1
 | 
						|
        if length(co2idx_ones) < co2n/2
 | 
						|
            co2scrub = co2scrub[co2idx_ones]
 | 
						|
        else
 | 
						|
            co2scrub = co2scrub[findall(is_zero_at_i, co2scrub)]
 | 
						|
        end
 | 
						|
        co2n = length(co2scrub)
 | 
						|
    end
 | 
						|
end
 | 
						|
 | 
						|
o2gen_v = parse(Int, o2gen[1], base=2)
 | 
						|
co2scrub_v = parse(Int, co2scrub[1], base=2)
 | 
						|
println("o2gen ", o2gen, " ", o2gen_v)
 | 
						|
println("co2scrub ", co2scrub, " ", co2scrub_v)
 | 
						|
println("prod ", o2gen_v * co2scrub_v)
 |