├── 2020 └── BQN │ ├── 1.bqn │ ├── 1.in │ ├── 10.bqn │ ├── 10.in │ ├── 11.bqn │ ├── 11.in │ ├── 12.bqn │ ├── 12.in │ ├── 13.bqn │ ├── 13.in │ ├── 14.bqn │ ├── 14.in │ ├── 15.bqn │ ├── 15.in │ ├── 16.bqn │ ├── 16.in │ ├── 17.bqn │ ├── 17.in │ ├── 18.bqn │ ├── 18.in │ ├── 19.bqn │ ├── 19.in │ ├── 2.bqn │ ├── 2.in │ ├── 20.bqn │ ├── 20.in │ ├── 21.bqn │ ├── 21.in │ ├── 22.bqn │ ├── 22.in │ ├── 23.bqn │ ├── 23.in │ ├── 24.bqn │ ├── 24.in │ ├── 25.bqn │ ├── 25.in │ ├── 3.bqn │ ├── 3.in │ ├── 4.bqn │ ├── 4.in │ ├── 5.bqn │ ├── 5.in │ ├── 6.bqn │ ├── 6.in │ ├── 7.bqn │ ├── 7.in │ ├── 8.bqn │ ├── 8.in │ ├── 9.bqn │ ├── 9.in │ └── test.bqn ├── 2021 ├── BQN │ ├── 1.bqn │ ├── 1.in │ ├── 10.bqn │ ├── 10.in │ ├── 10_explanation.bqn │ ├── 11.bqn │ ├── 11.in │ ├── 12.bqn │ ├── 12.in │ ├── 13.bqn │ ├── 13.in │ ├── 14.bqn │ ├── 14.in │ ├── 15.bqn │ ├── 15.in │ ├── 16.bqn │ ├── 16.in │ ├── 17.bqn │ ├── 17.in │ ├── 18.bqn │ ├── 18.in │ ├── 19.bqn │ ├── 19.in │ ├── 2.bqn │ ├── 2.in │ ├── 20.bqn │ ├── 20.in │ ├── 21.bqn │ ├── 21.in │ ├── 22.bqn │ ├── 22.in │ ├── 23.bqn │ ├── 23.in │ ├── 24.bqn │ ├── 24.in │ ├── 25.bqn │ ├── 25.in │ ├── 3.bqn │ ├── 3.in │ ├── 4.bqn │ ├── 4.in │ ├── 5.bqn │ ├── 5.in │ ├── 6.bqn │ ├── 6.in │ ├── 7.bqn │ ├── 7.in │ ├── 8.bqn │ ├── 8.in │ ├── 9.bqn │ ├── 9.in │ ├── alt │ │ ├── 12a.bqn │ │ ├── 15a.bqn │ │ ├── 17a.bqn │ │ ├── 18a.bqn │ │ ├── 22a.bqn │ │ └── 22b.bqn │ ├── other │ │ └── 18coltim.in │ └── test.bqn └── Java │ └── day5_p1 │ ├── Main.java │ └── inputs │ ├── example.in │ ├── given.in │ └── givenE6.in ├── 2022 └── BQN │ ├── 1.bqn │ ├── 1.in │ ├── 10.bqn │ ├── 10.in │ ├── 11.bqn │ ├── 11.in │ ├── 12.bqn │ ├── 12.in │ ├── 13.bqn │ ├── 13.in │ ├── 14.bqn │ ├── 14.in │ ├── 15.bqn │ ├── 15.in │ ├── 16.bqn │ ├── 16.in │ ├── 17.bqn │ ├── 17.in │ ├── 18.bqn │ ├── 18.in │ ├── 19.bqn │ ├── 19.in │ ├── 2.bqn │ ├── 2.in │ ├── 20.bqn │ ├── 20.in │ ├── 21.bqn │ ├── 21.in │ ├── 22.bqn │ ├── 22.in │ ├── 23.bqn │ ├── 23.in │ ├── 24.bqn │ ├── 24.in │ ├── 25.bqn │ ├── 25.in │ ├── 3.bqn │ ├── 3.in │ ├── 4.bqn │ ├── 4.in │ ├── 5.bqn │ ├── 5.in │ ├── 6.bqn │ ├── 6.in │ ├── 7.bqn │ ├── 7.in │ ├── 8.bqn │ ├── 8.in │ ├── 9.bqn │ ├── 9.in │ ├── test.bqn │ └── utils.bqn ├── 2023 ├── BQN │ ├── 1.bqn │ ├── 10.bqn │ ├── 10_orig.bqn │ ├── 11.bqn │ ├── 11_orig.bqn │ ├── 12.bqn │ ├── 12_orig.bqn │ ├── 13.bqn │ ├── 13_orig.bqn │ ├── 14.bqn │ ├── 14_orig.bqn │ ├── 15.bqn │ ├── 15_orig.bqn │ ├── 16.bqn │ ├── 16_orig.bqn │ ├── 17.bqn │ ├── 17_orig.bqn │ ├── 18.bqn │ ├── 18_orig.bqn │ ├── 19.bqn │ ├── 19_orig.bqn │ ├── 1_fast.bqn │ ├── 2.bqn │ ├── 20.bqn │ ├── 20_orig_part1.bqn │ ├── 20_orig_part2.bqn │ ├── 21.bqn │ ├── 21_orig.bqn │ ├── 22.bqn │ ├── 22_orig.bqn │ ├── 23.bqn │ ├── 23_orig.bqn │ ├── 24.bqn │ ├── 24_orig.bqn │ ├── 24_part2.bqn │ ├── 25.bqn │ ├── 2_orig.bqn │ ├── 3.bqn │ ├── 3_orig.bqn │ ├── 4.bqn │ ├── 4_orig.bqn │ ├── 5.bqn │ ├── 5_orig.bqn │ ├── 6.bqn │ ├── 6_orig.bqn │ ├── 7.bqn │ ├── 7_orig.bqn │ ├── 8.bqn │ ├── 8_orig.bqn │ ├── 9.bqn │ ├── 9_orig.bqn │ └── test.bqn ├── Singeli │ ├── .gitignore │ ├── 1.singeli │ ├── 1_prep.bqn │ ├── makefile │ └── test.c └── inputs │ ├── 1.txt │ ├── 10.txt │ ├── 11.txt │ ├── 12.txt │ ├── 13.txt │ ├── 14.txt │ ├── 15.txt │ ├── 16.txt │ ├── 17.txt │ ├── 18.txt │ ├── 19.txt │ ├── 2.txt │ ├── 20.txt │ ├── 21.txt │ ├── 22.txt │ ├── 23.txt │ ├── 24.txt │ ├── 25.txt │ ├── 3.txt │ ├── 4.txt │ ├── 5.txt │ ├── 6.txt │ ├── 7.txt │ ├── 8.txt │ ├── 9.txt │ └── gen │ ├── .gitignore │ └── 1_gen.bqn ├── 2024 ├── BQN │ ├── 1.bqn │ ├── 10.bqn │ ├── 10_fast.bqn │ ├── 10_orig.bqn │ ├── 11.bqn │ ├── 11_orig.bqn │ ├── 12.bqn │ ├── 12_ffi.bqn │ ├── 12_ffi.c │ ├── 12_orig.bqn │ ├── 13.bqn │ ├── 13_orig.bqn │ ├── 14.bqn │ ├── 14_orig.bqn │ ├── 15.bqn │ ├── 15_orig.bqn │ ├── 16.bqn │ ├── 16_orig.bqn │ ├── 17.bqn │ ├── 17_smt.bqn │ ├── 18.bqn │ ├── 18_orig.bqn │ ├── 19.bqn │ ├── 19_orig.bqn │ ├── 1_fast.bqn │ ├── 1_orig.bqn │ ├── 2.bqn │ ├── 20.bqn │ ├── 20_orig.bqn │ ├── 21_orig.bqn │ ├── 22.bqn │ ├── 22_orig.bqn │ ├── 23_orig.bqn │ ├── 24_orig.bqn │ ├── 25.bqn │ ├── 25_orig.bqn │ ├── 2_orig.bqn │ ├── 2_short.bqn │ ├── 3.bqn │ ├── 3_orig.bqn │ ├── 4.bqn │ ├── 4_fast.bqn │ ├── 4_orig.bqn │ ├── 5.bqn │ ├── 5_fast.bqn │ ├── 5_orig.bqn │ ├── 6.bqn │ ├── 6_orig.bqn │ ├── 7.bqn │ ├── 7_orig.bqn │ ├── 8.bqn │ ├── 8_orig.bqn │ ├── 9.bqn │ ├── 9_orig.bqn │ ├── makefile │ └── test.bqn └── inputs │ ├── 1.txt │ ├── 10.txt │ ├── 11.txt │ ├── 12.txt │ ├── 13.txt │ ├── 14.txt │ ├── 15.txt │ ├── 19.txt │ ├── 2.txt │ ├── 21.txt │ ├── 22.txt │ ├── 25.txt │ ├── 3.txt │ ├── 4.txt │ ├── 5.txt │ ├── 6.txt │ ├── 7.txt │ ├── 8.txt │ ├── 9.txt │ ├── gen.bqn │ └── gen_all.bqn ├── .gitignore ├── README.md ├── licenses ├── LICENSE-MIT ├── LICENSE-MIT-mattcl └── UNLICENSE └── utils ├── test.bqn └── utils.bqn /.gitignore: -------------------------------------------------------------------------------- 1 | *official.txt 2 | *official.bqn 3 | *.so -------------------------------------------------------------------------------- /2020/BQN/1.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Num,MDSlash⟩ ← •Import "../../utils/utils.bqn" 3 | l ← Num¨ •FLines ≠◶"1.in"‿⊑ •args 4 | 5 | # basic brute force; fast enough, but we can do better 6 | # F0 ← {×´ l⊏˜⊑MDSlash 2020=+⌜´ 𝕩⥊'L'=•FLines ≠◶"11.in"‿⊑ •args 4 | 5 | _run ← { GetCounts _𝕣 max: 6 | +´⥊{ 𝕊o: 7 | c ← GetCounts o 8 | (s∧ci # instruction type & arguments 13 | 14 | UB ← (2| ·⌊ (⌽÷2⋆↕36)⊸×) # unpack binary 15 | 16 | wm ← {𝕩⊏˜¯1+𝕩⍋↕≠it} /¬it # map from write instr to the nearest preceding mask 17 | wim ← (it/wm)⊏ia # write instruction masks 18 | wimX ← 0⊑¨wim # write ins mask keep/float 19 | wimS ← 1⊑¨wim # write ins mask set 20 | 21 | wil ← it/ia # write ins arguments 22 | wia ← 0⊑¨wil # write ins addresses 23 | wiv ← 1⊑¨wil # write ins values 24 | 25 | { 26 | wl ← ∊⌾⌽ wia # mask of which Write instructions are the Last for this address 27 | •Show +´RB¨ (wl/wimS)∨(wl/wimX)∧UB¨ wl/wiv 28 | } 29 | 30 | { 31 | e ← 2⋆+´¨wimX # number of sub-writes 32 | ewa ← ∾((wimS ∨ UB¨ wia)∧¬wimX) {𝕨<⊸∨ ⥊↕1+𝕩}¨ wimX # expanded write addresses 33 | ewv ← e/wiv # expanded write values 34 | wl ← ∊⌾⌽ ewa # mask of which Write instructions are the Last for this address 35 | •Show +´wl/ewv 36 | } -------------------------------------------------------------------------------- /2020/BQN/15.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Ints⟩ ← •Import "../../utils/utils.bqn" 3 | l ← Ints •FChars ≠◶"15.in"‿⊑ •args 4 | 5 | # •Show ¯1⊑{𝕩∾⊑ (≠𝕩)⊸≠⊸× 1+ (1↓⌽𝕩)⊐¯1⊑𝕩}⍟(2020-≠) l 6 | 7 | F ← { 8 | m ← 0∾˜ ∊⌾⌽ ¯1↓l 9 | p ← (/m)⌾((m/l)⊸⊏) (1+𝕩⌈´l)⥊¯1+2⋆31 10 | (¯1⊑l) { 11 | t←𝕩⊑p 12 | p 𝕨⌾(𝕩⊸⊑)↩ 13 | 0⌈𝕨-t 14 | }´⌽ ¯1+(≠l)↓↕𝕩 15 | } 16 | 17 | •Show F 2020 18 | •Show F 30000000 -------------------------------------------------------------------------------- /2020/BQN/15.in: -------------------------------------------------------------------------------- 1 | 2,15,0,9,1,20 -------------------------------------------------------------------------------- /2020/BQN/16.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨SplitF, Ints⟩ ← •Import "../../utils/utils.bqn" 3 | g0‿g1‿g2 ← "" SplitF •FLines ≠◶"16.in"‿⊑ •args 4 | 5 | fields ← {⟨𝕩↑˜⊑𝕩⊐':' ⋄ Ints 𝕩⟩}¨ g0 6 | mine ← Ints 1⊑g1 7 | other ← >Ints¨ 1↓g2 8 | 9 | fN ← ⊑¨ fields 10 | fV ← 1⊑¨ fields 11 | 12 | fS‿fE ← <˘⍉∧∘‿2⥊∾fV 13 | fE+↩ 1 # inclusive-exclusive ranges 14 | cEm ← fE⌊(¯1⊑fE)«fS # cut off ends covered by the next range 15 | okNums ← 2|(⥊⍉fS≍cEm)⍋other 16 | 17 | •Show +´ (¬okNums) /○⥊ other 18 | 19 | okMask ← ∧˝˘ okNums 20 | okCols ← <˘ ⍉okMask/other 21 | [s0,e0,s1,e1] ← ⍉>fV 22 | p ← {∧˝((𝕩≥⌜s0)∧𝕩≤⌜e0) ∨ (𝕩≥⌜s1)∧𝕩≤⌜e1}¨ okCols # for each column, its possibilities 23 | counts ← +´¨p # possibility count for each column 24 | o ← ⍋counts # order by counts 25 | pos ← ⋈⁼¨ o⊔⍋+˝ {!∨`⌾⌽⊸≡𝕩 ⋄ 𝕩} ¬>o⊏p # for each column, its position in 26 | •Show ×´ ("departure"⊸{𝕨≡(≠𝕨)↑𝕩}¨ pos⊏fN)/mine 27 | -------------------------------------------------------------------------------- /2020/BQN/17.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨SplitF, Ints⟩ ← •Import "../../utils/utils.bqn" 3 | l ← >'#'=•FLines ≠◶"17.in"‿⊑ •args 4 | 5 | Iter ← { 6 | x ← (2+≢𝕩)↑𝕩 7 | s ← x⌽˜𝕨⥊¯1 8 | n ← s -˜ x {+´¯2‿¯1‿0⌽⎉𝕨¨< 𝕩}´ 1+↕𝕨 9 | (n=3) ∨ s∧n=2 10 | } 11 | 12 | •Show +´⥊ 3 Iter⍟6 ≍l 13 | •Show +´⥊ 4 Iter⍟6 ≍≍l -------------------------------------------------------------------------------- /2020/BQN/17.in: -------------------------------------------------------------------------------- 1 | ##...#.# 2 | #..##..# 3 | ..#.#### 4 | .#..#... 5 | ######## 6 | ######.# 7 | .####..# 8 | .###.#.. -------------------------------------------------------------------------------- /2020/BQN/18.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | l ← •FLines ≠◶"18.in"‿⊑ •args 3 | 4 | BQNify ← {𝕩 '×'¨⌾(('*'=𝕩)⊸/) ↩} 5 | 6 | l BQNify¨ ↩ 7 | 8 | P1 ← { 9 | {i←"()"⊐𝕩 ⋄ (")("⊏˜2⊸≠⊸/i)⌾((i≠2)⊸/)𝕩} (⍒+`∨⟜»𝕩∊"+×()")⊏𝕩 10 | } 11 | 12 | P2 ← { 13 | 𝕩 {𝕩/˜1+𝕩∊"()"} ↩ 14 | m ← 𝕩='×' 15 | x ← 1+2×m 16 | "("∾ ")"∾˜ (")("⥊˜≠)⌾((<`x/m)⊸/) x/𝕩 17 | } 18 | 19 | EvalList ← {•BQN "⟩"∾˜"⟨"∾ ∾∾⟜','¨ 𝕩} 20 | •Show +´ EvalList P1¨ l 21 | •Show +´ EvalList P2¨ l -------------------------------------------------------------------------------- /2020/BQN/19.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Num, Ints, SplitF, Split⟩ ← •Import "../../utils/utils.bqn" 3 | rules0‿messages ← ⟨⟩ SplitF •FLines ≠◶"19.in"‿⊑ •args 4 | 5 | r ← { 6 | n‿v ← (⊑𝕩⊐':')(↑⋈2⊸+⊸↓)𝕩 7 | { 8 | '"'=⊑v? ⟨Num n, ⊑1↓¯1↓v⟩; 9 | ⟨Num n, Ints¨ '|'Split v⟩ 10 | } 11 | }¨ rules0 12 | 13 | rl ← 1=∘⊑¨r # rules with list 14 | [rn,ra] ← ⍉>r # rule name, arguments 15 | 16 | rm ← ra⌾(rn⊸⊏) @⥊˜1+⌈´rn # map from rule name to argument 17 | 18 | Get ← {v←𝕩⊑rm ⋄ {=v? ∾ (⥊·∾⌜´ Get¨)¨ v; ⋈⋈v}} 19 | # •Show +´(Get 0)∊messages # initial p1 impl; slower than using p2's method 20 | 21 | ! ⟨8‿11⟩ ≡ 0⊑rm # assumption 1: "0: 8 11" rule 22 | 23 | # 0: 8 11 # said assumption 24 | # 8: 42 | 42 8 # given 25 | # 11: 42 31 | 42 11 31 # given 26 | 27 | # 8: 42*w # w≥1 28 | # 11: 42*y 31*y # y≥1 29 | # 0: 42*w 42*y 31*y 30 | # 0: 42*x 31*y # x≥y+1; , x≥2 31 | 32 | ox ← Get 42 33 | oy ← Get 31 34 | 35 | gl ← ≠⊑ox # group length 36 | ! {∧´ ∧´¨ gl = ≠¨¨𝕩} ox‿oy # assumption 2: variations for 42 and 31 all have the same length 37 | 38 | ox >↩ 39 | oy >↩ 40 | 41 | messages (0=gl|≠¨)⊸/ ↩ # not required for my input (and probably everyone else's), but not much more difficult to do than an assuption 42 | 43 | Unjoin ← {((≠¨𝕨)/↕≠𝕨) ⊔ 𝕩} # TODO ⌾∾ 44 | 45 | mgs ← ∘‿gl⊸⥊¨ messages # message groups 46 | mx ← mgs Unjoin ox ∊˜ ∾mgs # message group has x 47 | my ← mgs Unjoin oy ∊˜ ∾mgs # message group has y 48 | v ← my {nx←+´∧`𝕩 ⋄ ny←+´∧`⌽𝕨 ⋄ (nx≥2) ∧ (ny≥1) ∧ (nx≥ny+1) ∧ (nx+ny)≥≠𝕩}¨ mx 49 | 50 | •Show +´ my{3=≠𝕩? (∧´2↑𝕩) ∧ 2⊑𝕨;0}¨mx # part 1: 42 42 31 51 | •Show +´ v # part 2 -------------------------------------------------------------------------------- /2020/BQN/2.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Ints⟩ ← •Import "../../utils/utils.bqn" 3 | l ← {p←⊑𝕩⊐':' ⋄ (Ints p↑𝕩)∾⟨𝕩⊑˜p-1, 𝕩↓˜2+p⟩}¨ •FLines ≠◶"2.in"‿⊑ •args 4 | 5 | •Show +´ {s‿e‿c‿p: (≥⟜s∧≤⟜e) +´c=p}¨ l 6 | •Show +´ {s‿e‿c‿p: 1=+´c=(s‿e-1)⊏p}¨ l -------------------------------------------------------------------------------- /2020/BQN/20.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Ints, Count, Split, SplitF⟩ ← •Import "../../utils/utils.bqn" 3 | l ← ⟨⟩ SplitF •FLines ≠◶"20.in"‿⊑ •args 4 | 5 | n ← ⋈⁼∘Ints∘⊑¨ l 6 | v ← '#'=>¨ 1↓¨l 7 | 8 | edges ← {>⊏¨ ⌽∘⍉⍟(↕4) 𝕩}¨ v # order: top, right, bottom, left; TODO do less stupidly 9 | all ← ∾ ∾⟜(⌽˘)¨ edges # order: t,r,b,l, reversed t,r,b,l 10 | allIdx ← 8/↕≠l # group index for each edge 11 | 12 | RotCW ← {(8×𝕩⊑allIdx) + (8|𝕩) ⊑ 1‿2‿3‿0‿7‿4‿5‿6} 13 | RotCCW ← {(8×𝕩⊑allIdx) + (8|𝕩) ⊑ 3‿0‿1‿2‿5‿6‿7‿4} 14 | # Opposite ← {(8×𝕩⊑allIdx) + (8|𝕩) ⊑ 2‿3‿0‿1‿6‿7‿4‿5} 15 | Opposite ← {(8×𝕩⊑allIdx) + (8|𝕩) ⊑ 6‿7‿4‿5‿2‿3‿0‿1} 16 | 17 | corners ← / 2=+´¨2= (¯1+`(4↑1)⥊˜≠)⊸⊔ all Count○(<˘)˜ ∾edges # TODO remove <˘ after Count makes sense of it 18 | 19 | # •Show ('0'+<˘all)≍˘allMap 20 | next ← ⊑¨ ∾⟜¯1¨ {i←⊐all ⋄ 𝕩⊸≠⊸(/¨) i⊏i⊔𝕩}↕≠all # for each edge, the adjacent ones' index 21 | 22 | # •Show next≍˘˜↕≠next 23 | 24 | •Show ×´ corners⊏n # no need to actually reconstruct the image yet 25 | 26 | GetNext ← { 𝕊i: # takes edge number, gets adjacent tile & gives its next edge 27 | nt ← i⊑next # next tile matching edge 28 | nt≢¯1? Opposite nt; ¯1 29 | } 30 | 31 | # horrible mess follows: 32 | 33 | start ← ⊑corners # top left corner tile number 34 | startEdge ← {!2=≠𝕩⋄⊑𝕩} {∧´¯1≠next⊏˜RotCW⊸⋈ 𝕩}¨⊸/ (8×start)+↕8 # edge of that which also continues after RotCW 35 | NextList ← {¯1↓ (⊢∾⟜< ·GetNext ¯1⊑⊢)•_while_(¯1≢¯1⊑⊢) ⋈𝕩} 36 | leftCol ← NextList startEdge 37 | 38 | gridNs ← >{NextList RotCW 𝕩}¨ leftCol 39 | 40 | grid0 ← ⌽(8|gridNs) {𝕨◶⟨⍉⌽,⊢,⌽⍉,⌽⌽˘,⌽∘⍉∘⌽,⌽,⍉,⌽˘⟩ 𝕩}¨ (gridNs⊏allIdx)⊏v 41 | ! {𝕩≡2/˘2/(2|↕¨≢𝕩)/𝕩} ¯2‿¯2↓1‿1⌽∾{0‿¯1⊸⊏˘0‿¯1⊏𝕩}¨ grid0 # verify that we've gotten the proper grid 42 | grid ← ∾{¯1‿¯1↓1‿1↓𝕩}¨ grid0 # and now with removed borders 43 | 44 | seaMonster ← '#'=(@+10)Split 1↓" 45 | # 46 | # ## ## ### 47 | # # # # # # " 48 | seaMonsterW ← ≠⊑seaMonster 49 | seaMonsterIdxs ← /¨ seaMonster 50 | 51 | # •Show grid⊏".#" 52 | # •Show ".#"⊏˜ >seaMonster 53 | 54 | Find ← { 55 | offs ← »⍟(↕seaMonsterW) 𝕩 56 | t ← {∧´𝕩⊏offs}¨ seaMonsterIdxs # for each sea monster row, where it matches in the grid 57 | a ← ∧´{(𝕩⥊0)⊸»˘𝕨}¨⟜(↕≠) t 58 | b ← ∧´{(𝕩⥊0)⊸«˘𝕨}¨⟜(↕≠) t 59 | a ∨ b 60 | } 61 | 62 | a ← Find grid 63 | b ← ⍉Find ⍉grid 64 | 65 | •Show (+´⥊grid) - (+´≠¨ seaMonsterIdxs) × +´+´∘⥊¨ a‿b # assumes sea monsters don't overlap -------------------------------------------------------------------------------- /2020/BQN/21.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Split⟩ ← •Import "../../utils/utils.bqn" 3 | l ← •FLines ≠◶"21.in"‿⊑ •args 4 | [is0,as0] ← ⍉>{' 'Split𝕩}‿{1↓¨','Split ¯1↓8↓𝕩}⊸({𝕎𝕩}¨)¨ '(' Split¨ l # ingredients, allergens 5 | 6 | ins ← ⍷∾is0 # ingredient names 7 | ans ← ⍷∾as0 # allergen names 8 | 9 | is ← ins⊸⊐⌾∾ is0 # ingredient indexes instead of names 10 | as ← ans⊸⊐⌾∾ as0 # allergen indexes instead of names 11 | 12 | ip ← (≠ins) ↑⟜(/⁼)¨ is # for each food, whether each ingredient is present 13 | aw ← {(∾𝕩)⊔/≠¨𝕩} as # which food each allergen is present in 14 | 15 | ao ← (∧´⊏⟜ip)¨ aw # for each allergen, which ingredients it could be in 16 | # •Show ⟨>ip, ≍˘aw, >ao⟩ 17 | 18 | ic ← /⁼∾is # ingredient counts 19 | ok ← ∨´ao # ingredients which may contain allergens 20 | •Show +´(¬ok)/ic 21 | 22 | ao2 ← ok⊸/¨ ao # filter out the impossible ingredient options 23 | ao2to1 ← /ok # the ao-index for each ao2 24 | where ← /¨ <˘⍉>ao2 # which food potentially is each allergen 25 | done ← 0¨⊑ao2 # which foods are completed; mutated 26 | count ← +´¨ ao2 # count of possible allergens for each food; mutated 27 | a2i ← ⍋⊸⊏˝⍉>{𝕊: 28 | n ← ⊑count⊐1 # allergen with only one ingredient option left 29 | m ← (¬done)∧n⊑ao2 # mask of that only ingredient 30 | i ← ⋈⁼ / m # and its index 31 | count -⟜1⌾((i⊑where)⊸⊏)↩ 32 | done∨↩ m 33 | n‿i 34 | }¨ ↕≠count 35 | 36 | •Out {𝕨∾','∾𝕩}´ ans ⍋⊸⊏ (a2i⊏ao2to1)⊏ins -------------------------------------------------------------------------------- /2020/BQN/22.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Num, SplitF⟩ ← •Import "../../utils/utils.bqn" 3 | cards ← Num¨¨ 1↓¨ ⟨⟩ SplitF •FLines ≠◶"22.in"‿⊑ •args 4 | 5 | P1 ← { 6 | c ← ⊑¨𝕩 7 | w ← <´c 8 | ∾⟜(⌽⍟w c)⌾(w⊸⊑) 1↓¨ 𝕩 9 | } 10 | 11 | Score ← {+´ (⌽×1+↕∘≠) ∾𝕩} 12 | 13 | •Show Score P1•_while_{∧´0<≠¨𝕩} cards 14 | 15 | Round ← { 16 | c ← ⊑¨ 𝕩 17 | w←{ 18 | ∧´c<≠¨𝕩? # recurse 19 | <´ ≠¨ Game c↑¨ 1↓¨𝕩 20 | ; # don't recurse 21 | <´ c 22 | }𝕩 23 | ∾⟜(⌽⍟w c)⌾(w⊸⊑) 1↓¨ 𝕩 24 | } 25 | 26 | # Hash ← ⊢ ⋄ In ← {𝕨⊸≡¨ 𝕩} 27 | Hash ← 1e9⊸×⊸+´•Hash ⋄ In ← = # TODO hashmap 28 | 29 | Game ← Hash⊸{ 30 | p ← ⋈𝕨 31 | r ← { 32 | n ← Round 𝕩 33 | nh ← Hash n 34 | ¬∨´nh In p? # not encountered yet 35 | p∾↩ {RB¨ "BR"=7(↑⋈↓)𝕩}¨ l 6 | i ← (⊢+8×⊣)´ p 7 | 8 | •Show ⌈´i 9 | •Show ⊑/{(¬𝕩∊i) ∧ ((𝕩-1)∊i) ∧ (𝕩+1)∊i} ↕⌈´i -------------------------------------------------------------------------------- /2020/BQN/6.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨SplitF⟩ ← •Import "../../utils/utils.bqn" 3 | l ← ⟨⟩ SplitF •FLines ≠◶"6.in"‿⊑ •args 4 | 5 | •Show +´ {+´∊∾𝕩}¨ l 6 | •Show +´ {≠(∊/⊣)´𝕩}¨ l -------------------------------------------------------------------------------- /2020/BQN/7.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Split,Num,_fx_⟩ ← •Import "../../utils/utils.bqn" 3 | l ← •FLines ≠◶"7.in"‿⊑ •args 4 | 5 | p ← { 6 | ws ← ' ' Split 𝕩 7 | t0 ← 2↑ws 8 | W ← (⊣∾' '∾⊢)´ 9 | ⟨W t0, {a‿b‿c←ws⊏˜4‿5‿6+4×𝕩 ⋄ ⟨Num a, W b‿c⟩}¨ ↕⌊4÷˜¯4+≠ws⟩ 10 | }¨ l 11 | 12 | s ← ⊑¨p 13 | n‿c ← <˘⍉> {(⊑¨⋈1⊑¨⊢) 𝕩}¨ 1⊑¨p 14 | ns ← s ⋄ ! ns ≡ ⍷s∾∾c 15 | si ← ns⊐s 16 | ci ← ns⊸⊐¨ c 17 | ri ← (∾ci) ⊔ (≠¨ci)/↕≠ci # reverse indices 18 | 19 | shiny ← ⊑ns⊐<"shiny gold" 20 | 21 | •Show ¯1+≠ {⍷𝕩∾∾𝕩⊏ri} _fx_≢ ⋈shiny 22 | 23 | •Show ⊑{s‿m‿l: ln←l⊏n ⋄ ⟨s+´∾m×ln ⋄ ((≠¨ln)/m)×∾ln ⋄ ∾l⊏ci⟩}•_while_{s‿m‿l:×≠l} ⟨0, ⋈1, ⋈shiny⟩ -------------------------------------------------------------------------------- /2020/BQN/8.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Num⟩ ← •Import "../../utils/utils.bqn" 3 | l ← •FLines ≠◶"8.in"‿⊑ •args 4 | 5 | ins0‿arg0 ← <˘⍉>{⟨3↑𝕩, -⍟('-'=4⊑𝕩) Num 5↓𝕩⟩}¨ l 6 | ins0 ⊐˜↩ "nop"‿"acc"‿"jmp" 7 | 8 | Eval ← { ins‿arg: 9 | { prev‿acc‿pc: 10 | { 11 | ⊑pc∊prev? acc‿pc; 12 | pc≥≠ins? {pc≡≠ins? ⋈acc; acc‿pc}; 13 | prev∾↩ pc 14 | argv ← pc⊑arg 15 | ⊢◶⟨ 16 | {𝕊: ⟨prev, acc, pc+1⟩} 17 | {𝕊: ⟨prev, acc+argv, pc+1⟩} 18 | {𝕊: ⟨prev, acc, pc+argv⟩} 19 | ⟩ pc⊑ins 20 | } 21 | }•_while_{3≡≠𝕩} ⟨⟩‿0‿0 22 | } 23 | 24 | •Show ⊑ {!2=≠𝕩⋄𝕩} Eval ins0‿arg0 25 | 26 | •Show ⋈⁼ ⋈⁼ (1=≠¨)⊸/ { 27 | Eval ⟨2⊸-⌾(𝕩⊸⊑) ins0, arg0⟩ 28 | }¨ /ins0∊0‿2 -------------------------------------------------------------------------------- /2020/BQN/9.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Ints, MDSlash⟩ ← •Import "../../utils/utils.bqn" 3 | l ← Ints •FChars ≠◶"9.in"‿⊑ •args 4 | w ← 25 5 | 6 | 7 | weak ← l⊑˜w+ ⋈⁼/ ¬∨˝˘ (w↓l) = (⥊+⌜˜)˘ ¯1↓w↕l 8 | •Show weak 9 | 10 | e‿s ← ⊑ MDSlash weak= -⌜˜ 0»+`l 11 | •Show (⌊´+⌈´) s↓e↑l -------------------------------------------------------------------------------- /2020/BQN/test.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | { 3 | exp ⇐ ⟨ 4 | ⟨"1", 1019371‿278064990⟩ 5 | ⟨"2", 439‿584⟩ 6 | ⟨"3", 198‿5140884672⟩ 7 | ⟨"4", 222‿140⟩ 8 | ⟨"5", 842‿617⟩ 9 | ⟨"6", 6735‿3221⟩ 10 | ⟨"7", 161‿30899⟩ 11 | ⟨"8", 1816‿1149⟩ 12 | ⟨"9", 57195069‿7409241⟩ 13 | ⟨"10", 2030‿42313823813632⟩ 14 | ⟨"11", 2243‿2027⟩ 15 | ⟨"12", 796‿39446⟩ 16 | ⟨"13", 2045‿402251700208309⟩ 17 | ⟨"14", 10452688630537‿2881082759597⟩ 18 | ⟨"15", 1280‿651639⟩ 19 | ⟨"16", 23044‿3765150732757⟩ 20 | ⟨"17", 295‿1972⟩ 21 | ⟨"18", 21993583522852‿122438593522757⟩ 22 | ⟨"19", 241‿424⟩ 23 | ⟨"20", 174206308298779‿2409⟩ 24 | ⟨"21", 2595‿"thvm,jmdg,qrsczjv,hlmvqh,zmb,mrfxh,ckqq,zrgzf"⟩ 25 | ⟨"22", 35202‿32317⟩ 26 | ⟨"23", "59374826"‿66878091588⟩ 27 | ⟨"24", 317‿3804⟩ 28 | ⟨"25", ⟨18433997⟩⟩ 29 | ⟩ 30 | 31 | path ⇐ •path 32 | args ⇐ •args 33 | all ⇐ •Repr¨ 1+↕25 34 | most ⇐ (¬∊⟜"15"‿"23")⊸/all 35 | orig ⇐ ⟨⟩ 36 | } •Import "../../utils/test.bqn" 37 | -------------------------------------------------------------------------------- /2021/BQN/1.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Num⟩ ← •Import "../../utils/utils.bqn" 3 | l ← Num¨ •FLines ≠◶"1.in"‿⊑ •args 4 | •Show +´<´˘2↕l 5 | •Show +´<´˘2↕+´˘3↕l 6 | -------------------------------------------------------------------------------- /2021/BQN/10.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | l ← •FLines ≠◶"10.in"‿⊑ •args 3 | 4 | p1‿p2 ← <˘⍉>{ 5 | s ← "[]()<>{}" 6 | i ← s⊐𝕩 7 | o ← 2|i 8 | d ← (+`¯1⋆o) - ¬o 9 | oob ← 2⥊≠𝕩 10 | score ← 57‿3‿25137‿1197‿0⊑˜ 2÷˜¯1+ (i∾9)⊑˜ ⌊´(⊔d) {(1+2× 0⊑∘⊐˜ =´˘∘‿2⥊ 𝕩∾9⥊˜2|≠𝕩) ⊑ 𝕨∾oob}¨ d⊔⌊i÷2 11 | ⟨ 12 | score 13 | {𝕊: 0 {𝕨+5×𝕩}´⌽ " )]}>"⊐ s⊏˜1+ (∾(-2|≠)⊸↑¨ ⌽⊔d) ⊏ i}¨ ↕score=0 14 | ⟩ 15 | }¨ l 16 | 17 | •Show +´ p1 18 | •Show (⌊0.5×≠)⊸⊑∧ ∾p2 -------------------------------------------------------------------------------- /2021/BQN/10_explanation.bqn: -------------------------------------------------------------------------------- 1 | # start: 2 | 57‿3‿25137‿1197‿0⊑˜ 2÷˜¯1+ (i∾9)⊑˜ ⌊´(⊔d) {(1+2× 0⊑∘⊐˜ =´˘∘‿2⥊ 𝕩∾9⥊˜2|≠𝕩) ⊑ 𝕨∾oob}¨ d⊔⌊i÷2 3 | (⊔d) # position per depth: each item is a list of positions of characters at the given depth 4 | d⊔⌊i÷2 # bracket type per depth: each item is a list of bracket types at the current depth 5 | (⊔d) {(1+2× 0⊑∘⊐˜ =´˘∘‿2⥊ 𝕩∾9⥊˜2|≠𝕩) ⊑ 𝕨∾oob}¨ d⊔⌊i÷2 # execute the block with 𝕨 as the positions, and 𝕩 as the types, per depth 6 | # example: 7 | # str: <()([])[}< 8 | # index: 0123456789 9 | # depth: 0111221111 10 | # i: 4232013074 11 | # type: 2111001032 12 | 13 | # consider depth 1: 14 | # ()( )[}< 15 | # 𝕨: 123 6789 → 1‿2‿3‿6‿7‿8‿9 (subset of the "index" above) 16 | # 𝕩: 111 1032 → 1‿1‿1‿1‿0‿3‿2 (subset of "type" above) 17 | # string: ()()[}< - for reference; we don't use it anywhere; the nature of depth guarantees that even indices are opening brackets and odd ones are closing ones 18 | # inner dfn: 19 | {(1+2× 0⊑∘⊐˜ =´˘∘‿2⥊ 𝕩∾9⥊˜2|≠𝕩) ⊑ 𝕨∾oob} # whole thing for reference 20 | 𝕩∾9⥊˜2|≠𝕩 # append a 9 to 𝕩 if the length is odd: 1‿1‿1‿1‿0‿3‿2‿9 21 | ∘‿2⥊ # reshape into two columns: [1‿1,1‿1,0‿3,2‿9] 22 | =´˘ # check if columns are equal: 1‿1‿0‿0 23 | 0⊑∘⊐˜ # find index of first mismatch: 2; This gives us the first mismatched character at the current depth 24 | # in the string "()()[}<", that's pointing at the 3rd bracket pair 25 | 1+2× # to convert to the actual index of "}", we need to double the index, and add one to point at the erroring closing brace and not the opening one 26 | ( 1+2× ) ⊑ 𝕨∾oob # pick that'th element of the indexes 27 | # or return OOB if there were no mismatches and thus ⊐ returned one-past-last 28 | # post-processing: 29 | ⌊´ # smallest erroring index from all of the ones we've seen 30 | (i∾9)⊑˜ # pick that from the indexes, returning the bracket character index 31 | 2÷˜¯1+ # convert the bracket character index to a type index 32 | 57‿3‿25137‿1197‿0⊑˜ # and get that-th element of the lookup table -------------------------------------------------------------------------------- /2021/BQN/11.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | l ← '0'-˜>•FLines ≠◶"11.in"‿⊑ •args 3 | 4 | Step ← { 5 | n←𝕩+1 6 | f←(≢𝕩)⥊0 7 | { 8 | m ← (n≥10)∧¬f 9 | f∨↩ m 10 | n+↩ 0(«+»+⊢)˘ (0¨⊏)⊸(«+»+⊢) m 11 | ∨´⥊m?𝕊@;@ 12 | }@ 13 | ⟨n׬f⋄f⟩ 14 | } 15 | •Show 1⊑ (⊢ +⟜(+´∘⥊)⌾(1⊸⊑) Step∘⊑)⍟100 ⟨l ⋄ 0⟩ 16 | •Show ⊑ {n‿c‿f: ⟨n+1⟩∾Step c}•_while_{n‿p‿f: ¬∧´⥊f} ⟨0 ⋄ l ⋄ (≢l)⥊0⟩ -------------------------------------------------------------------------------- /2021/BQN/11.in: -------------------------------------------------------------------------------- 1 | 8826876714 2 | 3127787238 3 | 8182852861 4 | 4655371483 5 | 3864551365 6 | 1878253581 7 | 8317422437 8 | 1517254266 9 | 2621124761 10 | 3473331514 -------------------------------------------------------------------------------- /2021/BQN/12.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Split⟩ ← •Import "../../utils/utils.bqn" 3 | rs‿re ← <˘⍉>'-'Split¨•FLines ≠◶"12.in"‿⊑ •args 4 | 5 | u ← ⍷rs∾re # unique caves 6 | big ← ('A'⊸≤∧≤⟜'Z') ⊑¨u # whether each of them are big 7 | sp‿ep ← u⊐"start"‿"end" # start and end cave numbers 8 | 9 | s‿e ← ∾¨⟜⌽rs‿re ⊐˜¨ {𝕊: # iterate through all caves 16 | cc ← 𝕩⊑c # get ones adjacent to the current one 17 | lm ← ∨´l⊸=¨cc # mask of paths adjacent to current ones; currently faster than l∊cc 18 | f ← (𝕩⊑big) ∨ ¬𝕩⊑v # always fine to add - either big, or not already present 19 | o ← lm ∧ f ∨ a # okay to add 20 | ⟨o/a∧f ⋄ (+´o)⥊𝕩 ⋄ 1¨⌾(𝕩⊸⊑) o⊸/¨ v⟩ 21 | }¨ mid 22 | done ← +´l∊ep⊑c # count number of rows that are adjacent to the end 23 | ⟨r+done ⋄ ∾as ⋄ ∾ls ⋄ ∾¨ <˘⍉>vs⟩ 24 | } 25 | 26 | •Show¨ {⊑ Step•_while_{0≠≠1⊑𝕩} ⟨0 ⋄ ⟨𝕩⟩ ⋄ ⟨sp⟩ ⋄ ⋈¨ sp=↕≠u⟩}¨ 0‿1 -------------------------------------------------------------------------------- /2021/BQN/12.in: -------------------------------------------------------------------------------- 1 | start-YA 2 | ps-yq 3 | zt-mu 4 | JS-yi 5 | yq-VJ 6 | QT-ps 7 | start-yq 8 | YA-yi 9 | start-nf 10 | nf-YA 11 | nf-JS 12 | JS-ez 13 | yq-JS 14 | ps-JS 15 | ps-yi 16 | yq-nf 17 | QT-yi 18 | end-QT 19 | nf-yi 20 | zt-QT 21 | end-ez 22 | yq-YA 23 | end-JS -------------------------------------------------------------------------------- /2021/BQN/13.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Ints, Split⟩ ← •Import "../../utils/utils.bqn" 3 | l ← •FLines ≠◶"13.in"‿⊑ •args 4 | 5 | p‿ins ← (l⊑∘⊐<⟨⟩)(↑⋈↓)l 6 | 7 | ins ↩ {⟨11⊑𝕩⋄⊑Ints𝕩⟩}¨ 1↓ins 8 | px‿py←<˘⍉> Ints¨ p 9 | 10 | Step ← { 11 | 'y'‿n𝕊px‿py: ⟨px⋄n-|n-py⟩; 12 | 'x'‿n𝕊px‿py: ⟨ n-|n-px⋄py⟩ 13 | } 14 | 15 | •Show +´∊⍉> (⊑ins) Step px‿py 16 | ep ← px‿py Step´⌽ ins 17 | •Out˘ " #"⊏˜ ⍉ 1¨⌾((<˘⍉>ep)⊸⊑) 0⥊˜ 1+ ⌈´¨ ep -------------------------------------------------------------------------------- /2021/BQN/14.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | l ← •FLines ≠◶"14.in"‿⊑ •args 3 | chrs ← (∊⟜('A'+↕26))⊸/ ⍷∾l # unique characters 4 | w ← ≠chrs 5 | s ← chrs⊐⊑l # initial state to its indexes 6 | 7 | tp ← chrs⊸⊐¨ 2↑¨ 2↓l # transformation pattern 8 | tn ← chrs⊐¯1⊑¨ 2↓l # transformation new char index 9 | map ← tn⌾(tp⊸⊑) (¯1⥊˜⋈˜w) # map to the new middle char from the pair given as x/y coords 10 | 11 | st0 ← (0⥊˜≢map) {1⊸+⌾(𝕨⊸⊑)𝕩}˝ 2↕s # initial state 12 | k ← (⊔⥊(w×↕≠map) + map) {𝕨∾¨○((𝕨⌈○≠𝕩)⊸↑)𝕩} ⊔⥊(↕≠map) +⎉1 w×map # which indexes to sum for this one 13 | s0 ← (w⥊0) {1⊸+⌾(𝕨⊸⊑)𝕩}´ s # initial state char counts 14 | •Show¨ 10‿40{-´¯1‿0⊏∧ s0+(𝕨-1)⊑𝕩}¨ <+`(+´¨(⥊map)⊔⊢)¨ {+´∘⊏⟜𝕩¨ k}⍟(↕40) ⥊st0 -------------------------------------------------------------------------------- /2021/BQN/14.in: -------------------------------------------------------------------------------- 1 | COPBCNPOBKCCFFBSVHKO 2 | 3 | NS -> H 4 | FS -> O 5 | PO -> C 6 | NV -> N 7 | CK -> B 8 | FK -> N 9 | PS -> C 10 | OF -> F 11 | KK -> F 12 | PP -> S 13 | VS -> K 14 | VB -> V 15 | BP -> P 16 | BB -> K 17 | BF -> C 18 | NN -> V 19 | NO -> F 20 | SV -> C 21 | OK -> N 22 | PH -> P 23 | KV -> B 24 | PN -> O 25 | FN -> V 26 | SK -> V 27 | VC -> K 28 | BH -> P 29 | BO -> S 30 | HS -> H 31 | HK -> S 32 | HC -> S 33 | HF -> B 34 | PC -> C 35 | CF -> B 36 | KN -> H 37 | CS -> N 38 | SP -> O 39 | VH -> N 40 | CC -> K 41 | KP -> N 42 | NP -> C 43 | FO -> H 44 | FV -> N 45 | NC -> F 46 | KB -> N 47 | VP -> O 48 | KO -> F 49 | CP -> F 50 | OH -> F 51 | KC -> H 52 | NB -> F 53 | HO -> P 54 | SC -> N 55 | FF -> B 56 | PB -> H 57 | FB -> K 58 | SN -> B 59 | VO -> K 60 | OO -> N 61 | NF -> B 62 | ON -> P 63 | SF -> H 64 | FP -> H 65 | HV -> B 66 | NH -> B 67 | CO -> C 68 | PV -> P 69 | VV -> K 70 | KS -> P 71 | OS -> S 72 | SB -> P 73 | OC -> N 74 | SO -> K 75 | BS -> B 76 | CH -> V 77 | PK -> F 78 | OB -> P 79 | CN -> N 80 | CB -> N 81 | VF -> O 82 | VN -> K 83 | PF -> P 84 | SH -> H 85 | FH -> N 86 | HP -> P 87 | KF -> V 88 | BK -> H 89 | OP -> C 90 | HH -> F 91 | SS -> V 92 | BN -> C 93 | OV -> F 94 | HB -> P 95 | FC -> C 96 | BV -> H 97 | VK -> S 98 | NK -> K 99 | CV -> K 100 | HN -> K 101 | BC -> K 102 | KH -> P -------------------------------------------------------------------------------- /2021/BQN/15.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | l ← >'0'-˜•FLines ≠◶"15.in"‿⊑ •args 3 | 4 | ll‿w‿sc‿c‿dirs←@‿@‿@‿@‿@ 5 | Init ← {𝕊: 6 | w ↩ ≠𝕩 7 | ll ↩ ⥊𝕩 8 | sc ↩ 0⌾⊑ (1+≠ll) ⥊ 10×+´ll 9 | c ↩ 1‿w 10 | dirs ↩ ⥊¨ ∾{⟨(≠ll)¨∘⊏⊸(«⋈»)𝕩 ⋄ (≠ll)(«˘⋈»˘)𝕩⟩} w‿w⥊↕≠ll 11 | } 12 | 13 | 14 | Step ← {𝕊: 15 | p ← c⊏sc 16 | nd ← c⊸⊏¨ dirs 17 | n ← (c⊏ll) + ⌊´ ⊏⟜sc¨ nd 18 | m ← n

‿<‿=´ rs 34 | } 35 | } 36 | 37 | res ← Packet s 38 | •Show vsum 39 | •Show res -------------------------------------------------------------------------------- /2021/BQN/16.in: -------------------------------------------------------------------------------- 1 | 4054460802532B12FEE8B180213B19FA5AA77601C010E4EC2571A9EDFE356C7008E7B141898C1F4E50DA7438C011D005E4F6E727B738FC40180CB3ED802323A8C3FED8C4E8844297D88C578C26008E004373BCA6B1C1C99945423798025800D0CFF7DC199C9094E35980253FB50A00D4C401B87104A0C8002171CE31C41201062C01393AE2F5BCF7B6E969F3C553F2F0A10091F2D719C00CD0401A8FB1C6340803308A0947B30056803361006615C468E4200E47E8411D26697FC3F91740094E164DFA0453F46899015002A6E39F3B9802B800D04A24CC763EDBB4AFF923A96ED4BDC01F87329FA491E08180253A4DE0084C5B7F5B978CC410012F9CFA84C93900A5135BD739835F00540010F8BF1D22A0803706E0A47B3009A587E7D5E4D3A59B4C00E9567300AE791E0DCA3C4A32CDBDC4830056639D57C00D4C401C8791162380021108E26C6D991D10082549218CDC671479A97233D43993D70056663FAC630CB44D2E380592FB93C4F40CA7D1A60FE64348039CE0069E5F565697D59424B92AF246AC065DB01812805AD901552004FDB801E200738016403CC000DD2E0053801E600700091A801ED20065E60071801A800AEB00151316450014388010B86105E13980350423F447200436164688A4001E0488AC90FCDF31074929452E7612B151803A200EC398670E8401B82D04E31880390463446520040A44AA71C25653B6F2FE80124C9FF18EDFCA109275A140289CDF7B3AEEB0C954F4B5FC7CD2623E859726FB6E57DA499EA77B6B68E0401D996D9C4292A881803926FB26232A133598A118023400FA4ADADD5A97CEEC0D37696FC0E6009D002A937B459BDA3CC7FFD65200F2E531581AD80230326E11F52DFAEAAA11DCC01091D8BE0039B296AB9CE5B576130053001529BE38CDF1D22C100509298B9950020B309B3098C002F419100226DC -------------------------------------------------------------------------------- /2021/BQN/17.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨SInts⟩ ← •Import "../../utils/utils.bqn" 3 | l ← ⊑•FLines ≠◶"17.in"‿⊑ •args 4 | xs‿xe‿ys‿ye←SInts l 5 | s←xs‿ys 6 | e←xe‿ye 7 | 8 | my ← 0 9 | n ← 0 10 | Step ← {⟨vy0 ⋄ p ⋄ v⟩: vx‿vy←v 11 | nx‿ny ← p+↩ v 12 | done ← ∧´(p≥s)∧p≤e 13 | my⌈´↩ done/vy0 14 | n+↩ +´done 15 | h ← (¬done)∧ny≥ys 16 | ⟨h/vy0 ⋄ (↩ »∨`ml 26 | mr ← »ml 27 | v+↩ «ml×v 28 | v+↩ »mr×v 29 | v×↩ ¬ml 30 | (¬mr)⊸/¨ v NHP d-ml 31 | ;𝕩 32 | } 33 | 34 | Spl ← {𝕊v‿d: 35 | m ← v>9 36 | ×+´m? 37 | m>↩ »∨`m 38 | i ← ⊑/m 39 | nv ← ∾⟨i↑v ⋄ (⌊NHP⌈)2÷˜i⊑v ⋄ (1+i)↓v⟩ 40 | nd ← (m+1)/d+m 41 | nv‿nd 42 | ;𝕩 43 | } 44 | 45 | Step ← {Spl⍟(𝕩⊸≡) Expl 𝕩} 46 | Simplify ← {⊑{p‿c:c NHP Step c}•_while_(≢´) 𝕩 NHP Step𝕩} 47 | 48 | Add ← {Simplify 1⊸+⌾(1⊸⊑) 𝕨∾¨𝕩} 49 | 50 | •Show Mag Add˜´⌽ l 51 | •Show ⌈´⥊ (≠⌜˜↕≠l) × Mag∘Add⌜˜ l -------------------------------------------------------------------------------- /2021/BQN/18.in: -------------------------------------------------------------------------------- 1 | [[[1,4],[1,6]],[0,[5,[6,3]]]] 2 | [[[8,2],5],[[[9,8],[3,5]],[2,1]]] 3 | [[[[6,2],[0,6]],[[9,8],[7,8]]],[[6,3],[[8,8],3]]] 4 | [[[1,[2,1]],[5,7]],[[[3,1],[3,1]],[[8,4],[8,5]]]] 5 | [[[[6,4],7],[[1,6],5]],[7,[9,5]]] 6 | [[7,[[5,3],[0,9]]],[[[6,2],[6,8]],[5,[5,7]]]] 7 | [9,5] 8 | [[[[7,8],[8,0]],[[3,8],[0,7]]],[[1,[1,2]],2]] 9 | [[[4,[5,5]],[[6,8],[4,3]]],[[9,9],[4,[3,6]]]] 10 | [[[[2,8],7],[[6,1],[1,0]]],[[6,2],9]] 11 | [[[8,1],3],[9,[[1,4],[4,1]]]] 12 | [[[[0,1],[3,9]],[[4,3],6]],[[[4,8],8],[[8,3],[9,5]]]] 13 | [[[[7,3],7],[[5,9],0]],7] 14 | [[[5,[1,6]],3],[[3,5],9]] 15 | [[[[2,5],[1,8]],[[6,5],[0,1]]],[[[4,1],1],[0,[9,6]]]] 16 | [[[4,8],[[3,6],[3,8]]],[[[2,3],3],[[9,8],[7,9]]]] 17 | [[[[5,6],0],[9,[4,4]]],[[[3,1],[3,6]],[[6,0],3]]] 18 | [[[[4,3],4],4],[[[1,6],7],[8,[6,0]]]] 19 | [[[0,2],1],5] 20 | [[[[7,2],[9,0]],[8,[0,1]]],2] 21 | [[[1,6],[[6,2],5]],[[1,[8,2]],[[9,8],7]]] 22 | [[[8,1],9],[[[4,3],2],[[2,9],6]]] 23 | [[[[9,4],0],[4,0]],4] 24 | [[[5,[2,8]],[[5,3],[6,4]]],[8,3]] 25 | [[0,5],[[[3,4],7],[[0,2],[9,1]]]] 26 | [[[8,[7,9]],[[1,8],6]],[[4,[6,0]],0]] 27 | [[[1,0],[[6,7],4]],[[[2,5],[9,7]],[[7,8],0]]] 28 | [[9,[[7,1],3]],[[[9,2],[4,3]],[2,[1,8]]]] 29 | [[[5,[9,6]],4],[1,[[9,2],[6,8]]]] 30 | [6,[[[6,1],7],6]] 31 | [[4,[[5,6],9]],[[9,[6,6]],[[6,1],[8,2]]]] 32 | [[1,[9,5]],[[[5,8],9],5]] 33 | [[[[6,6],[1,8]],6],[[[4,9],4],[8,[9,8]]]] 34 | [[[[6,5],[4,4]],[[0,2],8]],[[[0,6],[4,5]],3]] 35 | [[[1,[6,9]],[9,[5,8]]],[5,2]] 36 | [[2,[[2,8],[3,3]]],[[[1,9],9],6]] 37 | [[3,2],[9,[2,2]]] 38 | [4,[3,[6,[2,0]]]] 39 | [[[[1,0],4],3],[[0,9],[[9,8],[7,1]]]] 40 | [[[2,6],[3,8]],[[5,5],[2,3]]] 41 | [6,[[[8,8],4],[[8,1],[6,6]]]] 42 | [[[5,9],[5,3]],7] 43 | [[[5,[1,2]],[6,[7,2]]],[[[0,5],3],3]] 44 | [[8,[[7,3],[9,7]]],[[2,[3,9]],[[1,7],[5,7]]]] 45 | [8,[4,6]] 46 | [[[4,4],[[4,5],[2,5]]],[[[9,1],0],[[2,9],1]]] 47 | [[[2,[2,8]],9],[5,[6,9]]] 48 | [[[[4,1],5],[6,[2,7]]],[1,2]] 49 | [[[6,[3,5]],0],[[0,3],4]] 50 | [[[[3,2],[8,0]],[5,1]],[[[9,7],3],[[6,5],[2,6]]]] 51 | [[1,[0,[1,4]]],[[[8,6],[6,9]],[[4,9],8]]] 52 | [[[[5,2],[4,3]],[0,[3,5]]],[0,[1,7]]] 53 | [[[8,1],[3,[8,1]]],[[[7,9],[6,2]],[[0,8],2]]] 54 | [[[2,[9,7]],[[6,6],[2,7]]],[[8,[6,4]],0]] 55 | [[3,0],[[6,3],1]] 56 | [[[[5,5],2],[9,7]],[[0,[3,5]],7]] 57 | [[[[4,8],2],0],[[4,[7,9]],[6,6]]] 58 | [[[1,0],[[9,4],[8,8]]],2] 59 | [[[6,1],9],[5,2]] 60 | [[[7,[0,3]],[[5,5],7]],[5,[[0,5],[5,3]]]] 61 | [[[[8,0],4],[[5,5],[9,4]]],[[[9,0],[2,5]],[6,[8,1]]]] 62 | [[[7,8],[0,[5,4]]],[[[7,6],[0,9]],[7,2]]] 63 | [[[4,[0,2]],[3,[4,9]]],[[[4,7],8],3]] 64 | [[1,[5,[7,3]]],8] 65 | [[[[1,3],[6,8]],3],[[6,1],8]] 66 | [[[[7,9],5],[[6,2],4]],[[5,[6,9]],1]] 67 | [[2,[3,[9,3]]],[[6,[2,7]],[4,8]]] 68 | [7,[[6,2],[[6,7],[5,0]]]] 69 | [[[9,[8,6]],1],[[4,8],[[6,1],[0,1]]]] 70 | [[[[4,6],[4,0]],[[2,4],0]],[[[0,5],[9,8]],[[3,4],[2,5]]]] 71 | [9,[3,[[5,5],[3,1]]]] 72 | [[[5,[7,1]],3],[[[8,2],5],[[2,8],[0,0]]]] 73 | [[[[8,3],0],[[5,0],5]],[[3,[8,2]],[[8,2],3]]] 74 | [[4,[[9,4],5]],[[[1,6],[0,2]],[0,8]]] 75 | [[[0,0],[[1,8],2]],[[[1,8],1],[0,[0,8]]]] 76 | [[[6,[1,5]],5],[[2,[0,1]],9]] 77 | [[7,[2,[2,8]]],[4,[[1,1],5]]] 78 | [1,[[4,[0,5]],4]] 79 | [[3,[[3,1],[1,2]]],[[[5,3],8],[5,2]]] 80 | [[[3,[2,0]],6],[[9,3],[[3,0],[1,6]]]] 81 | [4,[[6,[5,9]],[[4,1],[6,6]]]] 82 | [8,[3,0]] 83 | [[[[5,3],[8,8]],[[5,1],4]],[[6,6],[8,2]]] 84 | [[1,[[7,1],5]],[[[2,3],7],[[7,6],0]]] 85 | [9,[[4,3],[[6,2],0]]] 86 | [[[[4,0],4],[1,7]],[[[3,8],8],[[9,1],1]]] 87 | [[[0,1],[9,9]],7] 88 | [[[[1,7],0],[1,5]],[1,[2,2]]] 89 | [[[[6,1],[3,3]],[6,[9,0]]],[[7,0],3]] 90 | [[[[6,1],[9,8]],[[2,2],2]],[8,[3,6]]] 91 | [[6,[5,0]],[7,[1,7]]] 92 | [[4,[[6,1],6]],[[2,5],7]] 93 | [8,[8,[[6,4],1]]] 94 | [[[[0,2],4],[[2,6],2]],0] 95 | [[2,[[6,1],9]],[[7,[0,5]],[5,[9,4]]]] 96 | [3,[[8,7],[[8,9],6]]] 97 | [[[[7,8],[1,1]],[[2,6],[3,7]]],4] 98 | [[[[6,1],1],5],5] 99 | [[9,[4,[6,6]]],[[5,1],[8,2]]] 100 | [[5,[[7,3],4]],9] -------------------------------------------------------------------------------- /2021/BQN/19.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨SInts⟩ ← •Import "../../utils/utils.bqn" 3 | 4 | l ← {{<˘⍉>SInts¨𝕩}¨ 2↓¨(¯1+`0=≠¨𝕩)⊔𝕩}⟨⟨⟩⟩∾ •FLines ≠◶"19.in"‿⊑ •args 5 | 6 | Rots ← { 7 | RotC←{x‿y:⟨-y⋄x⟩} 8 | fs←RotC⌾(1‿0‿1⊸/)⍟(↕4) 𝕩 9 | (∾RotC⌾(0‿1‿1⊸/)⍟(1‿3)¨ fs) ∾ ∾RotC⌾(1‿1‿0⊸/)⍟(↕4)¨ fs 10 | } 11 | 12 | 13 | scpos ← ⟨0‿0‿0⟩ 14 | Correct ← {a𝕊br: # assumes left arg is ground truth, and right arg is unknown 15 | at ← <˘⍉>a 16 | bd ← {∾(⍉∘>𝕩⊸-)¨at}¨ br 17 | d←{+´¬∊𝕩}¨ bd 18 | 11=⌈´d? 19 | i←⊑d⊐⌈´d 20 | rel ← -(bdc⊏˜⊑∘⊐⟜(⌈´)) ⊒ bdc←i⊑bd 21 | scpos∾↩ ⟨rel⟩ 22 | rel+i⊑br 23 | ; 24 | @ 25 | } 26 | 27 | todo ← Rots¨ 1↓l 28 | allDone ← 1↑l 29 | {𝕊prevDone: 30 | newDone ← ⟨⟩ 31 | todo ↩ {𝕊b: 32 | nv ← @ {a𝕊@:a Correct b; 𝕩}´ prevDone 33 | nv≢@? newDone∾↩ ⟨nv⟩ 34 | allDone∾↩ ⟨nv⟩ 35 | 0;1 36 | }¨⊸/ todo 37 | newDone 38 | }•_while_{𝕊:0≠≠todo} allDone 39 | 40 | •Show +´∊⍉>∾¨<˘⍉>allDone 41 | •Show ⌈´+´¨ ⥊|-⌜˜ scpos 42 | -------------------------------------------------------------------------------- /2021/BQN/2.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | l ← •FLines ≠◶"2.in"‿⊑ •args 3 | t ← ⊑¨l 4 | m ← '0'-˜¯1⊑¨l 5 | 6 | f ← 'f'=t 7 | d ← (t='d')-t='u' 8 | a ← +`m×d 9 | p ← +´f×m 10 | •Show p × ¯1⊑a 11 | •Show p × +´m×a×f 12 | -------------------------------------------------------------------------------- /2021/BQN/20.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB⟩ ← •Import "../../utils/utils.bqn" 3 | l ← '#'=•FLines ≠◶"20.in"‿⊑ •args 4 | p ← ⊑ l 5 | 6 | Step ← { s‿b: 7 | r ← {RB ⟨s‿s∾𝕩 ⋄ s∾𝕩∾s ⋄ 𝕩∾s‿s⟩}¨b 8 | r0 ← (s×7)¨⊑r 9 | ⟨p⊑˜511×s ⋄ ⊏⟜p¨ +´64‿8‿1×{⟨r0‿r0∾𝕩 ⋄ ∾⟨⟨r0⟩⋄𝕩⋄⟨r0⟩⟩ ⋄ 𝕩∾r0‿r0⟩}r⟩ 10 | } 11 | 12 | {•Show +´∾1⊑𝕩}¨ Step⍟2‿50 ⟨0 ⋄ 2↓l⟩ 13 | -------------------------------------------------------------------------------- /2021/BQN/21.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Ints, SInts, Split⟩ ← •Import "../../utils/utils.bqn" 3 | l ← 1⊑⟜Ints¨•FLines ≠◶"21.in"‿⊑ •args 4 | 5 | p ← l 6 | sc ← 0‿0 7 | d ← 1‿2‿3 8 | i ← {sc ↩ (sc+p↩(1+10|¯1+p++´d)⊣⌾((2|𝕩)⊸⊑) p) ⊣⌾((2|𝕩)⊸⊑) sc ⋄ d↩1+100|¯1+ 3+d ⋄ 𝕩+1}•_while_{𝕊:¬∨´sc≥1000}0 9 | •Show (⌊´sc)×3×i 10 | 11 | p ↩ ⋈˜10‿21⥊0 12 | s1‿s2 ← l-1 13 | p{i‿n𝕊𝕩: 1⌾(n‿0⊑i⊑⊢) 𝕩}´↩ ⟨0‿s1⋄1‿s2⟩ 14 | distr ← ≠¨⊔∧⥊+⌜´3⥊<1+↕3 15 | w ← 0‿0 16 | Step ← {𝕊: 17 | c ← (2|𝕩 )⊑p 18 | o ← (2|𝕩+1)⊑p 19 | oam ← +´⥊o 20 | n ← ((1+↕∘≠){(𝕨⥊0)∾𝕩∾(0⥊˜10-𝕨)}˘⊢) +´ distr {𝕨×𝕩⌽c}¨ -↕≠distr 21 | w ↩ (oam × +´⥊10‿¯10↑n)⊸+⌾((2|𝕩)⊸⊑) w 22 | p (10‿21↑n)⌾((2|𝕩)⊸⊑)↩ 23 | 𝕩+1 24 | } 25 | Step•_while_{𝕊:¬∧´∧´∘⥊¨0=p} 0 26 | •Show ⌈´w 27 | -------------------------------------------------------------------------------- /2021/BQN/21.in: -------------------------------------------------------------------------------- 1 | Player 1 starting position: 2 2 | Player 2 starting position: 8 -------------------------------------------------------------------------------- /2021/BQN/22.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨SInts, Repr⟩ ← •Import "../../utils/utils.bqn" 3 | l ← {⟨'n'=1⊑𝕩 ⋄ 0‿1+<˘⍉3‿2⥊SInts𝕩⟩}¨ •FLines ≠◶"22.in"‿⊑ •args 4 | 5 | # remove 𝕩 from the cube 𝕨; assumes there actually is an intersection 6 | Diff ← {𝕊s‿e: ∧´ssi↓¨rs 26 | ∨´i? 27 | pos ← si+⊑/i 28 | iv ← pos⊑¨¨rs‿re 29 | pos Add¨ as‿ae Diff iv; 30 | ·𝕊·: 31 | rs‿re∾¨¨↩ 𝕩 32 | @ 33 | } 34 | Sub ← {𝕊as‿ae: 35 | i ← ∧´(asrs 36 | iv ← i⊸/¨¨ rs‿re 37 | rs‿re (¬i)⊸/¨¨↩ 38 | (/i) {𝕨 Add¨ 𝕩 Diff as‿ae}¨ <˘⍉>{<˘⍉>𝕩}¨ iv 39 | } 40 | 41 | {𝕊 ⟨on⋄c⟩: on◶Sub‿Add c}¨ 𝕩 42 | +´{×´-˜´𝕩} rs‿re 43 | } 44 | 45 | •Show Do {⟨v⋄s‿e⟩: ∧´ (s≥¯50)∧e≤50}¨⊸/ l 46 | •Out Repr Do l -------------------------------------------------------------------------------- /2021/BQN/23.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env -S bqn -M 2000 2 | ⟨RB, B10, Num, Ints, SInts, Split⟩ ← •Import "../../utils/utils.bqn" 3 | l ← <˘⍉>'A'-˜2‿3⊏'A'⊸≤⊸/¨•FLines ≠◶"23.in"‿⊑ •args 4 | 5 | scores ← 1‿10‿100‿1000 6 | exits ← 2‿4‿6‿8 7 | 8 | Fmt ← {s‿f‿r: 9 | •Out "⎕"∾(f⊏"ABCD.")∾"⎕ "∾•Repr s 10 | lns ← <˘⍉>∾((≠⊑r)⥊'⎕')⊸⋈¨r⊏¨<"ABCD." 11 | (↕≠lns) { 12 | ×𝕨? •Out " "∾𝕩∾"⎕"; 13 | •Out "⎕⎕"∾𝕩∾"⎕⎕⎕" 14 | }¨ lns 15 | } 16 | 17 | Do ← { 18 | minSc←∞ 19 | nextKs ← ↕0 20 | nextVs ← ⟨⟩ 21 | # Next ← {Get 𝕩} # in case of a need for some filtering 22 | Next ← {nextKs∾↩ ⊑𝕩 ⋄ nextVs∾↩ <1↓𝕩 ⋄ 1} 23 | Get ← { s‿f‿r: 24 | ft ← 4≠f # which spaces outside are free 25 | { 26 | sorted ← ∧´¨ (r=4) ∨ r=↕4 27 | {∧´sorted? ∧´f=4? minSc⌊↩s;0} 28 | ee ← (4=⊑¨r) ∧ sorted # whether the room is currently enterable 29 | ∨´ee? { 30 | 0 { # try to move ones on the hallway to their homes 31 | v ← 𝕩⊑f 32 | v≠4? 33 | v⊑ee? 34 | end ← v⊑exits 35 | ∧´4=(end {s𝕊e: s+-⍟(ev"⊐>•FLines ≠◶"25.in"‿⊑ •args 3 | 4 | Step ← { 5 | Rot ← 𝕨◶⟨!,⌽˘,⌽⟩ 6 | e ← (𝕩=0) × ¯1 Rot 𝕨=𝕩 7 | (¬1 Rot e) × (e×𝕨) + (¬e)×𝕩 8 | }´⟜(⌽1+↕2) 9 | 10 | •Show 1+⊑{n‿·‿p: ⟨n+1,p,Step p⟩}•_while_{n‿p‿c: p≢c} ⟨0 ⋄ l ⋄ Step l⟩ -------------------------------------------------------------------------------- /2021/BQN/3.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | RB ← 2⊸×⊸+˜´⌽ 3 | _b ← {((≠𝕨)-𝕩) 𝔽 𝕩} # {≠⊸-𝔽⊢} 4 | rs ← '0'-˜ •FLines ≠◶"3.in"‿⊑ •args 5 | cs ← <˘⍉> rs 6 | Filter ← {𝕊f: 7 | n←0 8 | RB rs⊑˜⊑{(n+↩1) ⊢ 𝕩/˜ (⊢(⊣≠F _b)+´) 𝕩⊏n⊑cs}•_while_(1≠≠) ↕≠rs 9 | } 10 | 11 | •Show rs (< ×○RB >)_b +´¨cs 12 | •Show ×○Filter´ >‿≤ 13 | #{𝕊:×○(RB Filter⟜(cs∾⟨rs⟩))´ >‿≤}¨↕10000 -------------------------------------------------------------------------------- /2021/BQN/4.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Ints⟩ ← •Import "../../utils/utils.bqn" 3 | l ← •FLines ≠◶"4.in"‿⊑ •args 4 | l0 ← •BQN"⟩"∾˜"⟨"∾⊑l 5 | bs ← {{>Ints¨1↓𝕩}¨ 𝕩⊔˜⌊6÷˜↕≠𝕩} 1↓l 6 | 7 | bo ← l0⊸⊐¨ bs 8 | t ← (⌈˝ ⌊○(⌊´) ⌈˝˘)¨bo 9 | •Show¨ {(l0⊑˜𝕩⊑t) × +´⥊ (𝕩⊑bs)× (𝕩⊑bo)>𝕩⊑t}¨ 0‿¯1⊏⍋t 10 | #•Show¨ (0‿¯1⊏⍋t) ⊏ (t⊏l0) × +´∘⥊¨ bs×bo>t 11 | -------------------------------------------------------------------------------- /2021/BQN/5.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Num, RB, Ints⟩ ← •Import "../../utils/utils.bqn" 3 | 4 | ps ← {<˘2‿2⥊𝕩}¨ Ints¨ •FLines ≠◶"5.in"‿⊑ •args 5 | vs ← {s‿e: s<⊸+(<×e-s)×↕1+⌈´|e-s}¨ps 6 | {•Show +´1=⊒∾𝕩}¨ vs⋈˜vs/˜(∨´=´)¨ps 7 | 8 | # •Show +´˘•Show 1< •Show ⍉(0⥊˜1+⌈´∾vs) {1⊸+⌾(𝕨⊸⊑)𝕩}´ ∾vs -------------------------------------------------------------------------------- /2021/BQN/6.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Num, RB, Ints⟩ ← •Import "../../utils/utils.bqn" 3 | l ← Ints ⊑•FLines ≠◶"6.in"‿⊑ •args 4 | 5 | •Show¨ +´¨ {(⊑𝕩)⊸+⌾(6‿8⊸⊏) 0«𝕩}⍟80‿256 9↑ /⁼∧l -------------------------------------------------------------------------------- /2021/BQN/6.in: -------------------------------------------------------------------------------- 1 | 5,1,1,5,4,2,1,2,1,2,2,1,1,1,4,2,2,4,1,1,1,1,1,4,1,1,1,1,1,5,3,1,4,1,1,1,1,1,4,1,5,1,1,1,4,1,2,2,3,1,5,1,1,5,1,1,5,4,1,1,1,4,3,1,1,1,3,1,5,5,1,1,1,1,5,3,2,1,2,3,1,5,1,1,4,1,1,2,1,5,1,1,1,1,5,4,5,1,3,1,3,3,5,5,1,3,1,5,3,1,1,4,2,3,3,1,2,4,1,1,1,1,1,1,1,2,1,1,4,1,3,2,5,2,1,1,1,4,2,1,1,1,4,2,4,1,1,1,1,4,1,3,5,5,1,2,1,3,1,1,4,1,1,1,1,2,1,1,4,2,3,1,1,1,1,1,1,1,4,5,1,1,3,1,1,2,1,1,1,5,1,1,1,1,1,3,2,1,2,4,5,1,5,4,1,1,3,1,1,5,5,1,3,1,1,1,1,4,4,2,1,2,1,1,5,1,1,4,5,1,1,1,1,1,1,1,1,1,1,3,1,1,1,1,1,4,2,1,1,1,2,5,1,4,1,1,1,4,1,1,5,4,4,3,1,1,4,5,1,1,3,5,3,1,2,5,3,4,1,3,5,4,1,3,1,5,1,4,1,1,4,2,1,1,1,3,2,1,1,4 -------------------------------------------------------------------------------- /2021/BQN/7.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Num, RB, Ints⟩ ← •Import "../../utils/utils.bqn" 3 | l ← Ints ⊑•FLines ≠◶"7.in"‿⊑ •args 4 | 5 | # •Show ⊑∧{+´|l-𝕩}¨ ↕⌈´l 6 | # •Show ⊑∧{+´{2÷˜𝕩×𝕩+1}|l-𝕩}¨ ↕⌈´l 7 | ds←|l⊸-¨ ↕⌈´l 8 | •Show¨ {⌊´+´¨𝕏ds}¨ ⊢‿{2÷˜𝕩×𝕩+1} -------------------------------------------------------------------------------- /2021/BQN/7.in: -------------------------------------------------------------------------------- 1 | 1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,807,891,601,565,31,61,126,1220,923,21,750,38,834,1494,1187,235,138,344,438,1078,1664,936,451,86,34,292,782,923,154,1060,286,713,1557,1693,95,7,263,1100,402,472,342,384,95,968,319,193,1130,983,100,88,1020,720,693,790,113,30,30,759,151,1039,111,172,46,478,341,182,229,96,750,88,254,105,599,1074,20,366,307,286,25,467,927,1000,898,139,757,20,13,51,284,323,271,26,93,178,354,1016,165,39,1243,383,89,141,52,260,831,681,189,439,8,4,1849,272,1904,377,422,468,193,81,204,8,1165,919,16,404,442,1571,124,76,534,323,43,16,1039,68,203,177,60,963,100,34,35,433,17,14,432,60,835,789,191,248,256,68,367,326,1271,329,23,992,156,627,365,798,154,457,71,489,3,403,1138,23,1085,128,124,270,65,279,564,145,612,412,700,387,598,7,125,764,1456,1433,1010,874,262,3,39,3,157,280,182,1,1534,49,237,873,585,424,870,93,406,4,343,956,207,271,727,62,376,35,49,74,1532,1318,11,637,398,1508,143,200,339,331,14,447,121,886,512,0,79,246,1292,269,71,83,126,124,120,38,430,238,311,460,549,544,1050,421,421,67,82,25,1730,134,923,161,417,178,1730,81,34,921,1283,648,149,76,390,1311,677,182,184,1109,576,1079,569,136,106,35,1205,170,216,908,523,54,80,87,1278,1080,885,1028,423,310,71,183,395,1269,268,356,628,1173,1026,816,715,751,231,31,86,625,1231,310,86,226,405,111,991,261,145,487,196,183,234,346,987,268,28,299,40,313,597,387,44,648,134,78,302,1189,898,56,957,175,193,170,186,170,7,1789,8,632,503,587,229,72,286,522,145,653,5,29,31,486,145,258,784,513,341,331,223,387,986,374,512,1074,80,119,369,557,57,18,66,1113,251,151,319,138,485,623,577,398,487,281,113,864,273,30,29,1184,194,1531,503,147,402,1587,525,288,1015,921,601,53,346,972,646,151,163,61,585,242,67,258,36,586,347,322,341,251,112,1250,70,35,36,110,392,44,60,401,34,563,374,977,252,184,384,84,912,215,1198,176,630,708,791,1622,343,454,576,218,1054,118,14,10,665,551,20,259,497,289,176,72,524,4,147,1323,596,1512,104,278,332,283,43,804,326,258,247,776,665,435,1683,286,516,677,287,1227,409,411,49,425,207,78,157,487,364,727,976,347,158,292,28,139,1040,217,256,1385,600,95,339,32,64,53,31,394,154,281,1334,161,1291,1474,13,453,1461,25,272,594,832,473,1117,207,1107,595,732,1284,77,504,154,960,191,1416,429,587,654,457,462,457,697,45,366,960,1486,273,747,1366,389,453,1278,852,221,950,537,29,109,1052,112,331,349,790,903,215,135,1457,63,997,8,226,1440,587,1044,215,152,317,336,71,52,1612,438,218,1818,220,186,388,654,93,450,915,1083,145,69,296,712,185,569,849,236,1884,117,761,136,1260,319,1553,751,275,181,137,144,222,322,387,38,951,510,1051,614,1819,436,430,1425,8,271,387,19,205,215,21,585,462,798,27,1512,279,831,1113,97,371,998,677,1488,125,5,208,417,912,1083,276,39,919,91,29,182,1727,650,49,727,1389,1389,23,725,1109,890,594,397,246,47,520,305,282,81,157,283,1031,65,1355,773,480,0,162,655,46,786,677,394,506,1581,1011,149,248,107,212,446,163,553,1005,1,856,32,34,682,128,328,114,7,737,188,31,1157,590,397,159,366,449,601,225,121,320,163,849,746,122,141,443,616,72,660,244,964,718,1628,77,129,854,206,12,458,1130,207,26,622,502,934,979,577,611,1187,62,981,1692,38,11,85,1417,1152,1846,520,1001,104,595,586,945,8,624,1043,955,262,53,453,377,234,1363,239,1553,760,641,615,784,633,1289,853,1224,834,488,521,71,510,45,41,309,50,245,423,7,520,350,137,275,429,86,144,800,172,214,679,87,321,682,1005,278,518,330,705,613,818,1014,469,98,581,72,1647,165,25,510,369,1013,598,209,6,323,530,59,41,1266,787,1191,149,213,8,1490,105,288,88,607,556,50,17,76,513,717,1236,1532,1455,23,77,472,573,303,0,419,573,1217,1,144,648,95,326,999,1359,324,172,259,516,1178,84,1030,73,12,867,1477,70,147,317,999,1377,196,342,127,787,372,687,855,5,1663,49,552,380,95,469,132,58,397,213,194,35,1353,216,12,497,610,571,802,392,42,490,778,6,483,1451 -------------------------------------------------------------------------------- /2021/BQN/8.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨B10, Split⟩ ← •Import "../../utils/utils.bqn" 3 | k‿v ← ∧¨¨¨<˘⍉>{' ' Split¨ 2⊸↓⌾(1⊸⊑)(+`'|'=𝕩)⊔𝕩}¨ •FLines ≠◶"8.in"‿⊑ •args 4 | 5 | •Show +´(≠¨∾v)∊2‿3‿4‿7 6 | 7 | •Show +´ k { 8 | n0‿n1‿n2‿n3‿n4‿n5‿n6‿n7 ← ≠¨⊸⊔𝕨 # group patterns by segment count 9 | ⟨d1⟩ ← n2 # extract ones with only one example per segment count 10 | ⟨d4⟩ ← n4 11 | ⟨d7⟩ ← n3 12 | ⟨d8⟩ ← n7 13 | 14 | ⟨d3⟩ ← {∧´d1∊𝕩}¨⊸/ n5 # '3' is the only 5-segment superset of '1' 15 | n5(¬∘∊/⊣)↩ ⋈d3 # remove '3' from the list of unfinished 5-segment digits 16 | d9 ← ∧⍷d3∾d4 # '9' is '3' & '4' overlapped 17 | n6(¬∘∊/⊣)↩ ⋈d9 # remove '9' from the list of unfinished 6-segment digits 18 | d2‿d5 ← {∧´𝕩∊d9}¨⊸⊏ n5 # from '2'&'5', '5' is the only subset of '9' 19 | d6‿d0 ← {∧´d1∊𝕩}¨⊸⊏ n6 # from '6'&'0', '0' is the only superset of '1' 20 | ds ← d0‿d1‿d2‿d3‿d4‿d5‿d6‿d7‿d8‿d9 # all the gotten digits 21 | B10 ds⊐𝕩 # look up the wanted numbers, and decode them 22 | }¨ v 23 | 24 | 25 | # lookup tables: 26 | # •Show +´ k { 27 | # d14←0‿2⊏ns←(⍋≠¨𝕨)⊏𝕨 28 | # B10 𝕩⊐˜ 8‿0‿4‿6‿2‿5‿7‿1‿3‿9⊏(⍋4‿4‿6‿2‿2‿2‿0‿0‿0‿6-˜{+´∾𝕩⊸∊¨d14}¨)⊸⊏ ns 29 | # }¨ v 30 | 31 | # set operations: 32 | # U ← ⍷∾ # APL ∪ 33 | # I ← ∊/⊣ # APL ∩ 34 | # W ← ¬∘∊/⊣ # APL ~ 35 | # 36 | # •Show +´k { 37 | # ns ← (⍋≠¨𝕨)⊏𝕨 38 | # cf‿acf‿bcdf‿n5a‿n5b‿n5c‿n6a‿n6b‿n6c‿abcdefg ← ns 39 | # a ← acf W bcdf 40 | # adg ← n5a I n5b I n5c 41 | # abfg ← n6a I n6b I n6c 42 | # ag ← adg I abfg 43 | # g ← ag W a 44 | # d ← adg W ag 45 | # cd ← bcdf W cf 46 | # b ← cd W d 47 | # bcef ← abcdefg W adg 48 | # cef ← bcef W b 49 | # e ← cef W cf 50 | # f ← abfg W b U ag 51 | # c ← abcdefg W abfg U d U e 52 | # ds ← ∧¨ ⊏⟜(a∾b∾c∾d∾e∾f∾g)¨ "abcefg"‿"cf"‿"acdeg"‿"acdfg"‿"bcdf"‿"abdfg"‿"abdefg"‿"acf"‿"abcdefg"‿"abcdfg"-'a' 53 | # B10 ds⊐𝕩 54 | # }¨ v -------------------------------------------------------------------------------- /2021/BQN/9.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | l ← >'0'-˜•FLines ≠◶"9.in"‿⊑ •args 3 | 4 | lp ← (⊢ < (99¨⊏)⊸(»⌊«) ⌊ 99⊸(»˘⌊«˘)) l 5 | •Show +´ 1+lp/○⥊l 6 | 7 | 8 | Step ← { 9 | vs‿⟨⟩: ≠vs; 10 | vs‿ps: 11 | ps/˜↩ (9≠ps⊑l)∧¬ps∊vs 12 | ⟨vs∾ps ⋄ {∧´¨(𝕩≥0)∧𝕩<<≢l}⊸/⍷⥊ps+⌜⟨0‿1,1‿0,0‿¯1,¯1‿0⟩⟩ 13 | } 14 | •Show ×´ ¯3↑∧⊑¨ Step•_while_=∘{⟨⟩‿⟨𝕩⟩}¨ /○⥊⟜↕⟜≢ lp 15 | -------------------------------------------------------------------------------- /2021/BQN/alt/12a.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Split⟩ ← •Import"../utils.bqn" 3 | rs‿re ← <˘⍉>'-'Split¨•FLines ≠◶"../12.in"‿⊑ •args 4 | 5 | u ← ⍷rs∾re # unique caves 6 | big ← ('A'⊸≤∧≤⟜'Z') ⊑¨u # whether each of them are big 7 | sp‿ep ← u⊐"start"‿"end" # start and end cave numbers 8 | 9 | s‿e ← ∾¨⟜⌽rs‿re ⊐˜¨ '0'-˜•FLines ≠◶"../15.in"‿⊑ •args 3 | 4 | # high-rank version using high-depth ⊑ 5 | sc‿c←@‿@ 6 | Init ← {𝕊: 7 | sc ↩ 0⌾⊑ (≢l)⥊∞ 8 | c ↩ ⟨0‿1⋄1‿0⟩ 9 | } 10 | 11 | Step ← {𝕊: 12 | 0<≠c? 13 | wsc ← ∞¨∘⊏⊸(∾∾⊣) ∞∾˘˜∞∾˘sc 14 | p ← c⊑sc 15 | n ← (c⊑l) + ⌊´{(c+<𝕩)⊑wsc}¨ ⟨1‿0⋄0‿1⋄1‿2⋄2‿1⟩ 16 | c‿p‿n (n9? spld↩1 ⋄ (⌊⋈⌈)𝕩÷2; 34 | 𝕩 35 | }∘{spld↩0⋄𝕩} 36 | 37 | Expl ← { 38 | ExplSet⍟(aa≢0) 0 ExplGet 𝕩 39 | } 40 | 41 | Step ← {Spl⍟(𝕩⊸≡) Expl 𝕩} 42 | 43 | Simplify ← {⊑{p‿c:c⋈Step c}•_while_(≢´) 𝕩⋈Step𝕩} 44 | 45 | 46 | Add ← Simplify ⋈ 47 | 48 | 49 | Mag ← {𝕊l‿r: +´3‿2×𝕊¨l‿r; 𝕩} 50 | 51 | •Show Mag Add˜´⌽ l 52 | •Show ⌈´ ⥊Mag∘Add⌜˜ l -------------------------------------------------------------------------------- /2021/BQN/alt/22a.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | # iterates through compressed range points, sees which cube it intersected with last 3 | 4 | ⟨RB, B10, Num, Ints, SInts, Split⟩ ← •Import "../utils.bqn" 5 | l ← {⟨'n'=1⊑𝕩 ⋄ <˘⍉3‿2⥊SInts𝕩⟩}¨ •FLines ≠◶"../22.in"‿⊑ •args 6 | 7 | Do ← { 𝕊inp: 8 | on ← ⊑¨inp 9 | cs‿ce ← {<˘⍉>𝕩}¨ 0‿1+<˘⍉>1⊑¨inp 10 | u ← ⍷∘∧¨ cs∾¨ce 11 | 12 | mcs ← u⊐¨cs 13 | mce ← u⊐¨ce 14 | 15 | szx‿szy‿szz ← ≠¨u 16 | zi ← ↕szz-1 17 | zw ← (1⊸↓-¯1⊸↓) 2⊑u 18 | 19 | +´{𝕊xy: 20 | v ← /∧´(𝕩≥2↑mcs)∧𝕩<2↑mce 21 | s‿e‿con ← v⊸⊏¨ (2⊑¨mcs‿mce)∾𝕩}¨ 0‿1+<˘⍉>1⊑¨inp 12 | u ← ⍷∘∧¨ cs∾¨ce 13 | 14 | mcs ← u⊐¨cs 15 | mce ← u⊐¨ce 16 | sh ← ≠¨u 17 | b ← ¯2+(×´sh)⥊2 # don't bitarr 18 | on { 19 | s‿e ← 𝕩⊑¨¨mcs‿mce 20 | idxs ← {∾𝕨+<𝕩}´ (⌽×`1»⌽sh)× s +↕¨ e-s 21 | b (¯2+(≠idxs)⥊2+𝕨)⌾(idxs⊸⊏)↩ 22 | 1 23 | }¨ ↕≠⊑mcs 24 | 25 | zsz ← 2⊑sh 26 | zi ← ↕zsz-1 27 | zw ← (1⊸↓-¯1⊸↓) 2⊑u 28 | br ← sh⥊b 29 | +´{𝕊xy: 30 | (×´ ((1+xy)⊑¨2↑u) - (xy⊑¨2↑u)) × +´zw× ¯1↓br⊏´⌽xy 31 | }¨ ⥊↕(2↑sh)-1 32 | } 33 | 34 | 35 | •Show Do {⟨v⋄s‿e⟩: ∧´ (s≥¯50)∧e≤50}¨⊸/ l 36 | •Show Do l -------------------------------------------------------------------------------- /2021/BQN/other/18coltim.in: -------------------------------------------------------------------------------- 1 | [1,[[9,[5,8]],[[2,0],0]]] 2 | [[[6,4],6],[[1,[7,3]],[[0,1],[4,9]]]] 3 | [[[7,3],[8,6]],[[4,[1,2]],7]] 4 | [[[2,[4,5]],[[7,1],2]],1] 5 | [[[[4,4],[4,6]],9],[[4,2],6]] 6 | [[[9,8],[[4,0],0]],[[2,[5,1]],[[9,6],[9,2]]]] 7 | [[[6,[9,0]],0],[6,[[5,8],3]]] 8 | [[[[7,3],[5,4]],0],[3,[[0,6],3]]] 9 | [5,[[0,0],[[4,8],[8,6]]]] 10 | [[[3,[9,2]],9],[5,[0,6]]] 11 | [[[[6,3],[3,2]],[5,9]],2] 12 | [[[0,4],7],[8,[8,[4,2]]]] 13 | [[[8,[8,0]],9],[[1,[6,3]],[4,2]]] 14 | [[[[5,4],[1,5]],[1,3]],[[[9,0],[7,4]],9]] 15 | [[[5,[4,2]],[[9,2],3]],[[[6,2],[6,8]],[[2,4],[9,4]]]] 16 | [[9,6],[0,[[1,1],9]]] 17 | [[[8,[5,9]],[2,9]],[0,[[7,6],[7,6]]]] 18 | [[[5,[4,8]],[[7,7],[2,2]]],[[[2,6],[5,7]],[0,[6,2]]]] 19 | [[[[9,3],5],3],[[[1,5],2],[3,3]]] 20 | [[[2,[1,1]],[[5,8],[7,1]]],[[9,7],5]] 21 | [[9,9],3] 22 | [[[5,[6,1]],9],[1,[9,3]]] 23 | [[[[1,2],7],[[6,8],[4,1]]],[[2,3],[6,3]]] 24 | [[[[9,3],[7,9]],2],[[9,[3,4]],[[2,6],[7,0]]]] 25 | [[8,[4,9]],[[2,[5,6]],6]] 26 | [3,[[[9,7],7],[[2,6],4]]] 27 | [[[[3,4],[0,8]],[[6,4],[2,6]]],[[[1,4],[5,4]],8]] 28 | [8,[[0,[5,5]],[[1,2],1]]] 29 | [[[5,[8,1]],[[1,8],[4,0]]],[8,8]] 30 | [[[9,5],3],[[7,9],[1,6]]] 31 | [[[[1,1],1],[[2,0],[2,5]]],5] 32 | [[3,[[5,4],[7,4]]],[[4,4],[1,9]]] 33 | [[0,[[7,4],[7,2]]],[[8,0],[5,9]]] 34 | [0,[[[1,2],4],[[1,0],[6,4]]]] 35 | [[[[6,6],[9,8]],3],[[[5,5],[1,6]],[8,[5,3]]]] 36 | [[7,[[5,6],0]],[5,[[9,2],4]]] 37 | [[[4,[4,5]],[7,[4,5]]],[[[9,8],8],[[8,2],[3,0]]]] 38 | [[[8,[0,5]],[[0,4],[8,9]]],[8,[4,6]]] 39 | [[[4,[9,7]],[[7,4],[7,1]]],[[[8,4],0],[[6,9],[9,0]]]] 40 | [[3,6],[[3,[4,6]],[[6,0],0]]] 41 | [3,[1,[[4,0],1]]] 42 | [[[9,9],[0,[6,3]]],[3,2]] 43 | [7,4] 44 | [2,6] 45 | [[[3,[7,8]],7],[[0,[2,5]],[1,1]]] 46 | [0,5] 47 | [8,[8,[[2,4],5]]] 48 | [[[[8,2],1],[9,0]],[[[0,8],[3,0]],9]] 49 | [[[[7,0],1],[[0,1],[6,7]]],[[[3,1],[8,3]],7]] 50 | [8,0] 51 | [[[7,[1,3]],[7,[7,2]]],[[9,0],4]] 52 | [[[[0,3],5],[[1,0],8]],[[0,2],9]] 53 | [[5,[[7,6],[7,2]]],5] 54 | [[[[2,8],[5,4]],[1,6]],[[8,8],[[5,2],4]]] 55 | [[[[1,5],[1,8]],1],[[6,[2,4]],5]] 56 | [[[[9,7],[6,3]],2],[[3,[4,4]],[3,4]]] 57 | [[[9,2],[2,9]],[[[0,7],[0,8]],[[0,2],[6,7]]]] 58 | [[[[1,1],3],[[1,4],[8,9]]],[[8,[8,6]],[7,7]]] 59 | [5,[[1,[8,8]],[6,3]]] 60 | [[[1,4],3],7] 61 | [[[[0,1],[2,0]],2],[[8,8],7]] 62 | [[[[2,8],[4,4]],[[5,6],8]],[[[5,3],1],7]] 63 | [[9,[0,[8,3]]],[5,6]] 64 | [[[0,[8,9]],[6,[8,1]]],[[[2,3],8],[[4,0],8]]] 65 | [[2,[5,4]],[[7,4],[[5,0],3]]] 66 | [[[[1,1],2],[[3,0],[7,7]]],[[1,[3,8]],2]] 67 | [[[1,4],[6,[2,4]]],[[5,5],0]] 68 | [[[[4,4],8],[[4,3],[3,5]]],[[7,1],2]] 69 | [[[4,[0,8]],9],[[[6,9],2],8]] 70 | [[[[0,0],1],[1,1]],[2,[[0,0],[7,7]]]] 71 | [[2,[5,5]],9] 72 | [[[[5,8],[7,7]],[[9,8],5]],[[3,5],[8,8]]] 73 | [[5,[3,[3,9]]],[3,[9,8]]] 74 | [[8,[4,6]],[[5,0],[9,2]]] 75 | [[[3,[1,8]],[4,5]],[[0,[5,9]],6]] 76 | [9,[[1,1],0]] 77 | [[[[6,1],[9,2]],4],[5,3]] 78 | [[[[3,0],[0,5]],[1,[5,2]]],[[[2,0],[0,2]],[[6,4],4]]] 79 | [[[[1,1],[4,6]],[[3,8],[3,2]]],[[[4,3],7],[2,[7,8]]]] 80 | [4,[[1,5],5]] 81 | [8,[[1,1],0]] 82 | [[[[8,4],[9,9]],[3,[6,6]]],[[[7,9],[9,7]],7]] 83 | [[2,5],[8,[3,8]]] 84 | [[[6,1],[7,[3,5]]],9] 85 | [[1,[[3,6],[1,0]]],[[[2,8],8],[4,[2,7]]]] 86 | [[[3,[6,9]],[[9,6],[0,8]]],[[5,[6,4]],[[3,4],1]]] 87 | [[[[7,7],1],[5,[2,5]]],[[3,7],[[4,7],3]]] 88 | [[4,[3,[7,2]]],[[[8,8],[5,8]],8]] 89 | [[3,[[9,9],6]],6] 90 | [[6,7],[2,9]] 91 | [[[9,7],[1,[4,0]]],[[[3,4],0],[1,2]]] 92 | [9,[[8,[8,4]],3]] 93 | [[[4,[4,1]],[[4,7],[2,3]]],[8,[5,[1,5]]]] 94 | [7,[2,[4,1]]] 95 | [[[[1,5],7],[5,9]],8] 96 | [[[[1,5],[0,4]],8],[[[7,0],6],[8,3]]] 97 | [[[7,[3,5]],0],[8,[9,[5,6]]]] 98 | [[1,[[5,1],5]],[[5,1],[9,[3,0]]]] 99 | [3,[[[8,5],[7,5]],[9,4]]] 100 | [[[3,3],[2,[5,9]]],7] -------------------------------------------------------------------------------- /2021/BQN/test.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | { 3 | exp ⇐ ⟨ 4 | ⟨"1", 1564‿1611⟩ 5 | ⟨"2", 1989265‿2089174012⟩ 6 | ⟨"3", 3959450‿7440311⟩ 7 | ⟨"4", 44736‿1827⟩ 8 | ⟨"5", 7438‿21406⟩ 9 | ⟨"6", 377263‿1695929023803⟩ 10 | ⟨"7", 336701‿95167302⟩ 11 | ⟨"8", 255‿982158⟩ 12 | ⟨"9", 448‿1417248⟩ 13 | ⟨"10", 345441‿3235371166⟩ 14 | ⟨"11", 1683‿788⟩ 15 | ⟨"12", 4304‿118242⟩ 16 | ⟨"13", 751‿"### ## # # ### # # # # # # "‿"# # # # # # # # # # # # # # "‿"# # # #### # # ## # ## # "‿"### # ## # # ### # # # # # # "‿"# # # # # # # # # # # # # "‿"# ### # # # # # # #### # # ####"⟩ 17 | ⟨"14", 2602‿2942885922173⟩ 18 | ⟨"15", 456‿2831⟩ 19 | ⟨"16", 889‿739303923668⟩ 20 | ⟨"17", 10878‿4716⟩ 21 | ⟨"18", 3665‿4775⟩ 22 | ⟨"19", 442‿11079⟩ 23 | ⟨"20", 5765‿18509⟩ 24 | ⟨"21", 1196172‿106768284484217⟩ 25 | ⟨"22", 561032‿"1322825263376414"⟩ 26 | ⟨"23", 15472‿46182⟩ 27 | ⟨"24", "12934998949199"‿"11711691612189"⟩ 28 | ⟨"25", ⟨518⟩⟩ 29 | ⟩ 30 | 31 | path ⇐ •path 32 | args ⇐ •args 33 | all ⇐ •Repr¨ 1+↕25 34 | most ⇐ (¬∊⟜⟨"24"⟩)⊸/all 35 | orig ⇐ ⟨⟩ 36 | } •Import "../../utils/test.bqn" 37 | -------------------------------------------------------------------------------- /2021/Java/day5_p1/inputs/example.in: -------------------------------------------------------------------------------- 1 | 0,9 -> 5,9 2 | 8,0 -> 0,8 3 | 9,4 -> 3,4 4 | 2,2 -> 2,1 5 | 7,0 -> 7,4 6 | 6,4 -> 2,0 7 | 0,9 -> 2,9 8 | 3,4 -> 1,4 9 | 0,0 -> 8,8 10 | 5,5 -> 8,2 -------------------------------------------------------------------------------- /2022/BQN/1.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Num,SplitF⟩ ← •Import "../../utils/utils.bqn" 3 | l ← Num¨¨ ⟨⟩SplitF •FLines ≠◶"1.in"‿⊑ •args 4 | 5 | •Show¨ 0‿2⊏ +` 3↑∨ +´¨ l -------------------------------------------------------------------------------- /2022/BQN/10.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨SInts⟩ ← •Import "../../utils/utils.bqn" 3 | l ← •FLines ≠◶"10.in"‿⊑ •args 4 | n ← 'n'=⊑¨l 5 | 6 | v ← {⟨⟩:0; ⊑𝕩}¨ SInts¨ 5↓¨l 7 | x ← »1+`v 8 | 9 | cyc ← 1+`n⊏2‿1 10 | 11 | oc ← 20+40×↕⌈40÷˜¯20+¯1⊑cyc 12 | •Show +´ oc × (cyc⍋oc)⊏x 13 | 14 | cc ← 1+↕240 15 | •Out˘ ⊏⟜" #" ∘‿40⥊ (40|cc) {(𝕨≥𝕩)∧𝕨<𝕩+3} (cyc⍋cc)⊏x -------------------------------------------------------------------------------- /2022/BQN/10.in: -------------------------------------------------------------------------------- 1 | noop 2 | noop 3 | noop 4 | addx 4 5 | addx 3 6 | addx 3 7 | addx 3 8 | noop 9 | addx 2 10 | addx 1 11 | addx -7 12 | addx 10 13 | addx 1 14 | addx 5 15 | addx -3 16 | addx -7 17 | addx 13 18 | addx 5 19 | addx 2 20 | addx 1 21 | addx -30 22 | addx -8 23 | noop 24 | addx 3 25 | addx 2 26 | addx 7 27 | noop 28 | addx -2 29 | addx 5 30 | addx 2 31 | addx -7 32 | addx 8 33 | addx 2 34 | addx 5 35 | addx 2 36 | addx -12 37 | noop 38 | addx 17 39 | addx 3 40 | addx -2 41 | addx 2 42 | noop 43 | addx 3 44 | addx -38 45 | noop 46 | addx 3 47 | addx 4 48 | noop 49 | addx 5 50 | noop 51 | noop 52 | noop 53 | addx 1 54 | addx 2 55 | addx 5 56 | addx 2 57 | addx -3 58 | addx 4 59 | addx 2 60 | noop 61 | noop 62 | addx 7 63 | addx -30 64 | addx 31 65 | addx 4 66 | noop 67 | addx -24 68 | addx -12 69 | addx 1 70 | addx 5 71 | addx 5 72 | noop 73 | noop 74 | noop 75 | addx -12 76 | addx 13 77 | addx 4 78 | noop 79 | addx 23 80 | addx -19 81 | addx 1 82 | addx 5 83 | addx 12 84 | addx -28 85 | addx 19 86 | noop 87 | addx 3 88 | addx 2 89 | addx 5 90 | addx -40 91 | addx 4 92 | addx 32 93 | addx -31 94 | noop 95 | addx 13 96 | addx -8 97 | addx 5 98 | addx 2 99 | addx 5 100 | noop 101 | noop 102 | noop 103 | addx 2 104 | addx -7 105 | addx 8 106 | addx -7 107 | addx 14 108 | addx 3 109 | addx -2 110 | addx 2 111 | addx 5 112 | addx -40 113 | noop 114 | noop 115 | addx 3 116 | addx 4 117 | addx 1 118 | noop 119 | addx 2 120 | addx 5 121 | addx 2 122 | addx 21 123 | noop 124 | addx -16 125 | addx 3 126 | noop 127 | addx 2 128 | noop 129 | addx 1 130 | noop 131 | noop 132 | addx 4 133 | addx 5 134 | noop 135 | noop 136 | noop 137 | noop 138 | noop 139 | noop 140 | noop -------------------------------------------------------------------------------- /2022/BQN/11.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Split, LCM, SplitF, Num, Ints⟩ ← •Import "../../utils/utils.bqn" 3 | l ← ⟨⟩SplitF •FLines ≠◶"11.in"‿⊑ •args 4 | 5 | m ← { a‿b‿c‿d‿e‿f: 6 | id ⇐ ⊑Ints a 7 | start ⇐ Ints b 8 | op ← 19↓c 9 | fn ⇐ {f‿g‿h: F G H} {(⊑"+*"⊐𝕩)⊑+‿×}⌾(1⊸⊑) {"old": ⊢; Num 𝕩}¨⌾(0‿2⊸⊏) ' 'Split op 10 | test ⇐ Num 21↓d 11 | throw ⇐ ⟨Num 29↓e, Num 30↓f⟩ 12 | curr ⇐ @ ⋄ _modCurr ⇐ {curr 𝔽↩ 𝕩} 13 | insp ⇐ @ ⋄ _modInsp ⇐ {insp 𝔽↩ 𝕩} 14 | }¨ l 15 | 16 | mod ← LCM´ {𝕩.test}¨ m 17 | 18 | _step ← { Calm _𝕣 c: 19 | +c._modInsp ≠c.curr 20 | { 21 | w ← Calm c.Fn 𝕩 22 | to ← (0≠c.test|w)⊑c.throw 23 | ∾(to⊑m)._modCurr w 24 | }¨ (⊢ 𝕩._modCurr ⟨⟩) ⊢ 𝕩.curr 25 | } 26 | 27 | _rounds ← { 28 | {⊢ 𝕩._modInsp 0 ⋄ ⊢ 𝕩._modCurr 𝕩.start}¨ m 29 | 𝔽 _step¨∘m⍟𝕩 @ 30 | ×´2↑∨{𝕩.insp}¨ m 31 | } 32 | 33 | •Show {⌊𝕩÷3} _rounds 20 34 | •Show mod⊸| _rounds 10000 -------------------------------------------------------------------------------- /2022/BQN/11.in: -------------------------------------------------------------------------------- 1 | Monkey 0: 2 | Starting items: 66, 71, 94 3 | Operation: new = old * 5 4 | Test: divisible by 3 5 | If true: throw to monkey 7 6 | If false: throw to monkey 4 7 | 8 | Monkey 1: 9 | Starting items: 70 10 | Operation: new = old + 6 11 | Test: divisible by 17 12 | If true: throw to monkey 3 13 | If false: throw to monkey 0 14 | 15 | Monkey 2: 16 | Starting items: 62, 68, 56, 65, 94, 78 17 | Operation: new = old + 5 18 | Test: divisible by 2 19 | If true: throw to monkey 3 20 | If false: throw to monkey 1 21 | 22 | Monkey 3: 23 | Starting items: 89, 94, 94, 67 24 | Operation: new = old + 2 25 | Test: divisible by 19 26 | If true: throw to monkey 7 27 | If false: throw to monkey 0 28 | 29 | Monkey 4: 30 | Starting items: 71, 61, 73, 65, 98, 98, 63 31 | Operation: new = old * 7 32 | Test: divisible by 11 33 | If true: throw to monkey 5 34 | If false: throw to monkey 6 35 | 36 | Monkey 5: 37 | Starting items: 55, 62, 68, 61, 60 38 | Operation: new = old + 7 39 | Test: divisible by 5 40 | If true: throw to monkey 2 41 | If false: throw to monkey 1 42 | 43 | Monkey 6: 44 | Starting items: 93, 91, 69, 64, 72, 89, 50, 71 45 | Operation: new = old + 1 46 | Test: divisible by 13 47 | If true: throw to monkey 5 48 | If false: throw to monkey 2 49 | 50 | Monkey 7: 51 | Starting items: 76, 50 52 | Operation: new = old * old 53 | Test: divisible by 7 54 | If true: throw to monkey 4 55 | If false: throw to monkey 6 -------------------------------------------------------------------------------- /2022/BQN/12.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | l ← >•FLines ≠◶"12.in"‿⊑ •args 3 | 4 | h ← l-'a' 5 | s ← l='S' 6 | e ← l='E' 7 | 8 | h×↩ ¬s∨e 9 | h+↩ e×25 10 | 11 | pd ← (h-1)≤«h 12 | pu ← (h-1)≤»h 13 | pl ← (h-1)≤»˘h 14 | pr ← (h-1)≤«˘h 15 | 16 | Step ← {∨´⟨𝕩, »pd×𝕩, «pu×𝕩, «˘pl×𝕩, »˘pr×𝕩⟩} 17 | F ← {𝕊end: ⊑{n‿c: ⟨n+1,Step c⟩}•_while_{n‿c: ¬∨´⥊c∧end} ⟨0, e⟩} 18 | 19 | •Show F s 20 | •Show F h=0 -------------------------------------------------------------------------------- /2022/BQN/13.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | l ← •BQN "⟨"∾"⟩"∾˜{'⟩'¨⌾((𝕩=']')⊸/) '⟨'¨⌾((𝕩='[')⊸/)𝕩} •FChars ≠◶"13.in"‿⊑ •args 3 | 4 | l {𝕩{𝕩=1?𝕨;⥊𝕨𝕊¨𝕩-1}≡𝕩} ↩ 5 | •Show +´1+/¯1= •Cmp´˘ ∘‿2⥊ l 6 | 7 | div ← ⟨⟨⟨2⟩⟩, ⟨⟨6⟩⟩⟩ 8 | •Show ×´1+/ (∧div∾l) ∊ div 9 | 10 | # Cmp ← { 11 | # ¬𝕨∨○=𝕩? 𝕨•Cmp𝕩; 12 | # ¬𝕨∧○=𝕩? 𝕨𝕊○⥊𝕩; 13 | # 0≠c← 0{𝕨≠0?𝕨; Cmp´𝕩}˜´⌽ 𝕨 ⋈¨○((⌊´≠¨𝕨‿𝕩)⊸↑)𝕩? c; 14 | # 𝕨<○≠𝕩?¯1; 15 | # 𝕨>○≠𝕩? 1; 16 | # 0 17 | # } 18 | # 19 | # •Show +´1+/¯1= Cmp´¨ <˘∘‿2⥊ l 20 | # 21 | # div ← ⟨⟨⟨2⟩⟩, ⟨⟨6⟩⟩⟩ 22 | # b ← div ∾ l 23 | # 24 | # Sort ← { 25 | # 1≥≠𝕩? 𝕩; 26 | # ∾Sort¨ (1 + 𝕩 Cmp¨ <•rand.Range∘≠⊸⊑ 𝕩)⊔𝕩 27 | # } 28 | # 29 | # •Show ×´1+/(Sort b)∊div -------------------------------------------------------------------------------- /2022/BQN/14.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Ints⟩ ← •Import "../../utils/utils.bqn" 3 | i ← ∾{2↕∘‿2⥊𝕩}∘Ints¨•FLines ≠◶"14.in"‿⊑ •args 4 | 5 | Set1 ← {𝕨 1⌾((+´𝕩×1«≢𝕨)⊑⥊)↩} 6 | 7 | Run ← { 8 | min ← ¯1+0⊸⌊⌾(1⊸⊑) ⌊˝∾˝𝕩 9 | 𝕩 -⎉1⟜min↩ 10 | b ← ⍉ ∊⟜(∾{(<⊏𝕩)+⥊(<∘××·↕1+|)-˝⌽𝕩}¨ <˘𝕩)⌾⥊ ↕2+⌈˝∾˝𝕩 11 | oobx ← ¯1+≠⊏b 12 | ooby ← ¯1+≠b 13 | 14 | res ← 0 15 | Fill ← { 16 | 𝕨‿𝕩⊑b? 1; 17 | (ooby=𝕨+1)∨(𝕩=0)∨𝕩=oobx? 0; 18 | (𝕨+1) Fill 𝕩? 19 | (𝕨+1) Fill 𝕩-1? 20 | (𝕨+1) Fill 𝕩+1? 21 | res+↩ 1 22 | b Set1↩ 𝕨‿𝕩 23 | 1; 24 | 0 25 | } 26 | Fill´ ⌽500‿0-min 27 | •Show res 28 | } 29 | 30 | Run i 31 | ji ← 500‿0∾∾˝i 32 | max ← ⌈˝ji 33 | min ← ⌊˝ji 34 | by ← 2+1⊑max 35 | i2 ← i∾ [⟨by-˜⊑min, by⟩, ⟨by+⊑max, by⟩] 36 | Run i2 -------------------------------------------------------------------------------- /2022/BQN/15.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨SInts⟩ ← •Import "../../utils/utils.bqn" 3 | l ← SInts¨ •FLines ≠◶"15.in"‿⊑ •args 4 | 5 | sp ← <˘⍉>2↑¨l 6 | bp ← <˘⍉>2↓¨l 7 | d ← +´|sp-bp 8 | 9 | testInput ← 0 # ∧´(⌈˝∾2‿2⊸⥊¨ l)≤1000 10 | y ← {testInput? 10; 2000000} 11 | m ← {testInput? 20; 4000000} 12 | 13 | spx‿spy ← sp 14 | _ranges ← { 𝔽_𝕣 y: 15 | range ← d - |spy-𝕩 16 | xs‿ws ← ({t←' 'Split𝕩 ⋄ ⟨⊑Ints 𝕩, 1⊑t, ','⊸≠⊸/¨9↓t⟩}¨ •FLines ≠◶"16.in"‿⊑ •args 4 | 5 | si ← ⋈⁼ /"AA"⊸≡¨ s 6 | n ← s⊸⊐¨ ns 7 | ! (∧¨n) ≡ (∾n) ⊔ ∾↕∘≠⊸({𝕨¨𝕩}¨)n 8 | 9 | uf ← ¬(si≠↕≠s)∧f=0 10 | cost ← <˘⟨uf,uf⟩/>{ 11 | r ← ∞¨ s 12 | 1 { 13 | (𝕩⊑r)≤𝕨? @; 14 | r 𝕨⌾(𝕩⊸⊑)↩ 15 | (𝕨+1) 𝕊¨ 𝕩⊑n 16 | } 𝕩 17 | r 18 | }¨ ↕≠s 19 | 20 | si2 ← ⋈⁼ /si=uf/↕≠s 21 | f2 ← uf/f 22 | iota ← ↕≠f2 23 | 24 | Run ← { 25 | max ← 0 26 | Rec ← { c‿l‿r𝕊𝕩: 27 | nc ← 𝕩-c⊑cost 28 | ok ← l∧nc≥0 29 | ∨´ok? 30 | (ok/nc) {⟨𝕩, l∧iota≠𝕩, r+𝕨×𝕩⊑f2⟩ Rec 𝕨}¨ /ok ⋄ 1 31 | ; 32 | max⌈↩ 2⊑𝕨 33 | } 34 | 35 | ⟨si2, 𝕨, 0⟩ Rec 𝕩 36 | max 37 | } 38 | •Show (iota≠si2) Run 30 39 | 40 | p2 ← { 41 | 𝕩 ↩ •internal.Squeeze (si2↑𝕩)∾1∾si2↓𝕩 42 | (¬𝕩) Run 26 43 | }¨ ⥊↕1↓2¨iota 44 | •Show ⌈´p2+⌽p2 45 | -------------------------------------------------------------------------------- /2022/BQN/16.in: -------------------------------------------------------------------------------- 1 | Valve ZN has flow rate=0; tunnels lead to valves SD, ZV 2 | Valve HO has flow rate=17; tunnel leads to valve LT 3 | Valve FT has flow rate=6; tunnels lead to valves DW, BV, JA, FB, TV 4 | Valve AD has flow rate=0; tunnels lead to valves AA, JG 5 | Valve GE has flow rate=0; tunnels lead to valves JG, RD 6 | Valve GI has flow rate=0; tunnels lead to valves WJ, RD 7 | Valve RM has flow rate=0; tunnels lead to valves BU, WJ 8 | Valve GV has flow rate=0; tunnels lead to valves WB, HS 9 | Valve VA has flow rate=0; tunnels lead to valves AA, HS 10 | Valve TJ has flow rate=21; tunnel leads to valve CK 11 | Valve WB has flow rate=0; tunnels lead to valves GV, EV 12 | Valve DV has flow rate=19; tunnels lead to valves OI, NK 13 | Valve EL has flow rate=0; tunnels lead to valves HS, YC 14 | Valve KU has flow rate=0; tunnels lead to valves WJ, OI 15 | Valve WI has flow rate=16; tunnels lead to valves SD, AN, GS, JV 16 | Valve JG has flow rate=3; tunnels lead to valves SV, BU, GC, GE, AD 17 | Valve TC has flow rate=0; tunnels lead to valves TV, WJ 18 | Valve GC has flow rate=0; tunnels lead to valves JG, JA 19 | Valve LS has flow rate=0; tunnels lead to valves JH, YP 20 | Valve OI has flow rate=0; tunnels lead to valves KU, DV 21 | Valve ZH has flow rate=0; tunnels lead to valves YZ, RD 22 | Valve YZ has flow rate=0; tunnels lead to valves ZH, AA 23 | Valve YP has flow rate=0; tunnels lead to valves KS, LS 24 | Valve CK has flow rate=0; tunnels lead to valves EG, TJ 25 | Valve NY has flow rate=0; tunnels lead to valves HS, UU 26 | Valve IQ has flow rate=18; tunnel leads to valve YC 27 | Valve HI has flow rate=0; tunnels lead to valves SS, RD 28 | Valve DW has flow rate=0; tunnels lead to valves FT, JH 29 | Valve EV has flow rate=7; tunnels lead to valves SV, WB, SS, GS 30 | Valve SV has flow rate=0; tunnels lead to valves JG, EV 31 | Valve BU has flow rate=0; tunnels lead to valves JG, RM 32 | Valve GS has flow rate=0; tunnels lead to valves EV, WI 33 | Valve UY has flow rate=0; tunnels lead to valves WJ, FE 34 | Valve AA has flow rate=0; tunnels lead to valves VA, YZ, AD, FB 35 | Valve SD has flow rate=0; tunnels lead to valves WI, ZN 36 | Valve KS has flow rate=23; tunnel leads to valve YP 37 | Valve RD has flow rate=4; tunnels lead to valves GI, HI, BV, ZH, GE 38 | Valve ZV has flow rate=15; tunnel leads to valve ZN 39 | Valve HB has flow rate=0; tunnels lead to valves HS, AN 40 | Valve UU has flow rate=0; tunnels lead to valves EG, NY 41 | Valve SS has flow rate=0; tunnels lead to valves HI, EV 42 | Valve HS has flow rate=12; tunnels lead to valves HB, EL, VA, GV, NY 43 | Valve LT has flow rate=0; tunnels lead to valves DS, HO 44 | Valve JH has flow rate=5; tunnels lead to valves LS, FE, QU, NK, DW 45 | Valve AN has flow rate=0; tunnels lead to valves HB, WI 46 | Valve NK has flow rate=0; tunnels lead to valves DV, JH 47 | Valve JA has flow rate=0; tunnels lead to valves GC, FT 48 | Valve EG has flow rate=14; tunnels lead to valves CK, UU, DS 49 | Valve JV has flow rate=0; tunnels lead to valves QU, WI 50 | Valve WJ has flow rate=8; tunnels lead to valves GI, RM, KU, UY, TC 51 | Valve FE has flow rate=0; tunnels lead to valves JH, UY 52 | Valve TV has flow rate=0; tunnels lead to valves FT, TC 53 | Valve YC has flow rate=0; tunnels lead to valves IQ, EL 54 | Valve QU has flow rate=0; tunnels lead to valves JV, JH 55 | Valve DS has flow rate=0; tunnels lead to valves LT, EG 56 | Valve BV has flow rate=0; tunnels lead to valves FT, RD 57 | Valve FB has flow rate=0; tunnels lead to valves AA, FT -------------------------------------------------------------------------------- /2022/BQN/17.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Split,MDSlash⟩ ← •Import "../../utils/utils.bqn" 3 | l ← •FChars ≠◶"17.in"‿⊑ •args 4 | r ← '>'=l 5 | 6 | Set1 ← {𝕨 1⌾((+´𝕩×1«≢𝕨)⊑⥊)↩} 7 | 8 | pcb ← {⌽>'#'=(@+10)Split𝕩}¨ '-'Split" 9 | #### 10 | - 11 | .#. 12 | ### 13 | .#. 14 | - 15 | ..# 16 | ..# 17 | ### 18 | - 19 | # 20 | # 21 | # 22 | # 23 | - 24 | ## 25 | ##" 26 | 27 | pci ← MDSlash¨ pcb 28 | pcw ← ≠∘⊏¨ pcb 29 | pch ← ≠¨ pcb 30 | 31 | Game ← { 𝕊: 32 | board ← 100‿7⥊0 33 | h ⇐ 0 34 | 35 | pi ← 0 36 | p ← 3‿2 37 | si ← 0 38 | 39 | done ⇐ 0 40 | 41 | Step ⇐ { 𝕊: 42 | cr ← si⊑r 43 | cpci ← pi⊑pci 44 | p+↩ { 45 | cr? 0‿1×{(7-pi⊑pcw)>1⊑p? ¬∨´((p+ 0‿1)<⊸+cpci)⊑board; 0} 46 | ; 0‿¯1×{ 0 <1⊑p? ¬∨´((p+0‿¯1)<⊸+cpci)⊑board; 0} 47 | } 48 | fall ← {0<⊑p? ¬∨´((p+¯1‿0)<⊸+cpci)⊑board; 0} 49 | { 50 | fall? 51 | p+↩ ¯1‿0 52 | ; 53 | {board Set1↩ 𝕩 ⋄ 1}¨ p<⊸+ pi⊑pci 54 | h⌈↩ (⊑p)+pi⊑pch 55 | 56 | pi ↩ (≠pci)|pi+1 57 | 58 | p ↩ ⟨h+3, 2⟩ 59 | done+↩ 1 60 | {𝕊: 61 | board∾↩ 0¨board 62 | }⍟⊢ h>¯10+≠board 63 | } 64 | si ↩ (≠r)|1+si 65 | } 66 | Status ⇐ {𝕊: si‿pi} 67 | } 68 | 69 | p1 ← Game@ 70 | p1.Step •_while_ {𝕊: p1.done<2022} 1 71 | •Show p1.h 72 | 73 | 74 | iter ← 1000000000000 75 | p2 ← Game@ 76 | warm ← 2000 77 | p2.Step •_while_ {𝕊: p2.done≤warm} 1 ⋄ h1←p2.h ⋄ d1←p2.done ⋄ s1 ← p2.Status@ ⋄ p2.Step@ 78 | p2.Step •_while_ {𝕊: s1≢p2.Status@} 1 ⋄ h2←p2.h ⋄ d2←p2.done 79 | 80 | skip ← ⌊(iter-p2.done) ÷ (d2-d1) 81 | skipIter ← skip × d2-d1 82 | skipHeight ← skip × h2-h1 83 | 84 | p2.Step •_while_ {𝕊: p2.done < iter-skipIter} 1 85 | •Show p2.h + skipHeight 86 | -------------------------------------------------------------------------------- /2022/BQN/18.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Ints, MDSlash, _fx_⟩ ← •Import "../../utils/utils.bqn" 3 | l ← <˘∘‿3⥊Ints •FChars ≠◶"18.in"‿⊑ •args 4 | 5 | 6 | Adj ← {𝕩 +⌜ ∾⟜-=⟜<↕3} 7 | •Show +´¬(⥊Adj l)∊l 8 | 9 | w ← {¬1‿1‿1⌽⁼(2+𝕩)↑𝕩⥊1} 1+⌈´l 10 | n ← ∊⟜l⌾⥊ ¯1+↕3+⌈´l 11 | e ← {(¬n)∧∨´⟨𝕩,»𝕩,«𝕩,«⎉1𝕩,»⎉1𝕩,«⎉2𝕩,»⎉2𝕩⟩}_fx_≢ w 12 | •Show +´(⥊Adj MDSlash e)∊1+l -------------------------------------------------------------------------------- /2022/BQN/19.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Ints⟩ ← •Import "../../utils/utils.bqn" 3 | l ← 1↓¨ Ints¨ •FLines ≠◶"19.in"‿⊑ •args 4 | 5 | Try ← { minsMax‿splitPos𝕊a‿b‿c‿d‿e‿f: 6 | cs ← ⟨ 7 | a‿0‿0‿0 8 | b‿0‿0‿0 9 | c‿d‿0‿0 10 | e‿0‿f‿0 11 | ⟩ 12 | cm ← 0 ∾ ⋈⌾⊑ 𝕩; 24 | # l ← ⍷ <˘⍉> ∾ ⋈⌾⊑ 𝕩 25 | # i←0⋄t0←•MonoTime@ 26 | # { 27 | # (•MonoTime@) {t0↩𝕨⋄•Show𝕩}⍟(t0<-⟜1) (i+↩1)÷≠l 28 | # ¯1 Iter Sep ⋈¨ 𝕩}¨ l; 29 | 𝕨𝕊left‿ms‿rs: 30 | # •Show ≠⊑ms 31 | # tot+↩≠⊑ms 32 | nt ← 1+⌈´¨ ((cm/¨cs)-cm/¨{ 38 | cm ← 𝕩⊑m 39 | fdt←cm/𝕩⊑nt 40 | fms←((0‿2⊸⊏¨l) - <"AX" 4 | WinsTo ← ⊏⟜1‿2‿0 5 | LosesTo ← ⊏⟜2‿0‿1 6 | F ← {+´1+𝕩+(3×a=𝕩)+6×𝕩=WinsTo a} 7 | •Show F b 8 | •Show F ((LosesTo a) × b=0) + (a × b=1) + ((WinsTo a) × b=2) -------------------------------------------------------------------------------- /2022/BQN/20.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨SInts⟩ ← •Import "../../utils/utils.bqn" 3 | l ← SInts •FChars ≠◶"20.in"‿⊑ •args 4 | 5 | F ← { am 𝕊 l: 6 | is ← ↕≠𝕩 7 | r ← is { 𝕨𝕊n: 8 | v ← n⊑l 9 | {n⊸∾∘(v⌽⊢)1↓𝕩}∘((⊑𝕨⊐n)⌽⊢) 𝕨 # ∘s can be ⌾s to restore positions somewhat 10 | }˜´⌽ ∾am⥊𝕩↑˜¨⌈´≠¨𝕩}↩ 5 | b ← ¯1‿¯1⌽(2+≢b0)↑b0 6 | # b2 ← ' '¨b 7 | 8 | l ← Ints ⊑m 9 | dc ← 'R'∾∊⟜"RL"⊸/ ⊑m 10 | da ← 4|¯1+`(dc='R')-dc='L' # right/down/left/up 11 | dx ← (da=0)-da=2 12 | dy ← (da=1)-da=3 13 | 14 | p0 ← 1⋈⊑/'.'=1⊏b 15 | Password ← {+´1000‿4‿1×𝕩} 16 | 17 | •Show Password { 18 | p ← p0 19 | l { l𝕊delta: 20 | { 𝕊: 21 | # pv←⊑⟜">∨<∧"⊑delta⊐˜[0‿1,1‿0,0‿¯1,¯1‿0] ⋄ b2 pv⌾((+´p×1«≢b2)⊑⥊)↩ 22 | p2 ← delta+p 23 | v ← p2⊑b 24 | { 𝕊: 25 | p2 ↩ -⟜delta•_while_{𝕊: ' '≢(𝕩-delta)⊑b} p 26 | v ↩ p2⊑b 27 | }⍟⊢ v≡' ' 28 | {𝕊: p↩p2}⍟⊢ v≢'#' 29 | }¨ ↕l 30 | 𝕩 31 | }¨ <˘⍉[dy, dx] 32 | p ∾ ¯1⊑da 33 | } 34 | 35 | w ← {50≤≠b0? 50; 4} 36 | 37 | ds ← ⟨0‿1, 1‿0, 0‿¯1, ¯1‿0⟩ 38 | 39 | bs ← (w/¨↕¨ w÷˜≢b0)⊔b0 40 | bh ← ' '≠⊑¨bs 41 | be ← @¨ bh 42 | Fold ← { vs‿p: 43 | be vs⌾(p⊸⊑)↩ 44 | p2 ← p<⊸+ ds # right down left up 45 | h2 ← p2 {𝕩≡0? 0; (@≡𝕨⊑be)∧𝕨⊑bh}¨ ∧´¨ (p2≥0) ∧ p2<<≢bs 46 | (h2/↕4) { 47 | ds ← 𝕨⊑⟨1‿0,2‿0,0‿1,0‿2⟩ # move all vertices by the delta between the selected of these 48 | vs2 ← vs+<-´ds⊏vs 49 | vs2 {¯1⌈1⌊𝕩×<¯1⋆(∧´1=|𝕩)∧=´𝕩}⌾(({⊑3∊|𝕩}¨vs2)⊸/)↩ # actual fold crappy magic 50 | Fold ⟨vs2, p+𝕩⟩ 51 | }¨ h2/ds 52 | } 53 | Fold ⟨¯1+2×⟨0‿0‿0, 1‿0‿0, 0‿1‿0, 1‿1‿0⟩, ⊑MDSlash bh⟩ 54 | 55 | # •Show >¨ be 56 | # •Show {@:@;∾˘´<˘2‿1‿0⍉('0'+↕4)⌾((𝕩⊐˜⌽⥊¯1⋆↕2‿2‿2)⊏⥊) 2‿2‿2⥊' '}¨ be 57 | Translate ← { d0 𝕊 p0: 58 | e0 ← d0⊑⟨1‿3, 3‿2, 2‿0, 0‿1⟩ 59 | e0v ← e0 ⊏ p0⊑be 60 | p1 ← ⋈⁼ (MDSlash{@:0; ∧´e0v∊𝕩}¨ be) (¬∘∊/⊣) ⋈p0 61 | e1 ← e0v⊐˜p1⊑be 62 | d1 ← ⊑⟨0‿2,1‿0,3‿1,2‿3⟩⊐< e1 63 | # •Show ⟨d1, p1, '0'+e1⟩ 64 | d1‿p1 65 | } 66 | # •Show ⍷⥊2⊑¨(↕4) Translate⌜ MDSlash bh 67 | 68 | wp ← w-1 69 | 70 | •Show Password { 71 | p ← p0 72 | d ← 3 73 | l { l𝕊dd: 74 | d ↩ 4|d+dd 75 | { 𝕊: 76 | # •Show p∾d 77 | # pv←⊑⟜">∨<∧"⊑d ⋄ b2 pv⌾((+´p×1«≢b2)⊑⥊)↩ 78 | 79 | p1 ← (d⊑ds)+p 80 | d1 ← d 81 | { 𝕊: 82 | pb0 ← ⌊w÷˜p-1 83 | pwy‿pwx ← w|p-1 84 | off ← d ⊑ ⟨pwy,wp-pwx,wp-pwy,pwx⟩ 85 | # •Show "rot from "∾•Repr ⟨d, p, pb0, off⟩ 86 | 87 | d2‿pb2 ← d Translate pb0 88 | off ↩ wp-off 89 | 90 | d1 ↩ d2 91 | p1 ↩ 1 + (w×pb2) + d2⊑⟨⟨wp-off,0⟩, ⟨0,off⟩, ⟨off,wp⟩, ⟨wp,wp-off⟩⟩ 92 | # •Show "rot to "∾•Repr ⟨d2, p1, pb2, off⟩ 93 | }⍟⊢ ' '≡p1⊑b 94 | {𝕊: p↩p1 ⋄ d↩d1}⍟⊢ '#'≢p1⊑b 95 | }¨ ↕l 96 | 𝕩 97 | }¨ ¯1⋆dc='L' 98 | p ∾ d 99 | } 100 | # •Show b{𝕩≡' '?𝕨;𝕩}¨b2 -------------------------------------------------------------------------------- /2022/BQN/23.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨_fx_⟩ ← •Import "../../utils/utils.bqn" 3 | l ← '#'=>•FLines ≠◶"23.in"‿⊑ •args 4 | Step ← { 5 | 𝕩 ↩ ¯1‿¯1⌽(2+≢𝕩)↑𝕩 6 | n ← (»˘+«˘+⊢) (»+«+⊢) 𝕩 7 | s ← 𝕩∧1=n # stay where you are 8 | 9 | ah ← ¬(»˘∨«˘∨⊢) 𝕩 10 | av ← ¬(» ∨« ∨⊢) 𝕩 11 | an ← »ah ⋄ ae ← «˘av 12 | as ← «ah ⋄ aw ← »˘av 13 | 14 | o ← 4-+´∨`𝕨⌽an‿as‿aw‿ae 15 | s∨↩ o=4 16 | d ← (5×s∨¬𝕩)+4|𝕨+o # the direction each elf wants to move (≥4 if doesn't) 17 | 18 | c ← ((»1=d)∧«0=d) ∨ («˘2=d)∧»˘3=d # conflict 19 | co ← ((1=d)∧«c) ∨ ((0=d)∧»c) ∨ ((2=d)∧»˘c) ∨ ((3=d)∧«˘c) # conflict origin 20 | m ← 𝕩 ∧ (¬s) ∧ ¬co # all moving elves 21 | (𝕩∧¬m) ∨ («m∧d=0) ∨ (»m∧d=1) ∨ («˘m∧d=2) ∨ (»˘m∧d=3) # move elves 22 | } 23 | 24 | Trim ← {{𝕩/˜(∨`∧∨`⌾⌽)∨˝˘𝕩}∘⍉⍟2 𝕩} 25 | Step2 ← {n‿b: ⟨n+1, n Step b⟩} 26 | 27 | # •Show {1<≡𝕩? 𝕊¨𝕩; ∧´⥊(𝕩=0)∨𝕩=1? ⊏⟜".#" 𝕩; '0'+𝕩} {𝕩{(-⌊0.5×𝕩-≢𝕨)⌽𝕩↑𝕨}¨<⌈´≢¨𝕩}⎊⊢ l<⊸∾ (•FLines ≠◶"24.in"‿⊑ •args 3 | 4 | r ← i='>' 5 | l ← i='<' 6 | u ← i='^' 7 | d ← i='v' 8 | B ← {𝕊: (𝕩⌽u) ∨ ((-𝕩)⌽d) ∨ ((-𝕩)⌽˘r) ∨ 𝕩⌽˘l} 9 | 10 | b0 ← 0¨ i 11 | To ← { g 𝕊 m0‿i: 12 | 1+⊑{i‿m: ⟨i+1, (m0∨(⊢∨»∨«∨»˘∨«˘)m)∧¬B i+1⟩}•_while_{i‿m: ¬g⊑m} i⋈b0 13 | } 14 | 15 | m0 ← 1⌾⊑ 0¨ i 16 | •Show ¯1‿¯1 To m0⋈0 17 | •Show ¯1‿¯1 To m0⋈ 0‿0 To (⌽⌽˘m0)⋈ ¯1‿¯1 To m0⋈0 -------------------------------------------------------------------------------- /2022/BQN/25.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | l ← {+´(⌽5⋆↕≠𝕩)ׯ2+𝕩⊐˜"=-012"}¨ •FLines ≠◶"25.in"‿⊑ •args 3 | r ← "" 4 | {r∾↩ n←¯5⊸+⍟(>⟜2) 5|𝕩 ⋄ 5÷˜ 𝕩-n}•_while_{0≠𝕩} +´l 5 | •Out "=-012" ⊏˜ 2+⌽r -------------------------------------------------------------------------------- /2022/BQN/25.in: -------------------------------------------------------------------------------- 1 | 2==-1222101=0= 2 | 12200 3 | 1=1 4 | 1=20-212 5 | 100 6 | 1202=0==111--2 7 | 101-0--0-1 8 | 1=0--=2=1-- 9 | 10=0=02 10 | 1102-1 11 | 2110 12 | 20-12- 13 | 1110=-12-0=-0-01-0 14 | 1-11===212=102--- 15 | 1=22=00201122-1 16 | 10=02=11-212=-0=00 17 | 2020222-211=202 18 | 12=0-00 19 | 1=2--2--01--=-1010 20 | 1=011--0=002 21 | 1-212--=- 22 | 1020==01--1---0 23 | 11==0==2-2=011-= 24 | 20=000-1--=-00 25 | 2202=21-1 26 | 2--10=12-- 27 | 1000 28 | 111022 29 | 2-022 30 | 1=20==01=2-= 31 | 1-10=-- 32 | 2=2=21-=11-1--===20 33 | 1==02100 34 | 1-=0 35 | 2=021-0122= 36 | 1-0== 37 | 1220-0-0=2 38 | 1=0-0=2-=1 39 | 10=112201=102 40 | 12=0-2-112 41 | 1==22102-2 42 | 112-0=02=100 43 | 1-020- 44 | 1001-20- 45 | 20-2=1=0110 46 | 10=-==- 47 | 1-12 48 | 20-10==- 49 | 1=1111022202=2101 50 | 1==1=1-= 51 | 10100 52 | 2=21--1=11= 53 | 22002-021=02 54 | 22 55 | 1=101-0010111---2=== 56 | 1==20 57 | 1=2211001=-2001=-0 58 | 210==-=222-1 59 | 1=2-0=0 60 | 1-=12-2 61 | 20-1-0=--- 62 | 220==121= 63 | 1-1110=2=-0- 64 | 1012-01=0002 65 | 102=121-0-002==- 66 | 2200=2-120212 67 | 1=1=22--2 68 | 1=2=220-0-22=01 69 | 2101-10-1-21 70 | 2-0-1=--2==-01-2 71 | 1000-=0210=-- 72 | 1==11=-=1=0-1 73 | 1=0020-12-=- 74 | 1-220--1=-10-202- 75 | 1=-1===-2==02==-=21 76 | 21 77 | 2=2==-0-=2=-1-11= 78 | 1-0==20011201 79 | 1-100= 80 | 21=212-022=2- 81 | 2-10110=-0-2=1=-0= 82 | 1012 83 | 1==1== 84 | 1-11=0 85 | 1-20-0=-21111202== 86 | 1=0=012=-=2=10- 87 | 1=2=1 88 | 10-0 89 | 100-22==02 90 | 1===121-0=0===1-2=2 91 | 1-=-1-10222=12- 92 | 1==-0--0=2=2-2 93 | 10=2=11220021-= 94 | 211 95 | 202-0=11-=== 96 | 1==0 97 | 1--0221-0 98 | 1=-=000-2=101-0 99 | 1- 100 | 1=02=2 101 | 1== 102 | 1---==1-- 103 | 20=0 104 | 22-=1-011=2 105 | 12-112= 106 | 102==-=-021021=-=-2 107 | 1--2=2=100=20 108 | 12--10=0=2==2==2 109 | 1221-=2112-211 110 | 1222=12-110 111 | 2=--11 112 | 2-02 113 | 20222=-1222== 114 | 20-==- 115 | 2=-021 116 | 111-110-1= 117 | 10200=2010110 118 | 21==0= 119 | 101 120 | 1=-=-=1=1 121 | 21002010-20=- 122 | 1-122=02=2=2022=1 123 | 112--=221 124 | 1210 125 | 2=102=01102221-2 126 | 1=-1102111 127 | 2=- 128 | 2110=0==2-11001 129 | 1-00021100=-011 130 | 11 131 | 1=- 132 | 10000=00-=2=1-0 133 | 10= -------------------------------------------------------------------------------- /2022/BQN/3.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | l ← {m←𝕩≥'a' ⋄ 1+(26׬m)+𝕩-m⊏"Aa"}¨ •FLines ≠◶"3.in"‿⊑ •args 3 | 4 | •Show +´ {⋈⁼⍷(∊/⊣)´𝕩}¨ {<˘2‿∘⥊𝕩}¨ l 5 | •Show +´ {⋈⁼⍷(∊/⊣)´𝕩}¨ <˘∘‿3⥊ l -------------------------------------------------------------------------------- /2022/BQN/4.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Ints⟩ ← •Import "../../utils/utils.bqn" 3 | l ← <˘¨ 2‿2⊸⥊¨ Ints¨ •FLines ≠◶"4.in"‿⊑ •args 4 | 5 | •Show +´{∨´{⟨a‿b,c‿d⟩: (a≥c)∧b≤d}¨⟨𝕩,⌽𝕩⟩}¨ l 6 | •Show +´ {⟨a‿b,c‿d⟩: ¬(bd}¨ l 7 | -------------------------------------------------------------------------------- /2022/BQN/5.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Ints, SplitF⟩ ← •Import "../../utils/utils.bqn" 3 | a‿b ← ⟨⟩SplitF •FLines ≠◶"5.in"‿⊑ •args 4 | 5 | i ← ' '⊸≠⊸/¨ <˘ ¯1↓˘ {1+4×↕4÷˜1+≠𝕩}⊸⊏ ⍉> a 6 | 7 | _m ← { Op _𝕣 𝕩: 8 | ⊑¨ i {𝕨𝕊n‿s‿d: 9 | s‿d-↩1 10 | ∾˜⟜(Op n↑s⊑𝕨)⌾(d⊸⊑) n⊸↓⌾(s⊸⊑) 𝕨 11 | }˜´⌽ Ints¨ 𝕩 12 | } 13 | 14 | •Out ⌽ _m b 15 | •Out ⊢ _m b -------------------------------------------------------------------------------- /2022/BQN/6.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | l ← •FChars ≠◶"6.in"‿⊑ •args 3 | 4 | •Show 4+⊑/∧´∘∊˘ 4↕l 5 | •Show 14+⊑/∧´∘∊˘14↕l -------------------------------------------------------------------------------- /2022/BQN/7.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Num⟩ ← •Import "../../utils/utils.bqn" 3 | l ← •FLines ≠◶"7.in"‿⊑ •args 4 | 5 | curr ← ⟨⟩ 6 | fs ← @⊸≢¨⊸/ {(⊑𝕩){ 7 | "$ cd /"𝕊𝕩: curr ↩ ⟨⟩ ⋄ @; 8 | "$ cd .."𝕊𝕩: curr ↓˜↩ ¯1 ⋄ @; 9 | "$ ls"𝕊𝕩: { 10 | a ← (𝕩⊑∘⊐¨' ') ↑¨ 𝕩 11 | curr ⋈ +´ Num¨ "dir"⊸≢¨⊸/ a 12 | }𝕩; 13 | "$ cd "≡5↑𝕨? curr∾↩ <5↓𝕨 ⋄ @; 14 | •Exit 1 15 | }1↓𝕩}¨ (¯1+`'$'=⊑¨l)⊔l 16 | 17 | dszs ← ⟨⟩ 18 | used ← { 19 | ps ← ⊑¨ 𝕩 20 | cl ← 0=≠¨ ps 21 | msz ← +´1⊑¨cl/𝕩 22 | chsz ← +´ 𝕊∘({1⊸↓⌾⊑¨ 𝕩})¨ (⊐⊑¨(¬cl)/ps) ⊔ (¬cl)/𝕩 23 | dszs∾↩ msz+chsz 24 | msz+chsz 25 | } fs 26 | 27 | •Show +´≤⟜100000⊸/ dszs 28 | 29 | need ← 30000000 - 70000000-used 30 | •Show {(⊑/need≤𝕩)⊑𝕩} ∧dszs -------------------------------------------------------------------------------- /2022/BQN/8.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | l ← >'0'-˜•FLines ≠◶"8.in"‿⊑ •args 3 | 4 | F ← {𝕩>(¯1¨⊏𝕩)»⌈`𝕩} 5 | •Show +´⥊ (F ∨ F⌾⌽ ∨ F⌾⍉ ∨ F⌾(⌽⍉)) l 6 | 7 | t ← ⍉l 8 | •Show ⌈´⥊ l {𝕨𝕊a‿b: ×´ (+´∧`)¨ 1»¨ 𝕨 > ⟨(1+b)↓a⊏l, ⌽b↑a⊏l, (1+a)↓b⊏t, ⌽a↑b⊏t⟩}¨ ↕≢l -------------------------------------------------------------------------------- /2022/BQN/9.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Ints⟩ ← •Import "../../utils/utils.bqn" 3 | l ← {(Ints 𝕩)/(𝕩∊"RLUD")/𝕩} •FChars ≠◶"9.in"‿⊑ •args 4 | 5 | dx ← +`("RL"⊐l)⊏1‿¯1‿0 6 | dy ← +`("UD"⊐l)⊏¯1‿1‿0 7 | hps ← <˘⍉dx≍dy 8 | 9 | Step ← { t𝕊h: 10 | 1<⌈´|h-t? t+×h-t; 11 | 𝕨 12 | } 13 | F ← (<0‿0) Step` ⊢ 14 | 15 | •Show¨ +´∘∊¨ F⍟1‿9 hps -------------------------------------------------------------------------------- /2022/BQN/test.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | { 3 | exp ⇐ ⟨ 4 | ⟨"1", 70369‿203002⟩ 5 | ⟨"2", 12276‿9975⟩ 6 | ⟨"3", 8252‿2828⟩ 7 | ⟨"4", 588‿911⟩ 8 | ⟨"5", "RFFFWBPNS"‿"CQQBBJFCS"⟩ 9 | ⟨"6", 1287‿3716⟩ 10 | ⟨"7", 1243729‿4443914⟩ 11 | ⟨"8", 1676‿313200⟩ 12 | ⟨"9", 6271‿2458⟩ 13 | ⟨"10", 17180‿"### #### # # ### ### # # # ### "‿"# # # # # # # # # # # # # # "‿"# # ### #### # # # # # # # ### "‿"### # # # ### ### # # # # ##"‿"# # # # # # # # # # # # ##"‿"# # #### # # # # # #### ## ### "⟩ 14 | ⟨"11", 55944‿15117269860⟩ 15 | ⟨"12", 472‿465⟩ 16 | ⟨"13", 5013‿25038⟩ 17 | ⟨"14", 578‿24377⟩ 18 | ⟨"15", 4861076‿10649103160102⟩ 19 | ⟨"16", 1376‿1933⟩ 20 | ⟨"17", 3181‿1570434782634⟩ 21 | ⟨"18", 4636‿2572⟩ 22 | ⟨"19", 1365‿4864⟩ 23 | ⟨"20", 7228‿4526232706281⟩ 24 | ⟨"21", 276156919469632‿3441198826073⟩ 25 | ⟨"22", 144244‿138131⟩ 26 | ⟨"23", 3996‿908⟩ 27 | ⟨"24", 225‿711⟩ 28 | ⟨"25", ⟨"2-==10--=-0101==1201"⟩⟩ 29 | ⟩ 30 | 31 | path ⇐ •path 32 | args ⇐ •args 33 | all ⇐ •Repr¨ 1+↕25 34 | most ⇐ (¬∊⟜"15"‿"16"‿"19")⊸/all 35 | orig ⇐ ⟨⟩ 36 | } •Import "../../utils/test.bqn" 37 | -------------------------------------------------------------------------------- /2022/BQN/utils.bqn: -------------------------------------------------------------------------------- 1 | # ⟨RB, B10, Num, SInt, Ints, SInts, Split, SplitF, ReIndex, MDSlash, Unjoin, Count, GCD, LCM, _fx_, _fi_, T⟩ ← •Import "../../utils/utils.bqn" 2 | RB ⇐ 2⊸×⊸+˜´⌽ 3 | B10 ⇐ 10⊸×⊸+˜´∘⌽ 4 | Num ⇐ B10 -⟜'0' 5 | SInt ⇐ {'-'=⊑𝕩?-Num 1↓𝕩;Num𝕩} 6 | Ints ⇐ {Num¨((¯1+⊢×·+`»⊸<)𝕩∊'0'+↕10)⊔𝕩} 7 | SInts ⇐ {SInt¨((¯1+⊢×·+`»⊸<)𝕩∊'-'∾'0'+↕10)⊔𝕩} 8 | Split ⇐ {((¯1+⊢×·+`»⊸<)¬𝕩=𝕨)⊔𝕩} 9 | SplitF ⇐ {((¯1+⊢×·+`»⊸<)¬𝕨⊸≡¨𝕩)⊔𝕩} 10 | ReIndex ⇐ {<˘𝕨|⎉1⌊𝕩÷⌜1«×`⌾⌽𝕨} # convert indices into a shape-⟨×´𝕨⟩ array into corresponding indices into a shape-𝕨 one 11 | MDSlash ⇐ {(0.13×≠⥊𝕩)>+´⥊𝕩? (≢𝕩) ReIndex /⥊𝕩; /○⥊⟜↕⟜≢ 𝕩} 12 | Unjoin ⇐ {((≠¨𝕨)/↕≠𝕨) ⊔ 𝕩} 13 | Count ⇐ ⊐˜∘⊣⊏ ≠∘⊣↑ /⁼∘⊐ 14 | GCD ⇐ {𝕨(|𝕊⍟(>⟜0)⊣)𝕩} 15 | LCM ⇐ ×÷GCD 16 | _fx_ ⇐ {F _𝕣_ 𝔾 𝕩: ⊑{p‿n:n⋈F n}•_while_(𝔾´) 𝕩⋈𝔽𝕩} # fixed-point 17 | _fi_ ⇐ {F _𝕣_ 𝔾 𝕩: ¯1↓{𝕩∾𝕩; 20 | 1≡=⊑𝕩? <˘⍉⁼>𝕩; 21 | =⊸{<⎉𝕨⍉⍟𝕨>𝕩} 𝕩 22 | } # depth-transpose 23 | -------------------------------------------------------------------------------- /2023/BQN/1.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← •FLines 2023‿1 Input •args 4 | 5 | •Show +´•Parsefloat¨ {𝕩⊏˜0‿¯1⊏/𝕩∊'0'+↕10}¨ l 6 | 7 | names ← (⋈¨'0'+↕10)≍@‿"one"‿"two"‿"three"‿"four"‿"five"‿"six"‿"seven"‿"eight"‿"nine" 8 | •Show +´•Parsefloat¨ { 9 | p ← ∾¨˝ /¨ ⍷⟜𝕩¨ names 10 | '0'+{⊑/∨´¨𝕩}¨ ((⌊´⋈⌈´) ∾p) =

•FLines 2023‿10 Input •args 4 | 5 | l ← m∊"-J7S" 6 | r ← m∊"-FLS" 7 | u ← m∊"|LJS" 8 | d ← m∊"|F7S" 9 | 10 | l∧↩ »˘r 11 | r∧↩ «˘l 12 | u∧↩ » d 13 | d∧↩ « u 14 | 15 | ludr ← l‿u‿d‿r 16 | delta ← ⟨0‿¯1, ¯1‿0, 1‿0, 0‿1⟩ 17 | masks ← ⌽≠⟜< ↕4 18 | 19 | dist ← ⊑ {m‿i‿p: 20 | pp←4↓¨p 21 | ⟨ 22 | {i¨⌾((0=𝕩)⊸/)𝕩}⌾(pp⊸⊑)m, 23 | i+1, 24 | ∾pp {(𝕩/masks) (⊣∾𝕨+⊢)¨ 𝕩/delta}¨ (4↑¨p) ∧ <˘pp⊑⌜ludr 25 | ⟩ 26 | }_fx_(≢○⊑) ⟨0¨ m, 1, 1‿1‿1‿1⊸∾¨ MDSlash m='S'⟩ 27 | 28 | •Show ¯1 + ⌈´⥊dist 29 | 30 | mw ← dist≠0 # mini walls 31 | xw ← {mw‿l‿r‿u‿d: mw ∨´ ⟨»d,«u,»˘r,«˘l⟩} {(∧⌜´ ⥊⟜0‿1‿0¨ 3×≢𝕩) ∧ ⟨<3,<3⟩ / mw×𝕩}¨ mw‿l‿r‿u‿d # expanded walls 32 | 33 | f ← {𝕩 ∨´ (¬xw)⊸ר ⟨»𝕩,«𝕩,»˘𝕩,«˘𝕩⟩}_fx_≢ xw < 1(»˘∨«˘) 1¨∘⊏⊸(»∨«) 0¨ xw # flood fill into expanded space 34 | •Show +´⥊¬ mw ∨ (⥊⟜0‿1‿0¨ ≢f) / f -------------------------------------------------------------------------------- /2023/BQN/10_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨_fx_, Input⟩ ← •Import "../../utils/utils.bqn" 3 | m ← >•FLines 2023‿10 Input •args 4 | l ← m∊"-J7S" 5 | r ← m∊"-FLS" 6 | u ← m∊"|LJS" 7 | d ← m∊"|F7S" 8 | 9 | l∧↩»˘r 10 | r∧↩«˘l 11 | u∧↩»d 12 | d∧↩«u 13 | 14 | max ← 1+≠⥊m 15 | dist ← {a←1+𝕩 ⋄ 𝕩⌊´⟨(max׬d)+«a,(max׬u)+»a,(max׬l)+»˘a,(max׬r)+«˘a⟩}_fx_≢ 1¨⌾((⥊m='S')⊸/○⥊) max¨m 16 | •Show ¯1+⌈´max⊸≠⊸/ ⥊dist 17 | 18 | mw ← dist≠max 19 | # e ← {𝕩 ∨´ (¬mw)⊸ר ⟨»𝕩,«𝕩,»˘𝕩,«˘𝕩⟩}_fx_≢ mw<1(»˘∨«˘)1¨∘⊏⊸(»∨«)0¨ m 20 | # •Show dist≠max 21 | # •Show (¬e∨mw) ∧ m='.' 22 | # •Show (≠⥊m) - (+´⥊mw) + +´⥊e 23 | 24 | xw ← {mw‿l‿r‿u‿d: mw ∨´ ⟨»d,«u,»˘r,«˘l⟩} {(∾(1‿1⊸≡¨↕3‿3)¨𝕩)×3/˘3/𝕩}¨ mw⊸רmw‿l‿r‿u‿d 25 | 26 | e ← {𝕩 ∨´ (¬xw)⊸ר ⟨»𝕩,«𝕩,»˘𝕩,«˘𝕩⟩}_fx_≢ xw<1(»˘∨«˘)1¨∘⊏⊸(»∨«) 0¨ xw 27 | •Show +´⥊¬mw∨(⥊⟜0‿1‿0¨ ≢e)/e -------------------------------------------------------------------------------- /2023/BQN/11.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨MDSlashI, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← '#'=>•FLines 2023‿11 Input •args 4 | 5 | Dist ← {+´∾ +´| (¯1↓↑)¨⊸- MDSlashI 𝕩} 6 | 7 | dx ← Dist ⟨1+¬∨˝˘l, 1+¬∨˝l⟩/l # distances when expanded to 2 separating spaces 8 | •Show dx 9 | dr ← Dist l # distances when not expanded 10 | •Show dr + (1e6-1)×dx-dr # extrapolate behavior at 1000000 11 | -------------------------------------------------------------------------------- /2023/BQN/11_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨MDSlash, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← >'#'=•FLines 2023‿11 Input •args 4 | x ← ⟨1+¬∨˝˘l, 1+¬∨˝l⟩/l 5 | •Show a←2÷˜+´⥊{+´|𝕨-𝕩}⌜˜ MDSlash x 6 | y ← ⟨1+2׬∨˝˘l, 1+2׬∨˝l⟩/l 7 | b←2÷˜+´⥊{+´|𝕨-𝕩}⌜˜ MDSlash y 8 | •Show a + (1e6-2)×b-a 9 | -------------------------------------------------------------------------------- /2023/BQN/12.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Ints, Split, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← (Ints⌾(1⊑⊢) ' 'Split⊢)¨ •FLines 2023‿12 Input •args 4 | 5 | Fn ← { 𝕊 l‿i: 6 | max ← ⌈´i 7 | in ← ≠i 8 | i∾↩ ¯1 9 | n‿s‿o ← ⟨1⟩‿⟨0⟩‿⟨0⟩ # count,streak,offset 10 | { 𝕊 c: 11 | r1 ← {⊑c∊"?#"? m←s»𝕩)-˜/𝕩>«𝕩}¨ (l='#')⊸+¨ ⥊↕1+l='?' 9 | }¨ l 10 | 11 | •Show +´ { 12 | l‿n ← ' 'Split𝕩 13 | i ← Ints n 14 | l ↩ ¯1↓∾5⥊¨ ⟨⟩SplitF •FLines 2023‿13 Input •args 4 | # slightly overcomplicated solution, somewhat optimized for perf 5 | # converting inputs to bitarrs is slightly not worth it (even if padding cells to closest 8/16/32) 6 | 7 | p ← > {{((𝕩¨∾↓⟜𝕩¨) 1+2×i) {+´⥊ (𝕩↑𝕨) ≠ ⌽𝕩↑𝕩↓𝕨}¨ 1 + i ∾ ⌽i←↕⌊2÷˜≠𝕩}¨ ⟨𝕩,⍉𝕩⟩}¨ l 8 | •Show +´ ∾ ⥊ 100‿1 ×⎉1 {1+/0=𝕩}¨ p 9 | •Show +´ ∾ ⥊ 100‿1 ×⎉1 {1+/1=𝕩}¨ p 10 | -------------------------------------------------------------------------------- /2023/BQN/13_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨SplitF,Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← >¨ ⟨⟩SplitF •FLines 2023‿13 Input •args 4 | 5 | •Show +´⋈⁼¨ ∾¨ 100‿1⊸ר {1+/{≡´𝕩↑˜¨-⌊´≠¨𝕩}¨ ¯1↓1↓<˘⍉>⟨↑𝕩,⌽¨↓𝕩⟩}¨∘{⟨𝕩,⍉𝕩⟩}¨ l 6 | •Show +´⋈⁼¨ ∾¨ 100‿1⊸ר {1+/{1=+´⥊≠´𝕩↑˜¨-⌊´≠¨𝕩}¨ ¯1↓1↓<˘⍉>⟨↑𝕩,⌽¨↓𝕩⟩}¨∘{⟨𝕩,⍉𝕩⟩}¨ l 7 | -------------------------------------------------------------------------------- /2023/BQN/14.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨_fx_,Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← ".#O" ⊐ >•FLines 2023‿14 Input •args 4 | 5 | 6 | inds ← ⥊ (≠l) / ≍↕1+≠l 7 | main ← ⥊ (1¨l) ∾˘ 0¨⊏l 8 | zero ← ⥊ 0⥊˜1‿0+≢l 9 | Move ← { # filter a whole-array scan 10 | wm‿t1j‿tj1‿tj1s𝕊𝕩: 11 | cs ← tj1 / ⥊⍉ (⊢∾¯1⊏⊢) +`2=𝕩 12 | ls ← cs - »cs × tj1 / main 13 | xs ← / tj1s 14 | ys ← ls + t1j / inds 15 | wm -˜ 2׬ ≠` wm ≠ (≢𝕩) ⥊ 1¨⌾((xs+ys×≠𝕩)⊏⊢) zero 16 | } 17 | pre0‿pre1‿pre2‿pre3 ← { 18 | row1 ← 1¨⊏l 19 | wm ← 1=𝕏 l 20 | t1j ← ⥊⍉ row1 ∾ wm 21 | tj1m ← ⍉ wm ∾ row1 22 | tj1 ← ⥊ tj1m 23 | tj1s ← +˝˘tj1m 24 | wm‿t1j‿tj1‿tj1s 25 | }¨ ⟨⊢, ⍉, ⌽, ⌽⍉⟩ 26 | 27 | # l⊏↩1‿0‿2 ⋄ Move ↩ {𝕩 + «⊸- (1=»𝕩)∧2=𝕩}_fx_≢∘⊢ # simple iterative moving 28 | 29 | Load ← {+´ (+˝˘ × 1+⌽∘↕∘≠) 2=𝕩} 30 | 31 | •Show Load pre0 Move l 32 | 33 | p ← ⟨⟩ 34 | v ← l 35 | 36 | m ← •HashMap˜ ⟨⟩ 37 | { 𝕊: 38 | p∾↩ •FLines 2023‿14 Input •args 4 | TL ← {m ← ('.'=»𝕩)∧'O'=𝕩 ⋄ (≢𝕩)⥊ 'O'¨⌾((⥊«m)⊸/) '.'¨⌾((⥊m)⊸/) ⥊𝕩}_fx_≢ 5 | •Show +´1+⊑¨MDSlash⌽ 'O'=TL l 6 | 7 | m←•HashMap˜⟨⟩ 8 | i←0 9 | pr←⟨⟩ 10 | Q ← { 11 | r←TL⌾(⌽⍉) TL⌾⌽ TL⌾⍉ TL 𝕩 12 | pr∾↩⌜3+↕8) × (/1∾cc)+⌜1+↕8) ⊏ '!'∾c # extract keys & map them to numbers 27 | lo ← ∊⌾⌽ kn # last occurence mask 28 | fv ← ('0'-˜lo/vs)⌾((lo/kn)⊸⊏) ¯2⥊˜1+⌈´kn # map of key to final value 29 | ld ← ¬kn∊(lo∧vd)/kn # entries whose last operation is deletion 30 | kn‿vd ld⊸/¨ ↩ # filter those out 31 | pm ← ⊒⌾⌽ kn # progressive counter 32 | wi ← ⍉[kn,pm] # reference index for each key 33 | ti ← { # target reference index - first entry after last deletion 34 | c ← /⁼ kn # fake progressive counter for each unique key for fake deletes 35 | s ← (1+↕≠c) ∾ vd×1+kn # a non-zero element n at index i means that, at time i (which can be of a fake delete), key n+1 is deleted 36 | ⍉¯1 + s ≍○((∊⌾⌽s)⊸/) c∾pm # filter progressive counters to last delete, sort by key 37 | } 38 | tm ← wi∊ti # mask of entries at the target 39 | cb ← ≠⟜» /tm/ld/ea # beginning of included in partially-filtered hash list 40 | fhs ← 256 | (1«cb) / cb (ea/(≠ea)↑/⁼tm//ld)⊸/ _hashTil cs # final filtered hashes 41 | fvs ← (tm/kn) ⊏ fv # final filtered values 42 | fvs×↩ 1 + ⊒fhs # multiply values by their index in their bucket 43 | •Show +´ fvs × 1+fhs # multiply by bucket number, sum -------------------------------------------------------------------------------- /2023/BQN/15_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Split,Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← ','Split•FChars 2023‿15 Input •args 4 | 5 | H←{0{256|17×𝕩+𝕨}´⌽𝕩-@} 6 | •Show +´{0{256|17×𝕩+𝕨}´⌽𝕩-@}¨ l 7 | i←{⊑'='∊𝕩? '='Split 𝕩; ⟨¯1↓𝕩,@⟩}¨ l 8 | •Show +´∾(1+↕∘≠)⊸× {(1+↕∘≠)⊸וParseFloat¨ 1⊑¨𝕩}¨ (256⥊<⟨⟩) {𝕨𝕊a‿@: ((a≢⊑)¨⊸/)⌾((H a)⊸⊑) 𝕨; 𝕨𝕊a‿b: 𝕩⊸{m←(a≡⊑)¨𝕩⋄𝕨{∨´m? 𝕨¨⌾(m⊸/)𝕩; 𝕩∾<𝕨}𝕩}⌾((H a)⊸⊑) 𝕨}˜´⌽ i -------------------------------------------------------------------------------- /2023/BQN/16.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨_fx_, Tr,Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← >•FLines 2023‿16 Input •args 4 | # direction lists: up, right, down, left 5 | 6 | rots ← ⟨⊢, ⌽⍉, ⌽⌽˘, ⍉⌽⟩ 7 | n ← ≠l 8 | ym‿xm ← Tr ↕≢l 9 | iota ← ↕n 10 | 11 | info ← >(↕4) { dir 𝕊 𝕩: 12 | c ← ⊏⟜"/\-|." ("/\-|"‿"\/|-"⊑˜2|dir) ⊐ 𝕏l # unrotate to going upwards, aka negative y, aka towards ¯∞‿0 13 | e ← c ∊ "/\-" 14 | dist ← (⊢-·⌈`e×⊢)↕≠e 15 | ib ← ∨`e # in-bounds 16 | end ← ↕∘≠⊸- dist 17 | tgt ← (iota +⎉1 end×n) ⊏ ⥊c 18 | 𝕏⁼ Tr ⟨dist, (xm≠0)∧ib∧tgt∊"-\", (xm≠n-1)∧ib∧tgt∊"-/"⟩ 19 | }¨ rots 20 | 21 | 22 | memo ← •HashMap˜⟨⟩ 23 | deltas ← ⟨¯1‿0, 0‿1, 1‿0, 0‿¯1⟩ 24 | Get ← { 25 | dir‿y0‿x0: 26 | 𝕩 memo.Set 0 27 | dist‿l‿r ← 𝕩⊑info 28 | y1‿x1 ← y0‿x0 + dist×dir⊑deltas 29 | me ← { 30 | 2|dir? x0s‿x1s←∧x0‿x1 ⋄ (iota=y0) ∧⌜ (iota≥x0s) ∧ iota≤x1s; 31 | y0s‿y1s←∧y0‿y1 ⋄ (iota=x0) ∧⌜˜ (iota≥y0s) ∧ iota≤y1s 32 | } 33 | res ← me ∨´ (4|dir+3‿1) { 34 | ¬𝕩? 0; 35 | Get 𝕨 ∾ y1‿x1 + 𝕨⊑deltas 36 | }¨ l‿r 37 | 𝕩 memo.Set res 38 | res 39 | } {memo.Has 𝕩? memo.Get 𝕩; 𝔽 𝕩} 40 | 41 | Count ← { 42 | memo ↩ •HashMap˜⟨⟩ 43 | +´⥊Get 𝕩 44 | } 45 | 46 | •Show Count ⟨1,0,0⟩ 47 | 48 | •Show ⌈´ ∾⟨ 49 | {Count ⟨0, n-1, 𝕩⟩}¨ iota # up 50 | {Count ⟨1, 𝕩, 0⟩}¨ iota # right 51 | {Count ⟨2, 0, 𝕩⟩}¨ iota # down 52 | {Count ⟨3, 𝕩, n-1⟩}¨ iota # right 53 | ⟩ 54 | -------------------------------------------------------------------------------- /2023/BQN/16_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← >•FLines 2023‿16 Input •args 4 | 5 | lt ← {⊏⟜".-|\/" ".|-/\"⊐⍉⌽𝕩}⍟(↕4) l 6 | z←0¨l 7 | S←{{∨˝¯1‿0‿1⌽˘⍉>𝕩} lt {⌽⟨⌽⍉«˘𝕩∧𝕨∊"-/", »𝕩∧¬𝕨∊"-/\", ⍉⌽»˘𝕩∧𝕨∊"-\"⟩}¨𝕩} 8 | # F←{+´⥊×+´{⌽∘⍉⍟𝕩𝕨}¨⟜↕⟜≠ ×+´ S⍟(↕1000) 𝕩} # thing that originally got me the solution 9 | F←{ # thing I was in the middle of writing while waiting for the above to finish; finished outside of the initial solve 10 | +´⥊∨´ {⌽∘⍉⍟𝕩𝕨}¨⟜↕⟜≠ 1⊑{a‿m‿b: t←S⍟(↕10) b ⋄ m2←m∨´t ⋄ ⟨m≢m2,m2,¯1⊑t⟩}•_while_⊑ ⟨1,4⥊•FLines 2023‿17 Input •args 4 | n ← ≠l 5 | 6 | { 𝕊 jmin‿jmax: 7 | jmask ← jmin≤↕jmax 8 | ls ← ⟨0,0⟩ 9 | is ← 0‿1 ⋄ isC←2 10 | ns ← is •HashMap ⟨0‿0‿0,3‿0‿0⟩ 11 | todo ← (4∾≢l)⥊1 12 | 13 | deltas ← ⟨¯1‿0, 0‿1, 1‿0, 0‿¯1⟩ 14 | res ← ∞ 15 | Step ← { 𝕊: 16 | min ← ⊑⌊´⊸(⊐˜)ls 17 | k ← dir‿y‿x ← ns.Get mk←min⊑is 18 | len ← min⊑ls 19 | ls ↩ (min↑ls)∾(min+1)↓ls 20 | is ↩ (min↑is)∾(min+1)↓is 21 | ns.Delete mk 22 | k⊑todo? 23 | {𝕊: res⌊↩len}⍟⊢ y‿x≡¯1+≢l 24 | todo 0⌾(dir‿y‿x⊸⊑)↩ 25 | { 26 | p ← (y‿x+(𝕩⊑deltas)×⊢)¨ 1+↕jmax⌊𝕩⊑⟨y,n-x+1,n-y+1,x⟩ 27 | p≢⟨⟩? 28 | k2 ← 𝕩∾¨p 29 | m ← (k2⊑todo) ∧ (≠p)↑jmask 30 | vs ← m/k2 31 | ls∾↩ ×∘≠◶⟨↕0,•internal.Squeeze⟩ len + m/+`p⊑l 32 | ks ← isC+↕≠vs 33 | is∾↩ ks 34 | ks ns.Set¨ vs 35 | isC+↩ ≠vs 36 | 37 | ;⟨⟩ 38 | }¨ 4|dir+1‿3 39 | ;@ 40 | } 41 | 42 | Step•_while_{𝕊: 0≠≠ls} @ 43 | •Show res 44 | }¨ ⟨0‿3, 3‿10⟩ 45 | -------------------------------------------------------------------------------- /2023/BQN/17_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← '0'-˜>•FLines 2023‿17 Input •args 4 | n ← ≠l 5 | 6 | { 𝕊 jmin‿jmax: 7 | jmask ← jmin≤↕jmax 8 | ls ← ⟨0,0⟩ 9 | ns ← ⟨0‿0‿0‿@,3‿0‿0‿@⟩ 10 | todo ← (4∾≢l)⥊1 11 | path ← (4∾≢l)⥊@ 12 | 13 | deltas ← ⟨¯1‿0, 0‿1, 1‿0, 0‿¯1⟩ 14 | res ← ∞ 15 | Step ← { 𝕊: 16 | min ← ⊑⌊´⊸(⊐˜)ls 17 | k ← 3↑dir‿y‿x‿k0 ← min⊑ns 18 | len ← min⊑ls 19 | ls ↩ (min↑ls)∾(min+1)↓ls 20 | ns ↩ (min↑ns)∾(min+1)↓ns 21 | k⊑todo? 22 | {𝕊: res⌊↩len}⍟⊢ y‿x≡¯1+≢l 23 | todo 0⌾(dir‿y‿x⊸⊑)↩ 24 | # path k0⌾(k⊸⊑)⍟(k≢@)↩ 25 | { 26 | # p ← ((0≤⌊´¨)∧n>⌈´¨)⊸/ (y‿x+(𝕩⊑deltas)×⊢)¨ 1+↕3 27 | # p ← (y‿x+(𝕩⊑deltas)×⊢)¨ 1+↕3⌊𝕩⊑⟨y,n-x+1,n-y+1,x⟩ 28 | p ← (y‿x+(𝕩⊑deltas)×⊢)¨ 1+↕jmax⌊𝕩⊑⟨y,n-x+1,n-y+1,x⟩ 29 | # p!∧´ ((0≤⌊´¨)∧n>⌈´¨)p 30 | p≢⟨⟩? 31 | k2 ← 𝕩∾¨p 32 | m ← (k2⊑todo) ∧ (≠p)↑jmask 33 | # ls∾↩ len + m/+`p⊑l 34 | ls∾↩ ×∘≠◶⟨↕0,•internal.Squeeze⟩ len + m/+`p⊑l 35 | ns∾↩ (m/k2)∾¨<2⊑¨l -------------------------------------------------------------------------------- /2023/BQN/18_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Split,_fx_,Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← ' 'Split¨•FLines 2023‿18 Input •args 4 | d ← ⊑∘⊑¨l 5 | j ← (•ParseFloat 1⊑⊢)¨ l 6 | 7 | d ↩ j/d 8 | w ← {1¨⌾((<˘𝕩)⊸⊑) 0⥊˜1+⌈˝𝕩} c ← {𝕩-⎉1⌊˝𝕩} +`("URDL"⊐d)⊏[¯1‿0, 0‿1, 1‿0, 0‿¯1] 9 | 10 | pos ← (¯1+(⊑/"DL"⍷d)⊏c) # added after finishing to become general; I just tried random positions until it worked 11 | 12 | m ← w∨{(¬w)∧∨´⟨𝕩,»𝕩,«𝕩,»˘𝕩,«˘𝕩⟩}_fx_≢ 1⌾(pos⊸⊑) 0¨w 13 | # •Show ⊏⟜"·#" m 14 | •Show +´⥊m 15 | 16 | q←0 17 | x‿y ← <˘⍉>+`{ 18 | a ← ¯1↓2↓𝕩 19 | d ← ('0'-˜¯1⊑a)⊏[0‿1, 1‿0, 0‿¯1, ¯1‿0] 20 | l←+´(⌽16⋆↕5)×{𝕩+(𝕩≥10)×10+'0'-'a'} (¯1↓a)-'0' 21 | q+↩|l 22 | d × l 23 | }¨ 2⊑¨l 24 | 25 | •Show 1+2÷˜q+|+´(xׯ1⌽y)-yׯ1⌽x -------------------------------------------------------------------------------- /2023/BQN/19.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨SplitF,Split,Input⟩ ← •Import "../../utils/utils.bqn" 3 | workflows‿ratings ← ⟨⟩SplitF •FLines 2023‿19 Input •args 4 | 5 | rk‿rv ← <˘⍉>{ 6 | name‿rest←'{' Split 𝕩 7 | name ⋈ { 8 | cond‿next ← ':' Split 𝕩 9 | gt ← ⊑'>'∊cond 10 | l‿r ← (gt⊑"<>") Split cond 11 | ⟨⊑"xmas"⊐l, gt, •ParseFloat r, {"R": 0; "A": 1; 𝕩} next⟩ # alt: (⊑"R"‿"A"⊐){𝔽} const{𝔽}, next⟩}¨¨ rv 18 | in ← m1.Get "in" 19 | Get ← {c‿r←⊑l←𝕨 ⋄ a←𝕩 ⋄ {¬C a? (1↓l) Get a; 0==r? r; (m1.Get r) Get a}} 20 | •Show +´ {+´in⊸Get⊸× •ParseFloat¨ 2↓¨','Split ¯1↓1↓𝕩}¨ ratings 21 | 22 | 23 | 24 | m2 ← rk •HashMap rv 25 | # done ← •HashMap˜⟨⟩ # caching isn't beneficial 26 | all ← ⟨1‿1‿1‿1, 4001‿4001‿4001‿4001⟩ 27 | Do ← { 28 | # done.Has 𝕩? done.Get 𝕩; 29 | And ← {𝕨𝕊i‿gt‿c‿·: {gt? 𝕩⌈1+c; 𝕩⌊c}⌾(i⊑(¬gt)⊑⊢) 𝕨} 30 | AndN ← {𝕨𝕊i‿gt‿c‿·: {gt? 𝕩⌊1+c; 𝕩⌈c}⌾(i⊑ gt ⊑⊢) 𝕨} 31 | res ← ∾{¯1↓↑𝕩}⊸({0≡3⊑𝕩? ⟨⟩; 1≡3⊑𝕩? ⟨(all And 𝕩) AndN˜´⌽ 𝕨⟩; (⊢ AndN˜´⟜⌽ 𝕨˙)¨ And⟜𝕩¨ Do 3⊑𝕩}¨) m2.Get 𝕩 32 | # 𝕩 done.Set res 33 | res 34 | } 35 | 36 | •Show +´{×´-´⌽𝕩}¨ Do "in" -------------------------------------------------------------------------------- /2023/BQN/19_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨SplitF,Split,Input⟩ ← •Import "../../utils/utils.bqn" 3 | a‿b ← ⟨⟩SplitF •FLines 2023‿19 Input •args 4 | 5 | rules ← { 6 | n‿r←'{' Split 𝕩 7 | n ⋈ { 8 | c‿t←':'Split𝕩 9 | m ← ⋈⁼"<>"(∊/⊣)c 10 | l‿r ← m Split c 11 | ⟨(⊑l⊐˜"xmas")⊸⊑ ((⊑m⊐˜"<>")⊑<‿>){𝔽} (•ParseFloat r){𝔽}, t⟩ 12 | }¨⌾(¯1↓⊢) {1‿𝕩}⌾(¯1⊸⊑) ','Split ¯1↓r 13 | }¨ a 14 | 15 | rm ← •HashMap˝ ⍉>rules 16 | r0 ← rm.Get "in" 17 | Get ← {c‿r←⊑l←𝕨 ⋄ a←𝕩 ⋄ {C a? {1=≠r? r≡"A"; (rm.Get r) Get a}; (1↓l) Get a}} 18 | •Show +´+´¨{{r0 Get 𝕩}⊸× •ParseFloat¨ 2↓¨','Split ¯1↓1↓𝕩}¨ b 19 | 20 | 21 | rules2 ← { 22 | n‿r←'{' Split 𝕩 23 | n ⋈ { 24 | c‿t←':'Split𝕩 25 | m ← ⋈⁼"<>"(∊/⊣)c 26 | l‿r ← m Split c 27 | ⟨⊑l⊐˜"xmas", (⊑m⊐˜"<>")⊑"<>", •ParseFloat r, t⟩ 28 | }¨⌾(¯1↓⊢) {0‿'<'‿4001‿𝕩}⌾(¯1⊸⊑) ','Split ¯1↓r 29 | }¨ a 30 | 31 | rm ↩ •HashMap˝ ⍉>rules2 32 | 33 | done ← •HashMap˜⟨⟩ 34 | 35 | all ← ⟨1‿1‿1‿1, 4001‿4001‿4001‿4001⟩ 36 | Do ← { 37 | done.Has 𝕩? done.Get 𝕩; 38 | 𝕩 done.Set @ 39 | And ← { 40 | i‿o‿c‿· ← 𝕨 41 | {o≡'>'? (1+c)⌈𝕩; c⌊𝕩}⌾(i⊑{o≡'>'?0;1}⊑⊢) 𝕩 42 | } 43 | AndN ← { 44 | i‿o‿c‿· ← 𝕩 45 | {o≡'<'? c⌈𝕩; (c+1)⌊𝕩}⌾(i⊑{o≡'>'?1;0}⊑⊢) 𝕨 46 | } 47 | res ← ∾{¯1↓↑𝕩}⊸({"R"≡3⊑𝕩? ⟨⟩; "A"≡3⊑𝕩? ⟨(𝕩 And all) AndN˜´⌽ 𝕨⟩; (⊢ AndN˜´⟜⌽ 𝕨˙)¨ 𝕩⊸And¨ Do 3⊑𝕩}¨) rm.Get 𝕩 48 | 𝕩 done.Set res 49 | res 50 | } 51 | 52 | •Show +´{×´-´⌽𝕩}¨ Do "in" -------------------------------------------------------------------------------- /2023/BQN/1_fast.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← •file.MapBytes 2023‿1 Input •args 4 | # l ← •file.MapBytes "../inputs/gen/1_10MB.in" 5 | {l ↩ •file.MapBytes ⊑𝕩}⍟≠ •args 6 | ! (@+10) = ¯1⊑l 7 | 8 | _chunkByTo_ ← { F _𝕣_ char‿tgt raw: 9 | {min‿max: F min↓max↑raw}¨ <˘ 2↕⍷ (≠raw)∾˜ 0∾ { 10 | 𝕩 + 1+⊑ (𝕩↓raw) ⊐ char # fast due to slices & short-circuit 11 | }¨ tgt×↕⌈(≠𝕩)÷tgt 12 | } 13 | 14 | names ← "one"‿"two"‿"three"‿"four"‿"five"‿"six"‿"seven"‿"eight"‿"nine"-@ 15 | 16 | kmap ← ∾1↑¨⊔16| +´¨ 2↑¨ 1↓¨ names 17 | c0‿c1‿c2‿c3‿c4 ← <˘⍉>5↑¨ ⊑⟜names¨ kmap 18 | mults ← kmap ⊏ (49+↕9) - ⊑¨names 19 | 20 | 21 | part1 ← 0 22 | part2 ← 0 23 | 24 | FirstLast ← { 25 | 𝕩 ≤⟜('9'-@)⊸/↩ 26 | nls ← 𝕩=10 27 | d0 ← (1»nls) / 𝕩 28 | d1 ← (1«¯1↓nls) / ¯1↓𝕩 29 | ((+´d1) + 10 × +´d0) - 528×≠d0 30 | } 31 | 32 | 33 | { 34 | 𝕩 8‿8•bit._cast↩ 35 | part1+↩ FirstLast 𝕩 36 | 37 | sh1 ← 0«𝕩 38 | sh2 ← 0‿0«𝕩 39 | key ← 16|sh1+sh2 40 | m ← (key⊏c0) = 𝕩 41 | m∧↩ (key⊏c1) = sh1 42 | m∧↩ (key⊏c2) = sh2 43 | m∧↩ (t3=0) ∨ (t3←key⊏c3) = 0‿0‿0«𝕩 44 | m∧↩ (t4=0) ∨ (t4←key⊏c4) = 0‿0‿0‿0«𝕩 45 | 𝕩+↩ (key⊏mults) × m 46 | 47 | part2+↩ FirstLast 𝕩 48 | @ 49 | } {30000≤≠𝕩? 𝔽_chunkByTo_ ⟨@+10, 15000⟩ 𝕩; 𝔽 𝕩} l 50 | 51 | •Show part1 52 | •Show part2 -------------------------------------------------------------------------------- /2023/BQN/2.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Split, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← •FLines 2023‿2 Input •args 4 | 5 | cols ← "red"‿"green"‿"blue" 6 | l { 7 | { 8 | [n,s] ← ⍉>{•ParseFloat⌾⊑ ' 'Split𝕩}¨ ','Split 1↓𝕩 9 | n⌾((cols⊐s)⊸⊏) 3⥊0 10 | }¨ ';'Split 1⊑':'Split 𝕩 11 | }¨ ↩ 12 | 13 | •Show +´1+/ {∧´ {∧´12‿13‿14≥𝕩}¨ 𝕩}¨ l 14 | 15 | •Show +´ {×´ ⌈´ 𝕩}¨ l -------------------------------------------------------------------------------- /2023/BQN/20.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Split, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← {{⟨⊑𝕩⟩∾¯1↓¨2↓𝕩} ' ' Split 𝕩∾","}¨ •FLines 2023‿20 Input •args 4 | l∾↩< ⟨"%rx"⟩ 5 | kn← ⊑¨l 6 | vn← 1↓¨l 7 | v ← (1↓¨kn)⊸⊐¨ vn 8 | t ← ⊑¨kn 9 | k ← ↕≠kn 10 | cc ← ≠¨ ((∾v)∾≠k) ⊔ (≠¨v)/↕≠v # connection count 11 | ! ∧´ (∾v) ∊ k 12 | 13 | 14 | rx ← ¯1+≠k 15 | rx2 ← {/∨´¨ 𝕩⊸∊¨v}⍟2 ⋈rx 16 | 17 | 18 | om ← =⟜<↕≠k # one mask 19 | zm ← ¬om # zero mask 20 | ls ← 0 21 | hs ← 0 22 | first ← ∞¨ k 23 | bpc ← 0 24 | Step ← {𝕊m‿w‿i‿h: 25 | 26 | rxm ← h∧w∊rx2 27 | first bpc¨⊸⌊⌾((rxm/w)⊸⊏) ↩ 28 | 29 | ls+↩ +´¬h 30 | hs+↩ +´h 31 | 32 | f ← '%'=i⊏t 33 | a ← ¬f 34 | 35 | ff ← i/˜f∧¬h 36 | m ¬⌾(ff⊸⊏) ↩ 37 | ffr ← {l←≠¨𝕩 ⋄ ⟨l/ff, ∾𝕩, l/ff⊏m⟩} ff⊏v 38 | 39 | ah ← a∧h 40 | al ← a∧¬h 41 | {m ((𝕩⊑w)⊑om)⊸∨⌾((𝕩⊑i)⊸⊑) ↩ ⋄ 0}¨ /ah 42 | {m ((𝕩⊑w)⊑zm)⊸∧⌾((𝕩⊑i)⊸⊑) ↩ ⋄ 0}¨ /al 43 | anr ← (⊏⟜v)⊸{l←≠¨𝕨⋄⟨l/𝕩, ∾𝕨, l/(𝕩⊏cc) ≠ +´¨𝕩⊏m⟩} a/i 44 | 45 | ⟨m⟩∾ffr∾¨anr 46 | } 47 | 48 | br ← ⊑kn⊐<"broadcaster" 49 | bp0 ← {⟨br¨𝕩,𝕩,0¨𝕩⟩} br⊑v 50 | Press ← {ls+↩1 ⋄ ⊑Step •_while_{×≠1⊑𝕩} ⟨𝕩⟩∾bp0} 51 | 52 | mem0 ← (0¨ t){𝕩≡'&'? 𝕗; 0}¨ t 53 | { 54 | bpc+↩ 1 55 | r ← Press 𝕩 56 | •Show⍟(bpc≡1000) ls×hs 57 | r 58 | }•_while_{𝕊: (bpc<1000) ∨ (≠rx2) ≠ +´ ∞≠first} mem0 59 | 60 | •Show •math.LCM´ ∞⊸≠⊸/ first 61 | -------------------------------------------------------------------------------- /2023/BQN/20_orig_part1.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Split, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← {{⟨⊑𝕩⟩∾¯1↓¨2↓𝕩} ' ' Split 𝕩∾","}¨ •FLines 2023‿20 Input •args 4 | l∾↩< ⟨"%rx"⟩ 5 | kn← ⊑¨l 6 | vn← 1↓¨l 7 | v ← (1↓¨kn)⊸⊐¨ vn 8 | t ← ⊑¨kn 9 | k ← ↕≠kn 10 | conn ← ≠¨ ((∾v)∾≠k)⊔(≠¨v)/↕≠v # conn number 11 | om ← =⟜<↕≠k # one mask 12 | zm ← ¬om # zero mask 13 | 14 | rx ← ¯1+≠k 15 | 16 | # •Show (¬(∾v) ∊ k)/∾vn 17 | 18 | ! ∧´ (∾v) ∊ k 19 | 20 | # •Show¨ t∾¨k∾¨v 21 | br ← ⊑kn⊐<"broadcaster" 22 | mem0 ← (0¨ t){𝕩≡'&'? 𝕗; 0}¨ t 23 | 24 | ls ← 0 25 | hs ← 0 26 | Step ← {m𝕊w‿i‿h: 27 | ! ∧´(rx=i)/h 28 | ls+↩ +´¬h 29 | hs+↩ +´h 30 | # •Out "" 31 | # •Show m 32 | # •Show w‿i‿h 33 | f←'%'=i⊏t 34 | # •Out"--" 35 | # •Show ≡h 36 | # •Show h 37 | # •Show i 38 | ff←i/˜f∧¬h 39 | m¬⌾(ff⊸⊏)↩ 40 | ffr ← {⟨ff/˜≠¨𝕩,∾𝕩,(≠¨𝕩)/ff⊏m⟩} ff⊏v 41 | # •Show 1+ff 42 | ! 1≥≡2⊑ffr 43 | a ← ¬f 44 | ah ← a∧h 45 | al ← a∧¬h 46 | ! 1=≡(t='%')/m 47 | m ↩ m∨{∨´𝕩⊏om}¨ ((ah/i)∾≠m) ⊔ ah/w 48 | ! 1=≡(t='%')/m 49 | m ↩ m∧{∧´𝕩⊏zm}¨ ((al/i)∾≠m) ⊔ al/w 50 | #•Show ah 51 | #•Show al 52 | #•Show a/w 53 | ! 1=≡(t='%')/m 54 | anr ← (⊏⟜v)⊸{l←≠¨𝕨⋄⟨l/𝕩, ∾𝕨, l/(𝕩⊏conn) ≠ +´¨𝕩⊏m⟩} a/i 55 | ! 1≥≡2⊑anr 56 | # •Show v 57 | # •Out "" 58 | ⟨m, ffr∾¨anr⟩ 59 | } 60 | 61 | bp0 ← {⟨br¨𝕩,𝕩,0¨𝕩⟩} br⊑v 62 | Press ← {ls+↩1 ⋄ ⊑ Step´•_while_{×≠⊑1⊑𝕩} 𝕩‿bp0} 63 | Press⍟1000 mem0 64 | # •Show ls 65 | # •Show hs 66 | •Show ls×hs 67 | -------------------------------------------------------------------------------- /2023/BQN/20_orig_part2.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Split, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← {{⟨⊑𝕩⟩∾¯1↓¨2↓𝕩} ' ' Split 𝕩∾","}¨ •FLines 2023‿20 Input •args 4 | l∾↩< ⟨"%rx"⟩ 5 | kn← ⊑¨l 6 | vn← 1↓¨l 7 | v ← (1↓¨kn)⊸⊐¨ vn 8 | t ← ⊑¨kn 9 | k ← ↕≠kn 10 | conn ← ≠¨ ((∾v)∾≠k)⊔(≠¨v)/↕≠v # conn number 11 | om ← =⟜<↕≠k # one mask 12 | zm ← ¬om # zero mask 13 | 14 | 15 | # •Show¨ ∾(kn)(<⊸(⋈¨))¨vn 16 | # •Exit 0 17 | 18 | rx ← ¯1+≠k 19 | 20 | rx3 ← {/∨´¨ 𝕩⊸∊¨v}⍟2 ⋈rx 21 | 22 | # •Show (¬(∾v) ∊ k)/∾vn 23 | 24 | ! ∧´ (∾v) ∊ k 25 | 26 | 27 | # •Show¨ t∾¨k∾¨v 28 | br ← ⊑kn⊐<"broadcaster" 29 | mem0 ← (0¨ t){𝕩≡'&'? 𝕗; 0}¨ t 30 | 31 | ls ← 0 32 | hs ← 0 33 | hist ← ⟨⟩¨ k 34 | bpc←0 35 | Step ← {𝕊tick‿m‿w‿i‿h: 36 | # ! ∧´(rx=i)/h 37 | rxm ← h∧w∊rx3 38 | hist ∾⟜bpc¨⌾((rxm/w)⊸⊏)↩ 39 | # {hist∾⟜(𝕩⊑h)⌾((𝕩⊑i)⊸⊑)↩⋄1}¨ /rxm 40 | # {𝕩⊑h? hist∾⟜tick⌾((𝕩⊑i)⊸⊑)↩⋄1;1}¨ /rxm 41 | # •Show rxm/i 42 | ls+↩ +´¬h 43 | hs+↩ +´h 44 | # •Out "" 45 | # •Show m 46 | # •Show w‿i‿h 47 | f←'%'=i⊏t 48 | # •Out"--" 49 | # •Show ≡h 50 | # •Show h 51 | # •Show i 52 | ff←i/˜f∧¬h 53 | m¬⌾(ff⊸⊏)↩ 54 | ffr ← {⟨ff/˜≠¨𝕩,∾𝕩,(≠¨𝕩)/ff⊏m⟩} ff⊏v 55 | # •Show 1+ff 56 | # ! 1≥≡2⊑ffr 57 | a ← ¬f 58 | ah ← a∧h 59 | al ← a∧¬h 60 | # ! 1=≡(t='%')/m 61 | # ! 1=≡(t='%')/m 62 | # m ↩ m∨{∨´𝕩⊏om}¨ ((ah/i)∾≠m) ⊔ ah/w 63 | # m ↩ m∧{∧´𝕩⊏zm}¨ ((al/i)∾≠m) ⊔ al/w 64 | # {⟨⟩𝕊𝕩:0; m∨⟜(∨´𝕨⊏om)⌾(𝕩⊸⊑)↩⋄0}¨⟜↕⟜≠ ((ah/i)∾≠m) ⊔ ah/w 65 | # {⟨⟩𝕊𝕩:0; m∧⟜(∧´𝕨⊏zm)⌾(𝕩⊸⊑)↩⋄0}¨⟜↕⟜≠ ((al/i)∾≠m) ⊔ al/w 66 | {m∨⟜((𝕩⊑w)⊑om)⌾((𝕩⊑i)⊸⊑)↩⋄0}¨ /ah 67 | {m∧⟜((𝕩⊑w)⊑zm)⌾((𝕩⊑i)⊸⊑)↩⋄0}¨ /al 68 | #•Show ah 69 | #•Show al 70 | #•Show a/w 71 | ! 1=≡(t='%')/m 72 | anr ← (⊏⟜v)⊸{l←≠¨𝕨⋄⟨l/𝕩, ∾𝕨, l/(𝕩⊏conn) ≠ +´¨𝕩⊏m⟩} a/i 73 | ! 1≥≡2⊑anr 74 | # •Show v 75 | # •Out "" 76 | ⟨tick+1,m⟩∾ffr∾¨anr 77 | } 78 | 79 | bp0 ← {⟨br¨𝕩,𝕩,0¨𝕩⟩} br⊑v 80 | Press ← {ls+↩1 ⋄ 1⊑ t←Step •_while_{×≠2⊑𝕩} 0‿𝕩∾bp0} 81 | Press⍟1000 mem0 82 | •Show ls×hs 83 | # •Show ls 84 | # •Show hs 85 | 86 | 87 | # hist ← ⟨0,≠rx3⟩⥊0 88 | { 89 | bpc+↩1 90 | m ← Press 𝕩 91 | # hist∾↩ rx3⊏m 92 | m 93 | }⍟30000 mem0 94 | # {•Show -⟜» /{¬𝕩∊1‿2} -⟜»/𝕩}¨ rx3⊏hist 95 | # •Show¨ {q←-⟜» /{¬𝕩∊1‿2} -⟜»/𝕩 ⋄ 1<≠q? q; 20↑1000↓𝕩}¨ rx3⊏hist 96 | •Show •math.LCM´ ¯1⊑¨ {-⟜»𝕩}¨ ×∘≠¨⊸/ hist 97 | # vs ← {-⟜»/0‿1⍷𝕩}¨ <˘ ⍉hist 98 | # •Show¨ {⊐⌾⌽𝕩}¨ vs 99 | 100 | # > 35891323 # 6289×5707 101 | # < 123813786835145216L -------------------------------------------------------------------------------- /2023/BQN/21.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← >•FLines 2023‿21 Input •args 4 | 5 | nw ← l≠'#' # not wall 6 | ls ← l='S' # start 7 | 8 | Step ← {nw∧∨´⟨»𝕩,«𝕩,»˘𝕩,«˘𝕩⟩} 9 | •Show +´⥊Step⍟64 ls 10 | 11 | r0 ← 3 # how many repetitions of input to process 12 | s0 ← ≠l # initial side length 13 | x ← 1+2×r0 # number of blocks to expand to 14 | 15 | # expand input 16 | nw ↩ ∾ x‿x ⥊ •FLines 2023‿21 Input •args 4 | 5 | w ← l='#' 6 | nw ← ¬w 7 | ls ← l='S' 8 | 9 | Step ← {nw∧∨´⟨»𝕩,«𝕩,»˘𝕩,«˘𝕩⟩} 10 | •Show +´⥊Step⍟64 ls 11 | # •Show +´⥊{nw∧∨´⟨»𝕩,«𝕩,»˘𝕩,«˘𝕩⟩}⍟26501365 l='S' 12 | # 7329 13 | 14 | s0 ← ≠l 15 | x ← 11 16 | nw ↩ ∾x‿x⥊kv 30 | rtot ← rep + r0 31 | 32 | •Show +´{r‿v: {r≡r0? v×rtot; r≡r0-1? v×rtot-1; r≡טr0? v×טrtot; r≡טr0-1? v×טrtot-1; v}}¨ kv 33 | -------------------------------------------------------------------------------- /2023/BQN/22.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Split,Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← {0‿1 + {•ParseFloat¨ ',' Split 𝕩}¨ '~' Split 𝕩}¨ •FLines 2023‿22 Input •args 4 | 5 | {𝕩!∧´<´𝕩}¨ l 6 | 7 | x0‿y0‿z0‿x1‿y1‿z1 ← <˘⍉> ∾¨ l 8 | 9 | di ← {/((2⊑0⊑𝕩)≥z1) ∧ ((0‿1⊏1⊑𝕩)>x0‿y0)∧○(∧´)(0‿1⊏0⊑𝕩)∾¨l 8 | deps ← {/((2⊑0⊑𝕩)≥z1) ∧ ((0‿1⊏1⊑𝕩)>x0‿y0)∧○(∧´)(0‿1⊏0⊑𝕩)•FLines 2023‿23 Input •args 4 | w ← inp='#' 5 | nw ← ¬w 6 | z ← 0¨ inp 7 | 8 | { 9 | u ← (inp∊"^.") ∧ »nw 10 | d ← (inp∊"v.") ∧ «nw 11 | l ← (inp∊"<.") ∧ »˘nw 12 | r ← (inp∊">.") ∧ «˘nw 13 | next ← (<¨↕∘≢)⊸+ {@⊸≢¨⊸/ 𝕩}¨ Tr ⟨¯1‿0, 1‿0, 0‿¯1, 0‿1⟩ {⊑⟜@‿𝕨¨ 𝕩}¨ u‿d‿l‿r 14 | Run ← { pos‿vis: 15 | u ← 1⌾(pos⊸⊑)vis 16 | @⊸≢¨⊸/ {𝕩⊑vis? @; ⟨𝕩, u⟩}¨ pos⊑next 17 | } 18 | 19 | yE ← ¯1+≠inp 20 | mx ← 0 21 | i ← 0 22 | { 23 | mx⌈↩ i×∨´ yE = ⊑∘⊑¨ 𝕩 24 | i+↩ 1 25 | ∾Run¨ 𝕩 26 | }•_while_{×≠𝕩} ⟨{⟨𝕩, 1⌾(𝕩⊸⊑)z⟩} 0⋈⋈⁼/'.'=⊏inp⟩ 27 | •Show mx 28 | } 29 | 30 | { 31 | cj ← {𝕩×2<+´⟨»𝕩,«𝕩,»˘𝕩,«˘𝕩⟩}nw # conjunctions 32 | cj∨↩ nw ∧ ↕⊸(=⟜0 ∨ ⊣=¯1+⊢) ≠inp # add in start & end 33 | cji ← MDSlash cj 34 | 35 | sln ← nw ∧ ¬cj # straight line 36 | cjd ← { # distances between all conjunctions 37 | cjc ← ∞¨ cji 38 | i ← 0 39 | { 40 | n ← ∨´⟨»𝕩,«𝕩,»˘𝕩,«˘𝕩⟩ 41 | cjc⌊↩ i+÷cji⊑n∧¬𝕩 42 | i+↩ 1 43 | 𝕩∨sln∧n 44 | }_fx_≢ 1⌾(𝕩⊸⊑) z 45 | cjc 46 | }¨ cji 47 | 48 | cjc ← ∞≠cjd 49 | cjci ← /¨ cjc 50 | cjde ← ∞⊸≠⊸/¨ cjd 51 | mx ← 0 52 | ln ← ¯1+≠cjd 53 | Run ← { v𝕊d: # scan all paths through conjunctions 54 | l ← ¯1⊑v 55 | ns ← l⊑cjci 56 | ds ← l⊑cjde 57 | m ← ¬ns∊v 58 | (d×l=ln) ⌈´ (v⊸∾¨ m/ns) Run¨ d+m/ds 59 | } 60 | •Show ⟨0⟩ Run 0 61 | } -------------------------------------------------------------------------------- /2023/BQN/23_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨MDSlash, _fx_, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← >•FLines 2023‿23 Input •args 4 | w ← l='#' 5 | u ← l∊"^." 6 | d ← l∊"v." 7 | le← l∊"<." 8 | r ← l∊">." 9 | 10 | 11 | z ← 0¨l 12 | Run ← { prev‿curr: 13 | {⟨1⌾(𝕩⊸⊑)prev, 1⌾(𝕩⊸⊑)z⟩}¨ MDSlash (¬w∨prev)∧∨´⟨»curr∧d,«curr∧u, «˘curr∧le, »˘curr∧r⟩ 14 | } 15 | c ← 0 16 | { 17 | c+↩1 18 | # •Show ≠𝕩 19 | ∾Run¨ 𝕩 20 | }•_while_{×≠𝕩} ⋈ ⋈˜('.'=⊏l)⌾⊏ 0¨l 21 | •Show c-1 22 | 23 | cj ← {𝕩×2<+´⟨»𝕩,«𝕩,»˘𝕩,«˘𝕩⟩}¬w # conjunctions 24 | cj∨↩ (¬w) ∧ ↕⊸(=⟜0 ∨ ⊣=¯1+⊢) ≠w # add in start & end 25 | cji ← MDSlash cj 26 | 27 | sln ← (¬w) ∧ ¬cj # straight line 28 | cjd ← { 29 | cjc ← ∞¨ cji 30 | i←0 31 | { 32 | n ← ∨´⟨»𝕩,«𝕩,»˘𝕩,«˘𝕩⟩ 33 | cjc⌊↩ i+÷cji⊑n∧¬𝕩 34 | i+↩ 1 35 | 𝕩∨sln∧n 36 | }_fx_≢ 1⌾(𝕩⊸⊑) z 37 | cjc 38 | }¨ cji 39 | 40 | cjc ← ∞≠cjd # connected 41 | cjo ← (0¨cji)¨ cji # ok 42 | cjde ← ∞⊸≠⊸/¨ cjd 43 | mx ← 0 44 | ln ← ¯1+≠cjd 45 | { 46 | # •Show ≠𝕩 47 | ∾{v‿d: ⋄ l←¯1⊑v ⋄ mx⌈↩d×l=ln ⋄ n←/l⊑cjc ⋄ m←¬n∊v ⋄ (m/l⊑cjde) {⟨v∾𝕩,d+𝕨⟩}¨ m/n}¨𝕩 48 | }•_while_(×≠) ⟨⟨0⟩‿0⟩ 49 | •Show mx -------------------------------------------------------------------------------- /2023/BQN/24.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | # does only part 1; part 2 is in a separate file as it needs bqn-smt 3 | ⟨SInts, Tr, Input⟩ ← •Import "../../utils/utils.bqn" 4 | l ← (2‿3⥊SInts)¨ •FLines 2023‿24 Input •args 5 | 6 | [x1‿y1‿z1 ⋄ vx1‿vy1‿vz1] ← Tr l 7 | 8 | b0‿b1 ← 200000000000000‿400000000000000 9 | 10 | •Show +´⥊ {>⌜˜↕≠𝕩}⊸×> {[x2‿y2‿z2 ⋄ vx2‿vy2‿vz2]: 11 | s1 ← vy1 ÷ vx1 12 | s2 ← vy2 ÷ vx2 13 | o1 ← y1 - x1×s1 14 | o2 ← y2 - x2×s2 15 | px ← (o1-o2) ÷ s2-s1 16 | py ← o1 + s1 × px 17 | b ← ∧´ (px‿py≥b0) ∧ px‿py≤b1 18 | b∧↩ 0 ≤ (px-x1) ÷ vx1 19 | b∧↩ 0 ≤ (px-x2) ÷ vx2 20 | b 21 | }¨ l -------------------------------------------------------------------------------- /2023/BQN/24_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Repr, LCM, GCD, Ints, SInts, Split, SplitF, ReIndex, MDSlash, MDSlashI, Unjoin, Count, _fx_, _fi_, Tr, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← {<˘2‿3⥊SInts 𝕩}¨ •FLines 2023‿24 Input •args 4 | # l ↩ {{•ParseFloat¨ ','Split𝕩}¨ '@'Split ' '⊸≠⊸/ 𝕩}¨ •FLines 2023‿24 Input •args 5 | x‿y‿z‿vx‿vy‿vz ← Tr ∾¨l 6 | x2‿y2‿z2 ← x‿y‿z + vx‿vy‿vz 7 | b0‿b1 ← 200000000000000‿400000000000000 8 | {𝕊:b0‿b1 ↩ 7‿27}⍟⊢ 5=≠l 9 | 10 | Cr ← { x1‿y1‿x2‿y2‿x3‿y3‿x4‿y4: 11 | # px ← ((((x1×y2)-y1×x2)×(x3-x4)) - (x1-x2)×((x3×y4)-(y3×x4))) ÷ ((x1-x2)×(y3-y4)) - (y1-y2)×(x3-x4) 12 | # py ← ((((x1×y2)-y1×x2)×(y3-y4)) - (y1-y2)×((x3×y4)-(y3×x4))) ÷ ((x1-x2)×(y3-y4)) - (y1-y2)×(x3-x4) 13 | # px‿py 14 | vx1 ← x2-x1 15 | vy1 ← y2-y1 16 | vx3 ← x4-x3 17 | vy3 ← y4-y3 18 | # ax + c 19 | # bx + d 20 | s1 ← vy1÷vx1 21 | s3 ← vy3÷vx3 22 | o1 ← y1 - x1×s1 23 | o3 ← y3 - x3×s3 24 | px ← (o1-o3)÷s3-s1 25 | py ← o1 + s1 × px 26 | px‿py 27 | } 28 | •Show +´⥊ {>⌜˜↕≠𝕩}⊸×> {⟨x3‿y3‿z3, vx3‿vy3‿vz3⟩: 29 | x4‿y4‿z4 ← x3‿y3‿z3 + vx3‿vy3‿vz3 30 | px‿py ← Cr x‿y‿x2‿y2‿x3‿y3‿x4‿y4 31 | # •Show (px-x )÷vx 32 | # •Show (px-x3)÷vx3 33 | b ← ∧´(px‿py≥b0) ∧ px‿py≤b1 34 | b∧↩ 0 ≤ (px-x) ÷ vx 35 | b∧↩ 0 ≤ (px-x3) ÷ vx3 36 | # •Out "--------" 37 | # •Show >⌊x‿px 38 | # •Show >⌊y‿py 39 | # •Show 0 ≤ (py-y) ÷ vy 40 | # •Show 0 ≤ (py-y3) ÷ vy3 41 | # •Show 0 ≤ (px-x) ÷ vx 42 | # •Show 0 ≤ (px-x3) ÷ vx3 43 | # {•Show 𝕩⋈x3}⍟(×≠) x /˜ b ∧ ¬ (0 ≤ (px-x) ÷ vx) ∧ (0 ≤ (px-x3) ÷ vx3) 44 | # pr ← =´vx‿vy÷vx3‿vy3 45 | # b∧↩ ¬pr 46 | # pr ← (vy3×vx) = vx3×vy 47 | # •Show /pr ∧ =´ (x3‿y3-x‿y)÷vx‿vy 48 | # •Show (px∊∞‿¯∞)⊸/b 49 | # b∨↩ pr ∧ (px≠px) ∧ ∨´ {{∧´(𝕩≥b0)∧𝕩≤b1} Cr 𝕩∾⟨x3,y3,x4,y4⟩}¨ ⟨b0‿b0‿b0‿b1, b0‿b0‿b1‿b0, b1‿b0‿b1‿b1, b0‿b1‿b1‿b1⟩ 50 | # •Show px∊∞‿¯∞ 51 | # b∨↩ px≠px 52 | # •Show >px‿py‿pr 53 | # •Show >px‿py‿(((px=x ) ∨ (×vx ) = ×px-x) ∧ (px=x3) ∨ (×vx3) = ×px-x3)‿(¬=´vx‿vy÷vx3‿vy3) 54 | # •Show b 55 | b 56 | }¨ l 57 | # 14042 low 58 | # 14045 wrong 59 | # 28084 high 60 | 61 | 62 | ⟨newrepl⟩ ← •Import"path/to/bqn-smt/bitwuzla.bqn" # https://codeberg.org/dzaima/bqn-smt 63 | r ← Newrepl @ 64 | (r.REPLExec "l←@ ⋄ {l↩𝕩}") {𝕎𝕩} 4↑l 65 | 66 | r.REPLExec " 67 | 68 | r0 ← I64¨ ""xyz"" 69 | rv ← I64¨ 'v'∾¨ ""xyz"" 70 | 71 | Sat {⟨c0,cv⟩: t←I64@ ⋄ ∧´ (r0 + t×rv) = c0 + t×cv}¨ l 72 | •Show +´ EvalToBQN¨ Find¨ r0 73 | " -------------------------------------------------------------------------------- /2023/BQN/24_part2.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨SInts⟩ ← •Import "../../utils/utils.bqn" 3 | ⟨newrepl⟩ ← •Import ⊑•args # bqn-smt/bitwuzla.bqn (or cvc5.bqn or z3.bqn) - https://codeberg.org/dzaima/bqn-smt 4 | l ← {<˘2‿3⥊SInts 𝕩}¨ •FLines ≠◶"24.in"‿⊑ 1↓•args 5 | 6 | r ← Newrepl @ 7 | (r.REPLExec "l←@ ⋄ {l↩𝕩}") {𝕎𝕩} 4↑l 8 | 9 | r.REPLExec " 10 | r0 ← I64¨ ""xyz"" 11 | rv ← I64¨ 'v'∾¨ ""xyz"" 12 | 13 | Sat {⟨c0,cv⟩: t←I64@ ⋄ ∧´ (r0 + t×rv) = c0 + t×cv}¨ l 14 | •Show +´ EvalToBQN¨ Find¨ r0 15 | " -------------------------------------------------------------------------------- /2023/BQN/25.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Split, _fx_, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← ' 'Split¨ ':'⊸≠⊸/¨ •FLines 2023‿25 Input •args 4 | 5 | # original was: plug into graphviz layout=neato, see two clearly separated blobs, take the names of the nodes that connect those 6 | 7 | k ← ⊑¨l 8 | v ← 1↓¨l 9 | un ← ⍷k∾∾v 10 | kn ← ↕≠un 11 | vn ← un⊸⊐¨ v ∾ (un -○≠ v)⥊<⟨⟩ 12 | 13 | vn0 ← vn 14 | vn∾¨↩ (≠kn)↑ (∾vn) ⊔ (≠¨vn)/kn 15 | 16 | m ← ⍉⊸∨ (≠un)↑ >(≠un)↑¨/⁼¨ vn0 17 | 18 | r ← •MakeRand 38282246 # seeded to have consistent timings 19 | es0 ← > ∾vn⋈¨¨kn 20 | Try ← { 𝕊: 21 | m ← kn •HashMap ⋈¨ kn 22 | remap ← kn 23 | { 24 | 2≥m.Count@?@; 25 | a‿b: 26 | a‿b ⊏⟜remap _fx_≢↩ 27 | a≢b? 28 | remap b⌾(a⊸⊑)↩ 29 | as ← m.Get a 30 | m.Delete a 31 | b m.Set as∾m.Get b 32 | ;@ 33 | }˘ r.Deal∘≠⊸⊏ es0 34 | 35 | a‿b ← m.Values@ 36 | { 37 | 3 = +´∧´ a‿b ∊˜¨ <˘ ⍉es0? ≠¨ a‿b; 38 | @ 39 | } 40 | } 41 | 42 | •Show ×´ Try•_while_{@≡𝕩} @ -------------------------------------------------------------------------------- /2023/BQN/2_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Split, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← •FLines 2023‿2 Input •args 4 | 5 | n←"red"‿"green"‿"blue" 6 | m←12‿13‿14 7 | •Show +´1+/{ 8 | v←{n‿s←' 'Split𝕩⋄⟨•ParseFloat n,s⟩}¨¨ ','Split¨1↓¨';'Split 1⊑':'Split𝕩 9 | ∧´{∧´m≥(⊑¨𝕩)⌾((n⊐1⊑¨𝕩)⊸⊏) 3⥊0}¨ v 10 | }¨l 11 | 12 | •Show +´{ 13 | v←{n‿s←' 'Split𝕩⋄⟨•ParseFloat n,s⟩}¨¨ ','Split¨1↓¨';'Split 1⊑':'Split𝕩 14 | ×´⌈´{(⊑¨𝕩)⌾((n⊐1⊑¨𝕩)⊸⊏) 3⥊0}¨ v 15 | }¨l -------------------------------------------------------------------------------- /2023/BQN/3.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨MDSlash, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← '.'∾˜˘>•FLines 2023‿3 Input •args # adds a column of '.' so that there's always a '.' between adjacent numbers in deshape 4 | ld ← (≥⟜'0'∧≤⟜'9') l 5 | ni ← ¯1+(+`1∾1‿0⍷⊢)⌾⥊⊸× ld 6 | pn ← •ParseFloat¨ ni⊔l 7 | 8 | ai ← ⍷ ni /○⥊˜ ∨´∾ (<∾»˘⋈«˘)¨ (<∾»⋈«) ¬ld∨l='.' 9 | •Show +´pn ⊏˜ ¯1⊸≠⊸/ ai 10 | 11 | adj ← {¯1⊸≠⊸/⍷⥊(𝕩<⊸+¯1+↕3‿3)⊑ni}¨ MDSlash l='*' 12 | 13 | •Show +´ (×´⊏⟜pn)¨ adj/˜2≤≠¨adj -------------------------------------------------------------------------------- /2023/BQN/3_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨MDSlash, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← '.'∾˜˘>•FLines 2023‿3 Input •args # adds a column of '.' so that there's always a '.' between adjacent numbers in deshape 4 | nadj ← ∨´¬∊⟜('.'∾'0'+↕10)¨ ∾'.'(⊢<⊸∾»˘⋈«˘)¨ l<⊸∾ '.'¨∘⊏⊸(»⋈«)l 5 | D←≥⟜'0'∧≤⟜'9' 6 | ml0 ← D ⥊l 7 | a←ml0⊸((¬-˜⊢×·+`»⊸>)⊸⊔) ⥊l 8 | b←∨´¨ml0⊸((¬-˜⊢×·+`»⊸>)⊸⊔) ⥊nadj 9 | •Show +´•ParseFloat¨b/a 10 | 11 | ni ← ((1+`⊢)⌾⥊¬)⊸× D l # unique identifier for each number 12 | adn ← {0⊸≠⊸/⍷⥊(𝕩<⊸+¯1+↕3‿3)⊑ni}¨ MDSlash l='*' 13 | 14 | •Show +´{×´{•ParseFloat l/˜○⥊𝕩=ni}¨𝕩}¨ adn/˜2≤≠¨adn -------------------------------------------------------------------------------- /2023/BQN/4.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Split, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← {•ParseFloat¨¨ ' 'Split¨ '|'Split 1⊑':'Split𝕩}¨•FLines 2023‿4 Input •args 4 | 5 | sc ← +´∘∊´¨ l 6 | •Show +´ ⌊2⋆¯1+sc 7 | •Show +´ (1¨ l) {(𝕩⊑𝕨)⊸+⌾((𝕩⊑sc)↑(1+𝕩)↓⊢) 𝕨}˜´⌽ ↕≠l -------------------------------------------------------------------------------- /2023/BQN/4_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Split, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← {•ParseFloat¨¨ ' 'Split¨ '|'Split 1⊑':'Split𝕩}¨•FLines 2023‿4 Input •args 4 | 5 | •Show +´(0.5⊸≠⊸×) 2⋆¯1++´¨∊˜´¨l 6 | cs←1¨l 7 | (↕≠l) { 8 | p ← +´∊˜´𝕩 9 | 10 | cs (𝕨⊑cs)⊸+⌾(((p)⌊(¯1+≠cs)-𝕨)↑(1+𝕨)↓⊢)↩ 11 | }¨ l 12 | 13 | •Show +´cs -------------------------------------------------------------------------------- /2023/BQN/5.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Ints, SplitF, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← ⟨⟩SplitF •FLines 2023‿5 Input •args 4 | s ← Ints ⊑⊑l 5 | l ↩ {<˘⍉>Ints¨ 1↓𝕩}¨ 1↓l 6 | 7 | •Show ⌊´s { 𝕨 𝕊 d‿s‿l: 8 | 𝕨 + +˝˘ (d-s) ×⎉1 (𝕨 ≥⌜ s) ∧ 𝕨 <⌜ s+l 9 | }˜´⌽ l 10 | 11 | # current range set represented by ⟨list of starts ⋄ list of ends (exclusive)⟩ 12 | •Show ⌊´⊑ (<˘ +` ⍉ ∘‿2⥊s) { vs‿ve 𝕊 d‿s‿l: 13 | # vs,ve: start/end of previous sets 14 | # d,s,l: current mappings, serparated into columns 15 | ds ← d-s # delta a given mapping applies when matched 16 | 17 | ak ← ⍷∧vs∾ve∾s∾s+l # all key points at which values may change 18 | 19 | us ← ak⍋s-1 # offset in key where a mapping starts 20 | ue ← ak⍋s+l-1 # .. and where it ends 21 | tr ← +` -⟜ds⌾(ue⊸⊏) ds⌾(us⊸⊏) 0¨ ak # delta to apply at each key point; don't need some +´¨w⊔ds thing as, even though intervals can start where another ended, one will never start where another started or end where another ended 22 | 23 | rs ← ak⍋vs-1 # offset in key where a previous-range starts 24 | re ← ak⍋ve-1 # .. and where it ends 25 | 26 | mm ← +` rs -○((≠ak)↑/⁼) re # which key-point intervals are in the previous set; last element is always 0 27 | akns ← tr + ak # all key point next interval start 28 | akne ← tr + «ak # ..and end 29 | ⟨mm/akns, mm/akne⟩ # filter the intervals. Don't bother merging adjacent ones 30 | 31 | # alternative less array-y way of computing the result: 32 | # (<∘⍋⊑)⊸(⊏¨) <˘⍉> ∾ {crs‿cre: (crs↓cre↑tr) + <˘2↕crs↓(cre+1)↑ak}¨ rs ⋈¨ re 33 | }˜´⌽ l -------------------------------------------------------------------------------- /2023/BQN/5_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨SplitF, Ints, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← ⟨⟩SPlitF •FLines 2023‿5 Input •args 4 | 5 | s ← Ints⊑⊑l 6 | 7 | l ↩ Ints¨¨1↓¨1↓l 8 | 9 | # dst;src;len 10 | 11 | #•Show s 12 | #•Show ≍˘l 13 | 14 | •Show ⌊´s { 15 | 𝕨 + +˝˘𝕨 { 16 | d‿s‿l←𝕩 17 | (d-s) × (𝕨≥s) ∧ 𝕨𝕩 26 | ak ← ⍷∧vs∾ve∾s∾(s+l)#∾(90+↕10)∾48+↕10 27 | us ← ak⍋s-1 28 | ue ← ak⍋s+l-1 29 | #•Show (d-s)∾us≍ue 30 | ds ← d-s 31 | tr ← +`(-ds)⊸+⌾(ue⊸⊏) ds⌾(us⊸⊏) 0¨ak 32 | # •Show ak≍tr 33 | ms←ak⍋vs-1 ⋄ #•Show vs≍ms 34 | me←ak⍋ve-1 ⋄ #•Show ve≍me 35 | (⊑¨⋈1⊑¨⊢) ∾{ 36 | a‿b: 37 | (a↓b↑tr) + <˘2↕a↓(b+1)↑ak 38 | }¨ <˘⍉>ms≍me 39 | 40 | #!0 41 | }˜´⌽ l -------------------------------------------------------------------------------- /2023/BQN/6.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Ints, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← •FLines 2023‿6 Input •args 4 | 5 | # Calc ← {+´ 𝕩 < ≠⊸-⊸× ↕𝕨} # basic O(n) array thing 6 | # Calc ← {¬˜´ ⌈‿⌊{𝕎𝕩}¨ 2÷˜ 𝕨 (-⋈+) √(ט𝕨) - 4×𝕩+1} # basic math; ⌈‿⌊{𝕎𝕩}¨ → ⌈⊸⋈⟜⌊´ is a fun option 7 | Calc ← {𝕨 (⌊∘+ ¬ ⌈∘-)○(÷⟜2) √(ט𝕨) - 4×𝕩+1} # merging the math together with logic 8 | 9 | •Show ×´ Calc¨○Ints´ l 10 | •Show Calc´ {•ParseFloat ∊⟜('0'+↕10)⊸/ 𝕩}¨ l -------------------------------------------------------------------------------- /2023/BQN/6_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Ints, Split, SplitF, ReIndex, MDSlash, Unjoin, Count, GCD, LCM, _fx_, _fi_, Input⟩ ← •Import "../../utils/utils.bqn" 3 | t‿d ← Ints¨ a←•FLines 2023‿6 Input •args 4 | 5 | •Show ×´+´¨d {𝕨<(𝕩⊸-×⊢)↕𝕩}¨ t 6 | •Show +´{𝕨<(𝕩⊸-×⊢)↕𝕩}˜´ •ParseFloat¨ ∊⟜('0'+↕10)⊸/¨ a -------------------------------------------------------------------------------- /2023/BQN/7.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Split, Input⟩ ← •Import "../../utils/utils.bqn" 3 | k‿v ← <˘⍉> ' 'Split¨•FLines 2023‿7 Input •args 4 | v •ParseFloat¨↩ 5 | Winnings ← {+´ v × 1+⍋⍋𝕩} 6 | 7 | Rank ← ⊑ ⟨1‿1‿1‿1‿1 ⋄ 2‿1‿1‿1 ⋄ 2‿2‿1 ⋄ 3‿1‿1 ⋄ 3‿2 ⋄ 4‿1 ⋄ ⟨5⟩⟩ ⊐ < 8 | rank1 ← {(Rank ∨/⁼⊐ 𝕩)∾𝕩⊐˜⌽"AKQJT98765432"}¨ k 9 | •Show Winnings rank1 10 | 11 | 12 | # rank2 ← {(⌈´{Rank ∨/⁼⊐𝕩}¨ ('J'⊸≠⊸/𝕩)⊸∾¨ ⥊(<⟨⟩)<⊸∾⌜´(+´𝕩='J')⥊<"AKQT98765432")∾𝕩⊐˜⌽"AKQT98765432J"}¨ k # basic extension with cartesian product 13 | rank2 ← { 14 | c ← ∨/⁼⊐ 'J'⊸≠⊸/ 𝕩 15 | c ↩ (5-+´c)⊸+⌾⊑ (1⌈≠)⊸↑ c # redirect all jokers to the otherwise-most-common card 16 | (Rank c)∾𝕩⊐˜⌽"AKQT98765432J" 17 | }¨ k 18 | •Show Winnings rank2 19 | -------------------------------------------------------------------------------- /2023/BQN/7_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Ints, Split, SplitF, ReIndex, MDSlash, Unjoin, Count, GCD, LCM, _fx_, _fi_, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← ' 'Split¨•FLines 2023‿7 Input •args 4 | 5 | t ← {c←∧/⁼⊐ ⊑𝕩 ⋄ {c≡⟨5⟩? 5; c≡1‿4? 4; c≡2‿3? 3; ⊑3∊c? 2; 2≤+´2=c? 1; ⊑2∊c? 0; c≡5⥊1? ¯1}∾(⊑𝕩)⊐˜⌽"AKQJT98765432"}¨l 6 | •Show +´(•ParseFloat¨1⊑¨l) × (≠t)-⍋⍋-t 7 | # expected order: 15324 8 | 9 | 10 | t ↩ {c←∧/⁼⊐ 𝕩 ⋄ {c≡⟨5⟩? 5; c≡1‿4? 4; c≡2‿3? 3; ⊑3∊c? 2; 2≤+´2=c? 1; ⊑2∊c? 0; c≡5⥊1? ¯1}}{ 11 | (⌈´𝔽¨ 𝕩⊸{𝕩⌾((𝕨='J')⊸/)𝕨}¨ ⥊(<⟨⟩)<⊸∾⌜´(+´𝕩='J')⥊<"AKQT98765432")∾𝕩⊐˜⌽"AKQT98765432J" 12 | }¨ ⊑¨l 13 | •Show +´(•ParseFloat¨1⊑¨l) × (≠t)-⍋⍋-t 14 | # 248810748 wrong 15 | # 248889096 high 16 | # problems: 1) have to keep card order intact for tiebreaker; 2) no actually, the tiebreaker is its own thing -------------------------------------------------------------------------------- /2023/BQN/8.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← •FLines 2023‿8 Input •args 4 | 5 | k‿lv‿rv ← <˘⍉> {⟨3↑𝕩, 3↑7↓𝕩, 3↑12↓𝕩⟩}¨ 2↓l 6 | lr ← k ⊐ >lv‿rv 7 | 8 | 9 | 10 | r ← 'R'=⊑l 11 | Loop ← { c←𝕨 12 | ⊑{⟨1+𝕨, ⟨((≠r)|𝕨)⊑r, 𝕩⟩⊑lr⟩}´•_while_{c⊑˜1⊑𝕩} ⟨0, ⊑k⊐<𝕩⟩ 13 | } 14 | 15 | •Show ("ZZZ"⊸≢¨ k) Loop "AAA" 16 | •Show •math.LCM´ (('Z'≢2⊑⊢)¨ k)⊸Loop¨ ('A'=2⊑¨⊢)⊸/ k # works for input ¯\_(ツ)_/¯ 17 | -------------------------------------------------------------------------------- /2023/BQN/8_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Ints, Split, SplitF, ReIndex, MDSlash, Unjoin, Count, GCD, LCM, _fx_, _fi_, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← •FLines 2023‿8 Input •args 4 | s ← 'L'=⊑l 5 | 6 | k‿lv‿rv ← <˘⍉> {⟨3↑𝕩, 3↑7↓𝕩, 3↑12↓𝕩⟩}¨ 2↓l 7 | tl ← k •HashMap lv 8 | tr ← k •HashMap rv 9 | 10 | 11 | 12 | Step ← {𝕊: 13 | (𝕨+1) ⋈ ((((≠s)|𝕨)⊑s)⊑tr‿tl).Get 𝕩 14 | }´ 15 | F ← { 16 | Step•_while_ {'Z'≢2⊑1⊑𝕩} 𝕩 17 | } 18 | 19 | •Show ⊑ F 0⋈"AAA" 20 | 21 | •Show LCM´ ⊑¨ F¨ 0⋈¨ ('A'=2⊑¨k)/k # I don't think this quite makes sense but annoyingly it gives the right value so -------------------------------------------------------------------------------- /2023/BQN/9.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨SInts, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← SInts¨ •FLines 2023‿9 Input •args 4 | 5 | F ← ¯1⊑{S𝕩: {∧´0=1↓𝕩? 𝕩∾0; (⊑𝕩)∾S 1↓𝕩}⌾(+`⁼) 𝕩} 6 | •Show +´ F¨ l 7 | •Show +´ F∘⌽¨ l -------------------------------------------------------------------------------- /2023/BQN/9_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨SInts, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← SInts¨ •FLines 2023‿9 Input •args 4 | 5 | •Show +´{¯1⊑{+`(⊑𝕨)∾𝕩}´ ∾⟜0⌾(¯1⊑⊢) {𝕩∾<1↓+`⁼¯1⊑𝕩}•_while_{𝕊:∨´0≠¯1⊑𝕩} ⋈𝕩}¨ l 6 | •Show +´{¯1⊑{+`(⊑𝕨)∾𝕩}´ ∾⟜0⌾(¯1⊑⊢) {𝕩∾<1↓+`⁼¯1⊑𝕩}•_while_{𝕊:∨´0≠¯1⊑𝕩} ⋈⌽𝕩}¨ l 7 | # 1866957968 high - used Ints instead of SInts :/ -------------------------------------------------------------------------------- /2023/BQN/test.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | { 3 | exp ⇐ ⟨ 4 | ⟨"1", 54195‿55015⟩ 5 | ⟨"2", 635‿323355⟩ 6 | ⟨"3", 369151‿4098894⟩ 7 | ⟨"4", 34818‿156360836⟩ 8 | ⟨"5", 549862878‿189397914⟩ 9 | ⟨"6", 652080‿43391660⟩ 10 | ⟨"7", 243491531‿244191118⟩ 11 | ⟨"8", 9773‿1896142712543⟩ 12 | ⟨"9", 977717317‿159⟩ 13 | ⟨"10", 7005‿304⟩ 14 | ⟨"11", 8315986‿386893542214⟩ 15 | ⟨"12", 2660‿706901858⟩ 16 | ⟨"13", 9544‿52896⟩ 17 | ⟨"14", 101451‿84920⟩ 18 | ⟨"15", 533437‿313107⟩ 19 | ⟨"16", 17‿8052⟩ 20 | ⟨"17", 805‿911⟩ 21 | ⟨"18", 11876‿448056297829⟩ 22 | ⟨"19", 1170015‿173705530106043⟩ 23 | ⟨"20", 898583892‿82369726287731⟩ 24 | ⟨"21", 3770‿627633370348971⟩ 25 | ⟨"22", 530‿32762⟩ 26 | ⟨"23", 2562‿6494⟩ 27 | ⟨"24", ⟨42311⟩⟩ # 871899563895170 for part 2 28 | ⟨"25", ⟨629995⟩⟩ 29 | ⟩ 30 | 31 | path ⇐ •path 32 | args ⇐ •args 33 | all ⇐ •Repr¨ 1+↕25 34 | most ⇐ (¬∊⟜"17"‿"23"‿"25")⊸/ all 35 | orig ⇐ (∾⟜"_orig"¨ 1↓(¬∊⟜⟨"20","24","25"⟩)⊸/ all)∾⟨"20_orig_part2"⟩ 36 | } •Import "../../utils/test.bqn" 37 | -------------------------------------------------------------------------------- /2023/Singeli/.gitignore: -------------------------------------------------------------------------------- 1 | gen/ 2 | *.out -------------------------------------------------------------------------------- /2023/Singeli/1_prep.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | "gen/table16.c" •FChars 1↓∾(','∾•Repr)¨ ∾(16↑/∘⌽)¨ ⥊↕16⥊2 -------------------------------------------------------------------------------- /2023/Singeli/makefile: -------------------------------------------------------------------------------- 1 | SINGELI = singeli 2 | 3 | help: 4 | @echo 'Usage: make 1.out CBQNDIR=path/to/CBQN SINGELI=path/to/singeli/singeli [CC=custom-cc]' 5 | 6 | 7 | gen/1.c: 1.singeli 8 | $(SINGELI) -l cbqn=$(CBQNDIR) -o gen/1.c 1.singeli 9 | 10 | gen/table16.c: 1_prep.bqn 11 | ./1_prep.bqn 12 | 13 | 1.out: test.c gen/1.c gen/table16.c 14 | $(CC) -fno-strict-aliasing -g -o 1.out -O3 -march=native test.c 15 | 16 | clean: 17 | rm 1.out gen/1.c gen/table16.c 18 | -------------------------------------------------------------------------------- /2023/Singeli/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | uint8_t table16[] = { 3 | #include "gen/table16.c" 4 | }; 5 | #include "gen/1.c" 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | static inline uint64_t nsTime() { 13 | struct timespec t; 14 | // timespec_get(&t, TIME_UTC); // doesn't seem to exist on Android 15 | clock_gettime(CLOCK_REALTIME, &t); 16 | return (uint64_t)(t.tv_sec*1000000000ll + t.tv_nsec); 17 | } 18 | 19 | 20 | __attribute__((noinline)) uint64_t part1w(uint8_t* a, uint64_t l) { return part1(a, l); } 21 | __attribute__((noinline)) uint64_t part2w(uint8_t* a, uint64_t l) { return part2(a, l); } 22 | 23 | typedef uint64_t (*const Fn)(uint8_t*,uint64_t); 24 | 25 | Fn fns[] = {part1w, part2w}; 26 | 27 | int main(int argc, char* argv[]) { 28 | int fd = open(argv[1], 0); 29 | if (fd==-1) { printf("Bad file path\n"); exit(1); } 30 | uint64_t len = lseek(fd, 0, SEEK_END); 31 | void* buf = mmap(0, len, 1, 2, fd, 0); 32 | printf("part 1: %lld\n", (long long) part1w(buf, len)); 33 | printf("part 2: %lld\n", (long long) part2w(buf, len)); 34 | 35 | uint64_t target_ns = 1e9; // time to spend timing 36 | 37 | for (int i = 0; i < 2; i++) { 38 | Fn f = fns[i]; 39 | uint64_t rep0 = 10; 40 | 41 | uint64_t sum = 0; 42 | uint64_t rep = 0; 43 | uint64_t sns = nsTime(); 44 | uint64_t pns = sns; 45 | while (sum < target_ns) { 46 | // printf("rep0 %ld\n", rep0); 47 | for (uint64_t j = 0; j < rep0; j++) f(buf, len); 48 | uint64_t ens = nsTime(); 49 | sum = ens-sns; 50 | rep+= rep0; 51 | 52 | if (ens-pns < target_ns/20) rep0*= 2; 53 | pns = ens; 54 | } 55 | 56 | printf("part %d: %.1fns\n", i+1, sum*1.0/rep); 57 | } 58 | } -------------------------------------------------------------------------------- /2023/inputs/20.txt: -------------------------------------------------------------------------------- 1 | %ts -> bj 2 | &bh -> fr 3 | %zq -> px, rj 4 | %gs -> mm, mt 5 | %qz -> lh, gz 6 | %jv -> cd 7 | &ws -> fr 8 | %wq -> rc, ts 9 | %rj -> px, rh 10 | %cx -> px, sc 11 | %fv -> rc, zf 12 | &lh -> zx, rk, sl, gz, zl, jv, th 13 | %cd -> lh, th 14 | %cp -> lk 15 | &rk -> fr 16 | %rp -> px 17 | &fr -> rx 18 | &mm -> qv, xj, jr, tl, mt, jf 19 | &px -> zq, zp, sc, bh, ks, wn 20 | %xl -> lh, jv 21 | %wn -> rp 22 | %zx -> lh, sl 23 | %ks -> hj 24 | %tl -> lb 25 | %hj -> px, lj 26 | %gz -> td 27 | %lb -> mm, gd 28 | %mt -> vm 29 | %zp -> cx 30 | %sc -> gq 31 | %lj -> px, wn 32 | %jf -> bl 33 | %gq -> px, ks 34 | %rh -> px, zp 35 | %th -> fq 36 | %tz -> rc, fv 37 | %td -> lh, jw 38 | broadcaster -> qv, zx, wq, zq 39 | %vm -> mm, jf 40 | %lk -> tz 41 | %dm -> rc, cp 42 | %tm -> rc, dm 43 | %bj -> rc, nl 44 | %xj -> lg 45 | %nl -> mn 46 | %fq -> lh 47 | %zf -> rc 48 | %sl -> qz 49 | %fh -> mm, tl 50 | %bl -> mm 51 | %mn -> rc, tm 52 | %zl -> xl 53 | &rc -> wq, ts, nl, ws, cp, lk 54 | %lg -> mm, fh 55 | %gd -> mm, gs 56 | %jw -> lh, zl 57 | &jr -> fr 58 | %qv -> mm, xj 59 | -------------------------------------------------------------------------------- /2023/inputs/6.txt: -------------------------------------------------------------------------------- 1 | Time: 43 40 45 77 2 | Distance: 280 300 266 976 3 | -------------------------------------------------------------------------------- /2023/inputs/gen/.gitignore: -------------------------------------------------------------------------------- 1 | *.in -------------------------------------------------------------------------------- /2023/inputs/gen/1_gen.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | "1_ones.in" •FLines "1"⊸+¨ 100 (•MakeRand 1).Range 9 3 | "1_twos.in" •FLines <˘ '1' + 100‿2 (•MakeRand 2).Range 9 4 | 5 | # {•Show ⟨(+´÷≠)𝕩, ⌈´𝕩, +´𝕩⟩ ⋄ 60↑⎉1 ↕∘≠⊸{(𝕩≠0) /⎉1 𝕨≍𝕩} ⌊100×(⊢÷⌈´) /⁼ 𝕩} lengths 6 | 7 | MkRand ← { seed 𝕊 target: 8 | Range ← (•MakeRand seed).Range 9 | baseset ← ∾1‿3 / "1a" +⟜↕¨ 9‿26 10 | namesBelow ← "one"‿"two"‿"three"‿"four"‿"five"‿"six"‿"seven"‿"eight"‿"nine"⊸{(𝕩≥≠¨𝕨)/𝕨}¨ ↕6 11 | MkLine ← { 𝕊 l: 12 | a ← (l Range ≠)⊸⊏ baseset # base text 13 | 14 | { # sprinkle in names 15 | a 𝕩⌾(((↕≠𝕩) + Range 1+a -○≠ 𝕩)⊸⊏)↩ 16 | @ 17 | }¨ ({l≥3? Range ⌈l÷2.9; 0}Range≠)⊸⊏ (5⌊l)⊑namesBelow 18 | 19 | {𝕊: # have at least one digit 20 | a ('1'+Range 9)⌾((Range ≠a)⊸⊑)↩ 21 | }⍟¬ ∨´ (a≥'1') ∧ (a≤'9') 22 | a 23 | } 24 | LnLengths ← {𝕊 n: 1+ 1000 ⌊ 1 ⌈ (+˝2‿n Range 20) + ⌊÷1-ט n Range 0} 25 | 26 | l ← ⟨⟩ 27 | { 𝕊: 28 | c ← LnLengths ⌈target÷100 29 | l∾↩ c 30 | 𝕩 + +´ c 31 | }•_while_{𝕊: 𝕩 ≤ target} 0 32 | 33 | MkLine¨ ¯1+ {𝕩∾target-+´𝕩} ((target-2) ≥ +`l) / l 34 | } 35 | 36 | # 30 {! 𝕨 = +´1+≠¨ 𝕩 MkRand 𝕨}¨ 100000 •rand.Range 0 37 | "1_100KB.in" •FLines 3 MkRand 100e3 38 | "1_1MB.in" •FLines 4 MkRand 1e6 39 | "1_10MB.in" •FLines 5 MkRand 10e6 40 | "1_50MB.in" •FLines 6 MkRand 50e6 -------------------------------------------------------------------------------- /2024/BQN/1.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Ints, Input, Split, Count⟩ ← •Import "../../utils/utils.bqn" 3 | a‿b ← <˘⍉> Ints¨ •FLines 2024‿1 Input •args 4 | 5 | •Show +´ |-´ ∧¨ a‿b 6 | •Show +´ a× a Count b 7 | -------------------------------------------------------------------------------- /2024/BQN/10.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨MDSlash, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← '0'-˜>•FLines 2024‿10 Input •args 4 | _step ← {(l=𝕨) × (»𝕩) 𝔽 (»˘𝕩) 𝔽 («𝕩) 𝔽 «˘𝕩} 5 | _run ← {+´⥊ 𝕩 𝔽 _step´ ⌽1+↕9} 6 | 7 | empty ← 0¨ l 8 | •Show +´{∨_run 1⌾(𝕩⊸⊑)empty}¨ MDSlash l=0 9 | •Show +_run l=0 10 | -------------------------------------------------------------------------------- /2024/BQN/10_fast.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← '0'-˜>•FLines 2024‿10 Input •args 4 | 5 | n ← ≠l 6 | flat ← ⥊l 7 | deltas ← ⟨1, ¯1, n, -n⟩ 8 | inbounds ← ⥊¨ {⟨«˘𝕩, »˘𝕩, «𝕩, »𝕩⟩} (1¨ l) ∾ n⥊0 9 | tab ← ⍉> (↕∘≠⊸⋈ /9=flat) { 𝕨 𝕊 ids‿ps: 10 | m0 ← ps⊸⊏¨ inbounds 11 | ps2 ← ∾deltas + /⟜ps¨ m0 12 | m1 ← 𝕨=ps2⊏flat 13 | ⟨ 14 | m1/ ∾ /⟜ids¨ m0, 15 | m1/ ps2 16 | ⟩ 17 | }´ ↕9 18 | 19 | •Show ≠⍷tab 20 | •Show ≠tab 21 | -------------------------------------------------------------------------------- /2024/BQN/10_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Repr, LCM, GCD, Ints, SInts, Split, SplitF, ReIndex, MDSlash, MDSlashI, Unjoin, Count, _fx_, _fi_, _underSelect_, Tr, TW, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← '0'-˜>•FLines 2024‿10 Input •args 4 | Z←{v‿m: x←l=v↩1+v ⋄ v⋈x∧(»m)+(»˘m)+(«m)+«˘m} 5 | 6 | zz←0¨l 7 | •Show +´{+´⥊×1⊑Z⍟9 0⋈1⌾(𝕩⊸⊑)zz}¨ MDSlash l=0 8 | •Show +´⥊1⊑Z⍟9 0⋈l=0 -------------------------------------------------------------------------------- /2024/BQN/11.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Ints, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← Ints⊑•FLines 2024‿11 Input •args 4 | 5 | pows ← 10⋆↕16 6 | evens ← 0⌾⊑ 0=2|↕16 7 | rests ← 0⌾⊑ ¬evens 8 | 9 | •Show¨ +´∘⊑¨ { ns𝕊vs: 10 | lens ← pows⍋vs 11 | 12 | rn‿rv ← ∾¨<˘⍉>⟨ 13 | {ns←(lens=0)/ns ⋄ ⟨ns, 1¨ns⟩} 14 | {ns‿vs‿ls: ⟨ns∾ns, ((ls÷2)⊏pows) (| ∾ ⌊∘÷˜) vs⟩} (lens⊏evens)⊸/¨ ns‿vs‿lens 15 | 2024⊸×⌾(1⊸⊑) (lens⊏rests)⊸/¨ ns‿vs 16 | ⟩ 17 | 18 | g ← ⊐ rv 19 | ⟨+´¨ g ⊔ rn, (∊g)/rv⟩ 20 | }´⍟25‿75 1¨⊸⋈ l -------------------------------------------------------------------------------- /2024/BQN/11_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Repr, LCM, GCD, Ints, SInts, Split, SplitF, Indices, ReIndex, MDSlash, MDSlashI, Unjoin, Count, _fx_, _fi_, _underSelect_, Tr, TW, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← Ints⊑•FLines 2024‿11 Input •args 4 | 5 | •Show ≠(∾{0:⟨1⟩; n←•Repr 𝕩 ⋄ 0=2|≠n? •ParseFloat¨(2÷˜≠n)(↑⋈↓)n; ⋈𝕩×2024}¨)⍟25 l 6 | F ← {0:⟨1⟩; n←•Repr 𝕩 ⋄ 0=2|≠n? •ParseFloat¨(2÷˜≠n)(↑⋈↓)n; ⋈𝕩×2024} 7 | •Show +´⊑{b←F¨ 𝕩⋄c←((≠¨b)/𝕨)⋈∾b⋄g←⊐1⊑c⋄⌽⟨⍷1⊑c,+´¨g⊔⊑c⟩}´⍟75 1¨⊸⋈ l -------------------------------------------------------------------------------- /2024/BQN/12.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Input, _fx_⟩ ← •Import "../../utils/utils.bqn" 3 | l ← >•FLines 2024‿12 Input •args 4 | 5 | shifts ← ⟨»˘,»,«˘,«⟩ 6 | eq ← {l = 𝕏l}¨ shifts 7 | 8 | is ← ⊐⌾⥊ {𝕊 p: p ⌈´ eq {𝕨 × 𝕏p}¨ shifts} _fx_ ≢ ↕∘≠⌾⥊ l 9 | isp ← is+1 # so that Nudge shifting in zeroes doesn't collide with index 0 10 | 11 | shs ← {𝕏isp}¨ shifts 12 | edges ← isp⊸≠¨ shs 13 | 14 | areas ← /⁼⥊is 15 | •Show +´ areas × +´¨ is ⊔○⥊ +´edges 16 | •Show +´ areas × +´¨ is ⊔○⥊ +´edges ∧ (¯1⌽edges) ∨ (¯1⌽shifts) {isp=𝕎𝕩}¨ shs 17 | # TODO: remove ○⥊ from ⊔s when CBQN does that fast 18 | -------------------------------------------------------------------------------- /2024/BQN/12_ffi.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Input, _fx_⟩ ← •Import "../../utils/utils.bqn" 3 | l ← >•FLines 2024‿12 Input •args 4 | 5 | is0 ← {{𝕩 +⎉1 »+`1+¯1⊏𝕩}»+`𝕩≠«𝕩}{⟨𝔽𝕩, 𝔽⌾⍉𝕩⟩} l 6 | 7 | gm ← 1+⌈´ ⌈´∘⥊¨ is0 8 | 9 | minFFI ← "lib12.so" •FFI "&"‿"group_min_i16"‿"&i16"‿"*i16"‿"*i16"‿"u64" 10 | sumFFI ← "lib12.so" •FFI "&"‿"group_sum_i8_i16"‿"&i16"‿"*i16"‿"*i8"‿"u64" 11 | GroupMin ← {MinFFI ⟨gm⥊32767, 𝕨, 𝕩, ≠⥊𝕨⟩} 12 | GroupSum ← {SumFFI ⟨0⥊˜1+⌈´⥊𝕨, 𝕨, 𝕩, ≠⥊𝕨⟩} 13 | # GroupMin ← ⌊´¨⊔○⥊ 14 | # GroupSum ← +´¨ ⊔○⥊ 15 | 16 | is ← ⊐⌾⥊ ⊑ (⊢ ⋈ ⊣⊏GroupMin)´•_while_(≢´) is0 17 | # •Show z 18 | 19 | shifts ← ⟨»˘,»,«˘,«⟩ 20 | isp ← is+1 # so that Nudge shifting in zeroes doesn't collide with index 0 21 | shs ← {𝕏isp}¨ shifts 22 | edges ← isp⊸≠¨ shs 23 | 24 | areas ← /⁼⥊is 25 | •Show +´ areas × is GroupSum +´edges 26 | •Show +´ areas × is GroupSum +´edges ∧ (¯1⌽edges) ∨ (¯1⌽shifts) {isp=𝕎𝕩}¨ shs 27 | # TODO: remove ○⥊ from ⊔s when CBQN does that fast 28 | -------------------------------------------------------------------------------- /2024/BQN/12_ffi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void group_min_i16(int16_t*restrict dest, int16_t*restrict inds, int16_t*restrict vals, size_t count) { 5 | for (size_t i = 0; i < count; i++) { 6 | int16_t ind = inds[i]; 7 | int16_t val0 = dest[ind]; 8 | int16_t val1 = vals[i]; 9 | dest[ind] = val0•FLines 2024‿12 Input •args 4 | 5 | v←0¨l 6 | s1←0 7 | s2←0 8 | { 9 | 𝕩⊑v?1; 10 | lc ← 𝕩⊑l 11 | c ← 1⌾(𝕩⊸⊑) 0¨l 12 | { 13 | ps ← (lc=⊑⟜l)¨⊸/ (¬⊑⟜c)¨⊸/ (∧´≥⟜0∧<⟜(≠l))¨⊸/ ⥊𝕩 +⌜ ⟨¯1‿0,1‿0,0‿1,0‿¯1⟩ 14 | c 1¨⌾(ps⊸⊑)↩ 15 | ⍷ps 16 | }•_while_(×≠) ⋈𝕩 17 | a←+´⥊c 18 | b←+´+´∘⥊¨ {c∧¬𝕩}¨ {⟨«˘𝕩,»˘𝕩,»𝕩,«𝕩⟩}c 19 | b2←+´+´∘⥊¨ ⟨«,»,»˘,«˘⟩ {𝕩∧¬𝕎𝕩}¨ {c∧¬𝕩}¨ {⟨«˘𝕩,»˘𝕩,»𝕩,«𝕩⟩}c # originally this was b↩, and s1 was live-repurposed 20 | s1+↩a×b 21 | s2+↩a×b2 22 | v∨↩c 23 | }¨ ↕≢l 24 | # •Show¨ {⊏⟜"·#" 𝕩=l}¨ ⍷⥊l 25 | •Show s1 26 | •Show s2 -------------------------------------------------------------------------------- /2024/BQN/13.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Ints, SplitF, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← ⟨⟩SplitF Ints¨ •FLines 2024‿13 Input •args 4 | 5 | # ≡´ (⟨r0,r1⟩ - ⟨a0,a1⟩×an) ÷ ⟨b0,b1⟩ 6 | # ((r0 - a0×an) ÷ b0) = (r1 - a1×an) ÷ b1 7 | # (b1 × (r0 - a0×an)) = (r1 - a1×an) × b0 8 | # (b1×r0) - b1×a0×an = (r1×b0) - a1×an×b0 9 | # (b1×r0) = (r1×b0) + b1×a0×an - a1×an×b0 10 | # ((b1×r0) - (r1×b0)) = b1×a0×an - a1×an×b0 11 | # ((b1×r0) - (r1×b0)) = an×(b1×a0 - a1×b0) 12 | # an = ((b1×r0) - (r1×b0)) ÷ (b1×a0 - a1×b0) 13 | 14 | Calc ← { a‿b‿r: 15 | ah ← -´ r×⌽b 16 | al ← -´ a×⌽b 17 | 0=al|ah? 18 | an ← ah÷al 19 | bh ← (⊑r) - an×⊑a 20 | bl ← ⊑b 21 | 0=bl|bh? 22 | (3×an) + bh÷bl 23 | ; 0 24 | } 25 | 26 | •Show +´ Calc¨ l 27 | •Show +´ (Calc 1e13⊸+⌾(2⊸⊑))¨ l 28 | -------------------------------------------------------------------------------- /2024/BQN/13_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Repr, LCM, GCD, Ints, SInts, Split, SplitF, Indices, ReIndex, MDSlash, MDSlashI, Unjoin, Count, _fx_, _fi_, _underSelect_, Tr, TW, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← Ints¨¨ ⟨⟩SplitF•FLines 2024‿13 Input 1↓•args 4 | 5 | •Show +´∞⊸≠⊸/ { a‿b‿c: 6 | ⌊´+´¨ 3‿1⊸ר MDSlash c⊸≡¨ (a <⊸× ↕100) +⌜ b <⊸× ↕100 7 | }¨ l 8 | 9 | ⟨newrepl⟩ ← •Import ⊑•args # bqn-smt/cvc5.bqn (or z3.bqn) - https://codeberg.org/dzaima/bqn-smt 10 | r ← Newrepl @ 11 | 12 | foo ← r.REPLExec " 13 | { a‿b‿c: 14 | ac ← Int@ ⋄ _s ↩ {𝔽} 15 | bc ← Int@ ⋄ _u ↩ {𝔽} 16 | # ac ← I64@ 17 | # bc ← I64@ 18 | Sat q←⟨ac≥_s 0, bc≥_s 0, ∧´ c = (bc×b) + ac×a⟩? 19 | tk ← +´3‿1×ac‿bc 20 | tc ← Find tk 21 | # •Show FindToBQN¨ tc‿ac‿bc 22 | { 𝕊: 23 | # •Show FindToBQN¨ tc‿ac‿bc 24 | Sat q ∾ tk <_u tc? 25 | tc ↩ Find tk 26 | 1 27 | ;0 28 | }•_while_ ⊢ 1 29 | EvalToBQN tc 30 | ; @ 31 | } 32 | " 33 | 34 | # •Out 100⥊'#' 35 | •Show +´ @⊸≢¨⊸/ { a‿b‿c: 36 | c+↩ 10000000000000 37 | Foo a‿b‿c 38 | # •Show a‿b‿c 39 | # # •Show b×⌽c 40 | # •Show (-´c) ÷ •Show a +○(-´) b 41 | # # •Show (-´c)÷a-○(-´)b 42 | # •Show 43 | }¨ l 44 | -------------------------------------------------------------------------------- /2024/BQN/14.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨SInts, _underSelect_, Input⟩ ← •Import "../../utils/utils.bqn" 3 | ps‿vs ← ⌽¨ <˘2‿2⥊ <˘⍉> SInts¨ •FLines 2024‿14 Input •args 4 | 5 | sz ← 103‿101 6 | •Show {k←×𝕩 - 2÷˜sz-1 ⋄ ×´/⁼ +´1‿2× (∧´|k)⊸/¨ 1= k} sz | ps + vs×100 7 | 8 | empty ← sz ⥊ 0 9 | •Show 1⊑{ 𝕊 c‿i: 10 | m ← 1¨_underSelect_ c empty 11 | ∨´⥊ ∧⟜»⍟10 m? 12 | @‿i 13 | ; 𝕊 c‿i: 14 | ⟨sz|c+vs, 1+i⟩ 15 | }•_while_ (@≢⊑) ps‿0 16 | -------------------------------------------------------------------------------- /2024/BQN/14_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Repr, LCM, GCD, Ints, SInts, Split, SplitF, Indices, ReIndex, MDSlash, MDSlashI, Unjoin, Count, _fx_, _fi_, _underSelect_, Tr, TW, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← {<˘2‿2⥊𝕩}¨ SInts¨ •FLines 2024‿14 Input •args 4 | 5 | sz ← {(≠l)<15? 11‿7; 101‿103} 6 | ps←{p‿v: sz|p + v×100}¨ l 7 | # •Show ⍉1¨⌾(ps⊸⊑) sz⥊0 8 | z←⟨⟩ 9 | z∾↩+´∧´¨ m1←ps > <⌊sz÷2 10 | z∾↩+´∧´¨ m2←ps < <⌊sz÷2 11 | z∾↩+´∧´¨ m1 {⟨⊑𝕨,1⊑𝕩⟩}¨m2 12 | z∾↩+´∧´¨ m1 {⟨1⊑𝕨,⊑𝕩⟩}¨m2 13 | •Show×´z 14 | 15 | q←l 16 | { 𝕊: 17 | q↩{p‿v: ⟨sz|p + v,v⟩}¨q 18 | m ← 1¨⌾((⊑¨q)⊸⊑) sz⥊0 19 | zz←𝕩 20 | {𝕊: 21 | •Show zz 22 | !0 # throw & catch added afterwards to make solution terminate automatically 23 | # •Show m⊏"·#" 24 | }⍟⊢ {𝕩∧»𝕩}⍟10 m 25 | 𝕩+1 26 | }•_While_ 1⎊'e' 1 27 | # (↕100) {•Show 𝕨⋄•Show ⊏⟜"·#" 1¨⌾(𝕩⊸⊑)sz⥊0}¨ ⊑¨¨ {p‿v: ⟨sz|p + v,v⟩}¨⍟(↕100) l -------------------------------------------------------------------------------- /2024/BQN/15.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨SplitF, _underSelect_, Select, MDSlash, MDSlashI, Input⟩ ← •Import "../../utils/utils.bqn" 3 | a‿b ← ⟨⟩SplitF •FLines 2024‿15 Input •args 4 | a >↩ 5 | b ∾↩ 6 | 7 | Score ← {+´ +´¨ 100‿1 × MDSlashI 𝕨=𝕩} 8 | 9 | # part 1 10 | { 11 | c ← a 12 | y‿x ← ⋈⁼MDSlash c='@' 13 | c '.'⌾(y‿x⊸⊑)↩ 14 | ds ← ⟨0‿1,1‿0,0‿¯1,¯1‿0⟩ 15 | sh ← ≢c 16 | c ↩ c { 17 | d ← ⊑">v<^"⊐𝕨 18 | { 19 | (k←⊑𝕩⊐'.') < ⊑𝕩⊐'#'? 20 | y‿x+↩d⊑ds 21 | '.'∾(k↑𝕩)∾(1+k)↓𝕩; 22 | 𝕩 23 | } _underSelect_(sh {d≥2? (𝕩-1)-↕𝕩; 𝕩+↩1 ⋄ 𝕩+↕𝕨-𝕩}⌾((¬2|d)⊸⊑) y‿x) 𝕩 24 | }´ ⌽b 25 | •Show 'O' Score c 26 | } 27 | 28 | # part 2 29 | { 30 | Re ← {s‿d𝕊𝕩: d¨⌾((s=⥊𝕩)/⥊) 𝕩} 31 | c ← ∾˝˘ ("O[" Re a) ≍⎉0 "O]" Re "@." Re a 32 | 33 | h‿w ← ≢c 34 | 35 | y‿x ← ⋈⁼MDSlash c='@' 36 | { 37 | d ← ⊑">v<^"⊐𝕩 38 | { 39 | ⊑d∊0‿2? 40 | c { 41 | (k←⊑𝕩⊐'.') < ⊑𝕩⊐'#'? 42 | x+↩d⊑1‿0‿¯1‿0 43 | '.'∾(k↑𝕩)∾(1+k)↓𝕩; 44 | 𝕩 45 | } _underSelect_⟨y, {d=2? x-↕x+1; x+↕w-x}⟩↩ 46 | ; 47 | cy ← y 48 | dy ← d⊑0‿1‿0‿¯1 49 | ai ← ⟨⋈cy, ⋈x⟩ 50 | { 51 | 𝕊 xs: 52 | cy+↩dy 53 | (cy≥0) ∧ cy↩ 5 | b∾↩ 6 | # •Show a 7 | # •Show b 8 | c←a 9 | { 10 | d ← ⊑𝕩⊐˜"<^>v" 11 | c ⌽∘⍉⍟d↩ 12 | y‿x← ⋈⁼MDSlash c='@' 13 | c {𝕩↓˜↩1⋄('.'⊐˜𝕩)<○⊑'#'⊐˜𝕩? ".@"∾('O'⥊˜k)∾(1+k←⊑𝕩⊐'.')↓𝕩; 𝕩}⌾⌽⌾((1+x)↑y⊏⊢)↩ 14 | c ⌽∘⍉⍟(4-d)↩ 15 | 1 16 | }¨ b 17 | # •Show c 18 | •Show +´∾100‿1⊸ר MDSlash c='O' 19 | 20 | a2 ← 2/˘'.'¨⌾((a='O')⊸(/○⥊)) a 21 | a2 '.'⌾((1⊑MDSlash a2='@')⊸⊑)↩ 22 | # •Show a2 23 | {a2 "[]"⌾(((⊢⋈0‿1⊸+) 𝕩×1‿2)⊸⊑)↩⋄1}¨ MDSlash a='O' 24 | # •Show a2 25 | c2←a2 26 | { 27 | d ← ⊑𝕩⊐˜"<^>v" 28 | { 29 | ⊑d∊0‿2? 30 | c2 ⌽∘⍉⍟d↩ 31 | y‿x← ⋈⁼MDSlash c2='@' 32 | c2 {𝕩↓˜↩1⋄('.'⊐˜𝕩)<○⊑'#'⊐˜𝕩? ".@"∾(k↑𝕩)∾(1+k←⊑𝕩⊐'.')↓𝕩; 𝕩}⌾⌽⌾((1+x)↑y⊏⊢)↩ 33 | c2 ⌽∘⍉⍟(4-d)↩ 34 | ; 35 | c2 ⌽⍟(d=3)↩ 36 | m ← c2='@' 37 | ma ← m 38 | # •Show c2 39 | {𝕊: 40 | t←«m 41 | t2←t∧c2≠'.' 42 | t2∨↩ »˘ t2 ∧ c2='[' 43 | t2∨↩ «˘ t2 ∧ c2=']' 44 | ma∨↩t2 45 | m↩t2 46 | # •Show t2 47 | ∨´⥊t2 48 | }•_while_⊢ 1 49 | {𝕊: v0←ma/○⥊c2⋄ c2 '.'¨⌾((/⥊ma)⊏⥊)↩ ⋄ c2 v0⌾((/⥊«ma)⊏⥊)↩}⍟⊢ ∧´'#'≠(«ma)/○⥊c2 50 | # •Show c2 51 | c2 ⌽⍟(d=3)↩ 52 | } 53 | 1 54 | }¨ b 55 | •Show +´∾100‿1⊸ר MDSlash c2='[' 56 | -------------------------------------------------------------------------------- /2024/BQN/16.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨MDSlash, _fx_, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← >•FLines 2024‿16 Input •args 4 | nw ← l ≠ '#' 5 | vs ← l = 'S' 6 | ve ← l = 'E' 7 | 8 | start ← - 1000 × ×˜≠l # we want "infinity" to be zero (for »/«, and also to keep things in i32 range), so we make the start far into the negatives 9 | 10 | Mv ← ⟨»,»˘,«,«˘⟩{𝕎𝕩}¨⊢ 11 | MvI ← ⟨«,«˘,»,»˘⟩{𝕎𝕩}¨⊢ 12 | 13 | dists ← { 14 | nw⊸ר ⌊´⟨ 15 | Mv 𝕩+1, 16 | {⌊´ ⟨1⌽𝕩, ¯1⌽𝕩⟩} 𝕩+1000, 17 | 𝕩 18 | ⟩ 19 | }_fx_≢ (⊏⟜0‿start vs)⌾(1⊸⊑) 4⥊< 0¨vs 20 | 21 | best ← (⋈⁼ MDSlash ve) ⊑ ⌊´dists 22 | 23 | •Show best-start 24 | 25 | mv1 ← nw⊸∧¨ dists = MvI dists-1 26 | r1 ← nw⊸∧¨ dists = 1 ⌽ dists-1000 27 | r3 ← nw⊸∧¨ dists = ¯1 ⌽ dists-1000 28 | •Show +´⥊∨´ { 29 | ∨´⟨ 30 | mv1 ∧ MvI 𝕩, 31 | r1 ∧ 1⌽𝕩, 32 | r3 ∧ ¯1⌽𝕩, 33 | 𝕩 34 | ⟩ 35 | }_fx_≢ (•FLines 2024‿16 Input •args 4 | w←l='#' 5 | z←¯1000000 6 | # •Show l 7 | •Show best←⋈⁼⌊´-z-( Ints¨ •FLines 2024‿18 Input •args 4 | n0 ← 1024 # 12 for the example input 5 | zero ← (1+⌈´¨l) ⥊ 0 6 | start ← 1⌾⊑ zero 7 | 8 | Step ← {w𝕊b: (¬w)∧∨´⟨b,»b,«b,»˘b,«˘b⟩} 9 | Init ← {1¨_underSelect_(𝕩↑¨l) zero} 10 | 11 | 12 | w0 ← Init n0 13 | •Show ⊑{a‿b: ⟨a+1, w0 Step b⟩}•_while_{𝕊: ¬¯1‿¯1⊑1⊑𝕩} 0‿start 14 | 15 | •Show {𝕨∾','∾𝕩}´ •Repr¨ l⊑˜¨ ⊑ {m←⌊2÷˜+´𝕩 ⋄ m⌾((¬¯1‿¯1⊑(Init m)⊸Step _fx_ (≢∧·¬¯1‿¯1⊑⊣) start)⊸⊑) 𝕩}•_while_{¯1>-´𝕩} n0⋈≠⊑l 16 | -------------------------------------------------------------------------------- /2024/BQN/18_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Repr, LCM, GCD, Ints, SInts, Split, SplitF, Indices, ReIndex, MDSlash, MDSlashI, Unjoin, Count, _fx_, _fi_, _underSelect_, Select, Tr, TW, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← Ints¨ •FLines 2024‿18 Input •args 4 | m←1+⌈´l 5 | y0←{25=≠l? 12;1024} 6 | w ← 1¨⌾((y0↑l)⊸⊑) m⥊0 7 | # •Show w⊏"·#" 8 | Step ← {w𝕊a‿b: ⟨a+1, (¬w)∧∨´⟨b,»b,«b,»˘b,«˘b⟩⟩} 9 | init ← 0⋈1⌾⊑0¨w 10 | •Show ⊑w⊸Step•_while_{𝕊: ¬¯1‿¯1⊑1⊑𝕩} init 11 | 12 | •Out {𝕨∾','∾𝕩}´ •Repr¨ ⊑⟜l ¯1+ 1⊸+•_while_{¯1‿¯1⊑1⊑ (1¨⌾((𝕩↑l)⊸⊑) m⥊0)⊸Step _fx_(≢○(1⊸⊑)) init} y0 13 | -------------------------------------------------------------------------------- /2024/BQN/19.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Split, SplitF, Input⟩ ← •Import "../../utils/utils.bqn" 3 | a‿b ← ⟨⟩SplitF •FLines 2024‿19 Input •args 4 | a ↩ ','⊸≠⊸/¨ ' 'Split ⊑a 5 | 6 | chars ← ⍷∾a 7 | Tree ← { 8 | ⟨⟩: @; 9 | more ← (×≠¨)⊸/ 𝕩 10 | (𝕩-○≠more) ∾ Tree¨ 5 ↑ (chars ⊐ ¯1⊑¨more) ⊔ ¯1↓¨more 11 | } 12 | t←Tree a 13 | 14 | ns ← { 𝕊 c: 15 | n ← 1+chars⊐𝕩 16 | m ← •HashMap˜ ⟨⟩ 17 | 0 m.Set 1 18 | Get ← { 19 | @≢r←@m.Get 𝕩? r; 20 | 𝕩⊸m.Set⊸⊢ t {𝕩≥0? @≢tc←(𝕩⊑n)⊑𝕨? ((Get 𝕩) × ⊑tc) + tc 𝕊 𝕩-1; 0} 𝕩-1 21 | } 22 | 23 | Get ≠c 24 | }¨ b 25 | 26 | •Show +´ × ns 27 | •Show +´ ns 28 | -------------------------------------------------------------------------------- /2024/BQN/19_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Repr, LCM, GCD, Ints, SInts, Split, SplitF, Indices, ReIndex, MDSlash, MDSlashI, Unjoin, Count, _fx_, _fi_, _underSelect_, Select, Tr, TW, Input⟩ ← •Import "../../utils/utils.bqn" 3 | a‿b ← ⟨⟩SplitF •FLines 2024‿19 Input •args 4 | a ↩ ','⊸≠⊸/¨ ' 'Split ⊑a 5 | 6 | # •Show a 7 | •Show +´{ 𝕊c: 8 | # •Show 𝕩 9 | ⊑(≢c)∊{⍷⥊𝕩 +⎉1 a (≠∘⊣×≠⊸↑≡⊣)⌜ ↓⟜c¨ 𝕩}_fx_≢ ⋈0 10 | }¨ b 11 | 12 | 13 | •Show +´ { 𝕊 c: 14 | m←•HashMap˜⟨⟩ 15 | 0 m.Set 1 16 | Get ← { 17 | @≢r←@m.Get 𝕩? r; 18 | r ← +´𝕩{𝕩≡(-≠𝕩)↑𝕨↑c? Get 𝕨-≠𝕩; 0}¨ a 19 | 𝕩 m.Set r 20 | r 21 | } 22 | 23 | Get ≠c 24 | }¨ b -------------------------------------------------------------------------------- /2024/BQN/1_fast.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Input, Count⟩ ← •Import "../../utils/utils.bqn" 3 | bytes ← •file.MapBytes 2024‿1 Input •args 4 | 5 | lw ← 1 + ⊑bytes⊐@+10 # line width; 14 for input 6 | nw ← ¯2 + lw÷2 # number width; 5 for input 7 | si ← (⊢∾(3+nw)⊸+) ↕nw # indices to pick out from each row; 0‿1‿2‿3‿4‿8‿9‿10‿11‿12 for input 8 | 9 | nums ← ∘‿nw⥊ si⊸⊏˘ ∘‿lw⥊ bytes # (A) select by the indices (reshaping to n by lw matrix beforehand, and n×2 by nw afterward) 10 | digits ← ⌽ <˘ ⍉ nums-'0' # (B) subtract ASCII '0', transpose, split into list of lists, reverse that 11 | a‿b ← <˘⍉∘‿2⥊ {𝕨+10×𝕩}´ digits # (C) a+10×b-reduce, split into the two columns via reshape & transpose 12 | 13 | # 12345···67890↩54321···98765↩ 14 | # ↓ (A) 15 | # 12345···67890↩ 16 | # 54321···98765↩ 17 | # ↓ (A) 18 | # 1234567890 19 | # 5432198765 20 | # ↓ (A) 21 | # 12345 22 | # 67890 23 | # 54321 24 | # 98765 25 | # ↓ (B) 26 | # 1659 27 | # 2748 28 | # 3837 29 | # 4926 30 | # 5015 31 | # ↓ (C) 32 | # ⟨12345,67890,54321,98765⟩ 33 | # ↓ (C) 34 | # ⟨⟨12345,54321⟩, ⟨67890,98765⟩⟩ 35 | 36 | a‿b ∧¨↩ # sort both columns 37 | •Show +´ |a-b # sum of absolute differences 38 | h ← (1+⌈´a) ↑ /⁼b # indices-inverse (i.e. histogram) column b, up to max of a 39 | •Show +´ a× a ⊏ h # select from histogram by a, multiply, sum 40 | # better alternative to last two lines for large numbers, but slower for the ≤99999 inputs: 41 | # •Show +´ a× a Count b 42 | -------------------------------------------------------------------------------- /2024/BQN/1_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Repr, LCM, GCD, Ints, SInts, Split, SplitF, ReIndex, MDSlash, MDSlashI, Unjoin, Count, _fx_, _fi_, Tr, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← Num¨¨' 'Split¨•FLines 2024‿1 Input •args 4 | 5 | •Show +´|-´∧¨ <˘ ⍉>l 6 | 7 | a‿b←<˘⍉>l 8 | •Show +´a×a Count b -------------------------------------------------------------------------------- /2024/BQN/2.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Ints, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← Ints¨ •FLines 2024‿2 Input •args 4 | 5 | Deltas ← 1⊸↓ - ¯1⊸↓ 6 | vs ← { 7 | d ← Deltas 𝕩 8 | dsc ← (+´d≤0) > 0.5×≠𝕩 # input length ≥ 5; multiple zero deltas will fail regardless 9 | ⟨ 10 | (1⊸≤∧≤⟜3) -⍟dsc d 11 | -⍟dsc 𝕩 12 | ⟩ 13 | }¨ l 14 | 15 | P1 ← ∧´ ⊑ 16 | P2 ← { ok‿v: 17 | m ← (»∘¬ ∧`)⊸∨ ok 18 | ∨´ {∧´ (1⊸≤∧≤⟜3) Deltas 𝕩}¨ /⟜v¨ ⟨m∾1, 1∾m⟩ 19 | } 20 | 21 | •Show +´ P1¨ vs 22 | •Show +´ P2¨ vs 23 | -------------------------------------------------------------------------------- /2024/BQN/20.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨MDSlash, _fi_, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← >•FLines 2024‿20 Input •args 4 | 5 | ds ← ⌽¨⊸∾ ⋈⟜- 0‿1 6 | 7 | e ← ⋈⁼ MDSlash l='E' 8 | nw←l≠'#' 9 | path ← > {𝕩∾<⋈⁼ ((¯2+≠𝕩)⊑𝕩)⊸≢¨⊸/ ⊑⟜nw⊸/ (¯1⊑𝕩)⊸+¨ ds}•_while_{e≢¯1⊑𝕩} MDSlash l='S' 10 | pn ← ≠path 11 | 12 | Solve ← { 13 | cs ← > ⍷ ∾ (⍷·⥊+⌜)` 𝕩⥊< ds 14 | to ← path ⊐ path +⎉1⎉1‿2 cs 15 | saves ← pn - ⥊ (to≠pn) /○⥊ (+˝˘|cs) +⎉1 (↕pn) + pn-to 16 | +´ saves≥100 17 | } 18 | 19 | •Show Solve 2 20 | •Show Solve 20 21 | -------------------------------------------------------------------------------- /2024/BQN/20_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Repr, LCM, GCD, Ints, SInts, Split, SplitF, Indices, ReIndex, MDSlash, MDSlashI, Unjoin, Count, _fx_, _fi_, _underSelect_, Select, Tr, TW, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← >•FLines 2024‿20 Input •args 4 | nw←l≠'#' 5 | e ← ⋈⁼ MDSlash l='E' 6 | 7 | S0 ← {∨´⟨𝕩,»𝕩,«𝕩,»˘𝕩,«˘𝕩⟩} 8 | Step ← {nw∧S0 𝕩} 9 | zero ← 0¨l 10 | f0 ← (Step⌾(1⊸⊑) 1⊸+⌾⊑) _fi_(¬e⊑1⊑⊢) 0⋈l='S' 11 | path ← (MDSlash l='S')∾ {⋈⁼ MDSlash 𝕨<𝕩}´¨ <˘ 2↕ (⊢∾<∘Step∘(¯1⊑⊢)) 1⊑¨ f0 12 | Solve ← { 𝕊 k: 13 | cs ← ∧⍷∾{(⍷·⥊+⌜)´ 𝕩⥊< ⌽¨⊸∾ ⋈⟜- 0‿1}¨ 1↓↕k 14 | # •Show cs 15 | # 16 | z ← ¯1+(≠path)- ⥊{(𝕩≠≠path) /○⥊ (+´¨|cs) +⎉1 (↕≠path) + 𝕩} (⌽path) ⊐ path +⌜ cs 17 | # •Show ∧´∘ע⊸/ ⍉↕∘≠⊸≍ /⁼ 0⌈ z 18 | # •Show +´≥⟜50 z 19 | +´≥⟜100 z 20 | } 21 | •Show Solve 3 22 | •Show Solve 21 23 | 24 | # very slow part 1 that got me the answer before I managed to write the above: 25 | # { 26 | # f0 ← (Step⌾(1⊸⊑) 1⊸+⌾⊑) _fi_(¬e⊑1⊑⊢) 0⋈l='S' 27 | # z ← (⊑¯1⊑f0) - ∾{⟨a‿b,c‿d⟩: ∾{{a‿b: (e⊑Step b)⥊a} (Step⌾(1⊸⊑) 1⊸+⌾⊑) _fx_({(¬∨´⥊𝕩)∨¬e⊑𝕩}1⊑⊢) 1⊸+⌾⊑ {1⌾(𝕩⊸⊑)zero}⌾(1⊸⊑) 𝕩}¨ {a‿b: a⋈¨MDSlash b} S0⌾(1⊸⊑) ⟨c,d∧¬b⟩}¨ <˘ 2↕f0 28 | # •Show ⍉↕∘≠⊸≍ /⁼ 0⌈z 29 | # •Show +´z≥100 30 | # } 31 | -------------------------------------------------------------------------------- /2024/BQN/21_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Repr, LCM, GCD, Ints, SInts, Split, SplitF, Indices, ReIndex, MDSlash, MDSlashI, Unjoin, Count, _fx_, _fi_, _underSelect_, Tr, TW, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← •FLines 2024‿21 Input •args 4 | 5 | T0 ← { pt𝕊𝕩: 6 | br ← ⋈⁼ /∨˝˘ pt=' ' 7 | okm ← '>' ∾ {br=0? 'v'; '^'} 8 | ∾⟜'A'¨ { 9 | ∨´0=𝕩? 10 | ⋈(0⌈𝕩∾-𝕩)/"^"; 11 | 𝕨 {(((⊑¨𝕩)∊okm) ∨ (0≠1⊑⊑𝕨) = '<'≠⊑¨𝕩)/𝕩}⍟(∧´ (br=⊑¨𝕨) ∨ 0=1⊑¨𝕨) (∾⋈∾∘⌽) ⊑⊸=⊸⊔ (0⌈𝕩∾-𝕩)/"^" 12 | }⟜(-´) (≢pt) ReIndex (⥊pt) ⊐ 𝕩 13 | } 14 | 15 | T ← { pt𝕊𝕩: 16 | br ← ⋈⁼ /∨˝˘ pt=' ' 17 | okm ← '>' ∾ {br=0? 'v'; '^'} 18 | # ks ← MDSlash 𝕨≠' ' 19 | # •Show {{∧´0≠𝕩? 𝕩; @}¨ ks-<𝕩}¨ ks 20 | ⍷∾𝕨⊸{ 21 | ⋈𝕩 22 | ∾¨ ∾⟜'A'¨¨ ⥊(<⟨⟩)<⊸∾⌜´ { 23 | ∨´0=𝕩? 24 | ⋈(0⌈𝕩∾-𝕩)/"^"; 25 | 𝕨 {(((⊑¨𝕩)∊okm) ∨ (0≠1⊑⊑𝕨) = '<'≠⊑¨𝕩)/𝕩}⍟(∧´ (br=⊑¨𝕨) ∨ 0=1⊑¨𝕨) (∾⋈∾∘⌽) ⊑⊸=⊸⊔ (0⌈𝕩∾-𝕩)/"^" 26 | }⟜(-´)¨ <˘ 2↕(⋈⁼ MDSlash 𝕨='A')<⊸∾ (≢𝕨) ReIndex (⥊𝕨)⊐𝕩 27 | }¨𝕩 28 | } 29 | 30 | p0 ← ["789","456","123"," 0A"] 31 | p ← [" ^A",""] 32 | 33 | # •Show p0 T ⋈"029A" 34 | •Show +´{(•ParseFloat 3↑𝕩) × ⌊´≠¨ p⊸T⍟2 p0⊸T ⋈𝕩}¨ l 35 | # •Show {⍷≠¨ p⊸T⍟1 p0⊸T ⋈𝕩}¨ l 36 | # •Show {⍷≠¨ p⊸T⍟2 p0⊸T ⋈𝕩}¨ l 37 | 38 | root ← (p0 T ⋈)¨ l 39 | # scv← {•Show p T ⋈𝕩∾"A"}¨ sck←"<>^vA" 40 | # scv ↩ (⊑3⊸⌽)¨ scv 41 | # •Show scv 42 | # C ← (≠sck)↑/⁼ 43 | # sct ← >C¨ sck⊸⊐¨ scv 44 | # •Show sct 45 | # •Show { 46 | # +´(+˝sct×⊢)⍟1 •Show C sck⊐𝕩 47 | # }¨¨ root 48 | 49 | sck ← "<>^vA" 50 | scs ← ⋈⌜˜ sck 51 | sct ← {p T0 𝕨⋈𝕩}⌜˜ sck 52 | # •Show scs 53 | # •Show sct 54 | # •Show {+´ (⌊´≠¨)¨ ⊑⟜sct (≢scs) ReIndex (⥊scs) ⊐ <˘ 2↕'A'∾𝕩}¨¨ sct 55 | •Show ⌊´ ⥊{ 56 | sctb ← ((≢sct)⥊𝕩)⊑¨sct # ((¯1⊸⊑∘/⌊´=⊢)¨ {⌊´≠¨ p⊸T⍟2 ⋈𝕩}¨¨ sct)⊑¨ sct 57 | AA ← {(≢scs)⥊(≠⥊scs) ↑ /⁼ (⥊scs) ⊐ <˘ 2↕'A'∾𝕩} 58 | sctt ← {AA 𝕩}¨sctb 59 | # •Show sctt 60 | +´({•ParseFloat 3↑𝕩}¨ l) × ⌊´¨ {+´⥊ (+´∘⥊sctt×⊢)⍟25 AA 𝕩}¨¨ root 61 | }¨ ↕⥊≠¨ sct 62 | # •Show¨ {⊑⟜sct (≢scs) ReIndex (⥊scs) ⊐ <˘ 2↕'A'∾𝕩}¨¨ 1↑root 63 | -------------------------------------------------------------------------------- /2024/BQN/22.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Ints, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← Ints •FChars 2024‿22 Input •args 4 | 5 | _step_ ← { s‿e _𝕣_ len: 6 | shift ← {s≥0? (s⥊0)⊸«; ((-s)⥊0)⊸»} 7 | mask ← (32×len) ⥊ 32↑ (-s)≤↕e-s 8 | ⊢ ≠ mask ∧ Shift 9 | } 10 | 11 | Next ← ⊢ 12 | Next ↩ ¯6‿18 _step_ (≠l) Next # << 6 13 | Next ↩ 5‿24 _step_ (≠l) Next # >> 5 14 | Next ↩ ¯11‿13 _step_ (≠l) Next # << 11 15 | 16 | steps ← (⥊1‿32•bit._cast)¨ Next⍟(↕2001) 32‿1•bit._cast l 17 | 18 | •Show +´¯1⊑steps 19 | 20 | prices ← ⍉ 10| >steps 21 | deltas ← (1⊸↓ - ¯1⊸↓)˘ prices 22 | mdelta‿mprice ← <˘⍉> (<˘ prices) {m←∊𝕩 ⋄ ⟨m/𝕩, (0‿0‿0‿0∾m)/𝕨⟩}¨ 4↕¨ <˘ deltas 23 | uniq0 ← ⊐∾mdelta 24 | uniq ← ((≠¨mdelta) / ↕≠l) ⊔ uniq0 25 | zeroes ← (1+⌈´uniq0)⥊0 26 | •Show ⌈´ +´ uniq {𝕩⌾(𝕨⊸⊏) zeroes}¨ mprice -------------------------------------------------------------------------------- /2024/BQN/22_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Repr, LCM, GCD, Ints, SInts, Split, SplitF, Indices, ReIndex, MDSlash, MDSlashI, Unjoin, Count, _fx_, _fi_, _underSelect_, Select, Tr, TW, _memo, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← Num¨ •FLines 2024‿22 Input •args 4 | Xor ← 32•bit._xor 5 | S ← {{𝕩 Xor 16777216| 2048×𝕩} {16777216| 𝕩 Xor ⌊32÷˜𝕩} 16777216| 𝕩 Xor 𝕩×64} 6 | •Show +´S⍟2000 l 7 | 8 | d ← ⍉{(1↓𝕩)-¯1↓𝕩} o←10| >S⍟(↕2001) l 9 | # •Show ≠o 10 | ot ← <˘ ⍉o 11 | ws ← 4↕¨ <˘ d 12 | b ← ot {m←∊𝕩 ⋄ (m/𝕩)⋈(4+/m)⊏𝕨}¨ ws 13 | a ← ∾⊑¨b 14 | au ← ⍷a 15 | c ← ((≠∘⊑¨b)/↕≠l)⊔⊐a 16 | # •Show (((≠au)↑/⁼)¨ c) × 1⊑¨b 17 | # •Show c 18 | # •Show ≠au 19 | # •Show (1⊑¨b)⊏¨ <˘ ⍉o 20 | •Show ⌈´ +´ c {(≠au) ↑ +´¨ 𝕨⊔1⊑𝕩}¨ b 21 | -------------------------------------------------------------------------------- /2024/BQN/23_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Repr, LCM, GCD, Ints, SInts, Split, SplitF, Indices, ReIndex, MDSlash, MDSlashI, Unjoin, Count, _fx_, _fi_, _underSelect_, Select, Tr, TW, _memo, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← '-'Split¨•FLines 2024‿23 Input •args 4 | s‿e ← <˘⍉>l 5 | 6 | m ← •HashMap˜⟨⟩ 7 | s { 8 | 𝕨 m.Set (⟨⟩ m.Get 𝕨)∾<𝕩 9 | }¨{𝕨𝔽𝕩⋄𝕩𝔽𝕨} e 10 | # •Show ≠⍷{∧{⍷𝕩∾·∾⟨⟩⊸m.Get¨𝕩}_fx_≢ ⋈𝕩}¨ ⍷s∾e 11 | lan ← ⍷∾s { 12 | l ← m.Get 𝕨 13 | r ← m.Get 𝕩 14 | (∧·⍷𝕨‿𝕩∾⋈)¨ l(∊/⊣)r 15 | }¨ e 16 | 17 | •Show +´(∨´'t'=⊑¨)¨lan 18 | 19 | # •Show ∧{𝕩/˜⌈´⊸=⊒𝕩}s∾e 20 | # { 21 | # •Show (∊/⊣)´ m.Get¨ 𝕩 22 | # }¨ lan 23 | 24 | •Out 1↓∾'-'⊸∾¨ ⋈⁼ ⍷ ∾{ 25 | k ← ≠𝕩 26 | z←+˝(=⌜´↕¨∘≢)⊸∨ t←> 𝕩⊸∊¨ m.Get¨ 𝕩 27 | good ← 12≤+´13≤z 28 | # originally result manually derived from: 29 | # 𝕩{¬𝕩?𝕩;•Show¨ 𝕨‿t⋄𝕩} good 30 | good/⋈(13≤z)/𝕩 31 | }¨ ⍷{ 32 | ∧𝕩<⊸∾m.Get 𝕩 33 | }¨ s 34 | -------------------------------------------------------------------------------- /2024/BQN/24_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Repr, LCM, GCD, Ints, SInts, Split, SplitF, Indices, ReIndex, MDSlash, MDSlashI, Unjoin, Count, _fx_, _fi_, _underSelect_, Select, Tr, TW, _memo, Input⟩ ← •Import "../../utils/utils.bqn" 3 | a‿b ← ⟨⟩SplitF •FLines 2024‿24 Input •args 4 | 5 | a {¯1⊸↓⌾⊑' 'Split𝕩}¨↩ 6 | b {0‿1‿2‿4⊏' 'Split𝕩}¨↩ 7 | 8 | swaps ← ⟨ 9 | # part 2 done by manually adding entries here until the various prints at the bottom got more uniform 10 | ⟩ 11 | swapMap ← •HashMap´ <˘⍉∘‿2⥊>(⊢∾⌽¨) swaps 12 | 13 | b0 ← b 14 | b {𝕩 swapMap.Get 𝕩}⌾(3⊸⊑)¨↩ 15 | # •Show (b0≢¨b)/b0 16 | # •Exit 1 17 | 18 | b {∧⌾(0‿2⊸⊏) 𝕩}¨↩ 19 | b ∧↩ 20 | 21 | bm ← (3⊑¨b)•HashMap b 22 | 23 | m ← •HashMap´"1"⊸≡¨⌾(1⊸⊑) <˘⍉>a 24 | l ← ⟨⟩ 25 | Get ← { 26 | @≢r←@m.Get 𝕩? r; 27 | a‿b‿c‿d ← bm.Get 𝕩 28 | r ← ({"XOR":≠;"OR":∨;"AND":∧}b)´ Get¨ a‿c 29 | l∾↩<𝕩 30 | r ⊣ 𝕩 m.Set r 31 | } 32 | 33 | •Show +´{𝕩×2⋆↕≠𝕩} Get¨ ∧('z'=⊑¨)⊸/ ⍷3⊑¨b 34 | 35 | # { 36 | # x‿y←<˘2‿∘⥊⊑¨a 37 | # bm0 ← (⍷0⊑¨b) •HashMap (⊐0⊑¨b)⊔b 38 | # bm1 ← (⍷2⊑¨b) •HashMap (⊐2⊑¨b)⊔b 39 | # Uses ← {(@ bm1.Get 𝕩)bm0.Get 𝕩} 40 | # •Show >{{≡´1↓¨0‿2⊏𝕩}¨ 𝕩}¨ bm0.Get∘•Show¨ x 41 | # •Show >{𝕩⋈∧⌾(⌽¨)Uses 3⊑𝕩}¨1⊑¨1↓Uses¨ x 42 | # } 43 | # •Show >bm.Get¨ ('z'=⊑¨l)/l 44 | # # pn ← l•HashMap l{⊑(⊑𝕨)∊"xyz"? 𝕨; 'I'∾"000"«˜•Repr 𝕩}¨ ↕≠l 45 | # # •Show >{𝕩pn.Get𝕩}¨¨ bm.Get¨ l 46 | 47 | # # •Show >bm.Get¨ l 48 | •Show 1↓∾','∾¨∧∾swaps -------------------------------------------------------------------------------- /2024/BQN/25.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨SplitF, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← ⟨⟩SplitF •FLines 2024‿25 Input •args 4 | 5 | •Show +´ ⥊ ∧´¨ 7 ≥ +⌜´ ({⋈⁼⍷⊏𝕩}¨ ⊔ +˝¨) ('#'=>)¨ l 6 | -------------------------------------------------------------------------------- /2024/BQN/25_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Repr, LCM, GCD, Ints, SInts, Split, SplitF, Indices, ReIndex, MDSlash, MDSlashI, Unjoin, Count, _fx_, _fi_, _underSelect_, Select, Tr, TW, _memo, Input⟩ ← •Import "../../utils/utils.bqn" 3 | d‿u ← '#'={⋈⁼⍷'#'=⊏𝕩}¨⊸⊔ >¨ ⟨⟩SplitF •FLines 2024‿25 Input •args 4 | 5 | •Show +´⥊∧´¨7≥+⌜´+˝¨¨d‿u -------------------------------------------------------------------------------- /2024/BQN/2_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Repr, LCM, GCD, Ints, SInts, Split, SplitF, ReIndex, MDSlash, MDSlashI, Unjoin, Count, _fx_, _fi_, Tr, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← SInts¨ •FLines 2024‿2 Input •args 4 | 5 | •Show +´{∧´(≥⟜1∧≤⟜3)|-˝˘2↕𝕩}¨ (∧⊸≡∨∨⊸≡)¨⊸/ l 6 | # •Show +´{1≥+´¬(≥⟜1∧≤⟜3)|-˝˘2↕𝕩}¨ (∧⊸≡∨∨⊸≡)¨⊸/ l 7 | •Show +´{𝕊a: ∨´{(∧⊸≡∨∨⊸≡) z←𝕩/a? {∧´(≥⟜1∧≤⟜3)|-˝˘2↕𝕩}z;0}¨ ≠⟜<↕≠𝕩}¨ l 8 | -------------------------------------------------------------------------------- /2024/BQN/2_short.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Ints, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← Ints¨ •FLines 2024‿2 Input •args 4 | 5 | # not really a golfed solution, but just a shorter one than 2.bqn at the cost of being O(n^2) per line 6 | P1 ← {d←-˝˘2↕𝕩 ⋄ (1=≠⍷×d) ∧´ (1⊸≤∧≤⟜3) | d} 7 | P2 ← {∨´ P1∘/⟜𝕩˘ ≠⌜˜↕≠𝕩} 8 | •Show +´ P1¨ l 9 | •Show +´ P2¨ l 10 | -------------------------------------------------------------------------------- /2024/BQN/3.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← •FChars 2024‿3 Input •args 4 | 5 | ks ← (-≠l)↑"mul("⍷l # class 1: start mask (marks the opening paren) 6 | km ← l=',' # class 2: middle separator mask 7 | ke ← l=')' # class 3: end mask 8 | kn ← ('0'⊸≤∧≤⟜'9') l # class 4: number mask 9 | knc← kn∧»kn # class 5: number continuation mask 10 | class ← +´ ⟨ks, km, ke, kn, knc⟩ × 1‿2‿3‿4‿1 # class of each character 11 | si ← 1+/ {class {(≠𝕨)↑1‿4‿2‿4‿3⍷𝕨}⌾(𝕩⊸/) 0¨class} 5≠class # find the mul(n,n) pattern under filtering out number continuation chars; returns indices of each first number 12 | mi ← km /⊸(⍋⊏⊣) si # location of corresponding comma for each of the above 13 | ei ← ke /⊸(⍋⊏⊣) si # location of corresponding end for each of the above 14 | n0 ← •ParseFloat¨ si ↓¨ mi↑¨•FLines 2024‿4 Input •args 4 | 5 | a ← ⌽∘⍉⍟(↕4) l 6 | lp ← l∾˘ ' '¨l 7 | b ← (-↕≠l) ⌽˘ lp 8 | c ← ( ↕≠l) ⌽˘ lp 9 | 10 | •Show +´ {+´⥊"XMAS"⍷𝕩}¨ a∾ ⍉¨ ⌽¨⊸∾ b‿c 11 | 12 | •Show +´ {+´⥊ ∧´ ⥊ ["M.S",".A.","M.S"] {'.'𝕊𝕩: 1; 𝕨=𝕩}¨ 3‿3 TW 𝕩}¨ a 13 | # some alternatives for part 2: 14 | # •Show +´{+´⥊ ∧´ ⥊ ["M.S",".A.","M.S"] (= ∨ =⟜'.') 3‿3 TW 𝕩}¨ a 15 | # •Show +´{+´⥊ ∧˝∾˝ ["M.S",".A.","M.S"] (= ∨ =⟜'.') (¯2+≢)⊸↕𝕩}¨ a 16 | # •Show +´{+´⥊ ∧´ ⥊ ["M.S",".A.","M.S"] (= ∨ =⟜'.') <⎉2 (2-˜≢)⊸↕𝕩}¨ a 17 | # •Show +´{+´⥊ ∧´⎉1 ⥊⎉2 ["M.S",".A.","M.S"] (=⎉2 ∨ =⟜'.' ⊣⎉2 ⊢) 3‿3↕𝕩}¨ a 18 | -------------------------------------------------------------------------------- /2024/BQN/4_fast.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Input,TW, MDSlash⟩ ← •Import "../../utils/utils.bqn" 3 | l ← >•FLines 2024‿4 Input •args 4 | 5 | SuperFind ← { p𝕊src: 6 | cu ← '.'⊸≠⊸/ ⍷∾⥊¨p # unique characters 7 | 8 | pk ← ((≠cu) (⊢-+⟜1×=) cu⊸⊐)¨ p # pattern key: index in cu for each char; ¯1 for '.' 9 | cw ← pk ⊔○(∾⥊¨) (↕≠pk) ⋈¨¨ ↕∘≢¨ pk # where each character from cu is; list of lists of ⟨pattern index, y‿x⟩ 10 | 11 | shv ← ↑ ⟨⌈´≠¨p, 1⊑≢src⟩⥊0 # matrices of zeroes to shift in 12 | 13 | rm ← 1¨ pk # result masks 14 | cu { # for each unique character: 15 | ri‿ms ← <˘⍉>𝕩 # indices in result, mask shift 16 | i ← ⊐ms # indices in unique shifts 17 | msf ← (∊i)/ms # the unique shifts 18 | msy‿msx ← <˘⍉>msf # their y/x 19 | ri {rm∧⟜𝕩⌾(𝕨⊸⊑)↩⋄1}¨ i⊏(msy⊏shv) «¨ «˘⍟msx 𝕨=src # shift the source compared to the current character, and the masks in rm by it 20 | }¨ cw 21 | rm 22 | } 23 | 24 | p1 ← ⌽∘⍉⍟(↕4) ≍"XMAS" 25 | p1∾↩ {⌽˘¨⊸∾ ⌽⊸⋈ 𝕩} 4‿4⥊∾∾⟜"...."¨ "XMAS" 26 | 27 | p2 ← ⌽∘⍉⍟(↕4) ["M.S",".A.","M.S"] 28 | 29 | •Show¨ +´¨ (≠p1)(↑⋈↓) +´∘⥊¨ (p1∾p2) SuperFind l 30 | # running the two parts separately (slightly slower): 31 | # •Show +´ +´∘⥊¨ p1 SuperFind l 32 | # •Show +´ +´∘⥊¨ p2 SuperFind l 33 | -------------------------------------------------------------------------------- /2024/BQN/4_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Repr, LCM, GCD, Ints, SInts, Split, SplitF, ReIndex, MDSlash, MDSlashI, Unjoin, Count, _fx_, _fi_, Tr, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← >•FLines 2024‿4 Input •args 4 | 5 | b←(-↕≠l) ⌽˘ {𝕩∾' '¨𝕩}˘ l 6 | c←(↕≠l) ⌽˘ {𝕩∾' '¨𝕩}˘ l 7 | 8 | •Show +´{(+´·⥊"XMAS"⊸⍷) 𝕩}¨ ⟨⌽∘⍉⍟(↕4) l⟩⊑⊸∾ ⍉¨ ⌽¨⊸∾ ⟨b, c⟩ 9 | •Show +´{+´⥊ ∧´ {𝕩↑˜¨<⌊´≢¨𝕩} (2==)¨⊸/⥊ ["M.S",".A.","M.S"] {'.'𝕊𝕩:1; 𝕨=𝕩}¨ ↓⟜𝕩¨ ↕3‿3}¨ ⌽∘⍉⍟(↕4) l -------------------------------------------------------------------------------- /2024/BQN/5.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Ints, SplitF, Input⟩ ← •Import "../../utils/utils.bqn" 3 | a‿b ← Ints¨¨ ⟨⟩SplitF •FLines 2024‿5 Input •args 4 | 5 | m ← (1+⌈´∾a) ↑ ⊔´<˘⍉>a 6 | 7 | s ← {𝕩⊏˜ ⍋ +´ 𝕩⊸∊¨ 𝕩⊏m}¨ b 8 | (•Show +´)¨ (2∾˜s≢¨b) ⊔ {𝕩⊑˜⌊2÷˜≠𝕩}¨ s 9 | -------------------------------------------------------------------------------- /2024/BQN/5_fast.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Input⟩ ← •Import "../../utils/utils.bqn" 3 | bytes ← •file.MapBytes 2024‿5 Input •args 4 | 5 | nw ← 1+ ⊑bytes⊐'|' # number width, plus 1 6 | sep ← ⋈⁼/ (@‿@+10) ⍷ bytes # index of the the two newlines 7 | 8 | Nums ← {(1⊸⊏˘ + 10×0⊸⊏˘) '0'-˜∘‿nw⥊𝕩} # parse an nw×n string into n numbers, ignoring the last char of each 9 | 10 | a ← ∘‿2⥊ Nums (1+sep)↑bytes # parse the first section, ordering rules 11 | 12 | b0 ← (2+sep)↓bytes # the second section 13 | bw ← 3÷˜-⟜» /0∾b0=@+10 # number of pages in each each line: deltas between newlines, div 3 14 | b ← (/bw) ⊔ Nums b0 # parse the numbers, group into lines 15 | 16 | n ← 1+⌈´⥊a # number of nodes 17 | ma ← <˘ 1¨⌾((+˝n‿1×⍉a)⊏⥊) n‿n⥊0 # build adjacency matrix 18 | s ← {𝕩⊏˜ ⍋ +´ 𝕩⊸⊏¨ 𝕩⊏ma}¨ b # sort each page list by how many other pages in the list it's adjacent to 19 | (•Show +´)¨ (2∾˜s≢¨b) ⊔ {𝕩⊑˜⌊2÷˜≠𝕩}¨ s # extract middle of each sorted list, group by equality to the original, sum & output each 20 | -------------------------------------------------------------------------------- /2024/BQN/5_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Repr, LCM, GCD, Ints, SInts, Split, SplitF, ReIndex, MDSlash, MDSlashI, Unjoin, Count, _fx_, _fi_, Tr, Input⟩ ← •Import "../../utils/utils.bqn" 3 | a‿b ← Ints¨¨⟨⟩SplitF•FLines 2024‿5 Input •args 4 | pn←1+⌈´∾a 5 | m← pn↑⊔´<˘⍉>a 6 | ok←{ 7 | ¬∨´(𝕩) {∨´𝕩∊𝕨⊑m}¨ ¯1↓↑𝕩 8 | }¨ b 9 | •Show +´{𝕩⊑˜⌊2÷˜≠𝕩}¨ ok/b 10 | •Show +´{𝕩⊑˜⌊2÷˜≠𝕩}¨ { 11 | 𝕩⊏˜⍋⍉(𝕩⊏m) ⊑∘∊˜⌜ 𝕩 12 | }¨ (¬ok)/b -------------------------------------------------------------------------------- /2024/BQN/6.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨MDSlash, _underSelect_, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← >•FLines 2024‿6 Input •args 4 | 5 | n ← ≠l 6 | w ← l='#' 7 | p0 ← 0 ⋈ ⋈⁼MDSlash l='^' 8 | 9 | nd ← n-1 10 | RotPos ← ⊣◶⟨⊢, (⊣⋈˜nd-⊢)´⊢, nd⊸-, (⊢⋈nd-⊣)´⊢⟩ 11 | 12 | ds ← (⌽-⌾⊑)⍟(↕4) ¯1‿0 13 | 14 | mz ← 0¨ l 15 | v ← •HashMap˜⟨⟩ 16 | G ← { part1 𝕊 wr: 17 | m ← mz 18 | S ← { 19 | 0 v.Get 𝕩? 1; 20 | d‿p: 21 | 𝕩 v.Set 1 22 | y2‿x2 ← d RotPos p 23 | forward ← (-y2)↑x2⊑d⊑wr 24 | steps ← ⊑forward⊐1 25 | dc ← d⊑ds 26 | {𝕊: m 1¨ _underSelect_ (p + dc ×⌜ ↕steps+1) ↩}⍟⊢ part1 27 | 28 | steps ≠ ≠forward? 29 | ⟨4|1+d, p + steps×dc⟩ 30 | ; 31 | 0 32 | } 33 | st ← S•_while_(1==) p0 34 | v.Delete¨ v.Keys@ 35 | st‿m 36 | } 37 | # ! 0=⊑G w 38 | 39 | wr ← <˘¨ ¯1⌽ ⌽∘⍉⍟(↕4) w 40 | m0 ← 1⊑ 1 G wr 41 | •Show +´⥊ m0 42 | •Show +´ { 43 | ⊑ 0 G (RotPos⟜𝕩¨ ¯1⌽↕4) {x‿y𝕊𝕩: 1⌾(y⊑x⊑⊢)𝕩}¨ wr 44 | }¨ MDSlash m0 45 | -------------------------------------------------------------------------------- /2024/BQN/6_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Repr, LCM, GCD, Ints, SInts, Split, SplitF, ReIndex, MDSlash, MDSlashI, Unjoin, Count, _fx_, _fi_, Tr, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← >•FLines 2024‿6 Input •args 4 | w←l='#' 5 | p ← 0 ⋈ ⋈⁼MDSlash l='^' 6 | n←≠l 7 | 8 | ds ← (⌽-⌾⊑)⍟(↕4) ¯1‿0 9 | 10 | G ← { 𝕊 w: 11 | m ← 0¨l 12 | wr←<˘∘⍉¨ ⍉∘⌽⍟(↕4) ⌽w 13 | v←•HashMap˜⟨⟩ 14 | S ←{ 15 | 0 v.Get 𝕩? 1; 16 | ·‿p: ∨´(p≤0) ∨ p≥n-1? 0; 17 | ⟨d,p⟩: 18 | 𝕩 v.Set 1 19 | y2‿x2 ← (⌽(n-1)⊸(-)⌾⊑)⍟(-d) p 20 | # •Show ⟨d,p,y2‿x2,(-y2)↑x2⊑d⊑wr⟩ 21 | s ← +´∧`¬>(-y2)↑x2⊑d⊑wr 22 | m (1+d)¨⌾((p⊸+¨(d⊑ds)⊸ר↕s+1)⊸⊑) ↩ 23 | (4|1+d) ⋈ p+ (d⊑ds)×s 24 | } 25 | st ← S•_while_(1==) p 26 | 27 | st‿m 28 | } 29 | # •Show S⍟11 p 30 | # •Show +´⥊G@ 31 | •Show +´×⥊1⊑G w 32 | •Show +´{ 33 | w2 ← 1⌾(𝕩⊸⊑) w 34 | ⊑G w2 35 | }¨ ⥊↕n‿n 36 | -------------------------------------------------------------------------------- /2024/BQN/7.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Ints, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← Ints¨ •FLines 2024‿7 Input •args 4 | 5 | Part ← { part 𝕊 𝕩: 6 | exp ← 0⊑𝕩 7 | start ← 1⊑𝕩 8 | tail ← 2↓𝕩 9 | exp × ∨´start = ⟨exp⟩ { val𝕊acc: 10 | sub ← acc - val 11 | r ← sub ∾ ⌊⊸=⊸/ acc÷val 12 | {𝕊: 13 | r∾↩ ⌊⊸=⊸/ sub ÷ 10⋆≠•Repr val 14 | }⍟⊢ part=2 15 | r 16 | }´ tail 17 | }¨⟜l 18 | 19 | •Show +´ Part 1 20 | •Show +´ Part 2 21 | -------------------------------------------------------------------------------- /2024/BQN/7_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Repr, LCM, GCD, Ints, SInts, Split, SplitF, ReIndex, MDSlash, MDSlashI, Unjoin, Count, _fx_, _fi_, _underSelect_, Tr, TW, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← Ints¨ •FLines 2024‿7 Input •args 4 | 5 | •Show +´{h←1⊑𝕩⋄ t←⌽2↓𝕩 ⋄ (⊑𝕩)××+´(⊑𝕩)=h {b‿o𝕊a: a o◶+‿× b}´¨ t⊸(⋈¨)¨⥊↕2⥊˜≠t}¨ l 6 | C ← {(𝕨 × 10⋆≠•Repr𝕩) + 𝕩} 7 | •Show +´{h←1⊑𝕩⋄ t←⌽2↓𝕩 ⋄ (⊑𝕩)××+´(⊑𝕩)=h {b‿o𝕊a: a o◶+‿×‿C b}´¨ t⊸(⋈¨)¨⥊↕3⥊˜≠t}¨ l -------------------------------------------------------------------------------- /2024/BQN/8.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨MDSlash, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← >•FLines 2024‿8 Input •args 4 | 5 | n ← ≠l 6 | fs ← (⊐ '.'⊸≠⊸/ ⥊l) ⊔ MDSlash l≠'.' 7 | x0‿y0‿x1‿y1 ← ∾¨ <˘⍉> { 8 | xy ← <˘⍉>𝕩 9 | ns←ט n←≠𝕩 10 | (ns ⥊ ¬1↑˜n+1)⊸/¨ (n/¨xy) ∾ ns⥊¨xy 11 | }¨ fs 12 | 13 | ps ← ⟨x0≍˘y0, (x0-x1) ≍˘ y0-y1⟩ 14 | 15 | # ! 1 = ⌈´ (x0-x1) •math.GCD○| y0-y1 16 | 17 | InBounds ← ∧˝˘ ≥⟜0 ∧ <⟜n 18 | 19 | •Show ≠⍷ InBounds⊸/ +´ps 20 | 21 | •Show ≠⍷ ∾ ⊑¨ { p‿d: 22 | r←p+d 23 | (InBounds r)⊸/¨ ⟨r, d⟩ 24 | }⍟(↕n) ps -------------------------------------------------------------------------------- /2024/BQN/8_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Repr, LCM, GCD, Ints, SInts, Split, SplitF, ReIndex, MDSlash, MDSlashI, Unjoin, Count, _fx_, _fi_, _underSelect_, Tr, TW, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← >•FLines 2024‿8 Input •args 4 | s←≢l 5 | •Show +´⥊{1¨⌾(𝕩⊸⊑) 0¨l} (∧´≥⟜0∧<⟜s)¨⊸/ ∾{ 6 | (≠⌜˜∘(↕≠) /○⥊ {(2×𝕩)-𝕨}⌜˜) MDSlash 𝕩=l 7 | }¨ '.'⊸≠⊸/ ⍷⥊l 8 | •Show +´⥊{1¨⌾(𝕩⊸⊑) 0¨l} (∧´≥⟜0∧<⟜s)¨⊸/ ∾{ 9 | ∾(≠⌜˜∘(↕≠) /○⥊ {((𝕩⊸×1⊸+)-𝕨×⊢)¨↕⊑s}⌜˜) MDSlash 𝕩=l 10 | }¨ '.'⊸≠⊸/ ⍷⥊l -------------------------------------------------------------------------------- /2024/BQN/9.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← '0'-˜⊑•FLines 2024‿9 Input •args 4 | 5 | bn‿en ← <˘⍉> ↑‿2⥊l 6 | bs‿es ← <˘⍉> ∘‿2⥊0∾+`l 7 | bi ← ↕≠bn 8 | 9 | 10 | fs ← l / (≠l)↑ ⥊ bi≍˘¯1 11 | head‿tail ← ((+` »⊸≥ +`⌾⌽∘¬) fs=¯1) ⊔ fs 12 | •Show {+´ 𝕩 × ↕≠𝕩} (¯1⊸≠⊸/ ⌽tail)⌾((head=¯1)⊸/) head 13 | 14 | bs2 ← ⌽{ 15 | n ← 𝕩⊑bn 16 | 𝕩 ≠ i← ⊑1⊐˜ n ≤ 𝕩↑en? 17 | s ← i⊑es 18 | en -⟜n⌾(i⊸⊑)↩ 19 | es +⟜n⌾(i⊸⊑)↩ 20 | s 21 | ; 𝕩⊑bs 22 | }¨ ⌽bi 23 | 24 | •Show +´ bi × (2÷˜bn×bn-1) + bn×bs2 25 | -------------------------------------------------------------------------------- /2024/BQN/9_orig.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | ⟨RB, B10, Num, Repr, LCM, GCD, Ints, SInts, Split, SplitF, ReIndex, MDSlash, MDSlashI, Unjoin, Count, _fx_, _fi_, _underSelect_, Tr, TW, Input⟩ ← •Import "../../utils/utils.bqn" 3 | l ← '0'-˜⊑•FLines 2024‿9 Input •args 4 | 5 | z←↑‿2⥊l 6 | m ← z /○⥊ (↕≠z)≍˘¯1 7 | # •Show ⊏⟜("."∾'0'+↕10)1+ m 8 | t ← (+`⌾⌽ m≠¯1) > +`m=¯1 9 | # •Show t⊏"·#" 10 | b←{a‿b: (¯1⊸≠⊸/ ⌽a)⌾((b=¯1)⊸/)b} t⊔m 11 | 12 | •Show +´b×↕≠b 13 | 14 | p←m 15 | { 16 | n←+´𝕩=p 17 | ls ← ↕∘≠⊸(⊣-·⌈`×) p≠¯1 18 | i ← ⊑ls⊐n 19 | i<⊑p⊐𝕩? 20 | p ¯1¨⌾((p=𝕩)⊸/)↩ 21 | p 𝕩¨⌾(n↑(i-n-1)↓⊢)↩ 22 | # •Show ⊏⟜("."∾'0'+↕10)1+ p 23 | 24 | ; 25 | @ 26 | }¨ ⌽↕≠z 27 | 28 | # •Show 1+p 29 | •Show +´(0⌈p)×↕≠(0⌈p) 30 | # •Show ⊏⟜("."∾'0'+↕10)1+ p 31 | # -------------------------------------------------------------------------------- /2024/BQN/makefile: -------------------------------------------------------------------------------- 1 | default: 2 | @echo "Argument of day to build required" 3 | 4 | lib12.so: 12_ffi.c 5 | $(CC) -O3 -shared -o $@ $< 6 | 7 | 12: lib12.so -------------------------------------------------------------------------------- /2024/BQN/test.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | { 3 | exp ⇐ ⟨ 4 | ⟨ "1", 1808101‿27846004⟩ 5 | ⟨ "2", 82‿321⟩ 6 | ⟨ "3", 138539169‿98109282⟩ 7 | ⟨ "4", 1993‿934⟩ 8 | ⟨ "5", 4430‿5706⟩ 9 | ⟨ "6", 5222‿2178⟩ 10 | ⟨ "7", 178081517923419‿816730407299909⟩ 11 | ⟨ "8", 144‿515⟩ 12 | ⟨ "9", 6322758938834‿6346483454541⟩ 13 | ⟨"10", 135‿189⟩ 14 | ⟨"11", 204651‿241963594833562⟩ 15 | ⟨"12", 3126602‿2311062⟩ 16 | ⟨"13", 32709‿61867094825367⟩ 17 | ⟨"14", 224052864‿8221⟩ 18 | ⟨"15", 1366877‿1397937⟩ 19 | ⟨"19", 377‿562036329872229⟩ 20 | ⟨"21", 244000‿292038245006330⟩ 21 | ⟨"22", 15216936880‿1651⟩ 22 | ⟨"25", ⟨4742⟩⟩ 23 | ⟩ 24 | 25 | path ⇐ •path 26 | args ⇐ •args 27 | all ⇐ "21"⊸≢¨⊸/ ⊑¨ exp 28 | most ⇐ all 29 | orig ⇐ ∾⟜"_orig"¨ ⟨"21"⟩∾ "13"⊸≢¨⊸/ all 30 | } •Import "../../utils/test.bqn" 31 | -------------------------------------------------------------------------------- /2024/inputs/10.txt: -------------------------------------------------------------------------------- 1 | 93263245604301898789541105678800784501078701798 2 | 14198714312096544329878987929917453692899950149 3 | 99872104343687636010561126543328012786732845234 4 | 01263457257610521923475056716789410189744776765 5 | 74789867897989270014598845605210321670645689889 6 | 65654098765673981222343218784373014571232343671 7 | 04032149834399070341654505604284623446787654376 8 | 33210298421588678698789432312167746815698210215 9 | 21853239875674569989436521285098945694521789201 10 | 12764304562145478432127014106567897703406772345 11 | 91655013061212376561011905040122105412820431006 12 | 87786732170407765251243298788935076512941239887 13 | 96543244988959864100345142344840689301693243395 14 | 67820155997988693210166051056741290002887655431 15 | 54948926786577665429297814653492101915778666442 16 | 45082131794560571234310924348588732320669777891 17 | 32173022653221210045671230210679666565554998210 18 | 87364043060114327656589891672745541034403876807 19 | 98987659877165498433455762489829832322312145898 20 | 56709659201076789512566754690637841211569057909 21 | 93298784390196571001763453280321230400258760545 22 | 87627569787986789419019880179450189592349891221 23 | 66559878796545496328893743018766676971023760330 24 | 75443146659540376539065656972106457850301456456 25 | 06322065443281276432454247962043367164459315567 26 | 09401059451154921021093138210001298968768222348 27 | 98543392100063233210012029247690017070864122359 28 | 87654643761890101269256114339587823451951061490 29 | 96563657897346458948340104678788343267899870583 30 | 68498746983087976127198234575010254943266922679 31 | 57342345672156985036980165983123780855950123043 32 | 46211010767310569345672106074434516764800012554 33 | 35106806950223450570761057896599605993730983676 34 | 24287985349054322109652358066082780745625954109 35 | 76294560248763073506349859150143096035417567453 36 | 01156701197012104417290447243012345129026472367 37 | 62006792387654065326123567345120046687330881098 38 | 78945963456505171475014128346731357596540990569 39 | 86732170872212986584015679036832368101294230308 40 | 87888989083343490673470684123924579180087665219 41 | 96567239894758818980832191250611899054378573934 42 | 21550126743267809295678990361704567860365389873 43 | 63348745784704954367109719442863010971234987062 44 | 78292632043213452101058728553456783970549456256 45 | 60190342153210063467345687667869097901632345845 46 | 54148913764569876598498193189301656892323266936 47 | 93217654899765978701567070789982346765414867897 48 | -------------------------------------------------------------------------------- /2024/inputs/11.txt: -------------------------------------------------------------------------------- 1 | 0 9 10 675 8845 47290 451346 5096571 2 | -------------------------------------------------------------------------------- /2024/inputs/21.txt: -------------------------------------------------------------------------------- 1 | 954A 2 | 773A 3 | 716A 4 | 796A 5 | 075A 6 | -------------------------------------------------------------------------------- /2024/inputs/8.txt: -------------------------------------------------------------------------------- 1 | .......s.................E....k.............J..... 2 | ...........................I...................... 3 | .................p.........Bf.................b... 4 | .........w.......................m..........B..... 5 | .....................v........g.e.....7........... 6 | ............h...............O.............z....... 7 | ..................z...v...c..................f.... 8 | .t..................n.a........3Ck...d.....E...... 9 | ................B.......E.........C............... 10 | ..................g..............r6.y............. 11 | d..........k.....................L......M......... 12 | .................upx.............................. 13 | i..U................s............................. 14 | .....Q.......z..............N....G.W.............. 15 | .............................c.................... 16 | T.........I.........f..p..........SO..Q........... 17 | ..............p.........T......ua...F........M.5.. 18 | ....L..................................i.......... 19 | ...q.........Y...y.......D...................N.... 20 | ........................w............P............ 21 | ..Q.......V.......................m.........x..... 22 | ..............................3.......Yn....P...F. 23 | ......Y........O.G..............X.........D..W.S.. 24 | .T........v..........N............J....L....G..... 25 | .T..........W.I.P.................n............... 26 | ...........y.......6........il................m... 27 | .................z..........jI.................t.. 28 | ..........5..c..0.........E........DV........e.... 29 | ..L..d3...............h............g........0..... 30 | ...............................0....M...X......... 31 | ....................G.....U..X5................... 32 | ..........D................................v..q... 33 | ...6.m...................Q...4..............O..... 34 | ...............B............t.......c............. 35 | .................h.j......................y....... 36 | .................b.a.........M........U........... 37 | ......6......b...........................3........ 38 | ......................k........................... 39 | ......t.............rw....la.i.................... 40 | b...u...........................d....X....4....... 41 | ...........P......N.....................w......r.. 42 | .....h...Y7...eg.................................. 43 | .................V.............................x.. 44 | ...............................S.......fx....s.... 45 | ..S............l...................C.............. 46 | ..WC.............5..................s.........r.l. 47 | .7...J.......n................4......F............ 48 | .4j..........................................7.... 49 | ...................e..............q0u............. 50 | ...........j.U.V.F.............J.q................ 51 | -------------------------------------------------------------------------------- /2024/inputs/gen_all.bqn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bqn 2 | {⟨𝕩∾".txt"⟩•Import "gen.bqn"}¨ "1"‿"2"‿"3"‿"4"‿"5"‿"6"‿"7"‿"8"‿"9"‿"10"‿"11"‿"12"‿"13"‿"14"‿"15"‿"19"‿"21"‿"22"‿"25" -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | licenses/LICENSE-MIT applies to all files other than input files (`*.in`, `*.txt`). 2 | 3 | Year 2024 inputs (i.e. `2024/inputs/*.txt`) are under licenses/UNLICENSE (they have ben generated via `inputs/gen_all.bqn` on CBQN 0.8.0). 4 | 5 | Year 2023 inputs (i.e. `2023/inputs/*.txt`) are under licenses/LICENSE-MIT-mattcl; they're taken from https://github.com/mattcl/unofficial-aoc2023-inputs/tree/master (`input-001.txt` for each day). 6 | 7 | Year ≤2022 inputs were included as per https://old.reddit.com/r/adventofcode/comments/7lesj5/comment/drlt9am -------------------------------------------------------------------------------- /licenses/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 dzaima 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /licenses/LICENSE-MIT-mattcl: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Matt Chun-Lum 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /licenses/UNLICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to -------------------------------------------------------------------------------- /utils/utils.bqn: -------------------------------------------------------------------------------- 1 | # ⟨RB, B10, Num, Repr, LCM, GCD, Ints, SInts, Split, SplitF, Indices, ReIndex, MDSlash, MDSlashI, Unjoin, Count, _fx_, _fi_, _underSelect_, Select, Tr, TW, _memo, Input⟩ ← •Import "../../utils/utils.bqn" 2 | RB ⇐ 2⊸×⊸+˜´⌽ 3 | B10 ⇐ 10⊸×⊸+˜´⌽ 4 | Num ⇐ •ParseFloat 5 | Repr ⇐ {𝕩<1e10? •Repr𝕩; ∾{¯10↑𝕩∾˜10⥊'0'}⌾(1⊸⊑) •Repr¨ 1e10(⌊∘÷˜⋈|)𝕩} 6 | 7 | LCM ⇐ •math.LCM 8 | GCD ⇐ •math.GCD 9 | 10 | Ints ⇐ {•ParseFloat¨((¯1+⊢×·+`»⊸<)𝕩∊'0'+↕10)⊔𝕩} 11 | SInts ⇐ {•ParseFloat¨((¯1+⊢×·+`»⊸<)𝕩∊'-'∾'0'+↕10)⊔𝕩} 12 | 13 | Split ⇐ {((¯1+⊢×·+`»⊸<)¬𝕩=𝕨)⊔𝕩} 14 | SplitF ⇐ {((¯1+⊢×·+`»⊸<)¬𝕨⊸≡¨𝕩)⊔𝕩} 15 | 16 | Indices ⇐ <⥊¨·⊣⌜˜`⌾⌽↕¨ # <˘⍉⁼>↕𝕩 17 | ReIndexH ⇐ {𝕨|⎉1⌊𝕩÷⌜1«×`⌾⌽𝕨} 18 | ReIndex ⇐ <˘ ReIndexH # convert indices into a shape-⟨×´𝕨⟩ array into corresponding indices into a shape-𝕨 one 19 | MDSlash ⇐ {(0.13×≠⥊𝕩)>+´⥊𝕩? (≢𝕩) ReIndex /⥊𝕩; /○⥊⟜↕⟜≢ 𝕩} # multi-dimensional /𝕩 20 | MDSlashI ⇐ {<˘⍉ (≢𝕩) ReIndexH /⥊𝕩} # MDSlash but transposed output 21 | 22 | Unjoin ⇐ {((≠¨𝕨)/↕≠𝕨) ⊔ 𝕩} 23 | 24 | Count ⇐ ⊐˜∘⊣⊏ ≠∘⊣↑ /⁼∘⊐ 25 | 26 | _fx_ ⇐ {F _𝕣_ 𝔾 𝕩: ⊑{p‿n:n⋈F n}•_while_(𝔾´) 𝕩⋈𝔽𝕩} # fixed-point 27 | _fi_ ⇐ {F _𝕣_ 𝔾 𝕩: ¯1↓{𝕩∾𝕩; 35 | 1≡=⊑𝕩? <˘⍉⁼>𝕩; 36 | =⊸{<⎉𝕨⍉⍟𝕨>𝕩} 𝕩 37 | } 38 | TW ⇐ { # transposed depth-separated 𝕨↕arr 39 | 1=≠𝕨? (𝕨¬˜≠𝕩)↑¨↓⟜𝕩¨↕𝕨; 40 | <⎉(≠𝕨) ((≢𝕩)-𝕨-1) ↕ 𝕩 41 | } 42 | 43 | base ← •file.At "../" 44 | Input ⇐ {· 𝕊 ⟨path⟩: path; y‿d 𝕊 ⟨⟩: ∾⟨base, •Repr y, "/inputs/", •Repr d, ".txt"⟩} 45 | 46 | _memo ⇐ {F _𝕣: (•HashMap˜⟨⟩){𝔽_𝕣𝕩: 𝕗.Has𝕩? 𝕗.Get 𝕩; 𝔽_𝕣𝕩: 𝕩⊸𝕗.Set⊸⊢ F 𝕩}} 47 | --------------------------------------------------------------------------------