You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.2 KiB
Julia

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

function task0(file)
rand_numbers = readline(file) |> x -> split(x, ",") .|> x -> parse(Int, x)
boards = []
while !eof(file)
push!(boards,
[readline(file, keep = true) for i = 1:6] |>
x -> vcat("[", x, "]") |>
join |>
Meta.parse |>
eval |>
x -> convert(Array{Union{Int,Missing},2}, x)
)
end
for number in rand_numbers
for board in boards
board[(board.==number).&map(!, board .=== missing)] .= missing
if any(map(!, (board .=== missing)) * ones(size(board, 2)) .== 0) ||
any(map(!, (board .=== missing))' * ones(size(board, 1)) .== 0)
return sum(skipmissing(board)) * number
end
end
end
boards
end
##
println("Day 04 Task 1 Case 0")
open(task0, "day04\\data\\case00.txt") |> println
println("Day 04 Task 1 Case 1")
open(task0, "day04\\data\\case01.txt") |> println
##
function task1(file)
rand_numbers = readline(file) |> x -> split(x, ",") .|> x -> parse(Int, x)
boards = []
while !eof(file)
push!(boards,
[readline(file, keep = true) for i = 1:6] |>
x -> vcat("[", x, "]") |>
join |>
Meta.parse |>
eval |>
x -> convert(Array{Union{Int,Missing},2}, x)
)
end
for number in rand_numbers
toremove = []
for (i, board) in enumerate(boards)
board[(board.==number).&map(!, board .=== missing)] .= missing
if any(map(!, (board .=== missing)) * ones(size(board, 2)) .== 0) ||
any(map(!, (board .=== missing))' * ones(size(board, 1)) .== 0)
if length(boards) == 1
return sum(skipmissing(board)) * number
else
push!(toremove, i)
end
end
end
boards = toremove |> x -> deleteat!(boards, x)
end
boards
end
##
println("Day 04 Task 2 Case 0")
open(task1, "day04\\data\\case00.txt") |> println
println("Day 03 Task 2 Case 1")
open(task1, "day04\\data\\case01.txt") |> println