#!/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])