From 07b581cffaa33dfb9d066fff2aa69e74f3a37a6b Mon Sep 17 00:00:00 2001 From: Bryce Allen Date: Mon, 6 Dec 2021 22:07:20 -0500 Subject: [PATCH] add day4 solution --- day4.jl | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100755 day4.jl diff --git a/day4.jl b/day4.jl new file mode 100755 index 0000000..c0ad159 --- /dev/null +++ b/day4.jl @@ -0,0 +1,73 @@ +#!/usr/bin/env julia + +using DelimitedFiles + +infile = size(ARGS, 1) > 0 ? ARGS[1] : "day4input.txt" +println("infile = ", infile) + +M = Matrix{Int64}[] +B = Matrix{Bool}[] +W = Int64[] +S = Int64[] + +function mark_call(m::Matrix{Int64}, b::Matrix{Bool}, v::Int64) + score = 0 + idx_list = findall(m .== v) + b[idx_list] .= true + for idx in idx_list + if (all(b[idx[1], :] .== true) || all(b[:, idx[2]] .== true)) + score = sum(m[findall(b .== false)]) + end + if score > 0 + break + end + end + return score * v +end + +open(infile, "r") do io + line1 = readuntil(io, "\n\n") + calls = vec(readdlm(IOBuffer(line1), ',', Int)) + while true + line = readuntil(io, "\n\n") + if line == "" + break + else + push!(M, readdlm(IOBuffer(line), Int)) + push!(B, fill(false, 5, 5)) + push!(W, 0) + push!(S, 0) + end + end + + score = 0 + done = false + for (ncall, call) in enumerate(calls) + for i in 1:length(M) + m = M[i] + b = B[i] + score = mark_call(m, b, call) + if score > 0 + println("Round ", ncall, " Winner ", i) + display(m) + println() + println(score) + if (W[i] == 0) + W[i] = ncall + S[i] = score + if (all(W .!= 0)) + done = true + break + end + end + end + end + if (done) + break + end + end +end + +max_ncall, max_i = findmax(W) +println("last win ", max_i, " at ", max_ncall) +println("score ", S[max_i])