-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.jl
42 lines (39 loc) · 1.37 KB
/
day04.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
board_pattern = r"(((\d+)\s+){5}){5}"
numbers, boards = open("bingo.txt") do f
numbers = parse.(Int, split(readline(f), ","))
boards = map(m -> split.(strip.(replace.(m.match, r"\s+" => " ")), " "), eachmatch(board_pattern, join(readlines(f, keep = true))))
boards = map(r -> transpose(reshape(parse.(Int, r), 5, 5)), boards)
return numbers, boards
end
function find_bingo(numbers, boards)
for i in 5:length(numbers)
for b in boards
for c in eachcol(b)
if length(intersect(c, numbers[1:i])) == 5
# println("bingo")
return b, i
end
end
for r in eachrow(b)
if length(intersect(r, numbers[1:i])) == 5
# println("bingo")
return b, i
end
end
end
end
end
function find_last_bingo(numbers, boards)
board, n = nothing, nothing
while length(boards) > 0
board, n = find_bingo(numbers, boards)
# println(board, n, numbers[n])
filter!(b -> b != board, boards)
# println(length(boards))
end
return board, n
end
board, n = find_bingo(numbers, boards)
println(sum(setdiff(reshape(board, :, 1), numbers[1:n])) * numbers[n])
board, n = find_last_bingo(numbers, boards)
println(sum(setdiff(reshape(board, :, 1), numbers[1:n])) * numbers[n])