-
Notifications
You must be signed in to change notification settings - Fork 0
/
day03.jl
148 lines (137 loc) · 4.35 KB
/
day03.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
module Day03
using AdventOfCode2023
function day03(input::String = readInput(joinpath(@__DIR__, "..", "data", "day03.txt")))
data = map(x -> x[1], reduce(vcat, permutedims.(map(x -> split(x, ""), split(input)))))
return [part1(data), part2(data)]
end
function neighbour_idx(c::CartesianIndex{2}, data::Matrix{Char})
idx = Vector{CartesianIndex{2}}()
for i ∈ c[1] - 1 : c[1] + 1
for j ∈ c[2] - 1 : c[2] + 1
i == c[1] && j == c[2] && continue
if checkbounds(Bool, data, i, j)
push!(idx, CartesianIndex(i, j))
end
end
end
return idx
end
function part1(data::Matrix{Char})
symbolpos = findall(x -> x ∉ ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.'), data)
numbermap = zeros(Bool, size(data))
for pos ∈ symbolpos
for npos ∈ neighbour_idx(pos, data)
if isdigit(data[npos])
numbermap[npos] = true
end
end
end
_fill!(numbermap, data)
return _get_hit_numbers(numbermap, data) |> sum
end
function _fill!(hits::Matrix{Bool}, data::Matrix{Char})
for c ∈ findall(x -> x == true, hits)
ci, cj = c.I
j = cj + 1
while checkbounds(Bool, hits, ci, j) && isdigit(data[ci, j])
hits[ci, j] = true
j += 1
end
j = cj - 1
while checkbounds(Bool, hits, ci, j) && isdigit(data[ci, j])
hits[ci, j] = true
j -= 1
end
end
end
function _get_hit_numbers(hits::Matrix{Bool}, data::Matrix{Char})
numbers = Vector{Int}()
for i ∈ 1:size(data, 1)
j = 1
while checkbounds(Bool, data, i, j)
if hits[i, j]
temp = [parse(Int, data[i,j])]
j += 1
while checkbounds(Bool, data, i, j) && hits[i, j]
push!(temp, parse(Int, data[i, j]))
j += 1
end
s, m = 0, 1
for d ∈ reverse(temp)
s += d * m
m *= 10
end
push!(numbers, s)
end
j += 1
end
end
return numbers
end
function part2(data::Matrix{Char})
s = 0
for pos ∈ findall(x -> x == '*', data)
numbers = adjacent_numbers(data, pos)
if length(numbers) == 2
s += prod(numbers)
end
end
return s
end
function get_number(data::Matrix{Char}, pos::CartesianIndex{2})
i, j = pos[1], pos[2]
while checkbounds(Bool, data, i, j + 1) && isdigit(data[i, j + 1])
j += 1
end
m = 1
s = 0
while checkbounds(Bool, data, i, j) && isdigit(data[i, j])
s += parse(Int, data[i,j]) * m
m *= 10
j -= 1
end
return s
end
function adjacent_numbers(data::Matrix{Char}, pos::CartesianIndex{2})
numbers = Vector{Int}()
n = 0
i, j = pos[1], pos[2]
for k ∈ (i - 1, i + 1)
if checkbounds(Bool, data, k, j - 1 : j + 1)
if isdigit(data[k, j-1]) && !isdigit(data[k, j]) && isdigit(data[k, j+1])
n += 2
push!(numbers, get_number(data, CartesianIndex(k, j - 1)))
push!(numbers, get_number(data, CartesianIndex(k, j + 1)))
else
if isdigit(data[k, j - 1])
n += 1
push!(numbers, get_number(data, CartesianIndex(k, j - 1)))
elseif isdigit(data[k, j])
n += 1
push!(numbers, get_number(data, CartesianIndex(k, j)))
elseif isdigit(data[k, j + 1])
n += 1
push!(numbers, get_number(data, CartesianIndex(k, j + 1)))
end
end
else
for j2 = j - 1 : j + 1
if checkbounds(Bool, data, k, j2) && isdigit(data[k, j2])
n += 1
push!(numbers, get_number(data, CartesianIndex(k, j2)))
break
end
end
end
end
if checkbounds(Bool, data, i, j - 1) && isdigit(data[i, j - 1])
n += 1
push!(numbers, get_number(data, CartesianIndex(i, j - 1)))
end
if checkbounds(Bool, data, i, j + 1) && isdigit(data[i, j + 1])
n += 1
push!(numbers, get_number(data, CartesianIndex(i, j + 1)))
end
return numbers
end
end # module