trunk
HeNine 3 years ago
parent b5a80b931d
commit e627fa68df

@ -0,0 +1,4 @@
name = "day03"
uuid = "fcc5b334-7230-44ca-b424-8172184a1b5b"
authors = ["HeNine "]
version = "0.1.0"

@ -0,0 +1,12 @@
00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010

File diff suppressed because it is too large Load Diff

@ -0,0 +1,64 @@
function task0(file)
sums = missing
nrows = 0
for line in eachline(file)
nrows += 1
bits = split(line, "") .|> x -> parse(Int, x)
if sums === missing
sums = bits
else
sums += bits
end
end
(sums .> nrows / 2)' * [2^i for i = (length(sums)-1):-1:0] *
(sums .< nrows / 2)' * [2^i for i = (length(sums)-1):-1:0]
end
##
println("Day 03 Task 1 Case 0")
open(task0, "day03\\data\\case00.txt") |> println
println("Day 03 Task 1 Case 1")
open(task0, "day03\\data\\case01.txt") |> println
##
function task1(file)
bitvectors = eachline(file) .|>
line -> split(line, "") .|>
x -> parse(Int, x)
findval(1, bitvectors) *
findval(0, bitvectors)
end
function findval(gas, bitvectors, pos = 1)
if length(bitvectors) == 1
return bitvectors[1]' * [2^i for i = (length(bitvectors[1])-1):-1:0]
end
sums = reduce(+, bitvectors)
nrow = length(bitvectors)
findval(gas,
filter(
x ->
x[pos] == ((gas == 1 && sums[pos] >= nrow / 2) || (gas == 0 && sums[pos] < nrow / 2)),
bitvectors
),
pos + 1)
end
##
println("Day 03 Task 1 Case 0")
open(task1, "day03\\data\\case00.txt") |> println
println("Day 03 Task 1 Case 1")
open(task1, "day03\\data\\case01.txt") |> println
Loading…
Cancel
Save