Skip to content

Commit

Permalink
[Julia] day 11
Browse files Browse the repository at this point in the history
  • Loading branch information
Moelf committed Dec 11, 2023
1 parent 0b0117e commit 3692939
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
10 changes: 10 additions & 0 deletions inputs/11_example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....
2 changes: 2 additions & 0 deletions solutions/11_example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
374
82000210
2 changes: 2 additions & 0 deletions solutions/11_moelf.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
9795148
650672493820
33 changes: 33 additions & 0 deletions src/julia/11_moelf.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const CI = CartesianIndex
cityblock(a::CI, b::CI) = sum(abs, Tuple(a-b))

function impl(MAP, exp_const)
galaxies = findall(==('#'), MAP)
N = exp_const - 1
for (i, j) in zip(axes(MAP, 1), axes(MAP, 2))
if all(==('.'), @view MAP[i, :])
mask = findall(c -> c[1] < i, galaxies)
galaxies[mask] .-= CI(N, 0)
end

if all(==('.'), @view MAP[:, j])
mask = findall(c -> c[2] < j, galaxies)
galaxies[mask] .-= CI(0, N)
end
end

res = 0
for i in eachindex(galaxies), j in i+1:lastindex(galaxies)
res += cityblock(galaxies[i], galaxies[j])
end

res
end

function main(path)
MAP = stack(readlines(path); dims=1)
println(impl(MAP, 2))
println(impl(MAP, 1000000))
end

(abspath(PROGRAM_FILE) == @__FILE__) && main(ARGS[1])

0 comments on commit 3692939

Please sign in to comment.