-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.jl
67 lines (60 loc) · 1.88 KB
/
day12.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
module Day12
using AdventOfCode2024
function day12(input::String = readInput(joinpath(@__DIR__, "..", "data", "day12.txt")))
data = map(x -> x[1], reduce(vcat, permutedims.(map(x -> split(x, ""), split(input)))))
solve(data)
end
function solve(data)
p1, p2 = 0, 0
visited = zeros(Bool, size(data))
for i ∈ eachindex(view(data, 1:size(data, 1), 1:size(data, 2)))
visited[i] && continue
label = data[i]
area = 0
ntouches = 0
queue = [i]
sideinds = Set{Tuple{Int,Int,Int}}()
while !isempty(queue)
ind = popfirst!(queue)
visited[ind] && continue
area += 1
visited[ind] = true
for (nind, d) ∈ zip(CartesianIndex.(ind.I .+ x for x ∈ ((1, 0), (-1, 0), (0, 1), (0, -1))), (1,2,3,4))
if !checkbounds(Bool, data, nind)
push!(sideinds, (nind.I..., d))
continue
end
if data[nind] == label
ntouches += 1
if !visited[nind]
push!(queue, nind)
end
else
push!(sideinds, (nind.I..., d))
end
end
end
perimeter = 4 * area - ntouches
p1 += area * perimeter
p2 += area * calculate_sides(sideinds)
end
return [p1, p2]
end
function calculate_sides(sideinds::Set{Tuple{Int,Int,Int}})
count = 0
while !isempty(sideinds)
count += 1
queue = Tuple{Int,Int,Int}[first(sideinds)]
while !isempty(queue)
a = popfirst!(queue)
delete!(sideinds, a)
for b ∈ sideinds
if a[3] == b[3] && sum(abs.(a[1:2] .- b[1:2])) == 1
push!(queue, b)
end
end
end
end
return count
end
end # module