Skip to content

Commit

Permalink
feat: learn from dzaima's solution of day 16.
Browse files Browse the repository at this point in the history
  • Loading branch information
Panadestein committed Dec 17, 2024
1 parent 6941bd9 commit 82b83c0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/aoc24.org
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,26 @@ powers of the adjacency matrix. This method is unfortunately slower than simple
#+RESULTS:
: "Computed 1446158 and 1446175 in 0.0177348094s"

** XVI

I dislike graph algorithms for their messy, imperative solutions with impurities and fancy data structures.
This problem is usually solved with Dijkstra, but after days of "Fortranized" DFS and BFS, I turned to an
expert array programmer's [[https://github.com/dzaima/aoc][solution]] and reworked mine:

#+begin_src bqn :tangle ./bqn/aoc24.bqn :exports both
XVI ← {
r‿s‿e ← ⟨'#'⊸≠, 'S'⊸=, 'E'⊸=⟩{𝕎𝕩}¨<inp ← >•FLines Input𝕩
_fp ← {𝔽∘⊢⍟≢⟜𝔽_𝕣∘⊢⍟≢⟜𝔽𝕩} ⋄ _m ← {𝕩{𝕏𝕨}¨𝔽⟨»,»˘,«,«˘⟩} ⋄ inf ← -2⋆31
dij ← ⌊´∘{⟨⊢_m, 1⊸⌽⌊1⊸⌽⁼, ⊢⟩{r<⊸×𝕎𝕩}¨⟨𝕩+1, 𝕩+1e3, 𝕩⟩}_fp 0‿inf‿0‿0×<s
mn‿mr‿mrc ← r⊸∧¨¨ {⟨¯1+2⊸⌽_m𝕩, 1⌽𝕩-1e3, 1⌽⁼𝕩-1e3⟩=<𝕩}dij
eins ⇐ inf -˜ opt ← (⌊´dij) ⊑˜⊑/○⥊⟜(↕≢)e
zwei ⇐ +´⥊∨´ {∨´⟨mn∧2⊸⌽𝕩, mr∧1⌽𝕩, mrc∧1⌽⁼𝕩, 𝕩⟩}_fp e<⊸∧dij=opt
}
XVI _is 16
#+end_src

#+RESULTS:
: "Computed 99448 and 1 in 0.049628195s"

#+BEGIN_EXPORT html
<div style="text-align: center; font-size: 2em; padding: 20px 0;">
Expand Down
10 changes: 10 additions & 0 deletions src/bqn/aoc24.bqn
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,13 @@ XV ← {
wwdrid DFS´mzwei+´1001×1>/(↕≢)'['=w
}
XV _is 15

XVI ← {
rse ← ⟨'#', 'S'=, 'E'=⟩{𝕎𝕩}¨<inp>•FLines Input𝕩
_fp ← {𝔽𝔽_𝕣𝔽𝕩} ⋄ _m ← {𝕩{𝕏𝕨}¨𝔽»,»˘,«,«˘⟩} ⋄ inf-231
dij´{⟨_m, 1⌽⌊1, ⟩{r<×𝕎𝕩}¨𝕩+1, 𝕩+1e3, 𝕩⟩}_fp 0inf00×<s
mnmrmrcr¨¨ {⟨¯1+2_m𝕩, 1𝕩-1e3, 1𝕩-1e3=<𝕩}dij
einsinf -˜ opt ← (´dij) ˜⊑/(↕≢)e
zwei+´⥊∨´ {´mn2𝕩, mr1𝕩, mrc1𝕩, 𝕩⟩}_fp e<dij=opt
}
XVI _is 16

0 comments on commit 82b83c0

Please sign in to comment.