days 1-3
This commit is contained in:
20
day1.jl
Executable file
20
day1.jl
Executable file
@@ -0,0 +1,20 @@
|
||||
using DelimitedFiles
|
||||
|
||||
depths = readdlm("day1input.txt")
|
||||
|
||||
diffs = depths[2:end] - depths[1:end-1]
|
||||
|
||||
pos_diff_count = size(findall(diffs .> 0), 1)
|
||||
|
||||
println("depth increases: ", pos_diff_count, " / ", size(diffs, 1))
|
||||
|
||||
window_size = 3
|
||||
|
||||
window_sums = [sum(depths[i:i+2]) for i in 1:size(depths, 1)-2]
|
||||
|
||||
window_diffs = window_sums[2:end] - window_sums[1:end-1]
|
||||
|
||||
pos_wdiff_count = size(findall(window_diffs .> 0), 1)
|
||||
|
||||
println("window sum increases: ", pos_wdiff_count,
|
||||
" / ", size(window_diffs, 1))
|
||||
2000
day1input.txt
Normal file
2000
day1input.txt
Normal file
File diff suppressed because it is too large
Load Diff
46
day2.jl
Normal file
46
day2.jl
Normal file
@@ -0,0 +1,46 @@
|
||||
using DelimitedFiles
|
||||
|
||||
course = readdlm("day2input.txt")
|
||||
|
||||
depth = 0
|
||||
horizontal = 0
|
||||
|
||||
for row in 1:size(course, 1)
|
||||
global horizontal, depth
|
||||
direction = course[row, 1]
|
||||
value = course[row, 2]
|
||||
if direction == "forward"
|
||||
horizontal += value
|
||||
elseif direction == "up"
|
||||
depth -= value
|
||||
elseif direction == "down"
|
||||
depth += value
|
||||
end
|
||||
end
|
||||
|
||||
println("depth = ", depth)
|
||||
println("horiz = ", horizontal)
|
||||
println("prod = ", depth * horizontal)
|
||||
|
||||
depth = 0
|
||||
horizontal = 0
|
||||
aim = 0
|
||||
|
||||
for row in 1:size(course, 1)
|
||||
global horizontal, depth, aim
|
||||
direction = course[row, 1]
|
||||
value = course[row, 2]
|
||||
if direction == "forward"
|
||||
horizontal += value
|
||||
depth += value * aim
|
||||
elseif direction == "up"
|
||||
aim -= value
|
||||
elseif direction == "down"
|
||||
aim += value
|
||||
end
|
||||
end
|
||||
|
||||
println("depth = ", depth)
|
||||
println("horiz = ", horizontal)
|
||||
println("aim = ", aim)
|
||||
println("prod = ", depth * horizontal)
|
||||
1000
day2input.txt
Normal file
1000
day2input.txt
Normal file
File diff suppressed because it is too large
Load Diff
68
day3.jl
Normal file
68
day3.jl
Normal file
@@ -0,0 +1,68 @@
|
||||
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)
|
||||
1000
day3input.txt
Normal file
1000
day3input.txt
Normal file
File diff suppressed because it is too large
Load Diff
12
day3test.txt
Normal file
12
day3test.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
00100
|
||||
11110
|
||||
10110
|
||||
10111
|
||||
10101
|
||||
01111
|
||||
00111
|
||||
11100
|
||||
10000
|
||||
11001
|
||||
00010
|
||||
01010
|
||||
Reference in New Issue
Block a user