Skip to content

Commit

Permalink
2023 day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
taw10 committed Dec 4, 2023
1 parent 9b0a9e9 commit f975004
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions 2023/day04-scratchcards/day04.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
function readcards(filename)
Channel() do ch
for line in eachline(filename)
r = split(line, "|")
l = split(r[1])[3:end]
m = split(r[2])
put!(ch, (map(x->parse(Int32, x), l),
map(x->parse(Int32, x), m)))
end
end
end


function nwin(winners, card)
score = 0
for winner in winners
if winner card
score += 1
end
end
score
end


function score(winners, card)
let s = nwin(winners, card)
if s > 0
2^(s-1)
else
0
end
end
end


let allcards = collect(readcards("input"))

println("Part 1: ", sum(map(x->score(x[1],x[2]), allcards)))

let pending = [1 for _ in allcards], ncards = 0
for card in allcards
ncopies = popfirst!(pending)
ncards += ncopies
nmatch = nwin(card[1], card[2])
for i in 1:nmatch
pending[i] += ncopies
end
end
println("Part 2: ", ncards)
end

end

0 comments on commit f975004

Please sign in to comment.