Skip to content

Instantly share code, notes, and snippets.

@0racle
Last active May 29, 2025 05:21
Show Gist options
  • Select an option

  • Save 0racle/4c1c36d165d2c98c7deeb07cd06e0f5a to your computer and use it in GitHub Desktop.

Select an option

Save 0racle/4c1c36d165d2c98c7deeb07cd06e0f5a to your computer and use it in GitHub Desktop.
AOC 2018 Day 3

J solution (~0.2s runtime on my machine)

Nats =: '0123456789'&(i. ".@:{ ' ' ,~ [)
Claim =: ,"0/&((+ i.@])/)

claims =. |:@(2 2 $ }.)@Nats;._2 fread 'input'
claimed =. <@,@(1000 #:inv Claim)/"2 claims

echo # overlaps =. (~.@#~ -.@~:) ; claimed  NB. Part 1
echo >: 0 i.~ overlaps +./@e.S:0 claimed    NB. Part 2

BQN solution (~1.9s runtime on my machine)

str ← •Import "strings.bqn"
Claim ← +⌜´ 1000‿1 × ⋈○((⊣+↕∘⊢)´)

claims ← (⍉2‿2⥊1⊸↓∘str.ToNums)¨ •FLines "input"
claimed ← ⥊∘Claim˝¨ claims

•Show ≠ overlaps ← ⍷ ¬∘∊⊸/ ∾ claimed  # Part 1
•Show 1+⊑0⊐˜(∨´ overlaps⊸∊)¨ claimed  # Part 2

The main slow part is looking for overlaps in the claimed areas

overlaps⊸∊¨ claimed

Technically, the equivlent to J is probably closer to

overlaps ∊⚇1 claimed

but regardless, it's no faster. Additionally, the each-y version in J

(overlaps&e.)@> claimed

is just as fast as Spread (S:)

@0racle

0racle commented May 29, 2025 •

Copy link
Copy Markdown
Author

dzaima responded:

adding a overlaps •internal.Squeeze↩ between the two parts speeds gets it to 0.3s; seems CBQNs 𝔽⌜ still doesn't squeeze the result in some cases

Alternatively, since the issue is with 𝔽⌜, I could swap out the +⌜ in Claim to +⎉0‿∞ which does appear to squeeze the result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment