Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Combinatorics
- data = collect.(readlines("input"))
- diz = Dict{Char, Vector{Tuple{Int, Int}}}()
- for (i, row) in enumerate(data), (j, c) in enumerate(row)
- (isdigit(c) || isletter(c)) && push!(get!(diz, c, []), (i, j))
- end
- checkbounds(x::Tuple{Int,Int})::Bool = 1 <= x[1] <= length(data[1]) && 1 <= x[2] <= length(data)
- part1(data::Vector{Vector{Char}})::Int = begin
- v = Set{Tuple{Int, Int}}()
- for positions in values(diz)
- length(positions) > 1 && foreach(combinations(positions, 2)) do (a, b)
- diff = a .- b
- if diff[1]<0 || diff[2]<0
- checkbounds(a .+ diff) && push!(v, a .+ diff)
- checkbounds(b .- diff) && push!(v, b .- diff)
- else
- checkbounds(a .- diff) && push!(v, a .- diff)
- checkbounds(b .+ diff) && push!(v, b .+ diff)
- end
- end
- end
- length(v)
- end
- part2(data::Vector{Vector{Char}})::Int = begin
- v = Set{Tuple{Int, Int}}()
- for positions in values(diz)
- length(positions) > 1 && foreach(combinations(positions, 2)) do (a, b)
- diff = a .- b
- foreach(-div(50, maximum(abs.(diff)))-1:div(50, maximum(abs.(diff)))+1) do k
- x = a .+ k .* diff
- checkbounds(x) && push!(v, x)
- end
- end
- end
- length(v)
- end
- println(part1(data))
- println(part2(data))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement