141 lines
4.3 KiB
BQN
141 lines
4.3 KiB
BQN
ReadInt ⇐ (+ ⟜ (10⊸×))´ ∘ (-⟜'0') ∘ ⌽
|
||
IntFile ⇐ ReadInt¨ •FLines
|
||
|
||
aoc1 ⇐ {
|
||
A ⇐ +´ ∘ (>⟜(∞⊸»)) ∘ IntFile
|
||
B ⇐ +´ ∘ (>⟜(∞⊸»)) ∘ (+´˘) ∘ (3⊸↕) ∘ IntFile
|
||
}
|
||
|
||
################################################################################
|
||
|
||
Split ⇐ ((+`׬) - ⊢)∘= ⊔ ⊢ # from bqn crates lmao
|
||
Words ⇐ ((0≠≠)¨⊸/) ∘ (' '⊸Split)
|
||
CSV ⇐ ','⊸Split
|
||
CSVInt ⇐ ReadInt¨ CSV
|
||
|
||
aoc2 ⇐ {
|
||
Move1 ← { "forward" 𝕊 x: x‿0; "down" 𝕊 y: 0‿y; "up" 𝕊 y: ⟨0,-y⟩ }
|
||
Move ← Move1⟜ReadInt´ ∘ Words
|
||
MoveFile ← Move¨ ∘ •FLines
|
||
DoMoveB ← {dx‿da 𝕊 x‿y‿a : ⟨x+dx, y+(dx×a), a+da⟩}
|
||
|
||
A ⇐ ×´ +´ ∘ MoveFile
|
||
B ⇐ ×´ ∘ (2⊸↑) ∘ (0‿0‿0⊸(DoMoveB´)∘⌽) ∘ MoveFile
|
||
}
|
||
|
||
################################################################################
|
||
|
||
FromBin ⇐ +´ 2⊸⋆∘↕∘≠ × ⌽
|
||
BitFile ⇐ (-⟜'0') ∘ > ∘ •FLines
|
||
|
||
aoc3 ⇐ {
|
||
Most ← ⊑ ∘ ((⍒⊒) ⊸ ⊏)
|
||
Gamma ← Most˘ ∘ ⍉ ∘ BitFile
|
||
|
||
A ⇐ ¬⊸(×○FromBin) ∘ Gamma
|
||
B ⇐ {"nah"!0, 𝕩}
|
||
}
|
||
|
||
################################################################################
|
||
|
||
aoc4 ⇐ {
|
||
BingoFile ← {
|
||
lines ← •FLines 𝕩
|
||
picks ⇐ CSVInt ⊑lines
|
||
boards ⇐ ReadInt¨ > Words¨ 1⊸↓˘ ∘‿6 ⥊ 1↓lines
|
||
states ⇐ boards <⊸(≡◶⊣‿0¨`) picks
|
||
}
|
||
|
||
Win ← ∨´ ((∧´ 0⊸≡)˘)∘(⍉∾⊢)
|
||
Score ← +´⥊
|
||
|
||
A ⇐ {
|
||
picks‿states ← BingoFile 𝕩
|
||
⊑ 0⊸≠⊸/ +´¨ picks × (Score × Win)˘¨ states
|
||
}
|
||
|
||
B ⇐ {
|
||
picks‿states ← BingoFile 𝕩
|
||
pick‿cur‿prev ← ⊑ ((∧´ Win˘)¨ states) / (picks ∾¨ states ⋈¨ »states)
|
||
pick × Score (¬Win˘ prev) / cur
|
||
}
|
||
}
|
||
|
||
################################################################################
|
||
|
||
aoc5 ⇐ {
|
||
VentFile ← ReadInt¨∘∾∘(CSV¨ 0‿2⊸⊏)∘Words¨ •FLines
|
||
To ← {𝕨≡𝕩? 𝕩; ⌽⍟(𝕨>𝕩) (𝕨⌊𝕩) ↓ ↕(1+𝕨⌈𝕩)}
|
||
DrawLine ← {x1‿y1‿x2‿y2: (x1 To x2) ⋈¨ (y1 To y2)}
|
||
Orth ← {∨´ =´ <˘ 2‿∘ ⥊ 𝕩}
|
||
Danger ← {1⊑ /⁼ ⊒ ∾DrawLine¨ 𝕩}
|
||
A ⇐ Danger (Orth¨⊸/ VentFile), B ⇐ Danger VentFile
|
||
}
|
||
|
||
################################################################################
|
||
|
||
aoc6 ⇐ {
|
||
FishFile ← 9⊸↑ /⁼ ∘ CSVInt ∘ ⊑ ∘ •FLines
|
||
Step ← (⊑ × (/⁼6‿8)˙) + «
|
||
Fishies ← {+´ Step⍟𝕨 FishFile 𝕩}
|
||
A ⇐ 80⊸Fishies, B ⇐ 256⊸Fishies
|
||
}
|
||
|
||
################################################################################
|
||
|
||
aoc7 ⇐ {
|
||
CrabFile ← CSVInt ∘ ⊑ ∘ •FLines
|
||
|
||
Range ← {l + ↕(⌈´𝕩)¬(l←⌊´𝕩)}
|
||
|
||
FuelA ← +´ ∘| -
|
||
FuelB ← +´∘(÷⟜2)∘(⊢×+⟜1)∘| -
|
||
|
||
_Go ← {⌊´ ps⊸𝔽¨ Range ps←CrabFile 𝕩}
|
||
A ⇐ FuelA _Go, B ⇐ FuelB _Go
|
||
}
|
||
|
||
################################################################################
|
||
|
||
aoc8 ⇐ {A⇐B⇐{"maybe later"!0, 𝕩}}
|
||
|
||
################################################################################
|
||
|
||
aoc9 ⇐ {
|
||
SmokeFile ← > -⟜'0' ∘ •FLines
|
||
Adj ← {<⎉1 ⍉ > ⟨∞«˘𝕩, ∞»˘𝕩, ∞«˘⌾⍉𝕩, ∞»˘⌾⍉𝕩⟩}
|
||
Lowest ← ∧´¨ <⟜Adj
|
||
Risk ← 1⊸+ × Lowest
|
||
A ⇐ +´⥊ ∘ Risk ∘ SmokeFile
|
||
B ⇐ {"nah"!0, 𝕩}
|
||
}
|
||
|
||
################################################################################
|
||
|
||
Fst ⇐ {0 ≡ ≠𝕩? @; ⊑𝕩}
|
||
At ⇐ {1⊑ ⊑ (⊑ ≡ 𝕩˙)¨⊸/ 𝕨} # use ⎊ for default
|
||
|
||
aoc10 ⇐ {
|
||
BadScore1 ← ⟨')'‿3,']'‿57,'}'‿1197,'>'‿25137⟩⊸At⎊0
|
||
BadScore ← BadScore1 ∘ {'!'‿c: c; @}
|
||
|
||
Step ← {
|
||
'!' ≡ Fst 𝕨? 𝕨;
|
||
𝕨 𝕊 ')': '(' = Fst 𝕨? 1↓𝕨;
|
||
𝕨 𝕊 ']': '[' = Fst 𝕨? 1↓𝕨;
|
||
𝕨 𝕊 '}': '{' = Fst 𝕨? 1↓𝕨;
|
||
𝕨 𝕊 '>': '<' = Fst 𝕨? 1↓𝕨;
|
||
⊑ 𝕩∊"([{<"? 𝕩∾𝕨;
|
||
'!'‿𝕩
|
||
}
|
||
Find ← ""⊸(Step˜´) ⌽
|
||
|
||
GoodScore1 ← ⟨'('‿1,'['‿2,'{'‿3,'<'‿4⟩⊸At
|
||
GoodScoreN ← {0 ((GoodScore1 ⊣) + (5⊸× ⊢))´ 𝕩}
|
||
GoodScore ← GoodScoreN ⌽ ∘ {'!' ≡ Fst 𝕩? ""; 𝕩}
|
||
|
||
Middle ← ((⌊÷⟜2)∘≠ ⊑ ⊢) ∧ ∘ (0⊸≠¨⊸/)
|
||
|
||
A ⇐ +´ (BadScore∘Find¨) ∘ •FLines
|
||
B ⇐ Middle (GoodScore∘Find¨) ∘ •FLines
|
||
}
|