-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path:w
101 lines (76 loc) · 2.32 KB
/
:w
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# --------------------------------------------------------------------#
# Part 1 #
# --------------------------------------------------------------------#
struct Bingo
numbers::Matrix{Int64}
marked::Matrix{Bool}
Bingo(numbers::Matrix{Int64}) = new(numbers, fill(false, size(numbers)...))
end
function Bingo(raw::Vector{String})
numbers = Matrix{Int64}(undef, length(raw), length(raw))
for i in eachindex(raw)
row = parse.(Int, split(raw[i]))
for j in eachindex(row)
numbers[i,j] = row[j]
end
end
return Bingo(numbers)
end
function Base.show(io::Core.IO, b::Bingo)
string_b = string.(b.numbers) .* map(m -> m ? "x" : " ", b.marked)
for l in eachrow(string_b)
println(join(l, " "))
end
end
"Mark a number of the bingo board `b` at position `(i,j)`."
function mark!(b::Bingo, i::Int, j::Int)
b.marked[i,j] = true
nothing
end
"Mark a number of the bingo board `b` at position `ci`."
function mark!(b::Bingo, ci::CartesianIndex{2})
b.marked[ci] = true
nothing
end
"Check if `num` is in `b`."
function check(b::Bingo, num::Int)
pos = findall(==(num), b.numbers)
if length(pos) == 1
return only(pos)
else
return pos
end
end
"Check if a row in `b` is completed."
function row_completed(b::Bingo)
for r in eachrow(b.marked)
all(r) && return true
end
return false
end
"Check if a col in `b` is completed."
function col_completed(b::Bingo)
for c in eachcol(b.marked)
all(c) && return true
end
return false
end
"Check if `b` is completed"
completed(b::Bingo) = row_completed(b) || col_completed(b)
function read_file()
lines = readlines("input/input4.dat")
rand_numbers = lines[1]
rest = lines[3:end]
split_lines = findall(==(""), rest)
bings = Bingo[]
for i = 2:length(split_lines)
m = rest[split_lines[i - 1] + 1:split_lines[i] - 1]
push!(bings, Bingo(m))
end
return parse.(Int, split(rand_numbers, ',')), bings
end
rand_nubers, bs = read_file()
b = bs[1]
# --------------------------------------------------------------------#
# Part 2 #
# --------------------------------------------------------------------#