Advertisement
icarussiano

Day 8 Julia

Dec 8th, 2024 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 1.39 KB | None | 0 0
  1. using Combinatorics
  2. data = collect.(readlines("input"))
  3. diz = Dict{Char, Vector{Tuple{Int, Int}}}()
  4. for (i, row) in enumerate(data), (j, c) in enumerate(row)
  5.     (isdigit(c) || isletter(c)) && push!(get!(diz, c, []), (i, j))
  6. end
  7. checkbounds(x::Tuple{Int,Int})::Bool = 1 <= x[1] <= length(data[1]) && 1 <= x[2] <= length(data)
  8.  
  9. part1(data::Vector{Vector{Char}})::Int = begin
  10.     v = Set{Tuple{Int, Int}}()
  11.     for positions in values(diz)
  12.         length(positions) > 1 && foreach(combinations(positions, 2)) do (a, b)
  13.             diff = a .- b
  14.             if diff[1]<0 || diff[2]<0
  15.                 checkbounds(a .+ diff) && push!(v, a .+ diff)
  16.                 checkbounds(b .- diff) && push!(v, b .- diff)
  17.             else
  18.                 checkbounds(a .- diff) && push!(v, a .- diff)
  19.                 checkbounds(b .+ diff) && push!(v, b .+ diff)
  20.             end
  21.         end
  22.     end
  23.     length(v)
  24. end
  25.  
  26. part2(data::Vector{Vector{Char}})::Int = begin
  27.     v = Set{Tuple{Int, Int}}()
  28.     for positions in values(diz)
  29.         length(positions) > 1 && foreach(combinations(positions, 2)) do (a, b)
  30.             diff = a .- b
  31.             foreach(-div(50, maximum(abs.(diff)))-1:div(50, maximum(abs.(diff)))+1) do k
  32.                 x = a .+ k .* diff
  33.                 checkbounds(x) && push!(v, x)
  34.             end
  35.         end
  36.     end
  37.     length(v)
  38. end
  39.  
  40. println(part1(data))
  41. println(part2(data))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement