├── 2015 ├── 01.apl ├── 02.apl ├── 03.apl ├── 05.apl ├── 06.apl ├── 07.apl ├── 08.apl ├── 09.apl ├── 10.apl ├── 11.apl ├── 12.apl ├── 13.apl ├── 14.apl ├── 15.apl ├── 16.apl ├── 17.apl ├── 18.apl ├── 19.apl ├── 20.apl ├── 21.apl ├── 22.apl ├── 23.apl ├── 24.apl ├── 25.apl ├── README.md └── in │ ├── 01.txt │ ├── 02.txt │ ├── 03.txt │ ├── 05.txt │ ├── 06.txt │ ├── 06e.txt │ ├── 07.txt │ ├── 07e.txt │ ├── 08.txt │ ├── 08e.txt │ ├── 09.txt │ ├── 09e.txt │ ├── 10.txt │ ├── 10e.txt │ ├── 11.txt │ ├── 11e.txt │ ├── 12.txt │ ├── 12e.txt │ ├── 13.txt │ ├── 13e.txt │ ├── 14.txt │ ├── 14e.txt │ ├── 15.txt │ ├── 15e.txt │ ├── 16.txt │ ├── 17.txt │ ├── 17e.txt │ ├── 18.txt │ ├── 18e.txt │ ├── 19.txt │ ├── 19e.txt │ ├── 20.txt │ ├── 20e.txt │ ├── 21.txt │ ├── 21e.txt │ ├── 22.txt │ ├── 22e.txt │ ├── 23.txt │ ├── 23e.txt │ ├── 24.txt │ ├── 24e.txt │ ├── 25.txt │ └── 25e.txt ├── 2016 ├── 01.apl ├── 02.apl ├── 03.apl ├── 04.apl ├── 06.apl ├── 07.apl ├── 08.apl ├── 09.apl ├── 10.apl ├── 11.apl ├── 12.apl ├── 13.apl ├── 15.apl ├── 16.apl ├── 18.apl ├── 19.apl ├── 20.apl ├── 21.apl ├── 22.apl ├── 23.apl ├── 24.apl ├── 25.apl ├── README.md └── in │ ├── 01.txt │ ├── 01e.txt │ ├── 02.txt │ ├── 02e.txt │ ├── 03.txt │ ├── 03e.txt │ ├── 04.txt │ ├── 04e.txt │ ├── 06.txt │ ├── 06e.txt │ ├── 07.txt │ ├── 07e.txt │ ├── 08.txt │ ├── 08e.txt │ ├── 09.txt │ ├── 09e.txt │ ├── 10.txt │ ├── 10e.txt │ ├── 11.txt │ ├── 11e.txt │ ├── 12.txt │ ├── 12e.txt │ ├── 13.txt │ ├── 13e.txt │ ├── 15.txt │ ├── 15e.txt │ ├── 16.txt │ ├── 16e.txt │ ├── 18.txt │ ├── 18e.txt │ ├── 19.txt │ ├── 19e.txt │ ├── 20.txt │ ├── 20e.txt │ ├── 21.txt │ ├── 21e.txt │ ├── 22.txt │ ├── 22e.txt │ ├── 23.txt │ ├── 23e.txt │ ├── 24.txt │ ├── 24e.txt │ ├── 25.txt │ └── 25e.txt ├── 2017 ├── 01.apl ├── 02.apl ├── 03.apl ├── 04.apl ├── 05.apl ├── 06.apl ├── 07.apl ├── 08.apl ├── 09.apl ├── 10.apl ├── 11.apl ├── 12.apl ├── 13.apl ├── 14.apl ├── 15.apl ├── 16.apl ├── 17.apl ├── 18.apl ├── 19.apl ├── 20.apl ├── 21.apl ├── 22.apl ├── 23.apl ├── 24.apl ├── 25.apl ├── README.md └── in │ ├── 01.txt │ ├── 01e.txt │ ├── 02.txt │ ├── 02e.txt │ ├── 03.txt │ ├── 03e.txt │ ├── 04.txt │ ├── 04e.txt │ ├── 05.txt │ ├── 05e.txt │ ├── 06.txt │ ├── 06e.txt │ ├── 07.txt │ ├── 07e.txt │ ├── 08.txt │ ├── 08e.txt │ ├── 09.txt │ ├── 09e.txt │ ├── 10.txt │ ├── 10e.txt │ ├── 11.txt │ ├── 11e.txt │ ├── 12.txt │ ├── 12e.txt │ ├── 13.txt │ ├── 13e.txt │ ├── 14.txt │ ├── 14e.txt │ ├── 15.txt │ ├── 15e.txt │ ├── 16.txt │ ├── 16e.txt │ ├── 17.txt │ ├── 17e.txt │ ├── 18.txt │ ├── 19.txt │ ├── 19e.txt │ ├── 20.txt │ ├── 20e.txt │ ├── 21.txt │ ├── 21e.txt │ ├── 22.txt │ ├── 22e.txt │ ├── 23.txt │ ├── 24.txt │ ├── 24e.txt │ ├── 25.txt │ └── 25e.txt ├── 2018 ├── 01.apl ├── 02.apl ├── 03.apl ├── 04.apl ├── 05.apl ├── 06.apl ├── 07.apl ├── 08.apl ├── 09.apl ├── 10.apl ├── 11.apl ├── 12.apl ├── 13.apl ├── 14.apl ├── 15.apl ├── 16.apl ├── 18.apl ├── 19.apl ├── 20.apl ├── 21.apl ├── 22.apl ├── 23.apl ├── 25.apl ├── README.md └── in │ ├── 01.txt │ ├── 02.txt │ ├── 03.txt │ ├── 03e.txt │ ├── 04.txt │ ├── 04e.txt │ ├── 05.txt │ ├── 05e.txt │ ├── 06.txt │ ├── 06e.txt │ ├── 07.txt │ ├── 07e.txt │ ├── 08.txt │ ├── 08e.txt │ ├── 09.txt │ ├── 09e.txt │ ├── 10.txt │ ├── 10e.txt │ ├── 11.txt │ ├── 11e.txt │ ├── 12.txt │ ├── 12e.txt │ ├── 13.txt │ ├── 13e.txt │ ├── 14.txt │ ├── 14e.txt │ ├── 15.txt │ ├── 15e.txt │ ├── 16.txt │ ├── 17.txt │ ├── 17e.txt │ ├── 18.txt │ ├── 18e.txt │ ├── 19.txt │ ├── 19e.txt │ ├── 20.txt │ ├── 20e.txt │ ├── 21.txt │ ├── 22.txt │ ├── 22e.txt │ ├── 23.txt │ ├── 23e.txt │ ├── 24.txt │ ├── 25.txt │ └── 25e.txt ├── 2019 ├── 01.apl ├── 02.apl ├── 03.apl ├── 04.apl ├── 05.apl ├── 06.apl ├── 07.apl ├── 08.apl ├── 09.apl ├── 10.apl ├── 11.apl ├── 12.apl ├── 13.apl ├── 14.apl ├── 15.apl ├── 16.apl ├── 17.apl ├── 19.apl ├── Gemfile ├── Gemfile.lock ├── README.md ├── Rakefile ├── _01.rb ├── _02.rb ├── _03.rb ├── _04.rb ├── _05.rb ├── _06.rb ├── _07.rb ├── _08.rb ├── _09.rb ├── _10.rb ├── _11.rb ├── _12.rb ├── _13.rb ├── _14.rb ├── _15.rb ├── _16.rb ├── _17.rb ├── _18.rb ├── _19.rb ├── _20.rb ├── _21.rb ├── _22.rb ├── _23.rb ├── _24.rb ├── _25.rb ├── compiler │ ├── README.md │ ├── code_generator.rb │ ├── compiler.rb │ ├── env.rb │ ├── ir.rb │ ├── nodes.rb │ ├── samples │ │ ├── array.rb │ │ ├── fib.rb │ │ ├── math.rb │ │ └── starburst.rb │ ├── scope.rb │ └── test │ │ ├── _base.rb │ │ ├── array.rb │ │ ├── case.rb │ │ ├── func.rb │ │ ├── if.rb │ │ ├── loop.rb │ │ ├── math.rb │ │ ├── string.rb │ │ └── unsupported.rb ├── in │ ├── 01.txt │ ├── 02.txt │ ├── 03.txt │ ├── 03e.txt │ ├── 04.txt │ ├── 05.txt │ ├── 06.txt │ ├── 06e.txt │ ├── 07.txt │ ├── 08.txt │ ├── 09.txt │ ├── 10.txt │ ├── 10e.txt │ ├── 11.txt │ ├── 12.txt │ ├── 12e.txt │ ├── 13.txt │ ├── 14.txt │ ├── 14e.txt │ ├── 15.txt │ ├── 16.txt │ ├── 17.txt │ ├── 18.txt │ ├── 19.txt │ ├── 20.txt │ ├── 21.txt │ ├── 22.txt │ ├── 23.txt │ ├── 24.txt │ └── 25.txt ├── vm.apl └── vm.rb ├── 2020 ├── 01.apl ├── 01.ijs ├── 01.rb ├── 02.apl ├── 02.ijs ├── 02.rb ├── 03.apl ├── 03.ijs ├── 04.apl ├── 04.rb ├── 05.apl ├── 05.ijs ├── 06.apl ├── 06.ijs ├── 07.apl ├── 07.ijs ├── 08.apl ├── 08.ijs ├── 09.apl ├── 09.ijs ├── 10.apl ├── 10.ijs ├── 11.apl ├── 11.ijs ├── 12.apl ├── 12.ijs ├── 13.apl ├── 13.ijs ├── 14.apl ├── 14.ijs ├── 15.apl ├── 15.ijs ├── 15.rb ├── 16.apl ├── 16.rb ├── 17.apl ├── 17.ijs ├── 18.apl ├── 18.ijs ├── 19.apl ├── 19.rb ├── 20.ijs ├── 20.rb ├── 21.rb ├── 22.rb ├── 23.rb ├── 24.apl ├── 24.rb ├── 25.apl ├── 25.ijs ├── 25.rb ├── README.md ├── aoc.ijs └── in │ ├── 01.txt │ ├── 01e.txt │ ├── 02.txt │ ├── 02e.txt │ ├── 03.txt │ ├── 03e.txt │ ├── 04.txt │ ├── 04e.txt │ ├── 05.txt │ ├── 05e.txt │ ├── 06.txt │ ├── 06e.txt │ ├── 07.txt │ ├── 07e.txt │ ├── 08.txt │ ├── 08e.txt │ ├── 09.txt │ ├── 09e.txt │ ├── 10.txt │ ├── 10e.txt │ ├── 11.txt │ ├── 11e.txt │ ├── 12.txt │ ├── 12e.txt │ ├── 13.txt │ ├── 13e.txt │ ├── 14.txt │ ├── 14e.txt │ ├── 15.txt │ ├── 15e.txt │ ├── 16.txt │ ├── 16e.txt │ ├── 17.txt │ ├── 17e.txt │ ├── 18.txt │ ├── 18e.txt │ ├── 19.txt │ ├── 19e.txt │ ├── 19x.txt │ ├── 20.txt │ ├── 20e.txt │ ├── 21.txt │ ├── 21e.txt │ ├── 22.txt │ ├── 22e.txt │ ├── 23.txt │ ├── 23e.txt │ ├── 24.txt │ ├── 24e.txt │ ├── 25.txt │ └── 25e.txt ├── 2021 ├── 01.apl ├── 02.apl ├── 03.apl ├── 04.apl ├── 05.apl ├── 06.apl ├── 07.apl ├── 08.apl ├── 09.apl ├── 10.apl ├── 11.apl ├── 12.apl ├── 13.apl ├── 14.apl ├── 15.apl ├── 16.apl ├── 17.apl ├── 18.apl ├── 19.apl ├── 20.apl ├── 21.apl ├── 22.apl ├── 23.apl ├── 24.apl ├── 25.apl ├── README.md └── in │ ├── 01.txt │ ├── 01e.txt │ ├── 02.txt │ ├── 02e.txt │ ├── 03.txt │ ├── 03e.txt │ ├── 04.txt │ ├── 04e.txt │ ├── 05.txt │ ├── 05e.txt │ ├── 06.txt │ ├── 06e.txt │ ├── 07.txt │ ├── 07e.txt │ ├── 08.txt │ ├── 08e.txt │ ├── 09.txt │ ├── 09e.txt │ ├── 10.txt │ ├── 10e.txt │ ├── 11.txt │ ├── 11e.txt │ ├── 12.txt │ ├── 12e.txt │ ├── 13.txt │ ├── 13e.txt │ ├── 14.txt │ ├── 14e.txt │ ├── 15.txt │ ├── 15e.txt │ ├── 16.txt │ ├── 17.txt │ ├── 17e.txt │ ├── 18.txt │ ├── 18e.txt │ ├── 19.txt │ ├── 19e.txt │ ├── 20.txt │ ├── 20e.txt │ ├── 21.txt │ ├── 22.txt │ ├── 22e.txt │ ├── 23.txt │ ├── 24.txt │ ├── 25.txt │ └── 25e.txt ├── 2022 ├── 01.apl ├── 02.apl ├── 03.apl ├── 04.apl ├── 05.apl ├── 06.apl ├── 07.apl ├── 08.apl ├── 09.apl ├── 10.apl ├── 11.apl ├── 12.apl ├── 13.apl ├── 14.apl ├── 15.apl ├── 16.apl ├── 18.apl ├── 19.apl ├── 20.apl ├── 21.apl ├── 22.apl ├── 23.apl ├── 24.apl ├── 25.apl ├── README.md ├── aoc.k └── in │ ├── 01.txt │ ├── 01e.txt │ ├── 02.txt │ ├── 02e.txt │ ├── 03.txt │ ├── 03e.txt │ ├── 04.txt │ ├── 04e.txt │ ├── 05.txt │ ├── 05e.txt │ ├── 06.txt │ ├── 06e.txt │ ├── 07.txt │ ├── 07e.txt │ ├── 08.txt │ ├── 08e.txt │ ├── 09.txt │ ├── 09e.txt │ ├── 10.txt │ ├── 10e.txt │ ├── 11.txt │ ├── 11e.txt │ ├── 12.txt │ ├── 12e.txt │ ├── 13.txt │ ├── 13e.txt │ ├── 14.txt │ ├── 14e.txt │ ├── 15.txt │ ├── 15e.txt │ ├── 16.txt │ ├── 16e.txt │ ├── 17.txt │ ├── 17e.txt │ ├── 18.txt │ ├── 18e.txt │ ├── 19.txt │ ├── 19e.txt │ ├── 20.txt │ ├── 20e.txt │ ├── 21.txt │ ├── 21e.txt │ ├── 22.txt │ ├── 22e.txt │ ├── 23.txt │ ├── 23e.txt │ ├── 24.txt │ ├── 24e.txt │ ├── 25.txt │ └── 25e.txt ├── 2023 ├── 01.apl ├── 02.apl ├── 03.apl ├── 04.apl ├── 05.apl ├── 06.apl ├── 07.apl ├── 08.apl ├── 09.apl ├── 10.apl ├── 11.apl ├── 12.apl ├── 13.apl ├── 14.apl ├── 15.apl ├── 16.apl ├── 17.apl ├── 18.apl ├── 19.apl ├── 20.apl ├── 21.apl ├── 22.apl ├── 23.apl ├── 24.apl ├── 25.apl ├── README.md └── in │ ├── 01.txt │ ├── 01e.txt │ ├── 02.txt │ ├── 02e.txt │ ├── 03.txt │ ├── 03e.txt │ ├── 04.txt │ ├── 04e.txt │ ├── 05.txt │ ├── 05e.txt │ ├── 06.txt │ ├── 06e.txt │ ├── 07.txt │ ├── 07e.txt │ ├── 08.txt │ ├── 08e.txt │ ├── 09.txt │ ├── 09e.txt │ ├── 10.txt │ ├── 10e.txt │ ├── 11.txt │ ├── 11e.txt │ ├── 12.txt │ ├── 12e.txt │ ├── 13.txt │ ├── 13e.txt │ ├── 14.txt │ ├── 14e.txt │ ├── 15.txt │ ├── 15e.txt │ ├── 16.txt │ ├── 16e.txt │ ├── 17.txt │ ├── 17e.txt │ ├── 18.txt │ ├── 18e.txt │ ├── 19.txt │ ├── 19e.txt │ ├── 20.txt │ ├── 20e.txt │ ├── 21.txt │ ├── 21e.txt │ ├── 22.txt │ ├── 22e.txt │ ├── 23.txt │ ├── 23e.txt │ ├── 24.txt │ ├── 24e.txt │ ├── 25.txt │ └── 25e.txt ├── 2024 ├── 01.apl ├── 02.apl ├── 03.apl ├── 04.apl ├── 05.apl ├── 06.apl ├── 07.apl ├── 08.apl ├── 09.apl ├── 10.apl ├── 11.apl ├── 12.apl ├── 13.apl ├── 14.apl ├── 15.apl ├── 16.apl ├── 17.apl ├── 18.apl ├── 19.apl ├── 20.apl ├── 21.apl ├── 22.apl ├── 23.apl ├── 24.apl ├── 25.apl ├── README.md └── in │ ├── 01.txt │ ├── 01e.txt │ ├── 02.txt │ ├── 02e.txt │ ├── 03.txt │ ├── 03e.txt │ ├── 04.txt │ ├── 04e.txt │ ├── 05.txt │ ├── 05e.txt │ ├── 06.txt │ ├── 06e.txt │ ├── 07.txt │ ├── 07e.txt │ ├── 08.txt │ ├── 08e.txt │ ├── 09.txt │ ├── 09e.txt │ ├── 10.txt │ ├── 10e.txt │ ├── 11.txt │ ├── 11e.txt │ ├── 12.txt │ ├── 12e.txt │ ├── 13.txt │ ├── 13e.txt │ ├── 14.txt │ ├── 14e.txt │ ├── 15.txt │ ├── 15e.txt │ ├── 16.txt │ ├── 16e.txt │ ├── 17.txt │ ├── 18.txt │ ├── 18e.txt │ ├── 19.txt │ ├── 19e.txt │ ├── 20.txt │ ├── 20e.txt │ ├── 21.txt │ ├── 21e.txt │ ├── 22.txt │ ├── 22e.txt │ ├── 23.txt │ ├── 23e.txt │ ├── 24.txt │ ├── 24e.txt │ ├── 25.txt │ └── 25e.txt ├── README.md ├── everybody.codes └── 2024 │ ├── 01.apl │ └── in │ ├── 01_1.txt │ ├── 01_1e.txt │ ├── 01_2.txt │ ├── 01_2e.txt │ ├── 01_3.txt │ └── 01_3e.txt ├── icfp ├── 2006 │ └── vm.c └── 2024 │ └── eval.py └── tree.apl /2015/01.apl: -------------------------------------------------------------------------------- 1 | f←'()'∘.=⊃⊃⎕NGET'in/01.txt'1 2 | 3 | ⎕← -/+/f 4 | ⎕← 1+¯1⍳⍨-⌿+\f 5 | -------------------------------------------------------------------------------- /2015/02.apl: -------------------------------------------------------------------------------- 1 | l w h ← ⍪¨↓⍉↑'x'(⍎¨≠⊆⊢)¨⊃⎕NGET'in/02.txt'1 2 | 3 | ⎕← +/ (⌊/+2×+/) (l×w),(w×h),(h×l) 4 | ⎕← +/(×/l,w,h) + 2×⌊/(l+w),(w+h),(h+l) 5 | -------------------------------------------------------------------------------- /2015/03.apl: -------------------------------------------------------------------------------- 1 | l r d u ← ⍪¨↓'<>v^'∘.∊⊃⎕NGET'in/03.txt' 2 | 3 | walk ← {↓+⍀⍺×,⍨⍪(≢⍺)⍴⍵} 4 | 5 | xy ← (r-l),(d-u) 6 | 7 | ⎕← ≢∪ xy walk 1 8 | ⎕← ≢∪(xy walk 1 0),(xy walk 0 1) 9 | -------------------------------------------------------------------------------- /2015/05.apl: -------------------------------------------------------------------------------- 1 | f ← ⊃⎕NGET'in/05.txt'1 2 | 3 | a ← {3≤+/⍵∊'aeiou'} 4 | b ← (∨/2=/⊢) 5 | c ← {~∨/∊(⍷∘⍵)¨'ab' 'cd' 'pq' 'xy'} 6 | 7 | ⎕← +/(a∧b∧c)¨f 8 | 9 | d ← {2≤⌈/(⍳⍤≢ - ⍳⍨)2,/⍵} 10 | e ← {∨/{⍵[2]=⍵[0]}¨3,/⍵} 11 | 12 | ⎕← +/(d∧e)¨f 13 | -------------------------------------------------------------------------------- /2015/06.apl: -------------------------------------------------------------------------------- 1 | to ← {⍺+⍳1+⍵-⍺} 2 | 3 | lines ← (('nf '⍳6∘⌷),(⍎¨∊∘⎕D⊆⊢))¨⊃⎕NGET'in/06.txt'1 4 | shape ← 1+ ⌈⌿2 2⍴⌈⌿↑1↓¨lines 5 | 6 | run ← { 7 | map ← ⍵ 8 | _← ⍺⍺{ 9 | x ← ⊃to/⍵[1 3] ⋄ y ← ⊃to/⍵[2 4] 10 | ⊢map[x;y] ← (⊃⍵) ⍺⍺ map[x;y] 11 | }¨lines 12 | +/∊map 13 | } 14 | 15 | ⎕← {⍺=0: 1 ⋄ ⍺=1: 0 ⋄ ⍺=2: ~⍵} run shape⍴0 16 | ⎕← {⍺=0: ⍵+1 ⋄ ⍺=1: 0⌈⍵-1 ⋄ ⍺=2: ⍵+2} run shape⍴0 17 | -------------------------------------------------------------------------------- /2015/07.apl: -------------------------------------------------------------------------------- 1 | 'memo' 'cache'⎕CY'dfns' 2 | 3 | code ← ' '(≠⊆⊢)¨⊃⎕NGET'in/07.txt'1 4 | wires ← (⊃⌽)¨code 5 | 6 | _eval ← { 7 | ~(⊂⍵)∊wires: (16⍴2)⊤⍎⍵ 8 | instr ← ¯2↓(wires⍳⊂,⍵)⊃code 9 | 1=≢instr: eval 0⊃instr 10 | 2=≢instr: ~eval 1⊃instr 11 | 'AND'≡1⊃instr: (eval 0⊃instr)∧(eval 2⊃instr) 12 | 'OR'≡1⊃instr: (eval 0⊃instr)∨(eval 2⊃instr) 13 | 'LSHIFT'≡1⊃instr: 16↑(⍎2⊃instr)↓(eval 0⊃instr) 14 | 'RSHIFT'≡1⊃instr: 16↑(-16+⍎2⊃instr)↑(eval 0⊃instr) 15 | } 16 | 17 | eval ← _eval memo(cache ⍬) 18 | ⎕← a← 2⊥eval ,'a' 19 | 20 | code[wires⍳⊂,'b'] ← ⊂(⊂⍕a)@0⊢(wires⍳⊂,'b')⊃code 21 | eval ← _eval memo(cache ⍬) 22 | ⎕← 2⊥eval ,'a' 23 | -------------------------------------------------------------------------------- /2015/08.apl: -------------------------------------------------------------------------------- 1 | f ← ⊃⎕NGET'in/08.txt'1 2 | 3 | ⎕← (≢∊f)-≢('\\(.)'⎕R'\1')('\\x[a-f0-9]{2}'⎕R'X')∊{¯1↓1↓⍵}¨f 4 | ⎕← (≢∊f) -⍨ +/2+≢∘('"'⎕R'\\"')∘('\\'⎕R'\\\\')¨f 5 | -------------------------------------------------------------------------------- /2015/09.apl: -------------------------------------------------------------------------------- 1 | 'pmat'⎕CY'dfns' 2 | 3 | src _ dst _ len ← ↓⍉↑' '(≠⊆⊢)¨⊃⎕NGET'in/09.txt'1 4 | places ← ∪src,dst 5 | costs ← (⍉⌈⊢) (⍎¨len) @ (src(,¨⍥(places∘⍳))dst)⊢ (2/≢places)⍴0 6 | 7 | ⎕← (⌊/,⌈/) {+/2{costs[⍺;⍵]}/⍵}¨↓pmat ≢places 8 | -------------------------------------------------------------------------------- /2015/10.apl: -------------------------------------------------------------------------------- 1 | step ← {∊((⍕≢),⊃)¨(1,2≠/⍵)⊂⍵} 2 | c ← ⊃⊃⎕NGET'in/10.txt'1 3 | ⎕← ≢c←step⍣40⊢c 4 | ⎕← ≢ step⍣10⊢c 5 | -------------------------------------------------------------------------------- /2015/11.apl: -------------------------------------------------------------------------------- 1 | A ← ⎕C⎕A 2 | bad ← A⍳'iol' 3 | check ← { 4 | a ← ∨/1 1⍷-2-/⍵ 5 | b ← ~∨/⍵∊bad 6 | c ← 2=/⍵ 7 | ∧/a b (2≤+/c) (~∨/1 1⍷c) 8 | } 9 | 10 | sh ← 8/26 11 | next ← { check sh⊤⍵: ⍵ ⋄ ∇⍵+1 } 12 | n ← 26⊥A⍳ ⊃⊃⎕NGET'in/11.txt'1 13 | 14 | ⎕← A[sh⊤n←next 1+n] 15 | ⎕← A[sh⊤ next 1+n] 16 | -------------------------------------------------------------------------------- /2015/12.apl: -------------------------------------------------------------------------------- 1 | f ← ⊃⊃⎕NGET'in/12.txt'1 2 | 3 | nums ← ('-',⎕D)∘(⍎¨∊⍨⊆⊢) 4 | dpth ← 1+-⌿+\'{}'∘.=f 5 | mask ← ~⊃∨/{(⌽∧\⌽dpth[⍵]≤⍵↑dpth),∧\dpth[⍵]≤⍵↓dpth}¨⍸':"red"'⍷f 6 | 7 | ⎕← +/ nums f 8 | ⎕← +/ nums mask/f 9 | -------------------------------------------------------------------------------- /2015/13.apl: -------------------------------------------------------------------------------- 1 | 'pmat'⎕CY'dfns' 2 | 3 | f ← ↓⍉↑ {' '(≠⊆⊢)¯1↓('lose '⎕R'gain -')⍵}¨ ⊃⎕NGET'in/13.txt'1 4 | 5 | p1 p2 h ← f[0 10 3] 6 | ppl ← ∪p1,p2 7 | 8 | best ← { 9 | costs ← ⍵ 10 | n ← (≢costs)-1 11 | ⌈/ {+/2{costs[⍺;⍵]+costs[⍵;⍺]}/⍵}¨ ↓n,(pmat n),n 12 | } 13 | 14 | ⎕← best costs ← (⍎¨h)@(p1(,¨⍥(ppl∘⍳))p2)⊢(2/≢ppl)⍴0 15 | ⎕← best 0⍪0,costs 16 | -------------------------------------------------------------------------------- /2015/14.apl: -------------------------------------------------------------------------------- 1 | f ← ↓⍉↑' '(≠⊆⊢)¨ ⊃⎕NGET'in/14.txt'1 2 | lim ← 2503 3 | speed fly rest ← ⍎¨¨f[3 6 13] 4 | runs ← +\¨lim⍴¨(rest+fly)↑¨fly⍴¨speed 5 | 6 | ⎕← ⌈/⊢/↑runs 7 | ⎕← ⌈/+/↑runs=⌈/runs 8 | -------------------------------------------------------------------------------- /2015/15.apl: -------------------------------------------------------------------------------- 1 | 'pmat'⎕CY'dfns' 2 | 3 | cap dur flvr txtr kc ← ↓⍉↑('-',⎕D)∘(⍎¨∊⍨⊆⊢)¨⊃⎕NGET'in/15.txt'1 4 | mat ← ↑cap dur flvr txtr 5 | n ← ≢cap 6 | opts ← ↓∪(0∘=-1∘=) pmat n 7 | 8 | ⍝ Part 1: start with equal share, and hill climb 1 teaspoon at a time 9 | start ← {⍵/100÷⍵} ≢cap 10 | ⎕← start { 11 | other ← ×/0⌈↑(mat +.× ⊢)¨opts+⊂⍺ 12 | ⍵≥max←⌈/other: ⍵ 13 | max ∇⍨ ⍺+⊃opts[other⍳max] 14 | } ×/0⌈mat +.× start 15 | 16 | ⍝ Part 2: depth first search 17 | ⎕← { 18 | (n-1)=≢⍵: ∇⍵,100-+/⍵ 19 | n>≢⍵: ⌈/∇¨(⊂⍵),¨1+⍳100-+/⍵ 20 | 100≠+/⍵: 0 21 | 500≠+/kc×⍵: 0 22 | ×/0⌈mat +.× ⍵ 23 | } ⍬ 24 | -------------------------------------------------------------------------------- /2015/16.apl: -------------------------------------------------------------------------------- 1 | children←3∘= ⋄ samoyeds←2∘= ⋄ cars←2∘= akitas←0∘= ⋄ vizslas←0∘= ⋄ perfumes←1∘= 2 | cats ← 7∘= ⋄ pomeranians ← 3∘= ⋄ goldfish ← 5∘= ⋄ trees ← 3∘= 3 | 4 | f ← ↑{⊃¨{⍺,' ',⍵}/¨0 1 0 1 0 1 0⊂⍵}¨1↓¨' ,:'∘((~∊⍨)⊆⊢)¨⊃⎕NGET'in/16.txt'1 5 | ⎕← 1+⍸∧/⍎¨f 6 | 7 | cats ← 7∘< ⋄ pomeranians ← 3∘> ⋄ goldfish ← 5∘> ⋄ trees ← 3∘< 8 | ⎕← 1+⍸∧/⍎¨f 9 | -------------------------------------------------------------------------------- /2015/17.apl: -------------------------------------------------------------------------------- 1 | 'trav'⎕CY'dfns' 2 | 3 | s ← ⍎¨⊃⎕NGET'in/17.txt'1 4 | io ← ⍳≢s 5 | lim ← 150 6 | 7 | ⍝ accumulate number of containers 8 | r ← ⍬ {⍺,(lim=+/s[⍵])↑⊂≢⍵} trav {(⊂⍵),¨⍸io>⌈/⍵} ⍬ 9 | ⎕← ≢r 10 | ⎕← 1⊃{⍵[⊃⍋⍵;]}{⍺,≢⍵}⌸r 11 | -------------------------------------------------------------------------------- /2015/18.apl: -------------------------------------------------------------------------------- 1 | map ← '#'=↑⊃⎕NGET'in/18.txt'1 2 | 3 | step ← {+/∊⍵}⌺3 3∊¨3+0,¨⊢ 4 | ⎕← +/∊step⍣100⊢map 5 | 6 | step ← {2=+/|⍺:3 ⋄ +/∊⍵}⌺3 3∊¨3+0,¨⊢ 7 | map ← {⍵[1;1]∨2=+/|⍺}⌺3 3⊢map 8 | ⎕← +/∊step⍣100⊢map 9 | -------------------------------------------------------------------------------- /2015/19.apl: -------------------------------------------------------------------------------- 1 | start ← ⊃¯1↑f←⊃⎕NGET'in/19.txt'1 2 | src _ dst ← ↓⍉↑' '(≠⊆⊢)¨¯2↓f 3 | ⎕← ≢∪⊃,/src{⍬≡i←⍸⍺⍷start:i⋄(⊂⍵){∊1⌽⍵,⊂⍺}¨(≢⍺){((⍵+⍺)↓start)(⍵↑start)}¨i}¨dst 4 | 5 | ⍝ using formula from https://www.reddit.com/comments/3xflz8//cy4etju 6 | elements ← +/start∊⎕A 7 | rnar ← +/∊'Rn' 'Ar'⍷¨⊂start 8 | ys ← 2×+/'Y'=start 9 | ⎕← elements - rnar + 1 + ys 10 | -------------------------------------------------------------------------------- /2015/20.apl: -------------------------------------------------------------------------------- 1 | n ← ⍎⊃⊃⎕NGET'in/20.txt'1 2 | 3 | range ← {⍺+⍺×⍳⌊⍵÷⍺} 4 | take ← (⌊∘≢)↑⊢ 5 | 6 | sz ← ⌊n÷10 7 | c ← (sz+1)⍴1 8 | _← {c[⍵ range sz]+←⍵ ⋄ 0}¨2+⍳sz 9 | ⎕← (c≥sz)⍳1 10 | 11 | sz ← ⌊n÷11 12 | c ← (sz+1)⍴1 13 | _← {c[50 take ⍵ range sz]+←⍵ ⋄ 0}¨2+⍳sz 14 | ⎕← (c≥sz)⍳1 15 | -------------------------------------------------------------------------------- /2015/21.apl: -------------------------------------------------------------------------------- 1 | 'cmat'⎕CY'dfns' 2 | 3 | hp atk def ← ⎕D(⍎¨∊⍨⊆⊢)∊⊃⎕NGET'in/21.txt'1 4 | costs ← 8 10 25 40 74 13 31 53 75 102 25 50 100 20 40 80 5 | items ← ↑(4 5 6 7 8 0 0 0 0 0 1 2 3 0 0 0)(0 0 0 0 0 1 2 3 4 5 0 0 0 1 2 3) 6 | 7 | play ← { (⌈hp÷1⌈⍵[0]-def) ≤ (⌈100÷1⌈atk-⍵[1]) } 8 | 9 | opts ← ⊃(,∘.,)/(⍳5)((⊂⍬),5+⍳5)((⊂⍬),10+(⍳6),↓2 cmat 6) 10 | wins ← { play +/items[;⍵] }¨opts 11 | 12 | ⎕← ⌊/ {+/costs[⍵]}¨opts/⍨wins 13 | ⎕← ⌈/ {+/costs[⍵]}¨opts/⍨~wins 14 | -------------------------------------------------------------------------------- /2015/22.apl: -------------------------------------------------------------------------------- 1 | HP ATK ← ⎕D(⍎¨∊⍨⊆⊢)∊⊃⎕NGET'in/22.txt'1 2 | 3 | costs ← 53 73 113 173 229 4 | turns ← 1 1 6 6 5 5 | hps ← 0 2 0 0 0 6 | atks ← 4 2 0 3 0 7 | defs ← 0 0 7 0 0 8 | manas ← 0 0 0 0 101 9 | 10 | run ← { 11 | mana spent hp Ehp bossturn decay ← 6↑⍵ 12 | turn ← 6↓⍵ 13 | 0≥hp: ⍺ 14 | 15 | ⍝ apply spells 16 | hp +← +/hps/⍨×turn 17 | Ehp -← +/atks/⍨×turn 18 | mana+← +/manas/⍨×turn 19 | d ← +/defs/⍨×turn 20 | turn-← ×turn 21 | 0≥Ehp: spent 22 | ⍝ a) boss turn 23 | bossturn: ⍺ ∇ mana,spent,(hp-1⌈ATK-d),Ehp,0,decay,turn 24 | ⍝ b) player turn 25 | hp -← decay 26 | 0≥hp: ⍺ 27 | avl ← ⍸(⍺>spent+costs)∧(0=turn)∧mana≥costs 28 | avl≡⍬: ⍺ 29 | ⌊/⍺∇¨{(mana-costs[⍵]),(spent+costs[⍵]),hp,Ehp,1,decay,turns[⍵]@⍵⊢turn}¨avl 30 | } 31 | 32 | ⎕← 1000 run 500 0 50 HP 0 0,costs=0 33 | ⎕← 1300 run 500 0 50 HP 0 1,costs=0 34 | -------------------------------------------------------------------------------- /2015/23.apl: -------------------------------------------------------------------------------- 1 | ⍝ decompiled 2 | eval ← {1=⍵:⍺ ⋄ 2|⍵: (⍺+1)∇1+3×⍵ ⋄ (⍺+1)∇⍵÷2} 3 | 4 | ⎕← 0 eval 20895 5 | ⎕← 0 eval 60975 6 | -------------------------------------------------------------------------------- /2015/24.apl: -------------------------------------------------------------------------------- 1 | 'cmat'⎕CY'dfns' 2 | 3 | n ← ⍎¨⊃⎕NGET'in/24.txt'1 4 | solve ← { 5 | sz ← (+/n)÷⍵ 6 | ⌊/ {⌊/×/r⌿⍨sz=+/r←n[⍵ cmat ≢n]}¨ ⍳¯1+⌊(≢n)÷⍵ 7 | } 8 | 9 | ⎕← solve¨ 3 4 10 | -------------------------------------------------------------------------------- /2015/25.apl: -------------------------------------------------------------------------------- 1 | row col ← ⎕D(⍎¨∊⍨⊆⊢)⊃⊃⎕NGET'in/25.txt'1 2 | n ← {(⊢/⍵-1)+2!(+/⍵)-1} row col 3 | 4 | ⎕← {33554393|252533×⍵}⍣n⊢20151125 5 | -------------------------------------------------------------------------------- /2015/README.md: -------------------------------------------------------------------------------- 1 | # AoC 2015 2 | 3 | Did not participate, APL added in 2022. Missing day 4 as it relies on MD5. 4 | -------------------------------------------------------------------------------- /2015/in/06e.txt: -------------------------------------------------------------------------------- 1 | turn on 0,0 through 999,999 2 | toggle 0,0 through 999,0 3 | turn off 499,499 through 500,500 4 | -------------------------------------------------------------------------------- /2015/in/07e.txt: -------------------------------------------------------------------------------- 1 | 123 -> x 2 | 456 -> y 3 | x AND y -> d 4 | x OR y -> e 5 | x LSHIFT 2 -> f 6 | y RSHIFT 2 -> g 7 | NOT x -> h 8 | NOT y -> i 9 | x -> a 10 | x -> b 11 | -------------------------------------------------------------------------------- /2015/in/08e.txt: -------------------------------------------------------------------------------- 1 | "" 2 | "abc" 3 | "aaa\"aaa" 4 | "\x27" 5 | -------------------------------------------------------------------------------- /2015/in/09.txt: -------------------------------------------------------------------------------- 1 | Faerun to Norrath = 129 2 | Faerun to Tristram = 58 3 | Faerun to AlphaCentauri = 13 4 | Faerun to Arbre = 24 5 | Faerun to Snowdin = 60 6 | Faerun to Tambi = 71 7 | Faerun to Straylight = 67 8 | Norrath to Tristram = 142 9 | Norrath to AlphaCentauri = 15 10 | Norrath to Arbre = 135 11 | Norrath to Snowdin = 75 12 | Norrath to Tambi = 82 13 | Norrath to Straylight = 54 14 | Tristram to AlphaCentauri = 118 15 | Tristram to Arbre = 122 16 | Tristram to Snowdin = 103 17 | Tristram to Tambi = 49 18 | Tristram to Straylight = 97 19 | AlphaCentauri to Arbre = 116 20 | AlphaCentauri to Snowdin = 12 21 | AlphaCentauri to Tambi = 18 22 | AlphaCentauri to Straylight = 91 23 | Arbre to Snowdin = 129 24 | Arbre to Tambi = 53 25 | Arbre to Straylight = 40 26 | Snowdin to Tambi = 15 27 | Snowdin to Straylight = 99 28 | Tambi to Straylight = 70 29 | -------------------------------------------------------------------------------- /2015/in/09e.txt: -------------------------------------------------------------------------------- 1 | London to Dublin = 464 2 | London to Belfast = 518 3 | Dublin to Belfast = 141 4 | -------------------------------------------------------------------------------- /2015/in/10.txt: -------------------------------------------------------------------------------- 1 | 1113222113 2 | -------------------------------------------------------------------------------- /2015/in/10e.txt: -------------------------------------------------------------------------------- 1 | 111221 2 | -------------------------------------------------------------------------------- /2015/in/11.txt: -------------------------------------------------------------------------------- 1 | hepxcrrq 2 | -------------------------------------------------------------------------------- /2015/in/11e.txt: -------------------------------------------------------------------------------- 1 | abcdefgh 2 | -------------------------------------------------------------------------------- /2015/in/12e.txt: -------------------------------------------------------------------------------- 1 | [1,"red",{-100,x:"red",{-100,-100},-100},2,{-100,x:"red",-100},3,{4,5}] 2 | -------------------------------------------------------------------------------- /2015/in/13e.txt: -------------------------------------------------------------------------------- 1 | Alice would gain 54 happiness units by sitting next to Bob. 2 | Alice would lose 79 happiness units by sitting next to Carol. 3 | Alice would lose 2 happiness units by sitting next to David. 4 | Bob would gain 83 happiness units by sitting next to Alice. 5 | Bob would lose 7 happiness units by sitting next to Carol. 6 | Bob would lose 63 happiness units by sitting next to David. 7 | Carol would lose 62 happiness units by sitting next to Alice. 8 | Carol would gain 60 happiness units by sitting next to Bob. 9 | Carol would gain 55 happiness units by sitting next to David. 10 | David would gain 46 happiness units by sitting next to Alice. 11 | David would lose 7 happiness units by sitting next to Bob. 12 | David would gain 41 happiness units by sitting next to Carol. 13 | -------------------------------------------------------------------------------- /2015/in/14.txt: -------------------------------------------------------------------------------- 1 | Vixen can fly 8 km/s for 8 seconds, but then must rest for 53 seconds. 2 | Blitzen can fly 13 km/s for 4 seconds, but then must rest for 49 seconds. 3 | Rudolph can fly 20 km/s for 7 seconds, but then must rest for 132 seconds. 4 | Cupid can fly 12 km/s for 4 seconds, but then must rest for 43 seconds. 5 | Donner can fly 9 km/s for 5 seconds, but then must rest for 38 seconds. 6 | Dasher can fly 10 km/s for 4 seconds, but then must rest for 37 seconds. 7 | Comet can fly 3 km/s for 37 seconds, but then must rest for 76 seconds. 8 | Prancer can fly 9 km/s for 12 seconds, but then must rest for 97 seconds. 9 | Dancer can fly 37 km/s for 1 seconds, but then must rest for 36 seconds. 10 | -------------------------------------------------------------------------------- /2015/in/14e.txt: -------------------------------------------------------------------------------- 1 | Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds. 2 | Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds. 3 | -------------------------------------------------------------------------------- /2015/in/15.txt: -------------------------------------------------------------------------------- 1 | Sprinkles: capacity 5, durability -1, flavor 0, texture 0, calories 5 2 | PeanutButter: capacity -1, durability 3, flavor 0, texture 0, calories 1 3 | Frosting: capacity 0, durability -1, flavor 4, texture 0, calories 6 4 | Sugar: capacity -1, durability 0, flavor 0, texture 2, calories 8 5 | -------------------------------------------------------------------------------- /2015/in/15e.txt: -------------------------------------------------------------------------------- 1 | Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8 2 | Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3 3 | -------------------------------------------------------------------------------- /2015/in/17.txt: -------------------------------------------------------------------------------- 1 | 43 2 | 3 3 | 4 4 | 10 5 | 21 6 | 44 7 | 4 8 | 6 9 | 47 10 | 41 11 | 34 12 | 17 13 | 17 14 | 44 15 | 36 16 | 31 17 | 46 18 | 9 19 | 27 20 | 38 21 | -------------------------------------------------------------------------------- /2015/in/17e.txt: -------------------------------------------------------------------------------- 1 | 20 2 | 15 3 | 10 4 | 5 5 | 5 6 | -------------------------------------------------------------------------------- /2015/in/18e.txt: -------------------------------------------------------------------------------- 1 | .#.#.# 2 | ...##. 3 | #....# 4 | ..#... 5 | #.#..# 6 | ####.. 7 | -------------------------------------------------------------------------------- /2015/in/19e.txt: -------------------------------------------------------------------------------- 1 | e => H 2 | e => O 3 | H => HO 4 | H => OH 5 | O => HH 6 | 7 | HOHOHO 8 | -------------------------------------------------------------------------------- /2015/in/20.txt: -------------------------------------------------------------------------------- 1 | 33100000 2 | -------------------------------------------------------------------------------- /2015/in/20e.txt: -------------------------------------------------------------------------------- 1 | 1000 2 | -------------------------------------------------------------------------------- /2015/in/21.txt: -------------------------------------------------------------------------------- 1 | Hit Points: 103 2 | Damage: 9 3 | Armor: 2 4 | -------------------------------------------------------------------------------- /2015/in/21e.txt: -------------------------------------------------------------------------------- 1 | Hit Points: 12 2 | Damage: 7 3 | Armor: 2 4 | -------------------------------------------------------------------------------- /2015/in/22.txt: -------------------------------------------------------------------------------- 1 | Hit Points: 51 2 | Damage: 9 3 | -------------------------------------------------------------------------------- /2015/in/22e.txt: -------------------------------------------------------------------------------- 1 | Hit Points: 14 2 | Damage: 8 3 | -------------------------------------------------------------------------------- /2015/in/23.txt: -------------------------------------------------------------------------------- 1 | jio a, +22 2 | inc a 3 | tpl a 4 | tpl a 5 | tpl a 6 | inc a 7 | tpl a 8 | inc a 9 | tpl a 10 | inc a 11 | inc a 12 | tpl a 13 | inc a 14 | inc a 15 | tpl a 16 | inc a 17 | inc a 18 | tpl a 19 | inc a 20 | inc a 21 | tpl a 22 | jmp +19 23 | tpl a 24 | tpl a 25 | tpl a 26 | tpl a 27 | inc a 28 | inc a 29 | tpl a 30 | inc a 31 | tpl a 32 | inc a 33 | inc a 34 | tpl a 35 | inc a 36 | inc a 37 | tpl a 38 | inc a 39 | tpl a 40 | tpl a 41 | jio a, +8 42 | inc b 43 | jie a, +4 44 | tpl a 45 | inc a 46 | jmp +2 47 | hlf a 48 | jmp -7 49 | -------------------------------------------------------------------------------- /2015/in/23e.txt: -------------------------------------------------------------------------------- 1 | inc a 2 | jio a, +2 3 | tpl a 4 | inc a 5 | -------------------------------------------------------------------------------- /2015/in/24.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 3 3 | 5 4 | 11 5 | 13 6 | 17 7 | 19 8 | 23 9 | 29 10 | 31 11 | 41 12 | 43 13 | 47 14 | 53 15 | 59 16 | 61 17 | 67 18 | 71 19 | 73 20 | 79 21 | 83 22 | 89 23 | 97 24 | 101 25 | 103 26 | 107 27 | 109 28 | 113 29 | -------------------------------------------------------------------------------- /2015/in/24e.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | 3 4 | 4 5 | 5 6 | 7 7 | 8 8 | 9 9 | 10 10 | 11 11 | -------------------------------------------------------------------------------- /2015/in/25.txt: -------------------------------------------------------------------------------- 1 | To continue, please consult the code grid in the manual. Enter the code at row 2947, column 3029. 2 | -------------------------------------------------------------------------------- /2015/in/25e.txt: -------------------------------------------------------------------------------- 1 | To continue, please consult the code grid in the manual. Enter the code at row 4, column 2. 2 | -------------------------------------------------------------------------------- /2016/01.apl: -------------------------------------------------------------------------------- 1 | dist ← {+/ | 9 11○⍵} 2 | lines ← {(~⍵∊', ')⊆⍵}⊃⊃⎕NGET'in/01.txt'1 3 | 4 | d ← 0j1×1-2×'R'=⊃¨lines 5 | s ← (⍎1∘↓)¨lines 6 | 7 | ⎕← dist +/s××\d 8 | ⎕← dist {⍵[⊃⍸0=≠⍵]}⊃{⍵,⍺+⊃⌽⍵}/⌽0,s{⍵×1+⍳⍺}¨×\d 9 | -------------------------------------------------------------------------------- /2016/02.apl: -------------------------------------------------------------------------------- 1 | in ← ⊃⎕NGET'in/02.txt'1 2 | 3 | MAP1 ← 9 4 ⍴ 0 1 3 0 1 2 4 0 2 2 5 1 0 4 6 3 1 5 7 3 2 5 8 4 3 7 6 6 4 8 7 6 5 8 8 7 4 | MAP2 ← 13 4 ⍴ 0 0 2 0 1 2 5 1 0 3 6 1 3 3 7 2 4 5 4 4 1 6 9 4 2 7 10 5 3 8 11 6 8 8 8 7 5 10 9 9 6 11 12 9 7 11 11 10 10 12 12 12 5 | 6 | run ← { 7 | MAP ← ⍺ 8 | keys ← ⊃{⍵,{MAP[⍵;⍺]}/⌽(⊃⌽,⍵),'URDL'⍳⍺}/⌽4,⍵ 9 | (⎕D,⎕A)[1+1↓keys] 10 | } 11 | 12 | ⎕← MAP1 run in 13 | ⎕← MAP2 run in 14 | -------------------------------------------------------------------------------- /2016/03.apl: -------------------------------------------------------------------------------- 1 | in ← ↑⍎¨⊃⎕NGET'in/03.txt'1 2 | check ← {+/m<(+/⍵)-m←⌈/⍵} 3 | ⎕← check in 4 | ⎕← check (⍴in)⍴,⍉in 5 | -------------------------------------------------------------------------------- /2016/04.apl: -------------------------------------------------------------------------------- 1 | alf ← ⎕UCS 32+⎕UCS⎕A 2 | inp ← ⊃⎕NGET'in/04.txt'1 3 | sectors ← {⍎⍵/⍨⍵∊⎕D}¨ inp 4 | checksum ← {¯1↓(1+⍵⍳'[')↓⍵}¨ inp 5 | names ← {'-'~⍨⍵↑⍨⌊/⍵⍳⎕D}¨ inp 6 | sig ← {5↑{⍵[⍋⌽⍵;0]}{⍺,100-≢⍵}⌸⍵[⍋alf⍳⍵]} 7 | 8 | ⎕← +/sectors/⍨checksum≡¨sig¨names 9 | 10 | dec ← {alf[26|⍺+alf⍳⍵]} 11 | ⎕← sectors/⍨ sectors ('northpoleobjectstorage'≡dec)¨ names 12 | -------------------------------------------------------------------------------- /2016/06.apl: -------------------------------------------------------------------------------- 1 | lines ← {⌽⍺,≢⍵}⌸¨↓⍉↑⊃⎕NGET'in/06.txt'1 2 | ⎕← {⍵[⊃⍒⍵;1]}¨lines 3 | ⎕← {⍵[⊃⍋⍵;1]}¨lines 4 | -------------------------------------------------------------------------------- /2016/07.apl: -------------------------------------------------------------------------------- 1 | lines ← {⍵⊆⍨~⍵∊'[]'}¨⊃⎕NGET'in/07.txt'1 2 | abba ← {(⍵≡⌽⍵)∧≠/2↑⍵} 3 | tls ← {∨/abba¨4,/⍵}¨¨lines 4 | ⎕← +/ {(∨/⍵/⍨~m)∧~∨/⍵/⍨m←(≢⍵)⍴⍳2}¨tls 5 | 6 | ssl ←{{⍵/⍨abba¨⍵}3,/⍵}¨¨lines 7 | ⎕← +/ {∨/∊(⊃,/⍵/⍨~m)∘.≡{⍵[1 0 1]}¨⊃,/⍵/⍨m←(≢⍵)⍴⍳2}¨ssl 8 | -------------------------------------------------------------------------------- /2016/08.apl: -------------------------------------------------------------------------------- 1 | lines ← {⍵⊆⍨~⍵∊' x='}¨ ⊃⎕NGET'in/08.txt'1 2 | 3 | run ← {⊃{ 4 | inst ← ⍺ 5 | card ← ⍵ 6 | 'rect'≡⊃inst: card⊣card[,⍳⌽⍎¨1↓inst] ← 1 7 | axis by ← ⍎¨inst[¯3 ¯1 + ≢inst] 8 | 'column'≡1⊃inst: card⊣card[;axis] ← (-by)⌽card[;axis] 9 | card⊣card[axis;] ← (-by)⌽card[axis;] 10 | }/⌽(⊂⍺⍴0),⍵} 11 | 12 | ⎕← +/∊6 50 run lines 13 | ⎕← ' #'[6 50 run lines] 14 | -------------------------------------------------------------------------------- /2016/09.apl: -------------------------------------------------------------------------------- 1 | line ← ⊃⊃⎕NGET'in/09.txt'1 2 | parse ← { (⍎¨'x'(≠⊆⊢)1↓end↑⍵),end←⍵⍳')' } 3 | 4 | ⎕← { 5 | ⍺ ← 0 6 | 0=≢⍵: ⍺ 7 | '('≠⊃⍵: (⍺+d) ∇ (d←⍵⍳'(')↓⍵ 8 | len rep end ← parse ⍵ 9 | (⍺+rep×len) ∇ (1+end+len)↓⍵ 10 | } line 11 | 12 | ⎕← { 13 | ⍺ ← 0 14 | 0=≢⍵: ⍺ 15 | '('≠⊃⍵: (⍺+d) ∇ (d←⍵⍳'(')↓⍵ 16 | len rep end ← parse ⍵ 17 | sub ← 0 ∇ len↑1↓end↓⍵ 18 | (⍺+rep×sub) ∇ (1+end+len)↓⍵ 19 | } line 20 | -------------------------------------------------------------------------------- /2016/10.apl: -------------------------------------------------------------------------------- 1 | lines ← ' '(≠⊆⊢)¨⊃⎕NGET'in/10.txt'1 2 | 3 | output ← bots ← instrs ← 300↑⊂⍬ 4 | 5 | { val bot ← ⍎¨⍵[1 5] ⋄ bots[bot] ,← val }¨ lines/⍨{'value'≡⊃⍵}¨lines 6 | { instrs[⍎1⊃⍵] ← ⊂(5⊃⍵)(⍎6⊃⍵)(10⊃⍵)(⍎11⊃⍵) }¨ lines/⍨{'value'≢⊃⍵}¨lines 7 | 8 | _←{ 9 | bot ← (≢¨bots)⍳2 10 | bot=≢bots: ⍵ 11 | vals ← bot⊃bots 12 | bots[bot] ← ⊂⍬ 13 | tolow low tohi hi ← bot⊃instrs 14 | _ ← (1 0 1 0⊂bot⊃instrs) { 15 | 'bot'≡⊃⍺: ,bots[1⊃⍺] ,← ⍵ 16 | ,output[1⊃⍺] ← ⍵ 17 | }¨ (⌊/,⌈/) vals 18 | 17 61≡(⌊/,⌈/) vals: {⎕←bot ⋄ ⍵}bots 19 | bots 20 | }⍣≡ bots 21 | ⎕← ×/3↑output 22 | -------------------------------------------------------------------------------- /2016/11.apl: -------------------------------------------------------------------------------- 1 | run ← { 2 | ⍺ ← 0 3 | 0∧.=¯1↓⍵: ⍺ 4 | floor ← ⊃⍸0<⍵ 5 | (⍺ + ¯3+2×⍵[floor]) ∇ (0,+/)@(floor+⍳2)⊢⍵ 6 | } 7 | 8 | ⎕← run 8 2 0 0 9 | ⎕← run (4+8)2 0 0 10 | -------------------------------------------------------------------------------- /2016/12.apl: -------------------------------------------------------------------------------- 1 | ⍝ based on disassembled code 2 | fib ← {⊃(+\⌽)⍣⍵⊢1 2} 3 | 4 | X ← ×/ {⍎(⍵∊⎕D)/⍵}¨ (⊂16 17)⌷⊃⎕NGET'in/12.txt'1 5 | ⎕← {X + fib ⍵}¨ 26 + 0 7 6 | 7 | )off 8 | code ← ' '(≠⊆⊢)¨⊃⎕NGET'in/12.txt'1 9 | reg ← 'abcd' 10 | run ← { 11 | ⍺←0 12 | ip regs ← ⍺⍵ 13 | ip≥≢code: regs[0] 14 | op ← ⊃inst ← ip⊃code 15 | 'dec'≡op: (ip+1) ∇ ¯1∘+@(reg⍳⊃⊃inst[1])⊢regs 16 | 'inc'≡op: (ip+1) ∇ 1∘+@(reg⍳⊃⊃inst[1])⊢regs 17 | src ← {reg∊⍨⊃⍵: regs[reg⍳⊃⍵] ⋄ ⍎⍵} 1⊃inst 18 | 'cpy'≡op: (ip+1) ∇ src@(reg⍳2⊃inst)⊢regs 19 | 'jnz'≢op: 'err' 20 | 0=src: (ip+1) ∇ regs 21 | (ip+⍎2⊃inst) ∇ regs 22 | } 23 | ⎕← run 0 0 0 0 24 | ⎕← run 0 0 1 0 ⍝ takes over a minute 25 | -------------------------------------------------------------------------------- /2016/13.apl: -------------------------------------------------------------------------------- 1 | n ← ⍎⊃⊃⎕NGET'in/13.txt'1 2 | 3 | f ← {x←9○⍵ ⋄ y←11○⍵ ⋄ 0=2|+⌿2⊥⍣¯1⊢n + (x×x) + (3×x) + (2×x×y) + y + (y×y)} 4 | dir ← ¯1j0 1j0 0j1 0j¯1 5 | 6 | run ← { ⍝ [visited] (steps ∇∇ (steps ∇ next)) [next] 7 | ⍺←⍬ 8 | visited ← ⍺∪⍵ 9 | next ← (f next)/next ← {⍵/⍨∧/0≤↑9 11∘○¨⍵} ∪∊⍵+⊂dir 10 | ⍺⍺ ⍵⍵ next: ⍺⍺ visited 11 | visited ((⍺⍺+1)∇∇⍵⍵) next~visited 12 | } 13 | 14 | ⎕← 0⊃(1 run {31j39∊⍵}) ,1j1 15 | ⎕← ≢1⊃(1 run {50<⍺}) ,1j1 16 | -------------------------------------------------------------------------------- /2016/15.apl: -------------------------------------------------------------------------------- 1 | size pos ← ↓⍉↑{(⍎¨(⍵∊⎕D)⊆⍵)[1 3]}¨⊃⎕NGET'in/15.txt'1 2 | pos ← size|1+pos+⍳≢pos 3 | 4 | ⎕← { ⍵+~0∧.=size|⍵+pos }⍣≡0 5 | 6 | size ,← 11 ⋄ pos ,← 11|1+≢pos 7 | ⎕← { ⍵+~0∧.=size|⍵+pos }⍣≡0 8 | -------------------------------------------------------------------------------- /2016/16.apl: -------------------------------------------------------------------------------- 1 | d ← '1'=⊃⊃⎕NGET'in/16.txt'1 2 | grow ← { ⍺≤≢⍵: ⍺↑⍵ ⋄ ⍺ ∇ ⍵,0,~⌽⍵ } 3 | checksum ← {=/(.5×≢⍵)2⍴⍵}⍣{2|≢⍺} 4 | 5 | ⎕← '01'[checksum 272 grow d] 6 | ⎕← '01'[checksum 35651584 grow d] 7 | 8 | -------------------------------------------------------------------------------- /2016/18.apl: -------------------------------------------------------------------------------- 1 | row ← '^'=⊃⊃⎕NGET'in/18.txt'1 2 | 3 | step ← (1 3 4 6∊⍨2⊥⊢)⌺3 4 | run ← { 5 | 0=⊃⍵: ⍵[1] 6 | (step ⍺) ∇ (2↑⍵)+(¯1,+/~⍺) 7 | } 8 | 9 | ⎕← row run 40 10 | ⎕← row run 4e5 11 | -------------------------------------------------------------------------------- /2016/19.apl: -------------------------------------------------------------------------------- 1 | num ← ⍎⊃⊃⎕NGET'in/19.txt'1 2 | ⎕← {1+2×⍵-2*⌊2⍟⍵} num 3 | ⎕← {⍵-k+0⌈⍵-2×k←3*⌊3⍟⍵} num 4 | -------------------------------------------------------------------------------- /2016/20.apl: -------------------------------------------------------------------------------- 1 | data ← (⍎¨∊∘⎕D⊆⊢)¨⊃⎕NGET'in/20.txt'1 2 | 3 | merge ← { 4 | a b ← ⍺ 5 | c d ← ⍵ 6 | c≤b+1: a,b⌈d 7 | ⍵ 8 | } 9 | ranges ← merge\data[⍋⊣/¨data] 10 | ⎕← 1+⊢/∊ranges[⊢/⍸0=⊃¨ranges] 11 | ⎕← (4294967295+1) - +/1--/↑ranges[⍸⌽≠⌽⊣/¨ranges] 12 | -------------------------------------------------------------------------------- /2016/21.apl: -------------------------------------------------------------------------------- 1 | code ← ⊃⎕NGET'in/21.txt'1 2 | to ← {⍺+⍳1+⍵-⍺} 3 | swap ← {⌽@⍵⊢⍺} 4 | 5 | ⎕← ⊃{ 6 | op ← 11↑⍺ ⋄ args ← 2↓(⍺≠' ')⊆⍺ 7 | 'swap positi'≡op: ⍵ swap ⍎¨args[0 3] 8 | 'swap letter'≡op: ⍵ swap ⍵⍳∊args[0 3] 9 | 'reverse pos'≡op: ⍵ swap ⊃to/⍎¨args[0 2] 10 | 'rotate left'≡op: ⍵⌽⍨⍎⊃args 11 | 'rotate righ'≡op: ⍵⌽⍨-⍎⊃args 12 | 'move positi'≡op: ⍵[(⊃move)]((1⊃move){⍺⍺(↑⍪⍺⍪↓)⍵}) ((⊃move)↑⍵),(-(≢⍵)-1+⊃move←⍎¨args[0 3])↑⍵ 13 | 'rotate base'≡op: (-(i≥4)+1+i←⍵⍳4⊃args)⌽⍵ 14 | }/(⌽code),⊂'abcdefgh' 15 | 16 | ⎕← ⊃{ 17 | op ← 11↑⍺ ⋄ args ← 2↓(⍺≠' ')⊆⍺ 18 | 'swap positi'≡op: ⍵ swap ⍎¨args[0 3] 19 | 'swap letter'≡op: ⍵ swap ⍵⍳∊args[0 3] 20 | 'reverse pos'≡op: ⍵ swap ⊃to/⍎¨args[0 2] 21 | 'rotate left'≡op: ⍵⌽⍨-⍎⊃args 22 | 'rotate righ'≡op: ⍵⌽⍨⍎⊃args 23 | 'move positi'≡op: ⍵[(1⊃move)]((⊃move){⍺⍺(↑⍪⍺⍪↓)⍵}) ((1⊃move)↑⍵),(-(≢⍵)-1+1⊃move←⍎¨args[0 3])↑⍵ 24 | 'rotate base'≡op: ((5×0=2|i)+⌈.5×1+(≢⍵)|¯1+i←⍵⍳4⊃args)⌽⍵ 25 | }/code,⊂'fbgdceah' 26 | -------------------------------------------------------------------------------- /2016/22.apl: -------------------------------------------------------------------------------- 1 | disks ← ⎕D∘(⍎¨∊⍨⊆⊢)¨ 2↓⊃⎕NGET'in/22.txt'1 2 | 3 | ⎕← +/∊∘.(≢ ∧ (×3⊃⊣) ∧ (3⊃⊣)≤4⊃⊢)⍨ disks 4 | 5 | ⍝ part 2 is solved manually by visualizing the map 6 | ⎕← 45 + 30×5 ⍝ empty->goal + goal->origin × #moves-per-step 7 | 8 | sh ← 1+⊃⌈/2↑¨disks 9 | empty ← 1×0=3⊃¨disks 10 | full ← 2×100<3⊃¨disks 11 | origin ← 3×(≢disks)↑1 12 | goal ← 4×(≢disks)↑(-1+×/sh-1 0)↑1 13 | ⎕← '._#○G'[⍉sh⍴empty+full+origin+goal] 14 | -------------------------------------------------------------------------------- /2016/23.apl: -------------------------------------------------------------------------------- 1 | ⍝ based on disassembled code 2 | X ← ×/ {⍎(⍵∊⎕D)/⍵}¨ (⊂19 20)⌷⊃⎕NGET'in/23.txt'1 3 | ⎕← {X + ×/⍵-⍳⍵-1}¨ 7 12 4 | -------------------------------------------------------------------------------- /2016/24.apl: -------------------------------------------------------------------------------- 1 | 'span' 'stpath' 'pmat'⎕CY'dfns' 2 | 3 | map ← ↑{('#.',⎕D)⍳⍵}¨⊃⎕NGET'in/24.txt'1 4 | 5 | ⍝ graph from map 6 | flat ← ,map 7 | graph ← ,{ 8 | 0=⊃⊃⍵[1;1]: ⊂⍬ 9 | nei←(,3 3⍴⍳2)/,⍵ 10 | ⊂1⊃¨(×⊃¨nei)/nei 11 | }⌺3 3⊢map,¨(⍴map)⍴⍳≢flat 12 | 13 | ⍝ indices of locations, ensuring 0 is the first one 14 | marks ← (flat⍳2),⍸flat>2 15 | 16 | ⍝ shortest path between each location 17 | costs ← ↑{≢¨1↓¨(graph span ⍵)∘stpath¨marks}¨marks 18 | 19 | ⍝ distance of the shortest path 20 | shortest ← {⌊/+/costs[2,/⍵]} 21 | 22 | ⎕← shortest 0,(1+pmat (≢marks)-1) 23 | ⎕← shortest 0,(1+pmat (≢marks)-1),0 24 | -------------------------------------------------------------------------------- /2016/25.apl: -------------------------------------------------------------------------------- 1 | ⍝ based on decompilation 2 | val ← ×/ {⍎(⍵∊⎕D)/⍵}¨(⊂1 2)⌷ ⊃⎕NGET'in/25.txt'1 3 | want ← 2⊥ (2×⌈4⍟val)⍴1 0 4 | 5 | ⎕← want - val 6 | -------------------------------------------------------------------------------- /2016/README.md: -------------------------------------------------------------------------------- 1 | # AoC 2016 2 | 3 | Did not participate, APL added in 2022. 4 | -------------------------------------------------------------------------------- /2016/in/01.txt: -------------------------------------------------------------------------------- 1 | R1, L3, R5, R5, R5, L4, R5, R1, R2, L1, L1, R5, R1, L3, L5, L2, R4, L1, R4, R5, L3, R5, L1, R3, L5, R1, L2, R1, L5, L1, R1, R4, R1, L1, L3, R3, R5, L3, R4, L4, R5, L5, L1, L2, R4, R3, R3, L185, R3, R4, L5, L4, R48, R1, R2, L1, R1, L4, L4, R77, R5, L2, R192, R2, R5, L4, L5, L3, R2, L4, R1, L5, R5, R4, R1, R2, L3, R4, R4, L2, L4, L3, R5, R4, L2, L1, L3, R1, R5, R5, R2, L5, L2, L3, L4, R2, R1, L4, L1, R1, R5, R3, R3, R4, L1, L4, R1, L2, R3, L3, L2, L1, L2, L2, L1, L2, R3, R1, L4, R1, L1, L4, R1, L2, L5, R3, L5, L2, L2, L3, R1, L4, R1, R1, R2, L1, L4, L4, R2, R2, R2, R2, R5, R1, L1, L4, L5, R2, R4, L3, L5, R2, R3, L4, L1, R2, R3, R5, L2, L3, R3, R1, R3 2 | -------------------------------------------------------------------------------- /2016/in/01e.txt: -------------------------------------------------------------------------------- 1 | R8, R4, R4, R8 -------------------------------------------------------------------------------- /2016/in/02e.txt: -------------------------------------------------------------------------------- 1 | ULL 2 | RRDDD 3 | LURDL 4 | UUUUD -------------------------------------------------------------------------------- /2016/in/03e.txt: -------------------------------------------------------------------------------- 1 | 101 301 501 2 | 102 302 502 3 | 103 303 503 4 | 201 401 601 5 | 202 402 602 6 | 203 403 603 -------------------------------------------------------------------------------- /2016/in/04e.txt: -------------------------------------------------------------------------------- 1 | aaaaa-bbb-z-y-x-123[abxyz] 2 | a-b-c-d-e-f-g-h-987[abcde] 3 | not-a-real-room-404[oarel] 4 | totally-real-room-200[decoy] 5 | -------------------------------------------------------------------------------- /2016/in/06e.txt: -------------------------------------------------------------------------------- 1 | eedadn 2 | drvtee 3 | eandsr 4 | raavrd 5 | atevrs 6 | tsrnev 7 | sdttsa 8 | rasrtv 9 | nssdts 10 | ntnada 11 | svetve 12 | tesnvt 13 | vntsnd 14 | vrdear 15 | dvrsen 16 | enarar 17 | -------------------------------------------------------------------------------- /2016/in/07e.txt: -------------------------------------------------------------------------------- 1 | bba[mnop]qrstabba[mnop]qrst 2 | abcd[bddb]xyyx 3 | aaaa[qwer]tyui 4 | ioxxoj[asdfgh]zxcvbn 5 | aba[bab]xyz 6 | xyx[xyx]xyx 7 | aaa[kek]eke 8 | zazbz[bzbaca]cdb[xasdf]aca 9 | -------------------------------------------------------------------------------- /2016/in/08e.txt: -------------------------------------------------------------------------------- 1 | rect 3x2 2 | rotate column x=1 by 1 3 | rotate row y=0 by 4 4 | rotate column x=1 by 1 -------------------------------------------------------------------------------- /2016/in/09e.txt: -------------------------------------------------------------------------------- 1 | XFGHJKL(8x2)(3x3)ABCY -------------------------------------------------------------------------------- /2016/in/10e.txt: -------------------------------------------------------------------------------- 1 | value 5 goes to bot 2 2 | bot 2 gives low to bot 1 and high to bot 0 3 | value 3 goes to bot 1 4 | bot 1 gives low to output 1 and high to bot 0 5 | bot 0 gives low to output 2 and high to output 0 6 | value 2 goes to bot 2 7 | -------------------------------------------------------------------------------- /2016/in/11.txt: -------------------------------------------------------------------------------- 1 | The first floor contains a polonium generator, a thulium generator, a thulium-compatible microchip, a promethium generator, a ruthenium generator, a ruthenium-compatible microchip, a cobalt generator, and a cobalt-compatible microchip. 2 | The second floor contains a polonium-compatible microchip and a promethium-compatible microchip. 3 | The third floor contains nothing relevant. 4 | The fourth floor contains nothing relevant. 5 | -------------------------------------------------------------------------------- /2016/in/11e.txt: -------------------------------------------------------------------------------- 1 | The first floor contains a hydrogen-compatible microchip and a lithium-compatible microchip. 2 | The second floor contains a hydrogen generator. 3 | The third floor contains a lithium generator. 4 | The fourth floor contains nothing relevant. -------------------------------------------------------------------------------- /2016/in/12.txt: -------------------------------------------------------------------------------- 1 | cpy 1 a 2 | cpy 1 b 3 | cpy 26 d 4 | jnz c 2 5 | jnz 1 5 6 | cpy 7 c 7 | inc d 8 | dec c 9 | jnz c -2 10 | cpy a c 11 | inc a 12 | dec b 13 | jnz b -2 14 | cpy c b 15 | dec d 16 | jnz d -6 17 | cpy 16 c 18 | cpy 17 d 19 | inc a 20 | dec d 21 | jnz d -2 22 | dec c 23 | jnz c -5 24 | -------------------------------------------------------------------------------- /2016/in/12e.txt: -------------------------------------------------------------------------------- 1 | cpy 41 a 2 | inc a 3 | inc a 4 | dec a 5 | jnz a 2 6 | dec a 7 | -------------------------------------------------------------------------------- /2016/in/13.txt: -------------------------------------------------------------------------------- 1 | 1350 2 | -------------------------------------------------------------------------------- /2016/in/13e.txt: -------------------------------------------------------------------------------- 1 | 10 2 | -------------------------------------------------------------------------------- /2016/in/15.txt: -------------------------------------------------------------------------------- 1 | Disc #1 has 7 positions; at time=0, it is at position 0. 2 | Disc #2 has 13 positions; at time=0, it is at position 0. 3 | Disc #3 has 3 positions; at time=0, it is at position 2. 4 | Disc #4 has 5 positions; at time=0, it is at position 2. 5 | Disc #5 has 17 positions; at time=0, it is at position 0. 6 | Disc #6 has 19 positions; at time=0, it is at position 7. 7 | -------------------------------------------------------------------------------- /2016/in/15e.txt: -------------------------------------------------------------------------------- 1 | Disc #1 has 5 positions; at time=0, it is at position 4. 2 | Disc #2 has 2 positions; at time=0, it is at position 1. 3 | -------------------------------------------------------------------------------- /2016/in/16.txt: -------------------------------------------------------------------------------- 1 | 01111010110010011 2 | -------------------------------------------------------------------------------- /2016/in/16e.txt: -------------------------------------------------------------------------------- 1 | 10000 2 | -------------------------------------------------------------------------------- /2016/in/18.txt: -------------------------------------------------------------------------------- 1 | ^^.^..^.....^..^..^^...^^.^....^^^.^.^^....^.^^^...^^^^.^^^^.^..^^^^.^^.^.^.^.^.^^...^^..^^^..^.^^^^ 2 | -------------------------------------------------------------------------------- /2016/in/18e.txt: -------------------------------------------------------------------------------- 1 | .^^.^.^^^^ 2 | -------------------------------------------------------------------------------- /2016/in/19.txt: -------------------------------------------------------------------------------- 1 | 3012210 2 | -------------------------------------------------------------------------------- /2016/in/19e.txt: -------------------------------------------------------------------------------- 1 | 5 2 | -------------------------------------------------------------------------------- /2016/in/20e.txt: -------------------------------------------------------------------------------- 1 | 5-8 2 | 0-2 3 | 4-7 4 | -------------------------------------------------------------------------------- /2016/in/21e.txt: -------------------------------------------------------------------------------- 1 | swap position 4 with position 0 2 | swap letter d with letter b 3 | reverse positions 0 through 4 4 | rotate left 1 step 5 | move position 1 to position 4 6 | move position 3 to position 0 7 | rotate based on position of letter b 8 | rotate based on position of letter d 9 | -------------------------------------------------------------------------------- /2016/in/22e.txt: -------------------------------------------------------------------------------- 1 | root@ebhq-gridcenter# df -h 2 | Filesystem Size Used Avail Use% 3 | /dev/grid/node-x0-y0 10T 8T 2T 80% 4 | /dev/grid/node-x0-y1 11T 6T 5T 54% 5 | /dev/grid/node-x0-y2 32T 28T 4T 87% 6 | /dev/grid/node-x1-y0 9T 7T 2T 77% 7 | /dev/grid/node-x1-y1 8T 0T 8T 0% 8 | /dev/grid/node-x1-y2 11T 7T 4T 63% 9 | /dev/grid/node-x2-y0 10T 6T 4T 60% 10 | /dev/grid/node-x2-y1 9T 8T 1T 88% 11 | /dev/grid/node-x2-y2 9T 6T 3T 66% 12 | -------------------------------------------------------------------------------- /2016/in/23.txt: -------------------------------------------------------------------------------- 1 | cpy a b 2 | dec b 3 | cpy a d 4 | cpy 0 a 5 | cpy b c 6 | inc a 7 | dec c 8 | jnz c -2 9 | dec d 10 | jnz d -5 11 | dec b 12 | cpy b c 13 | cpy c d 14 | dec d 15 | inc c 16 | jnz d -2 17 | tgl c 18 | cpy -16 c 19 | jnz 1 c 20 | cpy 71 c 21 | jnz 75 d 22 | inc a 23 | inc d 24 | jnz d -2 25 | inc c 26 | jnz c -5 27 | -------------------------------------------------------------------------------- /2016/in/23e.txt: -------------------------------------------------------------------------------- 1 | cpy 2 a 2 | tgl a 3 | tgl a 4 | tgl a 5 | cpy 1 a 6 | dec a 7 | dec a 8 | -------------------------------------------------------------------------------- /2016/in/24e.txt: -------------------------------------------------------------------------------- 1 | ########### 2 | #1.0.....2# 3 | #.#######.# 4 | #4.......3# 5 | ########### 6 | -------------------------------------------------------------------------------- /2016/in/25.txt: -------------------------------------------------------------------------------- 1 | cpy a d 2 | cpy 4 c 3 | cpy 633 b 4 | inc d 5 | dec b 6 | jnz b -2 7 | dec c 8 | jnz c -5 9 | cpy d a 10 | jnz 0 0 11 | cpy a b 12 | cpy 0 a 13 | cpy 2 c 14 | jnz b 2 15 | jnz 1 6 16 | dec b 17 | dec c 18 | jnz c -4 19 | inc a 20 | jnz 1 -7 21 | cpy 2 b 22 | jnz c 2 23 | jnz 1 4 24 | dec b 25 | dec c 26 | jnz 1 -4 27 | jnz 0 0 28 | out b 29 | jnz a -19 30 | jnz 1 -21 31 | -------------------------------------------------------------------------------- /2016/in/25e.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pitr/aoc/49d274d1b103a6a7095147d7290ffc0e128b71ee/2016/in/25e.txt -------------------------------------------------------------------------------- /2017/01.apl: -------------------------------------------------------------------------------- 1 | f ← ⍎¨⊃⊃⎕NGET'in/01.txt'1 2 | ⎕← +/f/⍨f=1⌽f 3 | ⎕← +/f/⍨f=(.5×≢f)⌽f 4 | -------------------------------------------------------------------------------- /2017/02.apl: -------------------------------------------------------------------------------- 1 | f ← ⍎¨⊃⎕NGET'in/02.txt'1 2 | ⎕← +/(⌈/-⌊/)¨f 3 | ⎕← +/ {÷⍨/⍵[∊⍸∘.(≠∧0=|)⍨⍵]}¨f 4 | -------------------------------------------------------------------------------- /2017/03.apl: -------------------------------------------------------------------------------- 1 | n ← ⍎⊃⊃⎕NGET'in/03.txt'1 2 | 3 | ⍝ Part 1: formula from http://oeis.org/A214526 4 | layer ← ⌈(.5×n*.5)-.5 5 | ⎕← (2×layer)|n-1 6 | 7 | ⍝ Part 2: lookup in https://oeis.org/A141481/b141481.txt 8 | ⎕← 295229 9 | -------------------------------------------------------------------------------- /2017/04.apl: -------------------------------------------------------------------------------- 1 | pw ← ' '(≠⊆⊢)¨⊃⎕NGET'in/04.txt'1 2 | 3 | ⎕← +/(∪≡⊢)¨ pw 4 | ⎕← +/(∪≡⊢)¨ {⍵[⍋⍵]}¨¨ pw 5 | -------------------------------------------------------------------------------- /2017/05.apl: -------------------------------------------------------------------------------- 1 | n ← ⍎¨⊃⎕NGET'in/05.txt'1 2 | 3 | m←n ⋄ ⎕← { (⍺<0)∨⍺≥≢m:⍵ ⋄ o←m[⍺] ⋄ m[⍺]+←1 ⋄ (⍺+o) ∇ ⍵+1 }⍨ 0 4 | m←n ⋄ ⎕← { (⍺<0)∨⍺≥≢m:⍵ ⋄ o←m[⍺] ⋄ m[⍺]+←1-2×3≤m[⍺] ⋄ (⍺+o) ∇ ⍵+1 }⍨ 0 5 | -------------------------------------------------------------------------------- /2017/06.apl: -------------------------------------------------------------------------------- 1 | n ← ⍎⊃⊃⎕NGET'in/06.txt'1 2 | sz ← ≢n 3 | seen ← ⍬ 4 | 5 | last ← { 6 | (⊂⍵)∊seen: ⊂⍵ 7 | seen,← ⊂⍵ 8 | i ← ⍵⍳⌈/⍵ 9 | v ← ⍵[i] 10 | ∇ (0@i⊢⍵)+(-i+1)⌽sz↑+⌿↑(v⍴sz↑1)⊂v⍴1 11 | } n 12 | 13 | ⎕← ≢seen 14 | ⎕← (≢seen)-seen⍳last 15 | -------------------------------------------------------------------------------- /2017/07.apl: -------------------------------------------------------------------------------- 1 | ⎕CY'dfns' 2 | 3 | f ← (⎕D,⎕C⎕A)∘(∊⍨⊆⊢)¨⊃⎕NGET'in/07.txt'1 4 | names ← ⊃¨f 5 | costs ← 1(⍎⊃)¨f 6 | graph ← {(names⍳⊂)¨2↓⍵}¨f 7 | 8 | ⎕← ⊃names[start←⊃(⍳⍴names)~∊graph] 9 | _←{ 10 | kids ← ⍵⊃graph 11 | ⍬≡kids: costs[⍵] 12 | kids ← ∇¨kids 13 | 1=≢∪kids: costs[⍵]++/kids 14 | good ← ⊃kids/⍨0=≠kids 15 | grow ← good-kids~good 16 | ⎕ ← grow+costs[(kids≠good)/⍵⊃graph] 17 | costs[⍵]++/good⍨¨kids 18 | } start 19 | -------------------------------------------------------------------------------- /2017/08.apl: -------------------------------------------------------------------------------- 1 | vars op1 val1 _ other op2 val2 ← ↓⍉↑' '(≠⊆⊢)¨⊃⎕NGET'in/08.txt'1 2 | op1 ← '-+'[op1≡¨⊂'inc'] 3 | op2 ← '≤=≥<≠>'[('<=' '==' '>=' (,'<') '!=' (,'>'))⍳op2] 4 | code ← ⊃,¨/vars '←' vars op1 (⍕∘⍎¨val1) '×' other op2 (⍕∘⍎¨val2) 5 | {⍎⍵,'←0'}¨∪vars 6 | results ← ⍎¨code 7 | 8 | ⎕← ⌈/⍎¨∪vars,other 9 | ⎕← ⌈/results 10 | -------------------------------------------------------------------------------- /2017/09.apl: -------------------------------------------------------------------------------- 1 | s ← '!.'⎕R''⊃⊃⎕NGET'in/09.txt'1 2 | g ← '<[^>]*>'⎕S'&'⊢s 3 | s ← '<[^>]*>'⎕R''⊢s 4 | ⎕← +/(s='{')×-⌿+\'{}'∘.=s 5 | ⎕← +/(≢¨g)-2 6 | -------------------------------------------------------------------------------- /2017/10.apl: -------------------------------------------------------------------------------- 1 | run ← {⊃⊃{(⌽@(256|⍵[1]+⍳⍺)⊃⍵)(+/⍺,1↓⍵)(1+2⊃⍵)}/(⌽⍵),⊂(⍳256)0 0} 2 | 3 | ⎕← ×/2↑run ⍎f← ⊃⊃⎕NGET'in/10.txt'1 4 | 5 | n ← (⎕UCS f) , 17 31 73 47 23 6 | ⎕← (⎕D,'abcdef')[,⍉16 16⊤2⊥≠/2⊥⍣¯1⊢16 16⍴ run∊64/⊂n] 7 | -------------------------------------------------------------------------------- /2017/11.apl: -------------------------------------------------------------------------------- 1 | dirs ← 'ne' (,'n') 'nw' 'se' (,'s') 'sw' 2 | pos ← ¯1j¯1 ¯2 ¯1j1 1j¯1 2 1j1 3 | 4 | dist ← {(⊢⌿p)+.5×-⌿p←|9 11∘.○⍵} 5 | 6 | f ← ','(≠⊆⊢)⊃⊃⎕NGET'in/11.txt'1 7 | 8 | ⎕← (⊢/ , ⌈/) dist +\pos[dirs⍳f] 9 | -------------------------------------------------------------------------------- /2017/12.apl: -------------------------------------------------------------------------------- 1 | graph ← ∨.∧⍨⍣≡ ↑f∊⍨¨⊂⍳≢f← (⍎¨∊∘⎕D⊆⊢)¨ ⊃⎕NGET'in/12.txt'1 2 | 3 | ⎕← ⊃+/graph 4 | ⎕← ≢∪graph 5 | -------------------------------------------------------------------------------- /2017/13.apl: -------------------------------------------------------------------------------- 1 | p d ← ↓⍉↑(⍎¨∊∘⎕D⊆⊢)¨⊃⎕NGET'in/13.txt'1 2 | 3 | ⎕← +/p×d×0=(mod←2×d-1)|p 4 | step ← ⌊/mod 5 | ⎕← {0∊mod|p+⍵: ∇⍵+step ⋄ ⍵} 0 6 | -------------------------------------------------------------------------------- /2017/14.apl: -------------------------------------------------------------------------------- 1 | step ← {⊃⊃{(⌽@(256|⍵[1]+⍳⍺)⊃⍵)(+/⍺,1↓⍵)(1+2⊃⍵)}/(⌽⍵),⊂(⍳256)0 0} 2 | knot ← {,⍉≠/2⊥⍣¯1⊢16 16⍴ step∊64/⊂(⎕UCS ⍵),17 31 73 47 23} 3 | 4 | map ← ↑(⍳128){knot ⍵,'-',⍕⍺}¨⊃⎕NGET'in/14.txt'1 5 | 6 | ⎕← n←+/∊map 7 | 8 | nei ← ,1∊¨⍳3 3 9 | graph ← , {0=⍵[1;1]:⊂⍬ ⋄ ⊂1↓⍸⍣¯1⊢(nei/,⍵)~0}⌺3 3⊢(1+⍳n)@(⍸map)⊢map 10 | ⎕← ≢∪ ∨.∧⍨⍣≡ ↑graph~⊂⍬ 11 | -------------------------------------------------------------------------------- /2017/15.apl: -------------------------------------------------------------------------------- 1 | n ← ⎕D(⍎¨∊⍨⊆⊢)∊⊃⎕NGET'in/15.txt'1 2 | 3 | a ← 1 2⍴n 4 | {a⍪←2147483647|16807 48271×⍵}⍣4e7⊢n 5 | ⎕← +/=/a←65536|a 6 | ⎕← +/=⌿↑4 8{5e6↑⍵/⍨0=⍺|⍵}¨↓⍉a 7 | -------------------------------------------------------------------------------- /2017/16.apl: -------------------------------------------------------------------------------- 1 | start ← word ← 16↑⎕C⎕A 2 | s ← {word ⌽⍨← -⍵} ⋄ x ← {word[⌽⍵]←word[⍵]} ⋄ p ← {word[word⍳⍵]←⌽⍵} 3 | hist ← 0 16⍴' ' 4 | code ← {¯1⌽' ',1⌽⍵}¨','(≠⊆⊢)'([a-z])/([a-z])' '/'⎕R'''\1\2''' ' '⊃⊃⎕NGET'in/16.txt'1 5 | {hist⍪←word ⋄ start≡word⊣⍎¨code: 0 ⋄ ∇hist} ⍬ 6 | 7 | ⎕← hist[1;] 8 | ⎕← hist[(≢hist)|1e9;] 9 | -------------------------------------------------------------------------------- /2017/17.apl: -------------------------------------------------------------------------------- 1 | n ← ⍎⊃⊃⎕NGET'in/17.txt'1 2 | 3 | ⎕← ⊃{((len|n)⌽⍵),len←≢⍵}⍣2017⊢0 4 | 5 | p ← 0 6 | {p ,← 1+⍵|n+⊃⌽p}¨1+⍳5e7 7 | ⎕← ⊃⌽⍸p=1 8 | -------------------------------------------------------------------------------- /2017/18.apl: -------------------------------------------------------------------------------- 1 | ⍝ decompiled 2 | a ← (2*31)-1 3 | arr ← ⍬ 4 | { arr,←a|12345+129749×a|8505×⊃⌽⍵ }⍣127⊢ 464 5 | arr ← 10000|arr 6 | 7 | ⎕← ⊃⌽arr 8 | 9 | ⎕← 127× 1 { 10 | ∧/2≥/⍵: ⍺ 11 | ⍺ + (~⍺) ∇ (⊃⍵) { ⍵≡⍬:⍺ ⋄ (⍺⌈⊃⍵) , (⍺⌊⊃⍵) ∇ 1↓⍵ } 1↓⍵ 12 | } arr 13 | -------------------------------------------------------------------------------- /2017/19.apl: -------------------------------------------------------------------------------- 1 | map ← ↑⊃⎕NGET'in/19e.txt'1 2 | path ← '' 3 | 1 0{' '=c←⍵⌷map: ⋄ path,←c ⋄ turn←'+'=c ⋄ dir←turn⌽⍺ ⋄ dir×←1-2×turn∧' '=(⍵+dir)⌷map ⋄ dir∇⍵+dir} 0,map[0;]⍳'|' 4 | 5 | ⎕← path~'|-+' 6 | ⎕← ≢path 7 | -------------------------------------------------------------------------------- /2017/20.apl: -------------------------------------------------------------------------------- 1 | v ← ↑⊖¨3 3∘⍴¨('-',⎕D)∘(⍎¨∊⍨⊆⊢)¨⊃⎕NGET'in/20.txt'1 2 | 3 | ⎕← {⍵⍳⌊/⍵}+/| (+⍀⍤2⍣1e3⊢v)[;2;] 4 | ⎕← ≢{a←+⍀⍤2⊢⍵ ⋄ a⌿⍨{~⍵∊⍵/⍨0=≠⍵}↓a[;2;]}⍣100⊢ v 5 | -------------------------------------------------------------------------------- /2017/21.apl: -------------------------------------------------------------------------------- 1 | a _ b ← ↓⍉↑' '('.#'∘⍳¨≠⊆⊢)¨('/'⎕R'')⊃⎕NGET'in/21.txt'1 2 | two ← 4=≢¨a 3 | a ← {2(⊥∘,)¨∪(⊢,⌽¨)(⊢,⊖¨)(⊢,⍉¨)⊂(2/2+9=≢⍵)⍴⍵}¨a 4 | b ← {(2/3+16=≢⍵)⍴⍵}¨b 5 | a2 ← (two)/a ⋄ a3 ← (~two)/a 6 | b2 ← (two)/b ⋄ b3 ← (~two)/b 7 | 8 | run ← { 9 | s ← 2+2|≢⍵ 10 | n ← (≢⍵)÷s 11 | c ← (2⊥,)¨⊂[2 3]0 2 1 3⍉n s n s⍴⍵ 12 | 2=s: ⊃⍪/,/b2[{1⍳⍨⍵∊¨a2}¨c] 13 | 3=s: ⊃⍪/,/b3[{1⍳⍨⍵∊¨a3}¨c] 14 | } 15 | 16 | start ← 2 2 2⊤1 5 3 17 | ⎕← +/,run⍣5 ⊢start 18 | ⎕← +/,run⍣18⊢start 19 | -------------------------------------------------------------------------------- /2017/22.apl: -------------------------------------------------------------------------------- 1 | offset ← 1000 2 | map ← '#'=↑⊃⎕NGET'in/22.txt'1 3 | start ← +/1 0j1×offset-1+⌊(≢map)÷2 4 | map ← origin ← (2/2×offset)↑(-2/offset)↑map 5 | 6 | inf←0 7 | _←{ 8 | dir ← (1⊃⍵)×0j¯1 0j1[s ← map⌷⍨r c ← 11 9○⊃⍵] 9 | inf +← ~s 10 | map[r;c] ← ~s 11 | (dir+⊃⍵) dir 12 | }⍣1e4⊢ start 0j¯1 13 | ⎕← inf 14 | 15 | map ← 2×origin 16 | inf←0 17 | _←{ 18 | dir ← (1⊃⍵)×0j¯1 1 0j1 ¯1[s ← map⌷⍨r c ← 11 9○⊃⍵] 19 | inf +← 1=s 20 | map[r;c] ← 4|1+s 21 | (dir+⊃⍵) dir 22 | }⍣1e7⊢ start 0j¯1 23 | ⎕← inf 24 | -------------------------------------------------------------------------------- /2017/23.apl: -------------------------------------------------------------------------------- 1 | 'factors'⎕CY'dfns' 2 | 3 | n ← ⍎6↓⊃⊃⎕NGET'in/23.txt'1 4 | 5 | ⍝ decompiled 6 | ⎕← ×⍨n-2 7 | ⎕← +/1<(≢factors)¨ (100000+n×100)+ 17×⍳1+1e3 8 | -------------------------------------------------------------------------------- /2017/24.apl: -------------------------------------------------------------------------------- 1 | c ← (⌊/,⌈/)¨'/'(⍎¨≠⊆⊢)¨⊃⎕NGET'in/24.txt'1 2 | best ← 100⍴0 3 | 4 | _←{ 5 | l←⊃⌽⍵ 6 | past ← 2(⌊,⌈)/,⍵ 7 | nxt ← (c/⍨∨/¨c∊¨l)~past 8 | 0<≢nxt: ∇¨⍵∘,¨{(~⍵⍳l)⊃⍵}¨nxt~past 9 | best[≢⍵] ⌈← (2×+/⍵)-⊃⌽⍵ 10 | 0 11 | } 0 12 | ⎕← ⌈/best 13 | ⎕← ⊢/best~0 14 | -------------------------------------------------------------------------------- /2017/25.apl: -------------------------------------------------------------------------------- 1 | f ← ⊃⎕NGET'in/25.txt'1 2 | steps ← ⊃⎕D(⍎¨∊⍨⊆⊢)1⊃f 3 | sm ← ↑{v m n←↓3 2⍴22 22 27 27 26 26⊃¨⍵[2 6 3 7 4 8] ⋄ ⍉↑(v='1')(1-2×'l'=m)(⎕A⍳n)}¨1↓(⊢⊆⍨0<≢¨)f 4 | 5 | ex ← 1e4 6 | tape ← (2×ex)⍴0 7 | { (⊃⍵)0+1↓vms⊣tape[ex+⊃⍵]←⊃vms←sm[1⊃⍵;tape[ex+⊃⍵];] }⍣steps⊢0 0 8 | ⎕← +/tape 9 | -------------------------------------------------------------------------------- /2017/README.md: -------------------------------------------------------------------------------- 1 | # AoC 2017 2 | 3 | Did not participate, APL added in 2022. 4 | -------------------------------------------------------------------------------- /2017/in/01e.txt: -------------------------------------------------------------------------------- 1 | 123425 2 | -------------------------------------------------------------------------------- /2017/in/02e.txt: -------------------------------------------------------------------------------- 1 | 5 9 2 8 2 | 9 4 7 3 3 | 3 8 6 5 4 | -------------------------------------------------------------------------------- /2017/in/03.txt: -------------------------------------------------------------------------------- 1 | 289326 2 | -------------------------------------------------------------------------------- /2017/in/03e.txt: -------------------------------------------------------------------------------- 1 | 12 2 | -------------------------------------------------------------------------------- /2017/in/04e.txt: -------------------------------------------------------------------------------- 1 | abcde fghij 2 | abcde xyz ecdab 3 | a ab abc abd abf abj 4 | iiii oiii ooii oooi oooo 5 | oiii ioii iioi iiio 6 | -------------------------------------------------------------------------------- /2017/in/05e.txt: -------------------------------------------------------------------------------- 1 | 0 2 | 3 3 | 0 4 | 1 5 | -3 6 | -------------------------------------------------------------------------------- /2017/in/06.txt: -------------------------------------------------------------------------------- 1 | 14 0 15 12 11 11 3 5 1 6 8 4 9 1 8 4 2 | -------------------------------------------------------------------------------- /2017/in/06e.txt: -------------------------------------------------------------------------------- 1 | 0 2 7 0 2 | -------------------------------------------------------------------------------- /2017/in/07e.txt: -------------------------------------------------------------------------------- 1 | pbga (66) 2 | xhth (57) 3 | ebii (61) 4 | havc (66) 5 | ktlj (57) 6 | fwft (72) -> ktlj, cntj, xhth 7 | qoyq (66) 8 | padx (45) -> pbga, havc, qoyq 9 | tknk (41) -> ugml, padx, fwft 10 | jptl (61) 11 | ugml (68) -> gyxo, ebii, jptl 12 | gyxo (61) 13 | cntj (57) 14 | -------------------------------------------------------------------------------- /2017/in/08e.txt: -------------------------------------------------------------------------------- 1 | b inc 5 if a > 1 2 | a inc 1 if b < 5 3 | c dec -10 if a >= 1 4 | c inc -20 if c == 10 5 | -------------------------------------------------------------------------------- /2017/in/09e.txt: -------------------------------------------------------------------------------- 1 | <{o"i!a,<{i 2 | -------------------------------------------------------------------------------- /2017/in/10.txt: -------------------------------------------------------------------------------- 1 | 102,255,99,252,200,24,219,57,103,2,226,254,1,0,69,216 2 | -------------------------------------------------------------------------------- /2017/in/10e.txt: -------------------------------------------------------------------------------- 1 | 3, 4, 1, 5 2 | -------------------------------------------------------------------------------- /2017/in/11e.txt: -------------------------------------------------------------------------------- 1 | se,sw,se,sw,sw 2 | -------------------------------------------------------------------------------- /2017/in/12e.txt: -------------------------------------------------------------------------------- 1 | 0 <-> 2 2 | 1 <-> 1 3 | 2 <-> 0, 3, 4 4 | 3 <-> 2, 4 5 | 4 <-> 2, 3, 6 6 | 5 <-> 6 7 | 6 <-> 4, 5 8 | -------------------------------------------------------------------------------- /2017/in/13.txt: -------------------------------------------------------------------------------- 1 | 0: 3 2 | 1: 2 3 | 2: 4 4 | 4: 6 5 | 6: 4 6 | 8: 6 7 | 10: 5 8 | 12: 8 9 | 14: 8 10 | 16: 6 11 | 18: 8 12 | 20: 6 13 | 22: 10 14 | 24: 8 15 | 26: 12 16 | 28: 12 17 | 30: 8 18 | 32: 12 19 | 34: 8 20 | 36: 14 21 | 38: 12 22 | 40: 18 23 | 42: 12 24 | 44: 12 25 | 46: 9 26 | 48: 14 27 | 50: 18 28 | 52: 10 29 | 54: 14 30 | 56: 12 31 | 58: 12 32 | 60: 14 33 | 64: 14 34 | 68: 12 35 | 70: 17 36 | 72: 14 37 | 74: 12 38 | 76: 14 39 | 78: 14 40 | 82: 14 41 | 84: 14 42 | 94: 14 43 | 96: 14 44 | -------------------------------------------------------------------------------- /2017/in/13e.txt: -------------------------------------------------------------------------------- 1 | 0: 3 2 | 1: 2 3 | 4: 4 4 | 6: 4 5 | -------------------------------------------------------------------------------- /2017/in/14.txt: -------------------------------------------------------------------------------- 1 | vbqugkhl 2 | -------------------------------------------------------------------------------- /2017/in/14e.txt: -------------------------------------------------------------------------------- 1 | flqrgnkx 2 | -------------------------------------------------------------------------------- /2017/in/15.txt: -------------------------------------------------------------------------------- 1 | Generator A starts with 634 2 | Generator B starts with 301 3 | -------------------------------------------------------------------------------- /2017/in/15e.txt: -------------------------------------------------------------------------------- 1 | Generator A starts with 65 2 | Generator B starts with 8921 3 | -------------------------------------------------------------------------------- /2017/in/16e.txt: -------------------------------------------------------------------------------- 1 | s1,x3/4,pe/b 2 | -------------------------------------------------------------------------------- /2017/in/17.txt: -------------------------------------------------------------------------------- 1 | 337 2 | -------------------------------------------------------------------------------- /2017/in/17e.txt: -------------------------------------------------------------------------------- 1 | 3 2 | -------------------------------------------------------------------------------- /2017/in/18.txt: -------------------------------------------------------------------------------- 1 | set i 31 2 | set a 1 3 | mul p 17 4 | jgz p p 5 | mul a 2 6 | add i -1 7 | jgz i -2 8 | add a -1 9 | set i 127 10 | set p 464 11 | mul p 8505 12 | mod p a 13 | mul p 129749 14 | add p 12345 15 | mod p a 16 | set b p 17 | mod b 10000 18 | snd b 19 | add i -1 20 | jgz i -9 21 | jgz a 3 22 | rcv b 23 | jgz b -1 24 | set f 0 25 | set i 126 26 | rcv a 27 | rcv b 28 | set p a 29 | mul p -1 30 | add p b 31 | jgz p 4 32 | snd a 33 | set a b 34 | jgz 1 3 35 | snd b 36 | set f 1 37 | add i -1 38 | jgz i -11 39 | snd a 40 | jgz f -16 41 | jgz a -19 42 | -------------------------------------------------------------------------------- /2017/in/19e.txt: -------------------------------------------------------------------------------- 1 | | 2 | | +--+ 3 | A | C 4 | F---|----E|--+ 5 | | | | D 6 | +B-+ +--+ 7 | 8 | -------------------------------------------------------------------------------- /2017/in/20e.txt: -------------------------------------------------------------------------------- 1 | p=<-6,0,0>, v=< 3,0,0>, a=< 0,0,0> 2 | p=<-4,0,0>, v=< 2,0,0>, a=< 0,0,0> 3 | p=<-2,0,0>, v=< 1,0,0>, a=< 0,0,0> 4 | p=< 3,0,0>, v=<-1,0,0>, a=< 0,0,0> 5 | -------------------------------------------------------------------------------- /2017/in/21e.txt: -------------------------------------------------------------------------------- 1 | ../.. => .#./.#./.#. 2 | ../.# => ##./#../... 3 | .#./..#/### => #..#/..../..../#..# 4 | -------------------------------------------------------------------------------- /2017/in/22.txt: -------------------------------------------------------------------------------- 1 | .......##.#..####.#....## 2 | ..###....###.####..##.##. 3 | #..####.#....#.#....##... 4 | .#....#.#.#....#######... 5 | .###..###.#########....## 6 | ##...#####..#####.###.#.. 7 | .#..##.###.#.#....######. 8 | .#.##.#..####..#.##.....# 9 | #.#..###..##..#......##.# 10 | ##.###.##.#.#...##.#.##.. 11 | ##...#.######.#..##.#...# 12 | ....#.####..#..###.##..## 13 | ...#....#.###.#.#..#..... 14 | ..###.#.#....#.....#.#### 15 | .#....##..##.#.#...#.#.#. 16 | ...##.#.####.###.##...#.# 17 | ##.#.####.#######.##..##. 18 | .##...#......####..####.# 19 | #..###.#.###.##.#.#.##..# 20 | #..###.#.#.#.#.#....#.#.# 21 | ####.#..##..#.#..#..#.### 22 | ##.....#..#.#.#..#.####.. 23 | #####.....###.........#.. 24 | ##...#...####..#####...## 25 | .....##.#....##...#.....# 26 | -------------------------------------------------------------------------------- /2017/in/22e.txt: -------------------------------------------------------------------------------- 1 | ..# 2 | #.. 3 | ... 4 | -------------------------------------------------------------------------------- /2017/in/23.txt: -------------------------------------------------------------------------------- 1 | set b 65 2 | set c b 3 | jnz a 2 4 | jnz 1 5 5 | mul b 100 6 | sub b -100000 7 | set c b 8 | sub c -17000 9 | set f 1 10 | set d 2 11 | set e 2 12 | set g d 13 | mul g e 14 | sub g b 15 | jnz g 2 16 | set f 0 17 | sub e -1 18 | set g e 19 | sub g b 20 | jnz g -8 21 | sub d -1 22 | set g d 23 | sub g b 24 | jnz g -13 25 | jnz f 2 26 | sub h -1 27 | set g b 28 | sub g c 29 | jnz g 2 30 | jnz 1 3 31 | sub b -17 32 | jnz 1 -23 33 | -------------------------------------------------------------------------------- /2017/in/24.txt: -------------------------------------------------------------------------------- 1 | 48/5 2 | 25/10 3 | 35/49 4 | 34/41 5 | 35/35 6 | 47/35 7 | 34/46 8 | 47/23 9 | 28/8 10 | 27/21 11 | 40/11 12 | 22/50 13 | 48/42 14 | 38/17 15 | 50/33 16 | 13/13 17 | 22/33 18 | 17/29 19 | 50/0 20 | 20/47 21 | 28/0 22 | 42/4 23 | 46/22 24 | 19/35 25 | 17/22 26 | 33/37 27 | 47/7 28 | 35/20 29 | 8/36 30 | 24/34 31 | 6/7 32 | 7/43 33 | 45/37 34 | 21/31 35 | 37/26 36 | 16/5 37 | 11/14 38 | 7/23 39 | 2/23 40 | 3/25 41 | 20/20 42 | 18/20 43 | 19/34 44 | 25/46 45 | 41/24 46 | 0/33 47 | 3/7 48 | 49/38 49 | 47/22 50 | 44/15 51 | 24/21 52 | 10/35 53 | 6/21 54 | 14/50 55 | -------------------------------------------------------------------------------- /2017/in/24e.txt: -------------------------------------------------------------------------------- 1 | 0/2 2 | 2/2 3 | 2/3 4 | 3/4 5 | 3/5 6 | 0/1 7 | 10/1 8 | 9/10 9 | -------------------------------------------------------------------------------- /2017/in/25e.txt: -------------------------------------------------------------------------------- 1 | Begin in state A. 2 | Perform a diagnostic checksum after 6 steps. 3 | 4 | In state A: 5 | If the current value is 0: 6 | - Write the value 1. 7 | - Move one slot to the right. 8 | - Continue with state B. 9 | If the current value is 1: 10 | - Write the value 0. 11 | - Move one slot to the left. 12 | - Continue with state B. 13 | 14 | In state B: 15 | If the current value is 0: 16 | - Write the value 1. 17 | - Move one slot to the left. 18 | - Continue with state A. 19 | If the current value is 1: 20 | - Write the value 1. 21 | - Move one slot to the right. 22 | - Continue with state A. 23 | -------------------------------------------------------------------------------- /2018/01.apl: -------------------------------------------------------------------------------- 1 | f ← ⍎¨⊃⎕NGET'in/01.txt'1 2 | 3 | +/f 4 | F ← +\1e6⍴f 5 | F[(≠F)⍳0] 6 | -------------------------------------------------------------------------------- /2018/02.apl: -------------------------------------------------------------------------------- 1 | f ← ⊃⎕NGET'in/02.txt'1 2 | 3 | ×/+⌿ ↑{2 3∊1~⍨{≢⍵}⌸⍵}¨f 4 | ∩/f[⊃⍸∘.{1=0+.=⍺=⍵}⍨f] 5 | -------------------------------------------------------------------------------- /2018/03.apl: -------------------------------------------------------------------------------- 1 | f ← {↓+⍀2 2⍴ ⍎¨ 1↓(⍵∊⎕D)⊆⍵}¨⊃⎕NGET'in/03.txt'1 2 | 3 | coords ← {,⊃{(⊂⍺)+⍳⍵-⍺}/⍵} 4 | 1 +.< {≢⍵}⌸⊃,/ coords¨f 5 | 6 | intersect ← { ((⊃⌈/⍤⊣) ∧.< (⊃⌊/⍤⊢))/ ↓⍉↑⍺⍵ } 7 | 1+1⍳⍨+/∘.intersect⍨f 8 | -------------------------------------------------------------------------------- /2018/04.apl: -------------------------------------------------------------------------------- 1 | schedule ← 4000 60⍴0 2 | minute ← ⍬ 3 | 4 | { 5 | last ← ⍎⊃⌽ n←(⍵∊⎕D)⊆⍵ 6 | 6=≢n: guard ∘← last 7 | ⍬≡minute: minute∘← last 8 | schedule[guard; minute+⍳|minute-last] +← 1 9 | minute ∘← ⍬ 10 | }¨{⍵[⍋⍵]}⊃⎕NGET'in/04.txt'1 11 | 12 | max ← {⊃⍸⍵=⌈/⍵} 13 | 14 | {⍵×max ⍵⌷schedule} max +/schedule 15 | {⍵×max ⍵⌷schedule} max ⌈/schedule 16 | -------------------------------------------------------------------------------- /2018/05.apl: -------------------------------------------------------------------------------- 1 | f ← ⎕UCS ⊃⊃⎕NGET'in/05.txt'1 2 | 3 | react ← ≢{-⍵/⍨d∧¯1⌽d←32≠2-/⍵,0}⍣2⍣≡ 4 | 5 | react f 6 | ⌊/ {react f~⍵+0 32}¨⎕UCS ⎕A 7 | -------------------------------------------------------------------------------- /2018/06.apl: -------------------------------------------------------------------------------- 1 | f ← ⍎¨⊃⎕NGET'in/06.txt'1 2 | map ← {1<+/l=⌊/l←+/¨|f∘.-⊂⍵: 0 ⋄ 1+⊃⍸l=⌊/l}¨⍳2×⊃⌈/f 3 | 4 | ⌈/{≢⍵}⌸~∘0∊map×~map∊∪∊¯1 1{(∊⍺↑⍵),∊⍺↑[1]⍵}¨⊂map 5 | +/∊{10000>+/∊|f∘.-⊂⍵}¨⍳2×⊃⌈/f 6 | -------------------------------------------------------------------------------- /2018/07.apl: -------------------------------------------------------------------------------- 1 | map ← {1@⍵⊢0⍴⍨2⍴1+⌈/∊⍵} {⎕A⍳⍵[5 36]}¨⊃⎕NGET'in/07.txt'1 2 | workers duration ← 5 60 3 | 4 | {⎕A[⍵]} { 5 | next ← ⍵ ~⍨ ⍸(+⌿map)=+⌿(⊂⍵)⌷map 6 | 0=≢next: ⍵ 7 | ∇ ⍵,⊃next 8 | } ⍬ 9 | 10 | (0⍴⍨≢map) (0{ 11 | next ← ⍵ ~⍨ ⍸(+⌿map)=+⌿(⊂⍵~⍸×⍺)⌷map 12 | (workers=+/×⍺) ∨ (∨/×⍺) ∧ 0=≢next: (0⌈⍺-⌊/⍺~0) ((⍺⍺+⌊/⍺~0)∇∇)⍵ 13 | 0=≢next: ⍺⍺ 14 | ((duration+1+⊃next)@(⊃next)⊢⍺) ∇ ⍵,⊃next 15 | }) ⍬ 16 | 17 | -------------------------------------------------------------------------------- /2018/08.apl: -------------------------------------------------------------------------------- 1 | f ← ⍎⊃⊃⎕NGET'in/08.txt'1 2 | 3 | read ← { ⊃res i ∘← (f[i+⍳⍵]) (i+⍵) } 4 | 5 | run ← { 6 | i ∘← 0 ⋄ ⍺⍺ { 7 | n m ← read 2 8 | n=0: +/read m 9 | +/ (read m) ⍺⍺ 0,∇¨⍳n 10 | } ⍵ 11 | } 12 | 13 | , run 0 14 | {⍵[⍺ ∩ 1↓⍳≢⍵]} run 0 15 | -------------------------------------------------------------------------------- /2018/09.apl: -------------------------------------------------------------------------------- 1 | players worth ← ⍎¨(∊∘⎕D⊆⊢)⊃⊃⎕NGET'in/09.txt'1 2 | 3 | right ← {ring[⍵;1]} 4 | left ← {ring[⍵;0]} 5 | 6 | insert ← { 7 | j ← right⊢i ← right ⍵ 8 | cur ← ring[(i 1)(j 0)]←≢ring 9 | ring⍪← i j 10 | cur 11 | } 12 | 13 | delete ← { 14 | i ← left⊢scores ,← left⊢ j ← left⍣6⊢⍵ 15 | ring[(i 1)(j 0)] ← j i 16 | ring⍪← 0 17 | j 18 | } 19 | 20 | play ← { insert⍣(23|⍵) delete∘(insert⍣22)⍣(⌊⍵÷23)⊢0 } 21 | max ← { totals ← ⍵+marble ← 23×1+⍳≢⍵ ⋄ ⌈/{+/totals[⍵]}⌸players|marble} 22 | 23 | scores ← ⍬ ⋄ ring ← 1 2⍴0 ⋄ max scores⊣play worth 24 | scores ← ⍬ ⋄ ring ← 1 2⍴0 ⋄ max scores⊣play worth×100 25 | -------------------------------------------------------------------------------- /2018/10.apl: -------------------------------------------------------------------------------- 1 | pos vel ← ↓⍉↑{↓2 2⍴⍎¨(⍵∊'-',⎕D)⊆⍵}¨⊃⎕NGET'in/10.txt'1 2 | step ← vel∘+ 3 | 4 | show ← { 5 | min ← ⌊/⍵ 6 | m ← ⍴∘0⊃1+(⌈/-⌊/)⍵ 7 | ' ⎕'[⍉1@(⍵-min)⊢m] 8 | } 9 | 10 | part2 ← 0 11 | show step⍣{part2 +← 1 ⋄ 9=⌊/⊃(⌈/-⌊/)⍺}⊢pos 12 | part2 13 | -------------------------------------------------------------------------------- /2018/11.apl: -------------------------------------------------------------------------------- 1 | serial ← ⍎⊃⊃⎕NGET'in/11.txt'1 2 | 3 | grid ← {¯5+⊃10 100⊤(⍺+10)×serial+⍵×⍺+10}/¨1+⍳300 300 4 | 5 | max ← {(⌷∘total , +∘1)⊃⍸total= max←⌈⌿⌈/ total←⍵+⌿⍵+/grid} 6 | 7 | 1↓max 3 8 | {(300-i),⍨ 1↓⊃⍵[i←⊃⍒⍵]}max¨300-⍳300 9 | -------------------------------------------------------------------------------- /2018/12.apl: -------------------------------------------------------------------------------- 1 | input ← 15↓⊃f ← '#'=⊃⎕NGET'in/12.txt'1 2 | on ← {(2⊥5∘↑)¨⍵/⍨9⌷¨⍵}2↓f 3 | next ← on∊⍨ 2⊥¨ 5,/ 4⌽ (8/0), ⊢ 4 | generation ← {.25×(≢⍵)-≢input} 5 | score ← {+/(⍸⍵)-2×generation ⍵} 6 | 7 | score next⍣20⊢input 8 | 9 | stable ← next⍣(≡⍥(⍸-⍳∘1)) input ⍝ stabilize the pattern 10 | {⍵ + (⍵-⍨score next stable) × 50e9-generation stable} score stable 11 | -------------------------------------------------------------------------------- /2018/14.apl: -------------------------------------------------------------------------------- 1 | part1 ← 10⊥part2 ← ⍎¨⊃⊃⎕NGET'in/14.txt'1 2 | 3 | lookup ← ∘.(⍎¨∘⍕+)⍨⍳10 4 | scores ← 3 7 5 | batch ∘← ⍬ 6 | 7 | _←{ 8 | turns ← 1+⌊10÷⍨(≢scores)-1+⌈/⍵ 9 | batch ∘← ⍬ 10 | next ← { ⍵+1+ batch,← scores[⍵] }⍣turns⊢⍵ 11 | scores ,← ∊lookup[↓turns 2⍴batch] 12 | (≢scores)|next 13 | }⍣4⍣{∨/part2⍷scores} 0 1 14 | 15 | 10⊥scores[part1+⍳10] 16 | ⍸part2⍷scores 17 | -------------------------------------------------------------------------------- /2018/18.apl: -------------------------------------------------------------------------------- 1 | f ← '.|#'⍳↑⊃⎕NGET'in/18.txt'1 2 | 3 | step ← { 4 | c ← 4 ⌷ n ← ,⍵ 5 | 0=c: 3≤1+.= n 6 | 1=c: 1+3≤2+.= n 7 | 2×1 2 ∧.≤ +/1 2∘.= n 8 | }⌺3 3 9 | 10 | score ← {×/+/1 2∘.=,⍵} 11 | score step⍣10⊢f 12 | 13 | run ← { 14 | mem ← 1500⌶ ⍬ 15 | { key ← ⊂⍵ 16 | key∊mem: {⊃mem[⍵+(|⍵-≢mem)|1e9-⍵]} mem⍳key 17 | mem ,← key 18 | ∇ step ⍵ 19 | } ⍵ 20 | } 21 | 22 | score run f 23 | -------------------------------------------------------------------------------- /2018/19.apl: -------------------------------------------------------------------------------- 1 | f ← ⊃⎕NGET'in/19.txt'1 2 | ip ← ⍎⊃⌽⊃f 3 | asm ← 1↓f 4 | 5 | addr ← { (+/reg[2↑⍵])@(2⊃⍵)⊢reg } 6 | addi ← { (reg[⊃⍵]+1⊃⍵)@(2⊃⍵)⊢reg } 7 | mulr ← { (×/reg[2↑⍵])@(2⊃⍵)⊢reg } 8 | muli ← { (reg[⊃⍵]×1⊃⍵)@(2⊃⍵)⊢reg } 9 | setr ← { (reg[⊃⍵])@(2⊃⍵)⊢reg } 10 | seti ← { (⊃⍵)@(2⊃⍵)⊢reg } 11 | 12 | score ← +/(∪⊢∨⍳) ⍝ sum of divisors 13 | 14 | reg ← 5↑0 15 | score reg[2]⊣{ 1+ip⌷reg ∘← ⍎⊃asm[reg[ip]←⍵] }⍣{0=reg[ip]}⊢0 16 | 17 | reg ← 5↑1 18 | score reg[2]⊣{ 1+ip⌷reg ∘← ⍎⊃asm[reg[ip]←⍵] }⍣{0=reg[ip]}⊢0 19 | -------------------------------------------------------------------------------- /2018/20.apl: -------------------------------------------------------------------------------- 1 | f ← ¯1↓1↓ ⊃⊃⎕NGET'in/20.txt'1 2 | 3 | rooms ← stack ← 1 2⍴0 4 | { 5 | ⍵='(': stack ⍪← ⊢⌿rooms 6 | ⍵=')': stack ↓⍨← ¯1 7 | ⍵='|': rooms ⍪← ⊢⌿stack 8 | rooms ⍪← (⊢⌿rooms)+1,0j1*'NESW'⍳⍵ 9 | }¨f 10 | 11 | depths ← ⊃{⌊/⍵}⌸/⌽↓⍉↑rooms 12 | ⌈/depths 13 | +/depths≥1e3 14 | -------------------------------------------------------------------------------- /2018/21.apl: -------------------------------------------------------------------------------- 1 | input ← ⍎⊃⎕D(∊⍨⊆⊢)8⊃⊃⎕NGET'in/21.txt'1 2 | 3 | solve ← { 4 | c b a←256 256 256⊤⍵ 5 | (16777216|65899×16777216|+)/(c+~2|c),b,a,input 6 | } 7 | solve 0 8 | { ⍺←⍬ ⋄ ⍵∊⍺: ⊃⌽⍺ ⋄ (⍺,⍵) ∇ solve ⍵ } 0 9 | -------------------------------------------------------------------------------- /2018/22.apl: -------------------------------------------------------------------------------- 1 | ⎕CY'dfns' 2 | 3 | depth tx ty ← (⍎¨∊∘⎕D⊆⊢)∊⊃⎕NGET'in/22.txt'1 4 | mx ← 10×tx 5 | 6 | fmt ← {'.=|'[3|⍵]} 7 | 8 | erosion ← 1(mx+1)⍴20183|depth+16807×⍳mx+1 9 | map ← 3|erosion ⊣ { 10 | above ← ⊢⌿erosion 11 | ⊢erosion ⍪← ⌽⊃{⍵,⍨20183|depth+⍺×⊃⍵}/⌽(20183|depth+48271×⍵)@0⊢above 12 | }¨ 1 to ty 13 | map[ty;tx] ← 0 14 | 15 | +/∊(1+ty tx)↑map 16 | -------------------------------------------------------------------------------- /2018/23.apl: -------------------------------------------------------------------------------- 1 | f ← (⍎¨∊∘('-',⎕D)⊆⊢)¨⊃⎕NGET'in/23.txt'1 2 | +/ f ((3⊃⊢) ≥ (+/∘|-)⍥(3∘↑))¨ f[⊃⍒⊢/¨f] 3 | -------------------------------------------------------------------------------- /2018/25.apl: -------------------------------------------------------------------------------- 1 | f ← ⍎¨'-'⎕R'¯' ⊃⎕NGET'in/25.txt'1 2 | 3 | ≢∪∨.∧⍨⍣≡3≥∘.(+.(|-))⍨f 4 | -------------------------------------------------------------------------------- /2018/README.md: -------------------------------------------------------------------------------- 1 | # AoC 2018 2 | 3 | Done in Ruby (source lost). APL added in 2022. 4 | -------------------------------------------------------------------------------- /2018/in/03e.txt: -------------------------------------------------------------------------------- 1 | #1 @ 1,3: 4x4 2 | #2 @ 3,1: 4x4 3 | #3 @ 5,5: 2x2 -------------------------------------------------------------------------------- /2018/in/04e.txt: -------------------------------------------------------------------------------- 1 | [1518-11-01 00:00] Guard #10 begins shift 2 | [1518-11-01 00:05] falls asleep 3 | [1518-11-01 00:25] wakes up 4 | [1518-11-01 00:30] falls asleep 5 | [1518-11-01 00:55] wakes up 6 | [1518-11-01 23:58] Guard #99 begins shift 7 | [1518-11-02 00:40] falls asleep 8 | [1518-11-02 00:50] wakes up 9 | [1518-11-03 00:05] Guard #10 begins shift 10 | [1518-11-03 00:24] falls asleep 11 | [1518-11-03 00:29] wakes up 12 | [1518-11-04 00:02] Guard #99 begins shift 13 | [1518-11-04 00:36] falls asleep 14 | [1518-11-04 00:46] wakes up 15 | [1518-11-05 00:03] Guard #99 begins shift 16 | [1518-11-05 00:45] falls asleep 17 | [1518-11-05 00:55] wakes up -------------------------------------------------------------------------------- /2018/in/05e.txt: -------------------------------------------------------------------------------- 1 | dabAcCaCBAcCcaDA -------------------------------------------------------------------------------- /2018/in/06.txt: -------------------------------------------------------------------------------- 1 | 195, 221 2 | 132, 132 3 | 333, 192 4 | 75, 354 5 | 162, 227 6 | 150, 108 7 | 46, 40 8 | 209, 92 9 | 153, 341 10 | 83, 128 11 | 256, 295 12 | 311, 114 13 | 310, 237 14 | 99, 240 15 | 180, 337 16 | 332, 176 17 | 212, 183 18 | 84, 61 19 | 275, 341 20 | 155, 89 21 | 169, 208 22 | 105, 78 23 | 151, 318 24 | 92, 74 25 | 146, 303 26 | 184, 224 27 | 285, 348 28 | 138, 163 29 | 216, 61 30 | 277, 270 31 | 130, 155 32 | 297, 102 33 | 197, 217 34 | 72, 276 35 | 299, 89 36 | 357, 234 37 | 136, 342 38 | 346, 221 39 | 110, 188 40 | 82, 183 41 | 271, 210 42 | 46, 198 43 | 240, 286 44 | 128, 95 45 | 111, 309 46 | 108, 54 47 | 258, 305 48 | 241, 157 49 | 117, 162 50 | 96, 301 51 | -------------------------------------------------------------------------------- /2018/in/06e.txt: -------------------------------------------------------------------------------- 1 | 1, 1 2 | 1, 6 3 | 8, 3 4 | 3, 4 5 | 5, 5 6 | 8, 9 7 | -------------------------------------------------------------------------------- /2018/in/07e.txt: -------------------------------------------------------------------------------- 1 | Step C must be finished before step A can begin. 2 | Step C must be finished before step F can begin. 3 | Step A must be finished before step B can begin. 4 | Step A must be finished before step D can begin. 5 | Step B must be finished before step E can begin. 6 | Step D must be finished before step E can begin. 7 | Step F must be finished before step E can begin. -------------------------------------------------------------------------------- /2018/in/08e.txt: -------------------------------------------------------------------------------- 1 | 2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2 2 | -------------------------------------------------------------------------------- /2018/in/09.txt: -------------------------------------------------------------------------------- 1 | 429 players; last marble is worth 70901 points 2 | -------------------------------------------------------------------------------- /2018/in/09e.txt: -------------------------------------------------------------------------------- 1 | 10 players; last marble is worth 1618 points 2 | -------------------------------------------------------------------------------- /2018/in/11.txt: -------------------------------------------------------------------------------- 1 | 5235 2 | -------------------------------------------------------------------------------- /2018/in/11e.txt: -------------------------------------------------------------------------------- 1 | 18 2 | -------------------------------------------------------------------------------- /2018/in/12.txt: -------------------------------------------------------------------------------- 1 | initial state: ##.##.##..#..#.#.#.#...#...#####.###...#####.##..#####.#..#.##..#..#.#...#...##.##...#.##......####. 2 | 3 | ##.#. => # 4 | #.#.. => # 5 | ##... => . 6 | ...## => # 7 | ###.# => # 8 | #.##. => # 9 | #.### => # 10 | ####. => # 11 | .#..# => # 12 | ...#. => . 13 | #..#. => . 14 | #.#.# => . 15 | .##.# => . 16 | ..#.. => . 17 | .#.## => # 18 | ..##. => . 19 | .#.#. => # 20 | #..## => # 21 | ..#.# => # 22 | #.... => . 23 | ..### => . 24 | #...# => . 25 | ##### => # 26 | ###.. => # 27 | ....# => . 28 | ##.## => # 29 | .#### => . 30 | ..... => . 31 | ##..# => # 32 | .##.. => . 33 | .###. => . 34 | .#... => # 35 | -------------------------------------------------------------------------------- /2018/in/12e.txt: -------------------------------------------------------------------------------- 1 | initial state: #..#.#..##......###...### 2 | 3 | ...## => # 4 | ..#.. => # 5 | .#... => # 6 | .#.#. => # 7 | .#.## => # 8 | .##.. => # 9 | .#### => # 10 | #.#.# => # 11 | #.### => # 12 | ##.#. => # 13 | ##.## => # 14 | ###.. => # 15 | ###.# => # 16 | ####. => # 17 | -------------------------------------------------------------------------------- /2018/in/13e.txt: -------------------------------------------------------------------------------- 1 | />-<\ 2 | | | 3 | | /<+-\ 4 | | | | v 5 | \>+/ -------------------------------------------------------------------------------- /2018/in/14.txt: -------------------------------------------------------------------------------- 1 | 768071 2 | -------------------------------------------------------------------------------- /2018/in/14e.txt: -------------------------------------------------------------------------------- 1 | 2018 -------------------------------------------------------------------------------- /2018/in/15e.txt: -------------------------------------------------------------------------------- 1 | ####### 2 | #E.G#.# 3 | #.#G..# 4 | #G.#.G# 5 | #G..#.# 6 | #...E.# 7 | ####### 8 | -------------------------------------------------------------------------------- /2018/in/17e.txt: -------------------------------------------------------------------------------- 1 | x=5, y=2..7 2 | y=7, x=5..11 3 | x=11, y=3..7 4 | x=8, y=2..4 5 | x=16, y=1..2 6 | x=8, y=10..13 7 | x=14, y=10..13 8 | y=13, x=8..14 9 | -------------------------------------------------------------------------------- /2018/in/18e.txt: -------------------------------------------------------------------------------- 1 | .#.#...|#. 2 | .....#|##| 3 | .|..|...#. 4 | ..|#.....# 5 | #.#|||#|#| 6 | ...#.||... 7 | .|....|... 8 | ||...#|.#| 9 | |.||||..|. 10 | ...#.|..|. 11 | -------------------------------------------------------------------------------- /2018/in/19.txt: -------------------------------------------------------------------------------- 1 | #ip 3 2 | addi 3 16 3 3 | seti 1 7 1 4 | seti 1 7 5 5 | mulr 1 5 4 6 | eqrr 4 2 4 7 | addr 4 3 3 8 | addi 3 1 3 9 | addr 1 0 0 10 | addi 5 1 5 11 | gtrr 5 2 4 12 | addr 3 4 3 13 | seti 2 2 3 14 | addi 1 1 1 15 | gtrr 1 2 4 16 | addr 4 3 3 17 | seti 1 5 3 18 | mulr 3 3 3 19 | addi 2 2 2 20 | mulr 2 2 2 21 | mulr 3 2 2 22 | muli 2 11 2 23 | addi 4 2 4 24 | mulr 4 3 4 25 | addi 4 2 4 26 | addr 2 4 2 27 | addr 3 0 3 28 | seti 0 8 3 29 | setr 3 8 4 30 | mulr 4 3 4 31 | addr 3 4 4 32 | mulr 3 4 4 33 | muli 4 14 4 34 | mulr 4 3 4 35 | addr 2 4 2 36 | seti 0 7 0 37 | seti 0 9 3 38 | -------------------------------------------------------------------------------- /2018/in/19e.txt: -------------------------------------------------------------------------------- 1 | #ip 0 2 | seti 5 0 1 3 | seti 6 0 2 4 | addi 0 1 0 5 | addr 1 2 3 6 | setr 1 0 0 7 | seti 8 0 4 8 | seti 9 0 5 9 | -------------------------------------------------------------------------------- /2018/in/20e.txt: -------------------------------------------------------------------------------- 1 | ^ENNWSWW(NEWS|)SSSEEN(WNSE|)EE(SWEN|)NNN$ 2 | -------------------------------------------------------------------------------- /2018/in/21.txt: -------------------------------------------------------------------------------- 1 | #ip 1 2 | seti 123 0 4 3 | bani 4 456 4 4 | eqri 4 72 4 5 | addr 4 1 1 6 | seti 0 0 1 7 | seti 0 0 4 8 | bori 4 65536 5 9 | seti 10704114 0 4 10 | bani 5 255 2 11 | addr 4 2 4 12 | bani 4 16777215 4 13 | muli 4 65899 4 14 | bani 4 16777215 4 15 | gtir 256 5 2 16 | addr 2 1 1 17 | addi 1 1 1 18 | seti 27 2 1 19 | seti 0 4 2 20 | addi 2 1 3 21 | muli 3 256 3 22 | gtrr 3 5 3 23 | addr 3 1 1 24 | addi 1 1 1 25 | seti 25 5 1 26 | addi 2 1 2 27 | seti 17 5 1 28 | setr 2 6 5 29 | seti 7 8 1 30 | eqrr 4 0 2 31 | addr 2 1 1 32 | seti 5 3 1 33 | -------------------------------------------------------------------------------- /2018/in/22.txt: -------------------------------------------------------------------------------- 1 | depth: 7305 2 | target: 13,734 3 | -------------------------------------------------------------------------------- /2018/in/22e.txt: -------------------------------------------------------------------------------- 1 | depth: 510 2 | target: 10,10 3 | -------------------------------------------------------------------------------- /2018/in/23e.txt: -------------------------------------------------------------------------------- 1 | pos=<10,12,12>, r=2 2 | pos=<12,14,12>, r=2 3 | pos=<16,12,12>, r=4 4 | pos=<14,14,14>, r=6 5 | pos=<50,50,50>, r=200 6 | pos=<10,10,10>, r=5 7 | -------------------------------------------------------------------------------- /2018/in/25e.txt: -------------------------------------------------------------------------------- 1 | 1,-1,0,1 2 | 2,0,-1,0 3 | 3,2,-1,0 4 | 0,0,3,1 5 | 0,0,-1,-1 6 | 2,3,-2,0 7 | -2,2,0,0 8 | 2,-2,0,-1 9 | 1,-1,0,-1 10 | 3,2,0,2 -------------------------------------------------------------------------------- /2019/01.apl: -------------------------------------------------------------------------------- 1 | n ← ⍎¨⊃⎕NGET'in/01.txt'1 2 | 3 | step ← {0⌈¯2+⌊⍵÷3} 4 | 5 | ⎕← +/ step n 6 | ⎕← +/s⊣ step⍣{s+←⍺ ⋄ ⍺≡⍵} n⊣ s ← n×0 7 | -------------------------------------------------------------------------------- /2019/02.apl: -------------------------------------------------------------------------------- 1 | N ← ⍎⊃⊃⎕NGET'in/02.txt'1 2 | 3 | vm ← ⎕FIX'vm.apl' 4 | 5 | ⎕← 1 0⊃ vm.run 12 2@1 2⊢N 6 | ⎕← 100⊥⊃⍸19690720= ∘.{1 0⊃ vm.run ⍺ ⍵@1 2⊢N}⍨⍳99 7 | -------------------------------------------------------------------------------- /2019/03.apl: -------------------------------------------------------------------------------- 1 | n ← ','(≠⊆⊢)¨⊃⎕NGET'in/03.txt'1 2 | 3 | ⍝ s - steps 4 | s ← {+\∊⍵}¨ {1 ¯1 0j1 0j¯1['RLUD'⍳⊃⍵]/⍨⍎1↓⍵}¨¨n 5 | ⍝ x - intersection points 6 | x ← ⊃∩/s 7 | 8 | ⎕← ⌊/+⌿|9 11∘.○x 9 | ⎕← ⌊/2++⌿s∘.⍳x 10 | -------------------------------------------------------------------------------- /2019/04.apl: -------------------------------------------------------------------------------- 1 | 'trav'⎕CY'dfns' 2 | 3 | min max ← ⍎¨'-'(≠⊆⊢)⊃⊃⎕NGET'in/04.txt'1 4 | 5 | next ← { (⊂⍵),¨m+⍳10-m←(6=≢⍵)↓1⌈⌈/⍵ } 6 | acc ← { ⍺+(adj ⍵)∧(max≥n)∧min≤n←10⊥⍵ } 7 | 8 | adj ← ⊢≢∪ 9 | ⎕← 0 acc trav next ⍬ 10 | 11 | adj ← 2∊{≢⍵}⌸ 12 | ⎕← 0 acc trav next ⍬ 13 | -------------------------------------------------------------------------------- /2019/05.apl: -------------------------------------------------------------------------------- 1 | N ← ⍎¨','(≠⊆⊢)⊃⊃⎕NGET'in/05.txt'1 2 | 3 | vm ← ⎕FIX'vm.apl' 4 | 5 | ⎕← 3⊃1 vm.run N 6 | ⎕← 3⊃5 vm.run N 7 | -------------------------------------------------------------------------------- /2019/06.apl: -------------------------------------------------------------------------------- 1 | src dst ← ↓⍉↑')'(≠⊆⊢)¨⊃⎕NGET'in/06.txt'1 2 | 3 | pa ← dst⍳src 4 | ⎕← +/{1+⍵[pa],¯1}⍣≡0,pa≠pa 5 | ⎕← (≢∪~∩)/1↓¨{⍵=≢pa:⍬ ⋄ ⍵,∇pa[⍵]}¨dst⍳'YOU' 'SAN' 6 | -------------------------------------------------------------------------------- /2019/07.apl: -------------------------------------------------------------------------------- 1 | N ← ⍎¨','(≠⊆⊢)⊃⊃⎕NGET'in/07.txt'1 2 | 3 | vm ← ⎕FIX'vm.apl' 4 | 'pmat'⎕CY'dfns' 5 | 6 | ⎕← ⌈/{3 0⊃(⍺,⍵)vm.run N}/⍤1⌽0,pmat 5 7 | ⎕← ⌈/{ 8 | m ← (vm.run∘N)¨⍵ 9 | step ← {3 0⊃⊃m[⍺]←⊂⍵ vm.resume ⍺⊃m} 10 | {step/⌽⍵,⍳5}⍣≡0 11 | }⍤1⌽0,5+pmat 5 12 | -------------------------------------------------------------------------------- /2019/08.apl: -------------------------------------------------------------------------------- 1 | n ← {(150÷⍨≢⍵)6 25⍴⍵}⍎¨⊃⊃⎕NGET'in/08.txt'1 2 | 3 | ⎕← ×/+/1 2∘.=,n⌷⍨(⊢⍳⌊/){0+.=,⍵}⍤2⊢n 4 | ⎕← ' ⎕'[{⊃⍵~2}⍤1⊢2 0 1⍉n] 5 | -------------------------------------------------------------------------------- /2019/09.apl: -------------------------------------------------------------------------------- 1 | N ← ⍎¨','(≠⊆⊢)⊃⊃⎕NGET'in/09.txt'1 2 | 3 | vm ← ⎕FIX'vm.apl' 4 | 5 | ⎕← 3⊃1 vm.run N 6 | ⎕← 3⊃2 vm.run N 7 | -------------------------------------------------------------------------------- /2019/10.apl: -------------------------------------------------------------------------------- 1 | n ← 1 0J1+.×⍨↑⍸m←'#'=↑⊃⎕NGET'in/10.txt'1 2 | 3 | ⎕← ⌈/a←{≢∪12○⍵~0}⍤1⊢∘.-⍨n 4 | ⎕← 100⊥11 9○199⊃∪∊⍉↑{⍵[⍒⍵;1]}{(12○⍵-q){⍺⍵}⌸⍵}n[⍋10○n-q←n[⊃⍒a]] 5 | -------------------------------------------------------------------------------- /2019/11.apl: -------------------------------------------------------------------------------- 1 | N ← ⍎¨','(≠⊆⊢)⊃⊃⎕NGET'in/11.txt'1 2 | 3 | vm ← ⎕FIX'vm.apl' 4 | 5 | run ← { 6 | pos dir ← ⍵ 7 | m∘← m vm.resume⍨ pos∊⍺ 8 | seen,←pos 9 | 0=⊃m: ⍺ 10 | col turn ← 3⊃m 11 | dir←turn⊃dir(×,÷)0j1 12 | col: (⍺,pos)∇(pos+dir),dir 13 | (⍺~pos)∇(pos+dir),dir 14 | } 15 | 16 | seen ← ⍬ 17 | 18 | m ← vm.run N 19 | _ ← ⍬ run 0 ¯1 20 | 21 | ⎕← ≢∪seen 22 | 23 | m ← vm.run N 24 | p ← 0 run 0 ¯1 25 | 26 | p-← ⌊/p←↓⍉9 11∘.○p 27 | ⎕← ' #'[1@p⊢0⍴⍨1+⊃⌈/p] 28 | -------------------------------------------------------------------------------- /2019/12.apl: -------------------------------------------------------------------------------- 1 | p ← (⍎¨∊∘('-',⎕D)⊆⊢)¨⊃⎕NGET'in/12.txt'1 2 | step ← {p v←⍵ ⋄ v+←+⌿×∘.-⍨p ⋄ (p+v)v} 3 | 4 | ⎕← +⌿×⌿+/↑↑|step⍣1000⊢ p 0 5 | 6 | a←1 4 3↑0 ⋄ {a⍪←0=↑1⊃⍵ ⋄ step⍵}⍣15e4⊢ step p 0 7 | ⎕← 2×∧/1⍳⍨¨↓×/1 2 0⍉a 8 | -------------------------------------------------------------------------------- /2019/13.apl: -------------------------------------------------------------------------------- 1 | N ← ⍎¨','(≠⊆⊢)⊃⊃⎕NGET'in/13.txt'1 2 | 3 | vm ← ⎕FIX'vm.apl' 4 | 5 | ⎕← 2+.=⊢/{⍵⍴⍨3,⍨(⍴⍵)÷3}3⊃vm.run N 6 | 7 | ⎕← (24 44⍴0) { 8 | map ← ⍺ 9 | score ← ∊{x y b←⍵ ⋄ x=¯1:b ⋄ ⍬⊣map[y;x]←b}¨↓{⍵⍴⍨3,⍨(⍴⍵)÷3} 3⊃⍵ 10 | ⍝ ⎕← ' ⎕*-o'[map]⊣⎕DL÷10 ⍝ show the map 11 | 0=⊃⍵: score 12 | map∇⍵vm.resume⍨×-/{0 1⊃⍸⍵=map}¨ 4 3 13 | } vm.run 2@0⊢N 14 | -------------------------------------------------------------------------------- /2019/14.apl: -------------------------------------------------------------------------------- 1 | 'bsearch'⎕CY'dfns' 2 | 3 | n r ← ↓⍉↑{(⍎¨'\d+'⎕S'&'⊢⍵)('[A-Z]+'⎕S'&'⊢⍵)}¨⊃⎕NGET'in/14.txt'1 4 | e ← (⊂'ORE'),⊃∘⌽¨r 5 | N ← 9e99,⊢/¨n 6 | M ← 0⍪↑n{ (¯1↓⍺)@(e⍳¯1↓⍵)⊣0×N }¨r 7 | ore ← {⊃{ (c+.×M)+⍵-N×c←⌈⍵÷N }⍣≡ ⍵×e∊⊂'FUEL'} 8 | ⎕← ore 1 9 | ⎕← ¯1+{1e12≤ore⍵} bsearch 1,1e12 10 | -------------------------------------------------------------------------------- /2019/15.apl: -------------------------------------------------------------------------------- 1 | N ← ⍎¨','(≠⊆⊢)⊃⊃⎕NGET'in/15.txt'1 2 | 3 | vm ← ⎕FIX'vm.apl' 4 | 5 | m ← vm.run N 6 | 7 | dir ← ¯1 1 0J1 0J¯1 8 | H2O ← map ← 0 9 | 10 | step ← { 11 | m ← ⍺ 12 | depth pos ← ⍵ 13 | d ← (1+⍳4)/⍨~(pos+dir)∊map 14 | 0=≢d: ⍬ 15 | ∊{ 16 | m ← ⍵vm.resume m 17 | s ← 3⊃m 18 | 0=s: ⍬ 19 | map∪← pos 20 | 2=s: depth+1⊣H2O∘←pos 21 | nxt ← pos+dir[⊃⍵-1] 22 | m step (depth+1)(nxt) 23 | }¨d 24 | } 25 | 26 | ⎕← m step 0 0 27 | 28 | count ← 1 29 | ⎕← count⊣{map∩⍵,∊dir∘.+⍵⊣count+←1}⍣≡H2O 30 | -------------------------------------------------------------------------------- /2019/16.apl: -------------------------------------------------------------------------------- 1 | n ← ⎕D⍳⊃⊃⎕NGET'in/16.txt'1 2 | p ← ↑{1↓N⍴⍵/0 1 0 ¯1}¨1↓⍳N←1+≢n 3 | 4 | ⎕← 10⊥8↑{10⊤|p+.×⍵}⍣100⊢n 5 | ⎕← 10⊥8↑⌽{10⊤+\⍵}⍣100⌽(10⊥7↑n)↓n⍴⍨1e4×≢n 6 | -------------------------------------------------------------------------------- /2019/17.apl: -------------------------------------------------------------------------------- 1 | N ← ⍎¨','(≠⊆⊢)⊃⊃⎕NGET'in/17.txt'1 2 | 3 | vm ← ⎕FIX'vm.apl' 4 | 5 | d ← '.#^v<>'⍳⎕UCS↑10(≠⊆⊢)3⊃vm.run N 6 | ⎕← +/×/↑⍸{4=+/∊×⍵[1;1]×∊⍵×3 3⍴0 1}⌺3 3⊢d 7 | 8 | ⍝ show map, solve part 2 by hand 9 | ⍝ ⎕← '.#^v<>'[d] 10 | 11 | a ← ⎕UCS'R,6,L,10,R,8' 12 | b ← ⎕UCS'R,8,R,12,L,8,L,8' 13 | c ← ⎕UCS'L,10,R,6,R,6,L,8' 14 | m ← ⎕UCS'A,B,A,B,C,A,B,C,A,C' 15 | i ← ∊m a b c (⎕UCS'n'),¨10 16 | ⎕← ⊢/3⊃i vm.run 2@0⊢N 17 | -------------------------------------------------------------------------------- /2019/19.apl: -------------------------------------------------------------------------------- 1 | N ← ⍎¨','(≠⊆⊢)⊃⊃⎕NGET'in/19.txt'1 2 | vm ← ⎕FIX'vm.apl' ⋄ check ← ⊃3⊃vm.run∘N 3 | 4 | ⎕← +/∊check¨⍳50 50 5 | 6 | ⎕← { 7 | 0=check⍵: ∇⍵+0 1 8 | 0=check⍵+¯99 99: ∇⍵+1 0 9 | 10000⊥⍵-99 0 10 | } 500 500 11 | -------------------------------------------------------------------------------- /2019/Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # compiler 4 | gem 'parser' 5 | gem 'rake' 6 | gem 'minitest' 7 | gem 'simplecov' 8 | 9 | # answers 10 | gem 'curses' 11 | -------------------------------------------------------------------------------- /2019/Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | ast (2.4.0) 5 | curses (1.3.1) 6 | docile (1.3.2) 7 | json (2.3.0) 8 | minitest (5.13.0) 9 | parser (2.6.5.0) 10 | ast (~> 2.4.0) 11 | rake (13.0.1) 12 | simplecov (0.17.1) 13 | docile (~> 1.1) 14 | json (>= 1.8, < 3) 15 | simplecov-html (~> 0.10.0) 16 | simplecov-html (0.10.2) 17 | 18 | PLATFORMS 19 | ruby 20 | 21 | DEPENDENCIES 22 | curses 23 | minitest 24 | parser 25 | rake 26 | simplecov 27 | 28 | BUNDLED WITH 29 | 2.0.2 30 | -------------------------------------------------------------------------------- /2019/README.md: -------------------------------------------------------------------------------- 1 | # AoC 2019 2 | 3 | Done in Ruby, 100% complete. APL added in 2024. 4 | 5 | Of interest are the following files: 6 | 7 | - `vm.apl` - runs Intcode 8 | - `vm.rb` - runs Intcode. Can also decompile Intcode to C-like language using `rake decompile` 9 | - `compiler/` - compiles a subset of Ruby into Intcode. Use `rake compile` 10 | -------------------------------------------------------------------------------- /2019/_01.rb: -------------------------------------------------------------------------------- 1 | def day1_1(inp) 2 | puts inp.lines.map {|l| (l.strip.to_i/3).floor-2}.sum 3 | end 4 | 5 | day1_1("""12 6 | 14 7 | 1969 8 | 100756""") # should be 34241 9 | 10 | day1_1(File.read('in/01.txt')) 11 | 12 | 13 | def day1_2(inp) 14 | puts inp.lines.map do |l| 15 | s=l.strip.to_i 16 | t=0 17 | while s>0 18 | s=(s/3).floor-2 19 | t+=s if s>0 20 | end 21 | t 22 | end.sum 23 | end 24 | 25 | day1_2("""100756""") # should be 50346 26 | 27 | day1_2(File.read('in/01.txt')) 28 | -------------------------------------------------------------------------------- /2019/_06.rb: -------------------------------------------------------------------------------- 1 | def day6_1(inp) 2 | pa={} 3 | inp.lines {|l| a,b=l.strip.split(')'); raise '2 parents' if pa[b];pa[b]=a} 4 | de=Hash.new { |de, n| pn = pa[n]; de[n] = pn ? de[pn]+1 : 0 } 5 | pa.keys.each{|n|de[n]} 6 | de.values.sum 7 | end 8 | 9 | day6_1("""COM)B 10 | B)C 11 | C)D 12 | D)E 13 | E)F 14 | B)G 15 | G)H 16 | D)I 17 | E)J 18 | J)K 19 | K)L""") 20 | 21 | puts day6_1(File.read('in/06.txt')) 22 | 23 | 24 | def day6_2(inp) 25 | pa={} 26 | inp.lines {|l| a,b=l.strip.split(')'); raise '2 parents' if pa[b];pa[b]=a} 27 | de=Hash.new { |de, n| pn = pa[n]; de[n] = pn ? de[pn]+1 : 0 } 28 | def pas(n, pa) 29 | pa[n] ? [pa[n]] + pas(pa[n], pa) : [] 30 | end 31 | pa.keys.each{|n|de[n]} 32 | cp = (pas('SAN', pa) & pas('YOU', pa)).first 33 | de['SAN'] + de['YOU'] -2 - de[cp] * 2 34 | end 35 | 36 | day6_2("""COM)B 37 | B)C 38 | C)D 39 | D)E 40 | E)F 41 | B)G 42 | G)H 43 | D)I 44 | E)J 45 | J)K 46 | K)L 47 | K)YOU 48 | I)SAN""") 49 | 50 | puts day6_2(File.read('in/06.txt')) 51 | -------------------------------------------------------------------------------- /2019/_08.rb: -------------------------------------------------------------------------------- 1 | def day8_1(inp, w, h) 2 | layers = inp.strip.split('').each_slice(w*h).to_a 3 | layer = layers.min_by {|l| l.count('0')} 4 | layer.count('1') * layer.count('2') 5 | end 6 | 7 | day8_1("""123456789012""", 3, 2) 8 | 9 | day8_1(File.read('in/08.txt'), 25, 6) 10 | 11 | 12 | def day8_2(inp, w, h) 13 | layers = inp.strip.split('').each_slice(w*h).to_a 14 | r=('2'*(w*h)).split('').zip(*layers).map {|pp| pp.find{|p|p=='0'||p=='1'}} 15 | r.each_slice(w).map{|r|puts r.join('').tr('01',' X')};42 16 | end 17 | 18 | day8_2("""0222112222120000""", 2, 2) 19 | 20 | day8_2(File.read('in/08.txt'), 25, 6) 21 | -------------------------------------------------------------------------------- /2019/_09.rb: -------------------------------------------------------------------------------- 1 | require './vm' 2 | 3 | def day9_1(inp, input) 4 | VM.new(inp).run_to_end(input) 5 | end 6 | 7 | day9_1("109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99",[]) # should print self 8 | day9_1("1102,34915192,34915192,7,4,7,99,0",[]) # should be huge number 9 | day9_1("104,1125899906842624,99",[]) # should be huge number 10 | 11 | puts day9_1(File.read('in/09.txt'),[1]) 12 | 13 | 14 | 15 | def day9_2(inp, input) 16 | VM.new(inp).run_to_end(input) 17 | end 18 | 19 | 20 | puts day9_2(File.read('in/09.txt'), [2]) 21 | -------------------------------------------------------------------------------- /2019/_19.rb: -------------------------------------------------------------------------------- 1 | require './vm' 2 | 3 | def day19_1(inp) 4 | total = 0 5 | 50.times do |y| 6 | 50.times do |x| 7 | total+=1 if 1==VM.new(inp).run([x,y]) 8 | end 9 | end 10 | puts total 11 | end 12 | 13 | day19_1(File.read('in/19.txt')) 14 | 15 | def day19_2(inp) 16 | x=500 17 | y=500 18 | loop do 19 | if 1==VM.new(inp).run([x,y]) 20 | if 1==VM.new(inp).run([x-99,y+99]) 21 | puts (x - 99) * 10_000 + y 22 | return 23 | else 24 | x+=1 25 | end 26 | else 27 | y+=1 28 | end 29 | end 30 | end 31 | 32 | day19_2(File.read('in/19.txt')) 33 | -------------------------------------------------------------------------------- /2019/_21.rb: -------------------------------------------------------------------------------- 1 | require './vm' 2 | 3 | def day21_1(inp) 4 | vm = VM.new(inp) 5 | # (!A | !C) & D 6 | res = < < ==` 15 | - Control structures: if/else, while/break/next, case/when/else 16 | - Functions: recursion is supported 17 | - IO: print/read 18 | 19 | See `samples/` for examples that are supported. -------------------------------------------------------------------------------- /2019/compiler/compiler.rb: -------------------------------------------------------------------------------- 1 | require_relative 'ir' 2 | require_relative 'code_generator' 3 | 4 | class Compiler 5 | def compile(code, file_name) 6 | ir = IR.new(code, file_name).to_ir 7 | ic = CodeGenerator.new(ir).generate 8 | return ir, ic 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /2019/compiler/samples/array.rb: -------------------------------------------------------------------------------- 1 | def x 2 | return 3 3 | end 4 | 5 | data = [0, 1, 0, -1] 6 | print '0' + data[3] 7 | 8 | memory = Array.new(1024) 9 | 10 | a=5 11 | b=6 12 | c=a+b 13 | 14 | memory[99] = x + c 15 | print '0' + memory[99] 16 | -------------------------------------------------------------------------------- /2019/compiler/samples/fib.rb: -------------------------------------------------------------------------------- 1 | def fib(n) 2 | if n < 1 3 | # fib(0) is 0 4 | return 0 5 | elsif n==1 6 | # fib(1) is 1 7 | return 1 8 | else 9 | return fib(n-1)+fib(n-2) 10 | end 11 | end 12 | 13 | print '0' + fib(3) 14 | -------------------------------------------------------------------------------- /2019/compiler/samples/math.rb: -------------------------------------------------------------------------------- 1 | x = 5 2 | y = 7 3 | z = x + y 4 | print('0' + z) -------------------------------------------------------------------------------- /2019/compiler/test/_base.rb: -------------------------------------------------------------------------------- 1 | require 'simplecov' # These two lines must go first 2 | SimpleCov.start do 3 | add_filter "/test/" 4 | add_filter "/answers/" 5 | end 6 | 7 | require "minitest/autorun" 8 | 9 | require 'vm' 10 | require 'compiler' 11 | 12 | class Base < Minitest::Test 13 | def setup 14 | @compiler = Compiler.new 15 | end 16 | 17 | def execute(code, input=[]) 18 | _, ic = @compiler.compile(code, "") 19 | VM.new(ic).run_to_end(input) 20 | end 21 | 22 | def debug(code) 23 | ir, ic = @compiler.compile(code, "") 24 | p ir 25 | p ic 26 | VM.new(ic).to_ruby 27 | end 28 | end -------------------------------------------------------------------------------- /2019/compiler/test/case.rb: -------------------------------------------------------------------------------- 1 | class TestCase < Base 2 | 3 | def test_when 4 | assert_equal [5], execute("case 5;when 5 then print 5; else print 1; print 4; end") 5 | end 6 | 7 | def test_2_whens 8 | assert_equal [5], execute("case 5;when 4 then print 4;when 5 then print 5; else print 1;end") 9 | end 10 | 11 | def test_else 12 | assert_equal [5], execute("case 5;when 1 then print 1; else print 5;end") 13 | end 14 | 15 | def test_no_else 16 | assert_equal [], execute("case 5;when 1 then print 1;end") 17 | end 18 | 19 | def test_func 20 | assert_equal [5], execute("case read; when 5 then print 5; else print 1;end", [5]) 21 | end 22 | 23 | def test_variable 24 | assert_equal [5], execute("x=5;case x;when 5 then print 5; else print 1;end") 25 | end 26 | 27 | end 28 | -------------------------------------------------------------------------------- /2019/compiler/test/if.rb: -------------------------------------------------------------------------------- 1 | class TestIf < Base 2 | 3 | def test_if_true 4 | assert_equal [5], execute("print 5 if true") 5 | end 6 | 7 | def test_if_false 8 | assert_equal [], execute("print 5 if false") 9 | end 10 | 11 | def test_if_lt 12 | assert_equal [5], execute("print 5 if 1<5 ; print 9 if 5<1") 13 | end 14 | 15 | def test_if_gt 16 | assert_equal [5], execute("print 5 if 5>1 ; print 9 if 1>5") 17 | end 18 | 19 | def test_if_eq 20 | assert_equal [5], execute("print 5 if 5==5 ; print 9 if 1==5") 21 | end 22 | 23 | def test_if_eq_vars 24 | assert_equal [5], execute("x=5;print 5 if x==read ; print 9 if read==x", [5,1]) 25 | end 26 | 27 | def test_if_else 28 | assert_equal [5,5], execute(<<-END) 29 | x=5 30 | if x>4 31 | print 5 32 | else 33 | print 1 34 | end 35 | if x<4 36 | print 1 37 | else 38 | print 5 39 | end 40 | END 41 | end 42 | 43 | end 44 | -------------------------------------------------------------------------------- /2019/compiler/test/string.rb: -------------------------------------------------------------------------------- 1 | class TestStrings < Base 2 | 3 | def test_char 4 | assert_equal [120], execute("a='x';print a") 5 | assert_equal [120], execute("a='z';print a-2") 6 | end 7 | 8 | def test_strings 9 | assert_equal [120], execute('a="hello world\n";print a;print "hi again"') 10 | end 11 | 12 | end 13 | -------------------------------------------------------------------------------- /2019/in/02.txt: -------------------------------------------------------------------------------- 1 | 1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,13,1,19,1,9,19,23,2,23,13,27,1,27,9,31,2,31,6,35,1,5,35,39,1,10,39,43,2,43,6,47,1,10,47,51,2,6,51,55,1,5,55,59,1,59,9,63,1,13,63,67,2,6,67,71,1,5,71,75,2,6,75,79,2,79,6,83,1,13,83,87,1,9,87,91,1,9,91,95,1,5,95,99,1,5,99,103,2,13,103,107,1,6,107,111,1,9,111,115,2,6,115,119,1,13,119,123,1,123,6,127,1,127,5,131,2,10,131,135,2,135,10,139,1,13,139,143,1,10,143,147,1,2,147,151,1,6,151,0,99,2,14,0,0 2 | -------------------------------------------------------------------------------- /2019/in/03e.txt: -------------------------------------------------------------------------------- 1 | R8,U5,L5,D3 2 | U7,R6,D4,L4 -------------------------------------------------------------------------------- /2019/in/04.txt: -------------------------------------------------------------------------------- 1 | 136760-595730 2 | -------------------------------------------------------------------------------- /2019/in/06e.txt: -------------------------------------------------------------------------------- 1 | COM)B 2 | B)C 3 | C)D 4 | D)E 5 | E)F 6 | B)G 7 | G)H 8 | D)I 9 | E)J 10 | J)K 11 | K)L 12 | K)YOU 13 | I)SAN -------------------------------------------------------------------------------- /2019/in/10e.txt: -------------------------------------------------------------------------------- 1 | .#....#####...#.. 2 | ##...##.#####..## 3 | ##...#...#.#####. 4 | ..#.....#...###.. 5 | ..#.#.....#....## -------------------------------------------------------------------------------- /2019/in/12.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /2019/in/12e.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /2019/in/14e.txt: -------------------------------------------------------------------------------- 1 | 10 ORE => 10 A 2 | 1 ORE => 1 B 3 | 7 A, 1 B => 1 C 4 | 7 A, 1 C => 1 D 5 | 7 A, 1 D => 1 E 6 | 7 A, 1 E => 1 FUEL -------------------------------------------------------------------------------- /2019/in/16.txt: -------------------------------------------------------------------------------- 1 | 59765216634952147735419588186168416807782379738264316903583191841332176615408501571822799985693486107923593120590306960233536388988005024546603711148197317530759761108192873368036493650979511670847453153419517502952341650871529652340965572616173116797325184487863348469473923502602634441664981644497228824291038379070674902022830063886132391030654984448597653164862228739130676400263409084489497532639289817792086185750575438406913771907404006452592544814929272796192646846314361074786728172308710864379023028807580948201199540396460310280533771566824603456581043215999473424395046570134221182852363891114374810263887875638355730605895695123598637121 2 | -------------------------------------------------------------------------------- /2019/in/24.txt: -------------------------------------------------------------------------------- 1 | .##.# 2 | ###.. 3 | #...# 4 | ##.#. 5 | .###. 6 | -------------------------------------------------------------------------------- /2020/01.apl: -------------------------------------------------------------------------------- 1 | n ← ⍎¨⊃⎕NGET'in/01.txt'1 2 | 3 | ⎕← ×/ n/⍨n ∊ 2020-n 4 | ⎕← ×/ n/⍨ ∨/(∘.+⍨n)∊2020-n 5 | -------------------------------------------------------------------------------- /2020/01.ijs: -------------------------------------------------------------------------------- 1 | load 'aoc.ijs' 2 | 3 | m =: ".'m'in'01' NB. parse 4 | */m#~m e.2020-m NB. part 1 5 | */m#~+./(+/~m)e.2020-m NB. part 2 6 | 7 | NB. Learnings 8 | NB. fread read file (if a is b/m then) 9 | NB. cutLF lines 10 | NB. x e.y x in y 11 | NB. ". eval (". '42' => 42) 12 | NB. #~ swap a b and tally b#a (1 0 1 # 9 42 77 => 9 77) 13 | NB. ~. uniq -------------------------------------------------------------------------------- /2020/01.rb: -------------------------------------------------------------------------------- 1 | def day1_1(inp) 2 | h={} 3 | inp.lines.each {|l| h[l.strip.to_i]=true} 4 | h.each_key { |k| return k*(2020-k) if h[2020-k] } 5 | end 6 | 7 | puts day1_1(File.read("in/01e.txt")) # should be 514579 8 | 9 | puts day1_1(File.read('in/01.txt')) 10 | 11 | 12 | def day1_2(inp) 13 | h={} 14 | inp.lines.each {|l| h[l.strip.to_i]=true} 15 | h.each_key { |a| h.each_key { |b| 16 | next if a==b || a+b>=2020 17 | return a*b*(2020-a-b) if h[2020-a-b] 18 | }} 19 | end 20 | 21 | puts day1_2(File.read("in/01e.txt")) # should be 241861950 22 | 23 | puts day1_2(File.read('in/01.txt')) 24 | -------------------------------------------------------------------------------- /2020/02.apl: -------------------------------------------------------------------------------- 1 | x y c w ← ↓⍉↑ ' -:'∘((~∊⍨)⊆⊢)¨⊃⎕NGET'in/02.txt'1 2 | x y ← ⍎¨¨x y 3 | 4 | ⎕← +/ (x∘≤ ∧ y∘≥) +/¨ e←w=¨c 5 | ⎕← +/ ⊃≠/ x y ∊¨¨ ⊂1+⍸¨e 6 | -------------------------------------------------------------------------------- /2020/02.ijs: -------------------------------------------------------------------------------- 1 | load 'aoc.ijs' 2 | 3 | 'x y' =: ".>0 2{'x d y c w' =: |:;:'m'in'02' NB. parse 4 | +/(x&<:*.y&>:)+/"1 w={."1 c NB. part 1 5 | +/~:/"1(<:x,.y){"1 w={."1 c NB. part 2 6 | 7 | NB. Learnings 8 | NB. ;:a split by words 9 | NB. |:a transpose 10 | NB. a{b lookup (0 2 { 42+i.5 => 42 44) 11 | NB. 'x y'=:z split rows into vars 12 | NB. {.a head 13 | NB. {."1 a head each 14 | NB. a<:b a <= b 15 | NB. a*.b and 16 | NB. a,.b join columns 17 | NB. <:a decrement 18 | NB. a~:b not equal -------------------------------------------------------------------------------- /2020/02.rb: -------------------------------------------------------------------------------- 1 | def day2_1(inp) 2 | inp.lines.sum {|l| 3 | lo,hi,r,_,p=l.strip.split(/[-: ]/) 4 | c=p.count(r) 5 | c>=lo.to_i && c<=hi.to_i ? 1 : 0 6 | } 7 | end 8 | 9 | puts day2_1(File.read('in/02e.txt')) # should be 2 10 | puts day2_1(File.read('in/02.txt')) 11 | 12 | 13 | def day2_2(inp) 14 | inp.lines.sum {|l| 15 | lo,hi,r,_,p=l.strip.split(/[-: ]/) 16 | (p[lo.to_i-1]==r) ^ (p[hi.to_i-1]==r) ? 1 : 0 17 | } 18 | end 19 | 20 | puts day2_2(File.read('in/02e.txt')) # should be 1 21 | puts day2_2(File.read('in/02.txt')) 22 | -------------------------------------------------------------------------------- /2020/03.apl: -------------------------------------------------------------------------------- 1 | h w ← ⍴map ← '#'=↑⊃⎕NGET'in/03.txt'1 2 | 3 | go ← { +/map[(⍵×y),¨w|⍺×y←1↓⍳⌊h÷⍵] } 4 | ⎕← 3 go 1 5 | ⎕← ×/ go⌿ ⍎¨2 5⍴'1357111112' 6 | -------------------------------------------------------------------------------- /2020/03.ijs: -------------------------------------------------------------------------------- 1 | load 'aoc.ijs' 2 | 3 | m =: '#'='m'in'03' NB. parse 4 | 5 | go =: [ +/@:{~ $@[ <"1@|:@:| ] */ $@[ i.@>.@{.@:% ] 6 | 7 | 8 | m go 1 3 NB. part 1 9 | */_2 m&go\"."0'1113151721' NB. part 2 10 | 11 | 12 | NB. Learnings 13 | NB. f^:a: x call f(f(f(x))) until 0, return all 14 | NB. cut split by (default by blank) 15 | NB. ss search 16 | NB. a|b b mod a 17 | NB. a{b `b[x] for x in a` if a is array of coord boxes 18 | NB. _2 ]\ y group y into groups of 2 -------------------------------------------------------------------------------- /2020/04.apl: -------------------------------------------------------------------------------- 1 | passes ← {⊃,/' '∘(≠⊆⊢)¨⍵}¨ ((×≢¨)⊆⊢)⊃⎕NGET'in/04.txt'1 2 | 3 | ⎕← +/present←{7=≢'byr' 'iyr' 'eyr' 'hgt' 'hcl' 'ecl' 'pid' ∩ 3↑¨⍵}¨passes 4 | 5 | within ← (≤⍥⊃) ∧ (≥⍥(⊢/)) 6 | byr ← 1920 2002 within ⍎ 7 | iyr ← 2010 2020 within ⍎ 8 | eyr ← 2020 2030 within ⍎ 9 | hgt ← {'cm'≡u←¯2↑⍵: 150 193 within ⍎¯2↓⍵ ⋄ 'in'≡u: 59 76 within ⍎¯2↓⍵ ⋄ 0} 10 | hcl ← 'ok' ≡ ('^#[0-9a-f]{6}$'⎕R'ok') 11 | ecl ← 'amb' 'blu' 'brn' 'gry' 'grn' 'hzl' 'oth' ∊⍨ ⊂ 12 | pid ← 'ok' ≡ ('^[0-9]{9}$'⎕R'ok') 13 | cid ← 1⍨ 14 | 15 | ⎕← +/(∧/⍎¨)¨(':(.*)'⎕R' ''\1''')¨¨present/passes 16 | -------------------------------------------------------------------------------- /2020/05.apl: -------------------------------------------------------------------------------- 1 | n ← 2⊥'BR'∊⍨ ⍉↑⊃⎕NGET'in/05.txt'1 2 | ⎕← max←⌈/n 3 | ⎕← 1+n~max,n-1 4 | -------------------------------------------------------------------------------- /2020/05.ijs: -------------------------------------------------------------------------------- 1 | load 'aoc.ijs' 2 | 3 | #.'BR'e.~'m'in'05e' NB. test: should be 567 119 820 4 | 5 | n =: #.'BR'e.~'m'in'05' NB. load 6 | >./n NB. part 1 7 | {.n-.~1+n NB. part 2 -------------------------------------------------------------------------------- /2020/06.apl: -------------------------------------------------------------------------------- 1 | f ← ((×≢¨)⊆⊢) ⊃⎕NGET'in/06.txt'1 2 | 3 | ⎕← +/{≢⊃∪/⍵}¨f 4 | ⎕← +/{≢⊃∩/⍵}¨f 5 | -------------------------------------------------------------------------------- /2020/06.ijs: -------------------------------------------------------------------------------- 1 | load 'aoc.ijs' 2 | 3 | n =: cutpara in'06' NB. load 4 | i =: dyad : '(>x) -. (>x) -. >y' 5 | ws =: LF,' ' 6 | 7 | +/ (ws #@-.~ ~.) &> n NB. part 1 8 | +/ (ws #@-.~ >@(i/)@cut) &> n NB. part 2 -------------------------------------------------------------------------------- /2020/07.apl: -------------------------------------------------------------------------------- 1 | 'span'⎕CY'dfns' 2 | 3 | no← 0 4 | f ← ' '∘(≠⊆⊢)¨⊃⎕NGET'in/07.txt'1 5 | name wght graph ← ↓⍉↑{(⊃,/⍵[⍳2])(⍎¨r/⍨(≢r)⍴4↑1)((⊃,/)¨r⊆⍨(≢r←4↓⍵)⍴4↑0 1 1)}¨f 6 | graph ← {(name⍳⍵)~≢name}¨graph 7 | gold ← name⍳⊂'shinygold' 8 | upgraph ← ⍸¨↓⍉↑{⍸⍣¯1⊢⍵[⍋⍵]}¨graph 9 | 10 | ⎕← +/ 1< upgraph span gold 11 | ⎕← ¯1+ {⍬≡nxt←⍵⊃graph: 1 ⋄ 1++/(⍵⊃wght)×∇¨nxt} gold 12 | -------------------------------------------------------------------------------- /2020/07.ijs: -------------------------------------------------------------------------------- 1 | load 'aoc.ijs' 2 | 3 | n =: in'07' NB. load 4 | V =: a: , ([: < [ {.~ 1 { [: I. ' ' = ]) ;. _2 n NB. vertices 5 | 6 | get =: ([: ". 2 {. ]) , V i. [: < 2 }. ] 7 | 'W G' =: ({."1 ; {:"1) 0 , ([: get@> '\d+ \w+ \w+' rxall ]) ;. _2 n 8 | gold =: <'shiny gold' 9 | 10 | NB. up ys ::: goes up the bags and adds any that are reachable 11 | up =: +. G +./"1@e.~"1 I. 12 | 13 | <:+/ up^:_ gold=V NB. part 1 14 | 15 | NB. down y ::: recursively finds number of bags that y{V can contains (excluding self) 16 | down =: monad : 0 M. 17 | vs =. 0 -.~ y{G 18 | ws =. 0 -.~ y{W 19 | if. 0=#vs do. 0 20 | else. 21 | bags =. >:down"0 vs 22 | +/bags*ws 23 | end. 24 | ) 25 | 26 | down V i. gold NB. part 2 -------------------------------------------------------------------------------- /2020/08.apl: -------------------------------------------------------------------------------- 1 | f ← ⊃⎕NGET'in/08.txt'1 2 | ip←val←0 3 | acc←{val+←⍵ ⋄ ip+←1} ⋄ nop←{ip+←1} ⋄ jmp←{ip+←⍵} 4 | run ← {p←ip ⋄ p≥≢f:1 ⋄ p∊⍵:0 ⋄ _←⍎p⊃f ⋄ ∇⍵,p} 5 | ⎕← val⊣run⍬ 6 | 7 | acc←{val+←⍵ ⋄ ip+←1} ⋄ nop←{ip+←1 ⍵[ip=swap]} ⋄ jmp←{ip+←⍵ 1[ip=swap]} 8 | swap ← 0 9 | ⎕← val⊣1+⍣{swap+←1 ⋄ ip×←val×←0 ⋄ run⍬}0 10 | -------------------------------------------------------------------------------- /2020/08.ijs: -------------------------------------------------------------------------------- 1 | load 'aoc.ijs' 2 | 3 | 'acc jmp nop'=:0 1 _1 4 | C =: ((3 ".@{. ]) , 4 ".@}. ])&>'b'in'08' NB. load 5 | 6 | run =: 3 : 0"0 7 | 'pc acc' =. 0 0 8 | visited =. (#C) $ 0 9 | 10 | while. 1 do. 11 | if. pc >: #C do. break. 12 | elseif. pc{visited do. break. 13 | else. visited =. 1 pc}visited 14 | 'op arg' =. pc { C 15 | if. pc=y do. op =. -op end. 16 | select. op 17 | case. _1 do. pc =. pc + 1 18 | case. 0 do. pc =. pc+1 [ acc =. acc + arg 19 | case. 1 do. pc =. pc+arg end. 20 | end. 21 | end. 22 | acc,pc=#C 23 | ) 24 | 25 | {. run _ NB. part 1 26 | 27 | {. 3 : 0'' NB. part 2 28 | for_i. I. -. 0= 0{"1 C do. NB. select indices of jmps and nops 29 | 'acc ok' =. run i 30 | if. {: run i do. break. end. 31 | end. 32 | ) -------------------------------------------------------------------------------- /2020/09.apl: -------------------------------------------------------------------------------- 1 | n ← ⍎¨⊃⎕NGET'in/09.txt'1 2 | p ← 25 3 | ⎕← r←{⍵/⍨0=≢¨g∩¨⍵-g←¯1↓p,/n} p↓n 4 | 5 | d t←1+⊃⍸r=∘.-⍨+\n 6 | ⎕← (⌊/+⌈/) t↓d↑n 7 | -------------------------------------------------------------------------------- /2020/09.ijs: -------------------------------------------------------------------------------- 1 | load 'aoc.ijs' 2 | 3 | n =: ".&>'b'in'09' NB. load 4 | P =: 6+20*1000=#n NB. partition size is 6 or 26 based on input size 5 | 6 | NB. find ::: ys y -> returns y if 2 numbers in ys sum to y, else 0 7 | find =: 0:`{: @. (2 > }: +/@e. {: - }:) 8 | 9 | [R =: {.0-.~P find\n NB. part 1 10 | 11 | NB. sum ::: ys -> returns 1 sum of min and max of a prefix of ys whose total sum is R 12 | sum =: (R = +/\.) +/@:* <./\. + >./\. 13 | 14 | +/sum\ n{.~n i.R NB. part 2 -------------------------------------------------------------------------------- /2020/10.apl: -------------------------------------------------------------------------------- 1 | n ← ⍎¨⊃⎕NGET'in/10.txt'1 2 | 3 | ⎕← ×/+/1 3∘.=n←3,2-/n[⍒n],0 4 | 5 | tri ← 1 1 2 4 7 13 24 ⍝ first few tribonacci numbers 6 | ⎕← ×/tri[≢¨n⊆⍨1=n] 7 | -------------------------------------------------------------------------------- /2020/10.ijs: -------------------------------------------------------------------------------- 1 | load 'aoc.ijs' 2 | 3 | n =: 3, 2-/\ \:~ 0, ".'m'in'10' NB. load, add leading 0, sort, do pair diffs, add trailing 3 4 | 5 | */ +/ n=/1 3 NB. part 1 - product of num of 1s and 3s 6 | 7 | tri =: 0 1 1 2 4 7 13 24 44 NB. first few tribonacci numbers 8 | 9 | */ tri{~ 2-~/\ I.3=n,3 NB. part 2 - product of tribonacci of num of contiguous 1s -------------------------------------------------------------------------------- /2020/11.apl: -------------------------------------------------------------------------------- 1 | m ← '.'≠↑⊃⎕NGET'in/11.txt'1 2 | 3 | ⎕← +/, {m×(1+4×⍵)>{+/,⍵}⌺3 3⊢⍵}⍣≡2=m 4 | 5 | C ← 0⍪{↑(1↓¨↓⍵)[⍋⍵]}⊃,⌸/↓⍉{⍵⍪⌽⍵}↑⊃,/⊃,/(+/i)(-/i)(⊣/i)(⊢/i){⊂2,/⍵}⌸¨⊂1+⍳≢i←↑⍸m 6 | ⎕← +/{(1+4×⍵)>1↓+/(0,⍵)[C]}⍣≡0/⍨≢i 7 | -------------------------------------------------------------------------------- /2020/11.ijs: -------------------------------------------------------------------------------- 1 | load 'aoc.ijs' 2 | 3 | n =: 'L'='m'in'11' NB. load 4 | 5 | M =: 0,.2,1,.4 5 # 2 1 NB. lookup matrix for part 1 6 | next =: M {~ ([: +/ 2 = ,) <@, (<1 1) { ] NB. compute 1s/2s for 3x3 matrix 7 | step =: (3 3) (next;._3) 0,~0, 0,.],.0: NB. split into 3x3, compute 8 | +/2=, step^:_ n NB. part 1 9 | 10 | 11 | 12 | ob =: /: ; ])@.(2=[)"0 vis&.|: + vis&.|. NB. compute 2s for next iteration 19 | step =: n*1 + next NB. find 2s, mask with floors 20 | +/2=, step^:_ n NB. part 2 -------------------------------------------------------------------------------- /2020/12.apl: -------------------------------------------------------------------------------- 1 | f ← ('([A-Z])'⎕R'\1 ')⊃⎕NGET'in/12.txt'1 2 | 3 | pos ← 0 ⋄ N ← {pos-←0j1×⍵} ⋄ W ← {pos-←⍵} ⋄ L ← {dir×←0j¯1*⍵÷90} 4 | dir ← 1 ⋄ S ← {pos+←0j1×⍵} ⋄ E ← {pos+←⍵} ⋄ R ← {dir×←0j1 *⍵÷90} 5 | F ← {pos+←dir×⍵} 6 | 7 | ⎕← +/|9 11○pos⊣⍎¨f 8 | 9 | pos ← 0 ⋄ N ← {dir-←0j1×⍵} ⋄ W ← {dir-←⍵} 10 | dir ← 10j¯1 ⋄ S ← {dir+←0j1×⍵} ⋄ E ← {dir+←⍵} 11 | 12 | ⎕← +/|9 11○pos⊣⍎¨f 13 | -------------------------------------------------------------------------------- /2020/12.ijs: -------------------------------------------------------------------------------- 1 | load 'aoc.ijs' 2 | 3 | n =: |. (".@}. ,~ 'NSEWLRF' i. {.)&>'b'in'12' NB. load 4 | 5 | NB. NSEWLRF ::: use value of x (op,val) to mutate y (pos,dir) 6 | N =: ] + 0 ,~ 0j1 * {:@[ 7 | S =: ] + 0 ,~ 0j_1 * {:@[ 8 | E =: ] + 0 ,~ 1 * {:@[ 9 | W =: ] + 0 ,~ _1 * {:@[ 10 | L =: ] * 1 , 0j1 ^ 90 %~ {:@[ 11 | R =: ] * 1 , 0j_1 ^ 90 %~ {:@[ 12 | F =: ] + 0 ,~ {:@] * {:@[ 13 | 14 | do =: N`S`E`W`L`R`F 15 | run =: do@.({.@[)/@, 16 | +/ (**) +. {. n run 0 1 NB. part 1 - sum of abs of real/imag of pos 17 | 18 | NB. re-declare for part 2 19 | N =: ] + 0 , 0j1 * {:@[ 20 | S =: ] + 0 , 0j_1 * {:@[ 21 | E =: ] + 0 , 1 * {:@[ 22 | W =: ] + 0 , _1 * {:@[ 23 | 24 | +/ (**) +. {. n run 0 10j1 NB. part 2 - sum of abs of real/imag of pos -------------------------------------------------------------------------------- /2020/13.apl: -------------------------------------------------------------------------------- 1 | start ← ⍎⊃start b ← ⊃⎕NGET'in/13.txt'1 2 | 3 | x ← ⍬ ⋄ bus ← ⍎b 4 | ⎕← bus[t⍳⌊/t] × ⌊/t ← (bus~0)|-start 5 | 6 | ⎕FR ← 1287 7 | x ← 0 ⋄ bus ← ⍎b 8 | ⍝ based on https://aplcart.info/?q=crt 9 | gcd←{0=⍵:1 0 ⋄ (⍵∇⍵|⍺)+.×0 1,⍪1,-⌊⍺÷⍵} 10 | crt←{m|⍵+.×⍺(⊣×⊢|∘⊃gcd)¨⍨⍺÷⍨m←×/⍺} 11 | ⎕← {⍵ crt ⍵-⍸×bus} bus~0 12 | -------------------------------------------------------------------------------- /2020/13.ijs: -------------------------------------------------------------------------------- 1 | load 'aoc.ijs' 2 | 3 | s =: ".{. 't b' =: ];._2 in'13' NB. load 4 | b =: 0 -.~ a =: ,".;._1 ',',b NB. load times (with and without x's) 5 | 6 | t =: b(| -) s NB. bus times modulo start time 7 | */ ((i. <./) t) { b,.t NB. part 1 8 | 9 | NB. Chinese Remainder calculator from 10 | NB. https://code.jsoftware.com/wiki/Essays/Chinese_Remainder_Theorem 11 | g0 =: , ,. =@i.@2: 12 | it =: {: ,: {. - {: * <.@%&{./ 13 | gcd =: (}.@{.) @ (it^:(*@{.@{:)^:_) @ g0 14 | 15 | assert=: 3 : 'assert. y' 16 | ab =: |.@(gcd/ * [ % +./)@(,&{.) 17 | cr1 =: [: |/\ *.&{. , ,&{: +/ .* ab 18 | chkc =: [: assert ,&{: -: ,&{. | {:@cr1 19 | cr =: cr1 [ chkc 20 | 21 | 22 | {: cr/ x:(* # ] ,. ] | ] - i.@#) a NB. part 2 - format into mb pairs -------------------------------------------------------------------------------- /2020/14.apl: -------------------------------------------------------------------------------- 1 | f ← {(~⍵∊' =[]')⊆⍵}¨⊃⎕NGET'in/14.txt'1 2 | 3 | mask ← { MZ∘←'0'=⊃⍵ ⋄ MO∘←'1'=⊃⍵ ⋄ 0 } 4 | mem ← { k v ← ⍎¨⍵ ⋄ M[k] ← 2⊥MO∨(~MZ)∧(36/2)⊤v } 5 | M←1e5/0 ⋄ {(⍎⊃⍵)1↓⍵}¨f ⋄ ⎕←+/M 6 | 7 | mask ← { MZ∘←'0'=⊃⍵ ⋄ MO∘←'1'=⊃⍵ ⋄ MF∘←2⊥f⍀(n/2)⊤⍳2*n←+/f←'X'=⊃⍵ ⋄ 0 } 8 | mem ← { k v ← ⍎¨⍵ ⋄ k←MF+2⊥(~MO⍱MZ)∧MO∨(36/2)⊤k ⋄ V,←v/⍨≢K,←k } 9 | K←V←⍬ ⋄ {(⍎⊃⍵)1↓⍵}¨f ⋄ ⎕←+/V/⍨⌽≠⌽K 10 | -------------------------------------------------------------------------------- /2020/14.ijs: -------------------------------------------------------------------------------- 1 | load 'aoc.ijs' 2 | 3 | mask =: 7&}. 4 | mem =: 2 5 ".&>@{ ;: 5 | n =: |. ''; (mask`mem@.('e' = 1&{))&.>'b'in'14' NB. load and parse 6 | 7 | NB. set ::: sets mem[y1] = y2 8 | set =: 3 : 0 9 | mem =: (] {~ [: I.@~: {."1) y,mem 10 | ) 11 | 12 | NB. eval ::: takes mask and addr,val and updates mem 13 | eval =: 4 : 0 14 | 'zs oz' =. '01' =/x 15 | 'addr val' =. y 16 | set addr, #. oz +. (-.zs) *. (36#2) #: val 17 | ) 18 | 19 | run =: 4 : 0 20 | mask =. >y 21 | x =. >x 22 | if. 2 = #x do. 23 | mask eval x 24 | y 25 | else. 26 | . i - last{mem 15 | mem =: i last} mem 16 | last =: next 17 | 18 | i =: i+1 19 | end. 20 | 21 | last 22 | ) 23 | 24 | solve 2020 NB. part 1 25 | solve 3e6 NB. part 2 (takes ~20s) -------------------------------------------------------------------------------- /2020/15.rb: -------------------------------------------------------------------------------- 1 | N = File.read("in/15.txt").split(',').map(&:to_i) 2 | 3 | def solve(n) 4 | last = N.last 5 | mem = Array.new(n) 6 | N[0...-1].each_with_index { |e, i| mem[e] = i.succ } 7 | (N.size...n).each do |i| 8 | mem[last], last = i, mem[last].nil? ? 0 : i - mem[last] 9 | end 10 | p last 11 | end 12 | 13 | solve 2020 14 | solve 30000000 -------------------------------------------------------------------------------- /2020/16.apl: -------------------------------------------------------------------------------- 1 | rules ticket nearby ← {0 1 1↓¨(×≢¨⍵)⊆⍵}⊃⎕NGET'in/16.txt'1 2 | ticket ← ⍎⊃ticket 3 | nearby ← ↑⍎¨nearby 4 | rules ← {∊{⍺+⍳1+⍵-⍺}/2 2⍴⍎¨(⍵∊⎕D)⊆⍵}¨rules 5 | 6 | ⎕← +/nearby[⍸0=valid←nearby∊∪∊rules] 7 | 8 | nearby ⌿⍨← ∧/valid ⍝ keep only valid examples 9 | matches ← ∧⌿¨⊂[⍳2]nearby ∘.∊ rules 10 | ⎕← ×/ticket[⍸∨⌿(sorted<6)⌿2<⌿0⍪↑matches[sorted←⍋+/¨matches]] 11 | -------------------------------------------------------------------------------- /2020/16.rb: -------------------------------------------------------------------------------- 1 | input = File.read("in/16.txt").split("\n\n") 2 | 3 | rules = input[0].lines.map do |l| 4 | m = l.split(?:) 5 | [m.first, m.last.scan(/(\d+)-(\d+)/).map{|(x,y)|x.to_i..y.to_i}] 6 | end.to_h 7 | 8 | mine = input[1].lines.last.split(?,).map(&:to_i) 9 | 10 | tickets = input[2].lines[1..].map {|l| l.split(',').map(&:to_i) } 11 | 12 | p tickets.flatten.select {|n| rules.all?{|(_,rs)|rs.none?{|r|r===n}}}.sum 13 | 14 | valid_tickets = tickets.reject {|t| t.any? {|n| rules.all?{|(_,rs)| rs.none?{|r|r===n}} }} 15 | 16 | pos = rules.map do |name, rs| 17 | [name, rules.count.times.select {|i| valid_tickets.all? {|t| rs.any? {|r| r===t[i] }}}] 18 | end.sort_by { |_, positions| positions.count } 19 | 20 | ix = pos.map do |(name, num)| 21 | num = num.first 22 | pos.each { |_, p| p.delete(num) } 23 | num if name.start_with?("departure") 24 | end.compact 25 | 26 | p mine.values_at(*ix).reduce(:*) -------------------------------------------------------------------------------- /2020/17.apl: -------------------------------------------------------------------------------- 1 | cube ← ↑'#'=⊃⎕NGET'in/17.txt'1 2 | life ← {3=n-⍵×4=n←{+/∊⍵}⌺(3/⍨≢⍴⍵)⊢⍵}⍣6 3 | 4 | ⍝ smallest values that work for my input 5 | ⎕← +/∊ life ¯13 ¯18 ¯19↑7 12 13↑(1 ,⍴cube)⍴cube 6 | ⎕← +/∊ life ¯13 ¯13 ¯17 ¯19↑7 7 11 13↑(1 1,⍴cube)⍴cube 7 | -------------------------------------------------------------------------------- /2020/17.ijs: -------------------------------------------------------------------------------- 1 | load 'aoc.ijs' 2 | 3 | n =: (, $~ 1 , $) '#'='m'in'17' NB. load 4 | 5 | wrap =: 0,~0,0,.~0,.0,"1~ 0,"1 ] NB. wrap array 6 | M =: 1(<4 1)}1(3)}82 2$0 NB. lookup matrix 7 | next =: M {~ +/@, <@, ] {~ <@:*@$ NB. compute for 3x3x3.. matrix 8 | step =: (3 $~ #@$) (next;._3) wrap&wrap NB. split into 3x3x3.. 9 | 10 | +/, step^:6 n NB. part 1 11 | 12 | wrap =: 0,~0, 0,.~0,. 0,"2~ 0(,"2) 0,"1~ 0,"1 ] NB. wrap for 4-rank array 13 | 14 | +/, step^:6 ,: n NB. part 2 -------------------------------------------------------------------------------- /2020/18.apl: -------------------------------------------------------------------------------- 1 | 'parse'⎕CY'max' 2 | 3 | E ← {'×'@(⍸w='*')⊢w←⍵/⍨⍵≠' '}¨⊃⎕NGET'in/18.txt'1 4 | ⎕← +/⍎¨'\(' '\)'⎕R(,¨')(')¨⌽¨E 5 | 6 | eval ← { 1=≢⍵: ⍵ ⋄ (⍎⊃⍵)/eval¨1↓⍵ } 7 | 8 | ⎕← +/{eval'×+'parse⍵}¨E 9 | -------------------------------------------------------------------------------- /2020/18.ijs: -------------------------------------------------------------------------------- 1 | load 'aoc.ijs' 2 | 3 | n =: 'b'in'18' NB. load 4 | 5 | eval =: '())(' ".@|.@charsub ] 6 | +/ eval &> n NB. part 1 - eval reversed tokens (with brackets flipped) 7 | 8 | NB. a/m ::: adds/multiplies, ensuring that + has higher precedence than * 9 | a =: }.@] ,~ [ + {.@] 10 | m =: , 11 | 12 | NB. simple ::: evaluates simple expression without parentheses 13 | simple =: '+a*m' */@".@charsub ] 14 | 15 | NB. level ::: finds, evaluates and replaces first complete sub-expression 16 | level =: 3 : 0 17 | if. '('e.y do. 18 | end =. >: y i.')' NB. find first closing bracket (end of first flat sub-expression) 19 | ss =. end {. y 20 | start =. ss i: '(' NB. start of that sub-expression 21 | (start&{. , (,8!:2 simple start }. ss) , end&}.) y 22 | else. 23 | y 24 | end. 25 | ) 26 | 27 | eval =: x:@simple level^:_ 28 | +/ eval &> n NB. part 2 -------------------------------------------------------------------------------- /2020/19.apl: -------------------------------------------------------------------------------- 1 | rules msg ← {(×≢¨⍵)⊆⍵}⊃⎕NGET'in/19.txt'1 2 | rules ← { 3 | t ← {⍵⊆⍨⍵≠' '}⍵~':"' 4 | r ← ⍎⊃t 5 | (1 0⊃t)∊'ab': r,1⊃t ⍝ rule a/b 6 | r,⍎¨¨{⍵⊆⍨⍵≢¨⊂,'|'}1↓t ⍝ rule A B | C D 7 | }¨rules 8 | rules ← (1↓¨rules)@i⊢0/⍨1+⌈/i←⊃¨rules ⍝ sort by rule ID 9 | 10 | f ← {1=≡v←⍵⊃rules:∊,v ⋄ ⊃rules[⍵]←⊂∊'(?:', (∊{⍺,'|',⍵}/∇¨¨v), ')'} 11 | 12 | ⎕← +/≢¨('^',(f 0),'$')⎕S''¨msg 13 | ⎕← +/≢¨('^',(f 42),'+(',(f 42),'(|(?1))',(f 31),')$')⎕S''¨msg 14 | -------------------------------------------------------------------------------- /2020/19.rb: -------------------------------------------------------------------------------- 1 | rls, input = File.read("in/19.txt").split("\n\n") 2 | 3 | rls = rls.lines.map do |line| 4 | line = line.chomp.gsub(': ', '=>[[') + ']]' 5 | line.gsub(' | ', '],[').gsub(' ', ',') 6 | end.join(',') 7 | 8 | $rules = eval("{ #{rls} }") 9 | 10 | def mkregex 11 | Hash.new do |h,k| 12 | rule = $rules[k].map { |subrule| 13 | subrule.map { |subsubrule| 14 | String === subsubrule ? subsubrule : h[subsubrule] 15 | }.join 16 | }.then { |res| res.length == 1 ? res.first : "(#{res.join('|')})" } 17 | 18 | h[k] = rule 19 | end 20 | end 21 | 22 | rls = mkregex 23 | 24 | regex = Regexp.new("^#{rls[0]}$") 25 | 26 | p input.lines.grep(regex).count 27 | 28 | 29 | rls = mkregex 30 | rls[8] = "(#{rls[42]})+" 31 | rls[11] = "(?#{rls[42]}\\g?#{rls[31]})" 32 | 33 | regex = Regexp.new("^#{rls[0]}$") 34 | 35 | p input.lines.grep(regex).count -------------------------------------------------------------------------------- /2020/20.ijs: -------------------------------------------------------------------------------- 1 | load 'aoc.ijs' 2 | 3 | parse =: 3 : 0 4 | 't m' =. ':'cut>y 5 | t =. ".>{:;:t 6 | m =. }.'#'&=;._2 m,LF 7 | ({. #.@, {: , {."1 ,: {:"1) m 8 | ) 9 | [n =: parse &> LF2 splitstring in'20e' NB. load 10 | 11 | intersect =: e. # [ 12 | 13 | #@intersect/~"1 n 14 | 15 | NB. FAIL, done in ruby -------------------------------------------------------------------------------- /2020/21.rb: -------------------------------------------------------------------------------- 1 | A = {} 2 | I = Hash.new { |hsh, key| hsh[key] = 0 } 3 | 4 | File.read("in/21.txt").lines.each do |l| 5 | w=l.scan(/(\w+)/).flatten 6 | ix=w.index('contains') 7 | is,as = w[0...ix],w[ix.succ..] 8 | is.each { |i| I[i] += 1 } 9 | as.each { |a| A[a] = (A[a] ||= is) & is } 10 | end 11 | 12 | good = I.keys - A.values.flatten.uniq 13 | p good.inject(0) { |mem, i| mem + I[i] } 14 | 15 | while A.values.any? { |is| is.size > 1 } 16 | isolated = A.values.select { |is| is.size == 1 }.flatten 17 | A.each { |_,is| isolated.each { |i| is.delete(i) if is.size>1 } } 18 | end 19 | 20 | puts A.map { |k,v| [k,v.first] }.sort_by(&:first).map(&:last).join(?,) -------------------------------------------------------------------------------- /2020/24.apl: -------------------------------------------------------------------------------- 1 | dirs ← 'nw' (,'w') 'sw' 'ne' (,'e') 'se' 2 | pos ← ¯1j¯1 ¯2 ¯1j1 1j¯1 2 1j1 3 | tiles ← {+/pos[dirs⍳('(n|s)?(e|w)'⎕S'&')⍵]}¨⊃⎕NGET'in/24.txt'1 4 | tiles ← ∊{⊂(2|≢⍵)↑⊂⍺}⌸tiles 5 | 6 | ⎕← ≢ tiles 7 | ⎕← ≢ { t n ← ↓⍉{⍺,≢⍵}⌸∊pos+⊂⍵ ⋄ t/⍨(on∧n∊1 2)∨(~on←t∊⍵)∧n=2 }⍣100⊢tiles 8 | -------------------------------------------------------------------------------- /2020/25.apl: -------------------------------------------------------------------------------- 1 | d c ← ⍎¨⊃⎕NGET'in/25.txt'1 2 | 3 | loop ← 0 {⍵=d: ⍺ ⋄ (⍺+1) ∇ 20201227|7×⍵} 1 4 | ⎕← (20201227|c∘×)⍣loop⊢1 5 | -------------------------------------------------------------------------------- /2020/25.ijs: -------------------------------------------------------------------------------- 1 | load 'aoc.ijs' 2 | 3 | 'd c' =: ".&>'b'in'25' 4 | 5 | t =: <:# (20201227 | 7&*)^:(d&~:)^:a: 1 6 | 7 | (20201227 | c&*)^:t 1 -------------------------------------------------------------------------------- /2020/25.rb: -------------------------------------------------------------------------------- 1 | dp,cp = File.read("in/25.txt").split.map(&:to_i) 2 | 3 | def find_loop(s,t,v=1,l=0) 4 | until v==t 5 | l+=1; v = (v*s) % 20201227 6 | end 7 | l 8 | end 9 | 10 | def run(s,t,v=1) 11 | t.times { v = (v*s) % 20201227 } 12 | v 13 | end 14 | 15 | dl = find_loop(7,dp) 16 | 17 | p run(cp,dl) -------------------------------------------------------------------------------- /2020/README.md: -------------------------------------------------------------------------------- 1 | # AoC 2020 2 | 3 | First year using J, with Ruby as backup. APL added in 2023. 4 | 5 | J solves 67% of all puzzles, Ruby - 47%, APL - 84%. 6 | 7 | ## Day 11 8 | Part 1 simply uses 3x3 stencil and fixed point. Part 2 creates a connectivity matrix C where C[x;] are seats that the seat x is connected to. Seats get a 1-based sequence number in ravel order. For each seat location (i), their diagonal number(`(+/i)(-/i)`) as well as row and column are computed, and seats are grouped (`⌸`) on those. Along seats in each group, neighbour pairs are computes (`2,/`). Those neighbour pairs are made bi-directional and turned into an adjacency matrix in sequence order. After this, part 2 is simple with occupied seat mask and fixed point. 9 | -------------------------------------------------------------------------------- /2020/aoc.ijs: -------------------------------------------------------------------------------- 1 | in =: fread (''joinstring 'in/';;&'.txt') -------------------------------------------------------------------------------- /2020/in/01e.txt: -------------------------------------------------------------------------------- 1 | 1721 2 | 979 3 | 366 4 | 299 5 | 675 6 | 1456 7 | -------------------------------------------------------------------------------- /2020/in/02e.txt: -------------------------------------------------------------------------------- 1 | 1-3 a: abcde 2 | 1-3 b: cdefg 3 | 2-9 c: ccccccccc 4 | -------------------------------------------------------------------------------- /2020/in/03e.txt: -------------------------------------------------------------------------------- 1 | ..##....... 2 | #...#...#.. 3 | .#....#..#. 4 | ..#.#...#.# 5 | .#...##..#. 6 | ..#.##..... 7 | .#.#.#....# 8 | .#........# 9 | #.##...#... 10 | #...##....# 11 | .#..#...#.# -------------------------------------------------------------------------------- /2020/in/04e.txt: -------------------------------------------------------------------------------- 1 | iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884 2 | hcl:#cfa07d byr:1929 3 | 4 | hcl:#ae17e1 iyr:2013 5 | eyr:2024 6 | ecl:brn pid:760753108 byr:1931 7 | hgt:179cm 8 | 9 | hcl:#cfa07d eyr:2025 pid:166559648 10 | iyr:2011 ecl:brn hgt:59in 11 | 12 | eyr:1972 cid:100 13 | hcl:#18171d ecl:amb hgt:170 pid:186cm iyr:2018 byr:1926 14 | 15 | iyr:2019 16 | hcl:#602927 eyr:1967 hgt:170cm 17 | ecl:grn pid:012533040 byr:1946 18 | 19 | hcl:dab227 iyr:2012 20 | ecl:brn hgt:182cm pid:021572410 eyr:2020 byr:1992 cid:277 21 | 22 | hgt:59cm ecl:zzz 23 | eyr:2038 hcl:74454a iyr:2023 24 | pid:3556412378 byr:2007 25 | 26 | pid:087499704 hgt:74in ecl:grn iyr:2012 eyr:2030 byr:1980 27 | hcl:#623a2f 28 | 29 | eyr:2029 ecl:blu cid:129 byr:1989 30 | iyr:2014 pid:896056539 hcl:#a97842 hgt:165cm 31 | 32 | hcl:#888785 33 | hgt:164cm byr:2001 iyr:2015 cid:88 34 | pid:545766238 ecl:hzl 35 | eyr:2022 36 | 37 | iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719 38 | -------------------------------------------------------------------------------- /2020/in/05e.txt: -------------------------------------------------------------------------------- 1 | BFFFBBFRRR 2 | FFFBBBFRRR 3 | BBFFBBFRLL -------------------------------------------------------------------------------- /2020/in/06e.txt: -------------------------------------------------------------------------------- 1 | abc 2 | 3 | a 4 | b 5 | c 6 | 7 | ab 8 | ac 9 | 10 | a 11 | a 12 | a 13 | a 14 | 15 | b -------------------------------------------------------------------------------- /2020/in/07e.txt: -------------------------------------------------------------------------------- 1 | light red bags contain 1 bright white bag, 2 muted yellow bags. 2 | dark orange bags contain 3 bright white bags, 4 muted yellow bags. 3 | bright white bags contain 1 shiny gold bag. 4 | muted yellow bags contain 2 shiny gold bags, 9 faded blue bags. 5 | shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags. 6 | dark olive bags contain 3 faded blue bags, 4 dotted black bags. 7 | vibrant plum bags contain 5 faded blue bags, 6 dotted black bags. 8 | faded blue bags contain no other bags. 9 | dotted black bags contain no other bags. -------------------------------------------------------------------------------- /2020/in/08e.txt: -------------------------------------------------------------------------------- 1 | nop +0 2 | acc +1 3 | jmp +4 4 | acc +3 5 | jmp -3 6 | acc -99 7 | acc +1 8 | jmp -4 9 | acc +6 -------------------------------------------------------------------------------- /2020/in/09e.txt: -------------------------------------------------------------------------------- 1 | 35 2 | 20 3 | 15 4 | 25 5 | 47 6 | 40 7 | 62 8 | 55 9 | 65 10 | 95 11 | 102 12 | 117 13 | 150 14 | 182 15 | 127 16 | 219 17 | 299 18 | 277 19 | 309 20 | 576 -------------------------------------------------------------------------------- /2020/in/10.txt: -------------------------------------------------------------------------------- 1 | 99 2 | 128 3 | 154 4 | 160 5 | 61 6 | 107 7 | 75 8 | 38 9 | 15 10 | 11 11 | 129 12 | 94 13 | 157 14 | 84 15 | 121 16 | 14 17 | 119 18 | 48 19 | 30 20 | 10 21 | 55 22 | 108 23 | 74 24 | 104 25 | 91 26 | 45 27 | 134 28 | 109 29 | 164 30 | 66 31 | 146 32 | 44 33 | 116 34 | 89 35 | 79 36 | 32 37 | 149 38 | 1 39 | 136 40 | 58 41 | 96 42 | 7 43 | 60 44 | 23 45 | 31 46 | 3 47 | 65 48 | 110 49 | 90 50 | 37 51 | 43 52 | 115 53 | 122 54 | 52 55 | 113 56 | 123 57 | 161 58 | 50 59 | 95 60 | 150 61 | 120 62 | 101 63 | 126 64 | 151 65 | 114 66 | 127 67 | 73 68 | 82 69 | 162 70 | 140 71 | 51 72 | 144 73 | 36 74 | 4 75 | 163 76 | 85 77 | 42 78 | 59 79 | 67 80 | 64 81 | 86 82 | 49 83 | 2 84 | 145 85 | 135 86 | 22 87 | 24 88 | 33 89 | 137 90 | 16 91 | 27 92 | 70 93 | 133 94 | 130 95 | 20 96 | 21 97 | 83 98 | 143 99 | 100 100 | 41 101 | 76 102 | 17 -------------------------------------------------------------------------------- /2020/in/10e.txt: -------------------------------------------------------------------------------- 1 | 28 2 | 33 3 | 18 4 | 42 5 | 31 6 | 14 7 | 46 8 | 20 9 | 48 10 | 47 11 | 24 12 | 23 13 | 49 14 | 45 15 | 19 16 | 38 17 | 39 18 | 11 19 | 1 20 | 32 21 | 25 22 | 35 23 | 8 24 | 17 25 | 7 26 | 9 27 | 4 28 | 2 29 | 34 30 | 10 31 | 31 -------------------------------------------------------------------------------- /2020/in/11e.txt: -------------------------------------------------------------------------------- 1 | L.LL.LL.LL 2 | LLLLLLL.LL 3 | L.L.L..L.. 4 | LLLL.LL.LL 5 | L.LL.LL.LL 6 | L.LLLLL.LL 7 | ..L.L..... 8 | LLLLLLLLLL 9 | L.LLLLLL.L 10 | L.LLLLL.LL -------------------------------------------------------------------------------- /2020/in/12e.txt: -------------------------------------------------------------------------------- 1 | F10 2 | N3 3 | F7 4 | R90 5 | F11 -------------------------------------------------------------------------------- /2020/in/13.txt: -------------------------------------------------------------------------------- 1 | 1003055 2 | 37,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,x,433,x,x,x,x,x,x,x,23,x,x,x,x,x,x,x,x,17,x,19,x,x,x,x,x,x,x,x,x,29,x,593,x,x,x,x,x,x,x,x,x,x,x,x,13 -------------------------------------------------------------------------------- /2020/in/13e.txt: -------------------------------------------------------------------------------- 1 | 939 2 | 7,13,x,x,59,x,31,19 3 | -------------------------------------------------------------------------------- /2020/in/14e.txt: -------------------------------------------------------------------------------- 1 | mask = 000000000000000000000000000000X1001X 2 | mem[42] = 100 3 | mask = 00000000000000000000000000000000X0XX 4 | mem[26] = 1 -------------------------------------------------------------------------------- /2020/in/15.txt: -------------------------------------------------------------------------------- 1 | 15,12,0,14,3,1 -------------------------------------------------------------------------------- /2020/in/15e.txt: -------------------------------------------------------------------------------- 1 | 0,3,6 -------------------------------------------------------------------------------- /2020/in/16e.txt: -------------------------------------------------------------------------------- 1 | class: 0-1 or 4-19 2 | row: 0-5 or 8-19 3 | seat: 0-13 or 16-19 4 | 5 | your ticket: 6 | 11,12,13 7 | 8 | nearby tickets: 9 | 3,9,18 10 | 15,1,5 11 | 5,14,9 12 | 5,14,9 13 | 20,0,0 14 | 0,0,20 15 | 0,20,0 16 | 5,19,13 -------------------------------------------------------------------------------- /2020/in/17.txt: -------------------------------------------------------------------------------- 1 | .###..#. 2 | ##.##... 3 | ....#.#. 4 | #..#.### 5 | ...#...# 6 | ##.#...# 7 | #..##.## 8 | #....... -------------------------------------------------------------------------------- /2020/in/17e.txt: -------------------------------------------------------------------------------- 1 | .#. 2 | ..# 3 | ### -------------------------------------------------------------------------------- /2020/in/18e.txt: -------------------------------------------------------------------------------- 1 | 1 + 2 * 3 + 4 * 5 + 6 2 | 1 + (2 * 3) + (4 * (5 + 6)) 3 | 2 * 3 + (4 * 5) 4 | 5 + (8 * 3 + 9 + 3 * 4 * 3) 5 | 5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4)) 6 | ((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2 -------------------------------------------------------------------------------- /2020/in/19e.txt: -------------------------------------------------------------------------------- 1 | 0: 4 1 5 2 | 1: 2 3 | 3 2 3 | 2: 4 4 | 5 5 4 | 3: 4 5 | 5 4 5 | 4: "a" 6 | 5: "b" 7 | 8 | ababbb 9 | bababa 10 | abbbab 11 | aaabbb 12 | aaaabbb -------------------------------------------------------------------------------- /2020/in/19x.txt: -------------------------------------------------------------------------------- 1 | 0 -> 1 2 2 | 1 -> 3 N 3 | 1 -> 1 PP 4 | PP -> P 1 5 | 2 -> V 1 6 | 2 -> 2 PP 7 | 3 -> the 8 | 1 -> I 9 | N -> man 10 | N -> telescope 11 | P -> with 12 | V -> saw 13 | N -> cat 14 | N -> dog 15 | N -> pig 16 | N -> hill 17 | N -> park 18 | N -> roof 19 | P -> from 20 | P -> on 21 | P -> in 22 | -------------------------------------------------------------------------------- /2020/in/21e.txt: -------------------------------------------------------------------------------- 1 | mxmxvkd kfcds sqjhc nhms (contains dairy, fish) 2 | trh fvjkl sbzzf mxmxvkd (contains dairy) 3 | sqjhc fvjkl (contains soy) 4 | sqjhc mxmxvkd sbzzf (contains fish) -------------------------------------------------------------------------------- /2020/in/22.txt: -------------------------------------------------------------------------------- 1 | Player 1: 2 | 39 3 | 15 4 | 13 5 | 23 6 | 12 7 | 49 8 | 36 9 | 44 10 | 8 11 | 21 12 | 28 13 | 37 14 | 40 15 | 42 16 | 6 17 | 47 18 | 2 19 | 38 20 | 18 21 | 31 22 | 20 23 | 10 24 | 16 25 | 43 26 | 5 27 | 28 | Player 2: 29 | 29 30 | 26 31 | 19 32 | 35 33 | 34 34 | 4 35 | 41 36 | 11 37 | 3 38 | 50 39 | 33 40 | 22 41 | 48 42 | 7 43 | 17 44 | 32 45 | 27 46 | 45 47 | 46 48 | 9 49 | 25 50 | 30 51 | 1 52 | 24 53 | 14 -------------------------------------------------------------------------------- /2020/in/22e.txt: -------------------------------------------------------------------------------- 1 | Player 1: 2 | 9 3 | 2 4 | 6 5 | 3 6 | 1 7 | 8 | Player 2: 9 | 5 10 | 8 11 | 4 12 | 7 13 | 10 -------------------------------------------------------------------------------- /2020/in/23.txt: -------------------------------------------------------------------------------- 1 | 583976241 -------------------------------------------------------------------------------- /2020/in/23e.txt: -------------------------------------------------------------------------------- 1 | 389125467 -------------------------------------------------------------------------------- /2020/in/24e.txt: -------------------------------------------------------------------------------- 1 | sesenwnenenewseeswwswswwnenewsewsw 2 | neeenesenwnwwswnenewnwwsewnenwseswesw 3 | seswneswswsenwwnwse 4 | nwnwneseeswswnenewneswwnewseswneseene 5 | swweswneswnenwsewnwneneseenw 6 | eesenwseswswnenwswnwnwsewwnwsene 7 | sewnenenenesenwsewnenwwwse 8 | wenwwweseeeweswwwnwwe 9 | wsweesenenewnwwnwsenewsenwwsesesenwne 10 | neeswseenwwswnwswswnw 11 | nenwswwsewswnenenewsenwsenwnesesenew 12 | enewnwewneswsewnwswenweswnenwsenwsw 13 | sweneswneswneneenwnewenewwneswswnese 14 | swwesenesewenwneswnwwneseswwne 15 | enesenwswwswneneswsenwnewswseenwsese 16 | wnwnesenesenenwwnenwsewesewsesesew 17 | nenewswnwewswnenesenwnesewesw 18 | eneswnwswnwsenenwnwnwwseeswneewsenese 19 | neswnwewnwnwseenwseesewsenwsweewe 20 | wseweeenwnesenwwwswnew -------------------------------------------------------------------------------- /2020/in/25.txt: -------------------------------------------------------------------------------- 1 | 17115212 2 | 3667832 -------------------------------------------------------------------------------- /2020/in/25e.txt: -------------------------------------------------------------------------------- 1 | 17807724 2 | 5764801 -------------------------------------------------------------------------------- /2021/01.apl: -------------------------------------------------------------------------------- 1 | n ← ⍎¨ ⊃⎕NGET'in/01.txt'1 2 | 3 | +/ 2 window 9-f] ⍝ part 1 7 | 8 | ×/3↑ {⍵[⍒⍵]} {≢⍵}⌸ 0~⍨, {⌈/⍺,⍵××⍺} window⍣≡ (2+⍳≢lows)@lows ⊢ 9≠f 9 | ⍝ ^ multiply first three 10 | ⍝ ^ largest 11 | ⍝ ^ sizes of 12 | ⍝ ^ non-empty 13 | ⍝ ^ basins 14 | ⍝ filled-converged ^ 15 | ⍝ from numbered low points ^ 16 | ⍝ disregarding peaks ^ 17 | -------------------------------------------------------------------------------- /2021/10.apl: -------------------------------------------------------------------------------- 1 | f ← ⊃⎕NGET'in/10.txt'1 2 | 3 | open ← '([{<' 4 | close ← ')]}>' 5 | pairs ← open,¨close 6 | 7 | f ← {⍵/⍨ s⍱¯1⌽ s← +⌿↑ pairs ⍷¨ ⊂⍵}⍣≡¨ f ⍝ simplify 8 | 9 | +/3 57 1197 25137 0[i←{close⍳⊃⍵~open}¨f] 10 | (⊢⌷⍨⍋⌷⍨(⌊.5×≢)) {(⊣+5×⊢)/1+open⍳⍵}¨ f/⍨i=4 11 | -------------------------------------------------------------------------------- /2021/11.apl: -------------------------------------------------------------------------------- 1 | f ← ↑⍎¨¨⊃⎕NGET'in/11.txt'1 2 | 3 | step ← {0@(9∘<) ⍵+ ⍵{1+{+/,⍵}⌺3 3⊢9<⍺+⍵}⍣≡1} 4 | 5 | ⍝ pow :: A (A ∇ S -> A) ∇∇ (A ∇ S -> B) S => A where A=accumulator S=state B=bool 6 | pow ← {⍺ ⍵⍵ ⍵: ⍺ ⋄ (⍺∘⍺⍺ ∇ ⊢) step ⍵} 7 | 8 | +/ ⍬ {⍺,+/0=,⍵} pow {100=≢⍺} f 9 | 0 {⍺+1} pow {0∧.=,⍵} f 10 | -------------------------------------------------------------------------------- /2021/12.apl: -------------------------------------------------------------------------------- 1 | f ← ↑'-'(≠⊆⊢)¨⊃⎕NGET'in/12.txt'1 2 | 3 | small ← ⍸25=⎕A⍸nodes ← ∪'start' 'end',,f 4 | graph ← (0~⍨⍸)¨↓1@(↓nodes⍳f⍪⌽f)⊢{⍵ ⍵⍴0}≢nodes 5 | 6 | dfs ← { ⍝ count paths 7 | ⍺ ← 0 ⍝ initialize start node 8 | ⍺=1: 1 ⍝ return 1 on end 9 | ⍺ ⍺⍺ ⍵∩small: 0 ⍝ return 0 on stop condition 10 | +/ ∇∘(⍵,⍺)¨ ⍺⊃graph ⍝ sum paths to next nodes 11 | } 12 | 13 | ∊ dfs ⍬ 14 | (∊ ∧ ⊢≢∪⍤⊢) dfs ⍬ 15 | -------------------------------------------------------------------------------- /2021/13.apl: -------------------------------------------------------------------------------- 1 | pos xy ← (⊢⊆⍨0≠≢¨)⊃⎕NGET'in/13.txt'1 2 | board ← {1@⍵⊢(1+↑⌈/⍵)⍴0}⍎¨pos 3 | fold ← {(⌊.5×≢⍵) ↑ ⍵∨⊖⍵} 4 | foldxy ← {fold⍣⍵ ⍉ fold⍣⍺⊢board} 5 | 6 | +/,⊃foldxy/'xy'∊⊃xy 7 | ' █'[⊃foldxy/+/'xy'∘.∊xy] 8 | -------------------------------------------------------------------------------- /2021/14.apl: -------------------------------------------------------------------------------- 1 | f ← ⎕A∘⍳¨⊃⎕NGET'in/14.txt'1 2 | m ← ⊂26 26⍴0 ⋄ start ← 1@(2,/⊃f)⊃m 3 | rules ← ⊃{(¯1↑⍺)@(⊂2↑⍺)⊢⍵}/⌽m,2↓f 4 | step ← { 5 | w←⊃⍵ 6 | { 7 | (w[⊂⍺]+⊢)@(2,/2⌽rules[⊂⍺],⌽⍺)⊢⍵ 8 | }/⌽m,⍸×w 9 | } 10 | 11 | { (⌈/-⌊/) 0~⍨ ⌈.5×(+/ + +⌿)⊃ step⍣⍵⊂start }¨10 40 12 | -------------------------------------------------------------------------------- /2021/15.apl: -------------------------------------------------------------------------------- 1 | 'wpath' 'wcost'⎕CY'dfns' 2 | 3 | m ← ⍎¨↑⊃⎕NGET'in/15.txt'1 4 | 5 | risk ← { ⍝ using http://dfns.dyalog.com/n_wGraphs.htm 6 | w ← ,⍵ 7 | edges ← ,{⊂ ¯1+ 0~⍨ ,⍵×3 3⍴⍳2}⌺3 3⊢1+(⍴⍵)⍴⍳≢w 8 | costs ← {w[⍵]}¨edges 9 | graph ← ↑edges costs 10 | +/graph wcost graph wpath 0,(≢w)-1 11 | } 12 | 13 | risk m 14 | risk 10|{⍵+⍵>9} ⊃⍪/,/(+/¨⍳5 5)+5 5⍴⊂m 15 | -------------------------------------------------------------------------------- /2021/16.apl: -------------------------------------------------------------------------------- 1 | hex ← '0123456789ABCDEF' 2 | f ← ∊⍉2 2 2 2⊤hex⍳⊃⊃⎕NGET'in/16.txt'1 3 | 4 | read ← { ⊃res f ∘← (⍵↑f) (⍵↓f) } 5 | eval ← { ⍺=0:+/⍵ ⋄ ⍺=1:×/⍵ ⋄ ⍺=2:⌊/⍵ ⋄ ⍺=3:⌈/⍵ ⋄ ⍺=5:>/⍵ ⋄ ⍺=6: x2 y: 0 7 | x y2 ∧.≥ x1 y: 1 8 | (⍺+⍵) ∇ (⊢⌈0 1×⊢)⍵-1 9 | } 10 | 11 | 2!|y1 ⍝ part 1 12 | +/check¨, (⍳x1+x2) ∘., y1+⍳2×|y1 ⍝ part 2 13 | -------------------------------------------------------------------------------- /2021/18.apl: -------------------------------------------------------------------------------- 1 | parse ← { 2 | punc ← '[],'∘.=⍵ 3 | nums ← ⍎¨(~∨⌿punc) ⊆ ⍵ 4 | dpth ← ⊃¨(~∨⌿punc) ⊆ -⌿+\2↑punc 5 | nums dpth 6 | } 7 | 8 | explode ← { nums dpth ← ⍵ 9 | dpth∧.≤4: ⍵ 10 | a i j b c ← ¯1 0 1 2 3+dpth⍳5 11 | k ← c-≢nums 12 | nums ← (¯1↓i↑nums),(nums[i]+a↓i↑nums),0,(nums[j]+b↓k↓nums),(c↓nums) 13 | dpth ← (i↑dpth),(dpth[i]-1),(b↓dpth) 14 | nums dpth 15 | } 16 | 17 | split ← { nums dpth ← ⍵ 18 | nums∧.≤9: ⍵ 19 | i j ← 0 1+(nums>9)⍳1 20 | nums ← (i↑nums), ((⌊,⌈)nums[i]÷2), j↓nums 21 | dpth ← (i↑dpth), (2⍴1+dpth[i]), j↓dpth 22 | nums dpth 23 | } 24 | 25 | merge ← split∘(explode⍣≡)⍣≡ 0 1+,¨ 26 | 27 | mag ← { nums dpth ← ⍵ 28 | 1=≢dpth: nums 29 | i j ← 0 2+dpth⍳⌈/dpth 30 | nums ← (i↑nums), (+/3 2×2↑i↓nums), (j↓nums) 31 | dpth ← (i↑dpth), (dpth[i]-1), (j↓dpth) 32 | ∇ nums dpth 33 | } 34 | 35 | f ← parse¨⊃⎕NGET'in/18.txt'1 36 | 37 | mag ⊃merge⍨/ ⌽f 38 | ⌈/∊∘.(mag⍤merge × ≢)⍨f 39 | -------------------------------------------------------------------------------- /2021/20.apl: -------------------------------------------------------------------------------- 1 | s m ← {(⊃⍵)(↑2↓⍵)} '#'=⊃⎕NGET'in/20.txt'1 2 | 3 | pad ← {(⍺+⍺+⍴⍵)↑(-⍺+⍴⍵)↑⍵} 4 | unpad ← {(⍺ ⍺)↓(-⍺ ⍺)↓⍵} 5 | 6 | {+/,⍵unpad{s[2⊥,⍵]}⌺3 3⍣⍵⊢(⍵×2)pad m}¨2 50 7 | -------------------------------------------------------------------------------- /2021/21.apl: -------------------------------------------------------------------------------- 1 | p1 p2 ← ¯1+(⍎1⊃∊∘⎕D⊆⊢)¨⊃⎕NGET'in/21.txt'1 2 | 3 | { ⍝ [part 1] ⍵ - cur/other player positions, cur/other scores, dice 4 | p q s t dice ← ⍵ 5 | 1000≤t: s×dice 6 | ∇ q p t (s+1+p←10|p+6+3×dice) (dice+3) 7 | } 5↑p1 p2 8 | 9 | sums freqs ← ↓⍉{⍺,≢⍵}⌸+/↑,1+⍳3 3 3 10 | 11 | MEM ← 10 10 21 21 2⍴0 12 | ⌈/ { ⍝ [part 2] ⍵ - cur/other player positions, cur/other scores 13 | p q s t ← ⍵ 14 | 21≤t: 0 1 15 | 0∨.≠MEM[p;q;s;t;]: MEM[p;q;s;t;] 16 | ⊢MEM[p;q;s;t;] ← ⌽+⌿↑freqs × ∇¨ {q ⍵ t (s+⍵+1)}¨ 10|p+sums 17 | } 4↑p1 p2 18 | -------------------------------------------------------------------------------- /2021/22.apl: -------------------------------------------------------------------------------- 1 | ⎕PP←17 2 | 3 | f ← {(⍵[1]='n'),1∘+@1¨↓3 2⍴⍎¨('-',⎕D)(∊⍨⊆⊢)⍵}¨⊃⎕NGET'in/22.txt'1 4 | 5 | ∆ ← ⊃{⍺+⍳⍵-⍺}/ 6 | 7 | vol ← { 8 | cu ← {∪{⍵[⍋⍵]}∊⍵}¨1↓↓⍉↑⍵ ⍝ possible cuboid coordinates 9 | m ← (¯1+≢¨cu)⍴0 ⍝ initialize empty cuboid map 10 | 11 | _←{ x y z ← cu ⍳¨ 1↓⍵ ⍝ get intersecting cuboids 12 | ⊃m[∆ x;∆ y;∆ z] ← ⊃⍵ ⍝ update cuboids with on/off 13 | }¨⍵ ⍝ for each line 14 | 15 | +.×/⌽ (⊂m), 2-⍨/¨cu ⍝ calculate volume 16 | } 17 | 18 | vol f/⍨(50≥⌈/⍤|⍤∊)¨f 19 | vol f 20 | -------------------------------------------------------------------------------- /2021/24.apl: -------------------------------------------------------------------------------- 1 | 'words'⎕CY'dfns' 2 | a b ← ↓{3(⍎⊃∘words)¨⍵[5 15∘.+18×⍳14]}⊃⎕NGET'in/24.txt'1 3 | 4 | solve ← { 5 | ∆ ← ⍵⍵ ⋄ ∆∆ ← ⍺⍺ 6 | i v ← ↓⍉↑⍬{ 7 | ⍵=≢a:⍬ 8 | a[⍵]>0: (⍵,⍺)∇(⍵+1) 9 | (⊂((a[⍵]∆-b[1↑⍺])⊃⍵,⊃⍺),|a[⍵]+b[⊃⍺]), (1↓⍺)∇(⍵+1) 10 | }0 11 | 10⊥v ∆∆⍨@i⊢⍵ 12 | } 13 | - solve > 14⍴9 14 | + solve < 14⍴1 15 | -------------------------------------------------------------------------------- /2021/25.apl: -------------------------------------------------------------------------------- 1 | m ← 1-↑'>.v'∘⍳¨⊃⎕NGET'in/25.txt'1 2 | 3 | east ← ⊢{(⍺+¯1⌽⍵)-⍵} 0∘<∧0=1∘⌽ 4 | south ← ⊢{(⍺+¯1⊖⍵)-⍵} ¯1× 0∘>∧0=1∘⊖ 5 | 6 | { ⍵≡next ← south east ⍵: 1 ⋄ 1+∇next } m 7 | -------------------------------------------------------------------------------- /2021/README.md: -------------------------------------------------------------------------------- 1 | # AoC 2021 2 | 3 | Done in APL. 4 | -------------------------------------------------------------------------------- /2021/in/01e.txt: -------------------------------------------------------------------------------- 1 | 199 2 | 200 3 | 208 4 | 210 5 | 200 6 | 207 7 | 240 8 | 269 9 | 260 10 | 263 -------------------------------------------------------------------------------- /2021/in/02e.txt: -------------------------------------------------------------------------------- 1 | forward 5 2 | down 5 3 | forward 8 4 | up 3 5 | down 8 6 | forward 2 -------------------------------------------------------------------------------- /2021/in/03e.txt: -------------------------------------------------------------------------------- 1 | 00100 2 | 11110 3 | 10110 4 | 10111 5 | 10101 6 | 01111 7 | 00111 8 | 11100 9 | 10000 10 | 11001 11 | 00010 12 | 01010 -------------------------------------------------------------------------------- /2021/in/04e.txt: -------------------------------------------------------------------------------- 1 | 7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1 2 | 3 | 22 13 17 11 0 4 | 8 2 23 4 24 5 | 21 9 14 16 7 6 | 6 10 3 18 5 7 | 1 12 20 15 19 8 | 9 | 3 15 0 2 22 10 | 9 18 13 17 5 11 | 19 8 7 25 23 12 | 20 11 10 24 4 13 | 14 21 16 12 6 14 | 15 | 14 21 17 24 4 16 | 10 16 15 9 19 17 | 18 8 23 26 20 18 | 22 11 13 6 5 19 | 2 0 12 3 7 -------------------------------------------------------------------------------- /2021/in/05e.txt: -------------------------------------------------------------------------------- 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 -------------------------------------------------------------------------------- /2021/in/06.txt: -------------------------------------------------------------------------------- 1 | 2,5,3,4,4,5,3,2,3,3,2,2,4,2,5,4,1,1,4,4,5,1,2,1,5,2,1,5,1,1,1,2,4,3,3,1,4,2,3,4,5,1,2,5,1,2,2,5,2,4,4,1,4,5,4,2,1,5,5,3,2,1,3,2,1,4,2,5,5,5,2,3,3,5,1,1,5,3,4,2,1,4,4,5,4,5,3,1,4,5,1,5,3,5,4,4,4,1,4,2,2,2,5,4,3,1,4,4,3,4,2,1,1,5,3,3,2,5,3,1,2,2,4,1,4,1,5,1,1,2,5,2,2,5,2,4,4,3,4,1,3,3,5,4,5,4,5,5,5,5,5,4,4,5,3,4,3,3,1,1,5,2,4,5,5,1,5,2,4,5,4,2,4,4,4,2,2,2,2,2,3,5,3,1,1,2,1,1,5,1,4,3,4,2,5,3,4,4,3,5,5,5,4,1,3,4,4,2,2,1,4,1,2,1,2,1,5,5,3,4,1,3,2,1,4,5,1,5,5,1,2,3,4,2,1,4,1,4,2,3,3,2,4,1,4,1,4,4,1,5,3,1,5,2,1,1,2,3,3,2,4,1,2,1,5,1,1,2,1,2,1,2,4,5,3,5,5,1,3,4,1,1,3,3,2,2,4,3,1,1,2,4,1,1,1,5,4,2,4,3 2 | -------------------------------------------------------------------------------- /2021/in/06e.txt: -------------------------------------------------------------------------------- 1 | 3,4,3,1,2 -------------------------------------------------------------------------------- /2021/in/07e.txt: -------------------------------------------------------------------------------- 1 | 16,1,2,0,4,2,7,1,2,14 -------------------------------------------------------------------------------- /2021/in/08e.txt: -------------------------------------------------------------------------------- 1 | be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb cefbgd gcbe 2 | edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc 3 | fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb cbg 4 | fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega | efabcd cedba gadfec cb 5 | aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga | gecf egdcabf bgf bfgea 6 | fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf | gebdcfa ecba ca fadegcb 7 | dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf | cefg dcbef fcge gbcadfe 8 | bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe cdgba cbgef 9 | egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg cgb 10 | gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg bagce -------------------------------------------------------------------------------- /2021/in/09e.txt: -------------------------------------------------------------------------------- 1 | 2199943210 2 | 3987894921 3 | 9856789892 4 | 8767896789 5 | 9899965678 -------------------------------------------------------------------------------- /2021/in/10e.txt: -------------------------------------------------------------------------------- 1 | [({(<(())[]>[[{[]{<()<>> 2 | [(()[<>])]({[<{<<[]>>( 3 | {([(<{}[<>[]}>{[]{[(<()> 4 | (((({<>}<{<{<>}{[]{[]{} 5 | [[<[([]))<([[{}[[()]]] 6 | [{[{({}]{}}([{[{{{}}([] 7 | {<[[]]>}<{[{[{[]{()[[[] 8 | [<(<(<(<{}))><([]([]() 9 | <{([([[(<>()){}]>(<<{{ 10 | <{([{{}}[<[[[<>{}]]]>[]] -------------------------------------------------------------------------------- /2021/in/11.txt: -------------------------------------------------------------------------------- 1 | 6111821767 2 | 1763611615 3 | 3512683131 4 | 8582771473 5 | 8214813874 6 | 2325823217 7 | 2222482823 8 | 5471356782 9 | 3738671287 10 | 8675226574 11 | -------------------------------------------------------------------------------- /2021/in/11e.txt: -------------------------------------------------------------------------------- 1 | 5483143223 2 | 2745854711 3 | 5264556173 4 | 6141336146 5 | 6357385478 6 | 4167524645 7 | 2176841721 8 | 6882881134 9 | 4846848554 10 | 5283751526 -------------------------------------------------------------------------------- /2021/in/12.txt: -------------------------------------------------------------------------------- 1 | vn-DD 2 | qm-DD 3 | MV-xy 4 | end-xy 5 | KG-end 6 | end-kw 7 | qm-xy 8 | start-vn 9 | MV-vn 10 | vn-ko 11 | lj-KG 12 | DD-xy 13 | lj-kh 14 | lj-MV 15 | ko-MV 16 | kw-qm 17 | qm-MV 18 | lj-kw 19 | VH-lj 20 | ko-qm 21 | ko-start 22 | MV-start 23 | DD-ko -------------------------------------------------------------------------------- /2021/in/12e.txt: -------------------------------------------------------------------------------- 1 | dc-end 2 | HN-start 3 | start-kj 4 | dc-start 5 | dc-HN 6 | LN-dc 7 | HN-end 8 | kj-sa 9 | kj-HN 10 | kj-dc -------------------------------------------------------------------------------- /2021/in/13e.txt: -------------------------------------------------------------------------------- 1 | 6,10 2 | 0,14 3 | 9,10 4 | 0,3 5 | 10,4 6 | 4,11 7 | 6,0 8 | 6,12 9 | 4,1 10 | 0,13 11 | 10,12 12 | 3,4 13 | 3,0 14 | 8,4 15 | 1,10 16 | 2,14 17 | 8,10 18 | 9,0 19 | 20 | fold along y=7 21 | fold along x=5 -------------------------------------------------------------------------------- /2021/in/14e.txt: -------------------------------------------------------------------------------- 1 | NNCB 2 | 3 | CH -> B 4 | HH -> N 5 | CB -> H 6 | NH -> C 7 | HB -> C 8 | HC -> B 9 | HN -> C 10 | NN -> C 11 | BH -> H 12 | NC -> B 13 | NB -> B 14 | BN -> B 15 | BB -> N 16 | BC -> B 17 | CC -> N 18 | CN -> C -------------------------------------------------------------------------------- /2021/in/15e.txt: -------------------------------------------------------------------------------- 1 | 1163751742 2 | 1381373672 3 | 2136511328 4 | 3694931569 5 | 7463417111 6 | 1319128137 7 | 1359912421 8 | 3125421639 9 | 1293138521 10 | 2311944581 -------------------------------------------------------------------------------- /2021/in/17.txt: -------------------------------------------------------------------------------- 1 | target area: x=207..263, y=-115..-63 2 | -------------------------------------------------------------------------------- /2021/in/17e.txt: -------------------------------------------------------------------------------- 1 | target area: x=20..30, y=-10..-5 -------------------------------------------------------------------------------- /2021/in/18e.txt: -------------------------------------------------------------------------------- 1 | [[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]] 2 | [[[5,[2,8]],4],[5,[[9,9],0]]] 3 | [6,[[[6,2],[5,6]],[[7,6],[4,7]]]] 4 | [[[6,[0,7]],[0,9]],[4,[9,[9,0]]]] 5 | [[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]] 6 | [[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]] 7 | [[[[5,4],[7,7]],8],[[8,3],8]] 8 | [[9,3],[[9,9],[6,[4,9]]]] 9 | [[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]] 10 | [[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]] -------------------------------------------------------------------------------- /2021/in/20e.txt: -------------------------------------------------------------------------------- 1 | ..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..# 2 | 3 | #..#. 4 | #.... 5 | ##..# 6 | ..#.. 7 | ..### -------------------------------------------------------------------------------- /2021/in/21.txt: -------------------------------------------------------------------------------- 1 | Player 1 starting position: 8 2 | Player 2 starting position: 10 3 | -------------------------------------------------------------------------------- /2021/in/23.txt: -------------------------------------------------------------------------------- 1 | ############# 2 | #...........# 3 | ###C#D#D#A### 4 | #B#A#B#C# 5 | ######### -------------------------------------------------------------------------------- /2021/in/25e.txt: -------------------------------------------------------------------------------- 1 | v...>>.vv> 2 | .vv>>.vv.. 3 | >>.>v>...v 4 | >>v>>.>.v. 5 | v>v.vv.v.. 6 | >.>>..v... 7 | .vv..>.>v. 8 | v.v..>>v.v 9 | ....v..v.> -------------------------------------------------------------------------------- /2022/01.apl: -------------------------------------------------------------------------------- 1 | nums ← {⍵[⍒⍵]} (+/⍎¨)¨ ((×≢¨)⊆⊢) ⊃⎕NGET'in/01.txt'1 2 | 3 | ⎕← +/1↑nums 4 | ⎕← +/3↑nums 5 | -------------------------------------------------------------------------------- /2022/02.apl: -------------------------------------------------------------------------------- 1 | d ← ↑{3~⍨23|⎕A⍳⍵}¨ ⊃⎕NGET'in/02.txt'1 2 | 3 | ⎕← (1+⊢/d) +.+ 3×3|1--/d 4 | ⎕← (3×⊢/d) +.+ 1+3|2++/d 5 | 6 | ⍝n alternative, using precomputed values 7 | ⎕← +/4 8 3 1 5 9 7 2 6[3⊥⍉d] 8 | ⎕← +/3 4 8 1 5 9 2 6 7[3⊥⍉d] 9 | -------------------------------------------------------------------------------- /2022/03.apl: -------------------------------------------------------------------------------- 1 | r ← 58|20+⎕UCS¨ ⊃⎕NGET'in/03.txt'1 2 | 3 | ⎕← +/∊{∪¨∩/↓2(2÷⍨≢⍵)⍴⍵}¨r 4 | ⎕← +/∊ ∪¨∩/(3÷⍨≢r)3⍴r 5 | -------------------------------------------------------------------------------- /2022/04.apl: -------------------------------------------------------------------------------- 1 | al ah bl bh ← ↓⍉↑(⍎¨⎕D∘(∊⍨⊆⊢))¨⊃⎕NGET'in/04.txt'1 2 | 3 | ⎕← +/ ((al≤bl)∧(bh≤ah)) ∨ ((bl≤al)∧(ah≤bh)) 4 | ⎕← +/ (al≤bh)∧(bl≤ah) 5 | -------------------------------------------------------------------------------- /2022/05.apl: -------------------------------------------------------------------------------- 1 | cranes moves ← {(×≢¨⍵)⊆⍵}⊃⎕NGET'in/05.txt'1 2 | cranes ← {(⍵/⍨1=4|⍳⍴⍵)∩¨⊂⎕A}↓⍉↑cranes 3 | moves ← {0 ¯1 ¯1+⍎¨(⍵∊⎕D)⊆⍵}¨moves 4 | 5 | run ← { 6 | boxes ← ⍺⍺ (⊃⍺)↑⊃⍵[⍺[1]] 7 | ((⊂(⊃⍺)↓⊃),(⊂boxes,1⊃⊢))@(1↓⍺)⊢⍵ 8 | } 9 | 10 | ⎕← ⊃¨⊃⌽run/⌽(⊂cranes),moves 11 | ⎕← ⊃¨⊃⊢run/⌽(⊂cranes),moves 12 | -------------------------------------------------------------------------------- /2022/06.apl: -------------------------------------------------------------------------------- 1 | s ← ⊃⊃⎕NGET'in/06.txt'1 2 | 3 | ⎕← {⍵+⍵⍳⍨≢¨⍵∪/s}¨4 14 4 | -------------------------------------------------------------------------------- /2022/07.apl: -------------------------------------------------------------------------------- 1 | lines ← ⊃⎕NGET'in/07.txt'1 2 | 3 | sz ← ⍬ ⍝ sizes of all dirs 4 | sz,← ⊃{ 5 | '$ cd ..'≡⍺: 1↓⍵ ⊣sz ,← ⊃⍵ ⍝ pop size of current dir, append to sz 6 | '$ cd'≡4↑⍺: 0,⍵ ⍝ push size of new dir 7 | ⎕D∊⍨⊃⍺: ⍵ + ⍎(⍺∊⎕D)/⍺ ⍝ increment sizes of current and parent dirs 8 | ⍵ ⍝ do nothing otherwise 9 | }/⌽(⊂⍬),lines 10 | 11 | ⎕← +/sz/⍨sz≤ 1e5 12 | ⎕← ⌊/sz/⍨sz≥ ⌈/sz-4e7 13 | -------------------------------------------------------------------------------- /2022/08.apl: -------------------------------------------------------------------------------- 1 | grid ← ⍎¨↑⊃⎕NGET'in/08.txt'1 2 | 3 | p1 ← {∧/0= ¯1↓ ,⍵}¨⊢≤,\ 4 | p2 ← {⊃1+⍸⌽¯1↓1@0,⍵}¨⊢≤,\ 5 | dirs ← {⊃⍺⍺/(⍵⍵⍵)(⌽⍵⍵⌽⍵)(⍉⌽⍵⍵⌽t)(⍉⍵⍵t←⍉⍵)} 6 | 7 | ⎕← +/∊ (⌈ dirs p1) grid 8 | ⎕← ⌈/∊ (× dirs p2) grid 9 | -------------------------------------------------------------------------------- /2022/09.apl: -------------------------------------------------------------------------------- 1 | lines ← ⊃⎕NGET'in/09.txt'1 2 | moves ← +\ 0,(1(⍎↓)¨lines) / 0j1*'RDLU'⍳⊃¨lines 3 | 4 | run ← ⊃{ 5 | head tails ← ⍺⍵ 6 | tail ← ⊢/tails 7 | dist ← 9 11○head-tail 8 | move ← +/¯9 ¯11○×dist 9 | tails, (dist≢×dist) ↑ tail+move 10 | }/∘⌽ 11 | 12 | ⍝ 40% faster 13 | run ← ⊃(⊢,(≢∘×⍨dist)↑tail+( +/¯9 ¯11○∘×(dist←9 11○⊣-(tail←⊢/⊢))))/∘⌽ 14 | 15 | ⎕← ≢∪run moves 16 | ⎕← ≢∪run⍣9⊢moves 17 | -------------------------------------------------------------------------------- /2022/10.apl: -------------------------------------------------------------------------------- 1 | vals ← +\1,∊{'n'≡⊃⍵:0 ⋄ 0,⍎5↓⍵}¨⊃⎕NGET'in/10.txt'1 2 | 3 | ⎕← +/{⍵×vals[⍵-1]} 20+40×⍳6 4 | ⎕← ' #'[1≥|vals-⍥{6 40⍴⍵}⍳40] 5 | -------------------------------------------------------------------------------- /2022/11.apl: -------------------------------------------------------------------------------- 1 | file ← 1↓⍉↑((×≢¨)⊆⊢)⊃⎕NGET'in/11.txt'1 2 | file ← ('× old'⎕R'* 2')∘('\*'⎕R'×')¨file 3 | items arg mod true false ← ↓{⍎(⊃⍸⍵∊⎕D)↓⍵}¨file 4 | op ← '+×*'⍳23⌷¨file[1;] 5 | math ← { 0=⍺⍺: ⍺+⍵ ⋄ 1=⍺⍺: ⍺×⍵ ⋄ 2=⍺⍺: ⍵*⍺ } 6 | MOD ← ∧/mod 7 | 8 | run ← { 9 | num mul ← ⍺⍵ 10 | pack ← ,¨items 11 | inspects ← (≢items)⍴0 12 | _← { 13 | items ← MOD|⌊mul× arg[⍵] (op[⍵]math)¨ ⍵⊃pack 14 | 0=≢items: ⍵ 15 | pack[⍵] ← ⊂⍬ 16 | inspects[⍵]+← ≢items 17 | to ← ⍵⊃¨true false[×mod[⍵]|items] 18 | ⍵⊣to {pack[⍺],← ⍵}¨ items 19 | }¨⍣num⊢⍳≢pack 20 | ×/inspects[2↑⍒inspects] 21 | } 22 | 23 | ⎕← 20 run ÷3 24 | ⎕← 1e4 run 1 25 | -------------------------------------------------------------------------------- /2022/12.apl: -------------------------------------------------------------------------------- 1 | 'stpath' 'span'⎕CY'dfns' 2 | 3 | a S E ← 0 ¯1 1 + ⎕UCS'aaz' 4 | map ← S@(=∘(⎕UCS'S')) E@(=∘(⎕UCS'E')) ⎕UCS↑ ⊃⎕NGET'in/12.txt'1 5 | flat ← ,map 6 | 7 | graph ← ,{ 8 | ele ← ⊃⊃⍵[1;1] 9 | nei ← ,⍵×3 3⍴⍳2 ⍝ neighbors :: [(elevation,id)] 10 | ⊂ 1⊃¨ (ele≤1+⊃¨nei)/nei ⍝ ids of neighbors that can be visited 11 | }⌺3 3⊢map,¨ (⍴map)⍴⍳≢flat 12 | 13 | tree ← graph span flat⍳E ⍝ span tree trom "E" 14 | 15 | ⍝ using http://dfns.dyalog.com/n_stpath.htm 16 | steps ← { ⌊/ ({≢ 1↓ tree stpath ⍵}¨ ⍸flat=⍵) ~ 0 } 17 | 18 | ⎕← steps S 19 | ⎕← steps a 20 | -------------------------------------------------------------------------------- /2022/13.apl: -------------------------------------------------------------------------------- 1 | data ← (⎕JSON¨¨(×≢¨)⊆⊢)⊃⎕NGET'in/13.txt'1 2 | 3 | compare ← { 4 | i j ← 0=≡¨⍺⍵ ⍝ 1. check if integers 5 | i∧j: ⍺(<->)⍵ 6 | i∨j: ∇/(⊂,)@j⊢⍺⍵ 7 | 8 | ∨/e←0=≢¨⍺⍵: -/e ⍝ 2. check if empty 9 | 10 | 0≠h←∇/⊃¨⍺⍵: h ⍝ 3. compare heads 11 | ∇/1↓¨⍺⍵ ⍝ 4. compare tails 12 | } 13 | 14 | ⎕← +/ 1+⍸ 1=compare/¨data 15 | ⎕← ×/ 1 2+ +⌿ 1= 2 6 ∘.compare⍨ ⊃,/data 16 | -------------------------------------------------------------------------------- /2022/14.apl: -------------------------------------------------------------------------------- 1 | to ← {(⍺⌊⍵)+⍳1+|⍺-⍵} 2 | walls ← ∪∊ {2{+/1 0j1×⍺ to¨⍵}/ ↓(.5×≢l)2⍴ l←⍎¨(⍵∊⎕D)⊆⍵}¨ ⊃⎕NGET'in/14.txt'1 3 | floor ← 1+⌈/11○walls 4 | down ← 0j1 + 0 ¯1 1 5 | 6 | ⍝ Part 1. Depth-first search: mid-left-right post-order iterative traversal 7 | ⍝ ⍺ - stack ⋄ ⍺⍺ - visited ⋄ ⍵ - count ⋄ terminates once floor is reached 8 | ⎕← 500 (walls{⊃(⍺⍺∪c)∇∇/(floor=11○c)↓(⊂next,1↓⍺),⊂⍵+⍬≡next ← ⍺⍺ ~⍨ (down+c),c←⊃⍺}) 0 9 | 10 | ⍝ Part 2. Breadth-first search: count all possible places for sand at each depth 11 | ⍝ ⍺ - grains from a level above ⋄ ⍵ - count per level 12 | ⎕← +/ 500 { ⊃∇/ (floor=≢⍵)↓ (⊂next),⊂⍵,≢next ← (∪∊down+⊂⍺) ~ walls } 1 13 | -------------------------------------------------------------------------------- /2022/15.apl: -------------------------------------------------------------------------------- 1 | x y bx by ← ↓⍉↑{⍎¨(⍵∊⎕D,'-')⊆⍵}¨ ⊃⎕NGET'in/15.txt'1 2 | r ← ⊃+/ | x y - bx by 3 | 4 | row ← 10 2e6[14<≢x] 5 | to ← {(⍺⌊⍵)+⍳1+|⍺-⍵} 6 | dy ← r - |row-y 7 | covered ← ⊃{⍵∪⊃(-to+)/⍺}/⌽(⊂⍬), (0≤dy)/x,¨dy 8 | beacons ← (row=by)/bx 9 | ⎕← ≢ covered ~ beacons 10 | 11 | max ← 20 4e6[14<≢x] 12 | d1 d2 ← {(y+⍵)(-∪+)(r+1)}¨ (-x)x 13 | ⎕← ⊃∊ d1 ∘.{ 14 | pos ← ⌊.5× ⍵(-,+)⍺ 15 | (0≥pos) ∨.∨ (pos≥max): ⍬ 16 | r ∨.≥ ⊃pos +.(|-) x y: ⍬ 17 | pos +.× 4e6 1 18 | } d2 19 | -------------------------------------------------------------------------------- /2022/16.apl: -------------------------------------------------------------------------------- 1 | names flows links ← ↓⍉↑ {(⊃⍵)(⍎1⊃⍵)(2↓⍵)}∘('[A-Z]{2}|(\d+)'⎕S'&')¨⊃⎕NGET'in/16.txt'1 2 | flows ← flows[sort ← ⍒flows] 3 | names ← names[sort] 4 | links ← (names∘⍳)¨links[sort] 5 | start ← names⍳⊂'AA' 6 | mask ← ⍳×/1+0 498,6 -> 496,6 2 | 503,4 -> 502,4 -> 502,9 -> 494,9 3 | -------------------------------------------------------------------------------- /2022/in/15e.txt: -------------------------------------------------------------------------------- 1 | Sensor at x=2, y=18: closest beacon is at x=-2, y=15 2 | Sensor at x=9, y=16: closest beacon is at x=10, y=16 3 | Sensor at x=13, y=2: closest beacon is at x=15, y=3 4 | Sensor at x=12, y=14: closest beacon is at x=10, y=16 5 | Sensor at x=10, y=20: closest beacon is at x=10, y=16 6 | Sensor at x=14, y=17: closest beacon is at x=10, y=16 7 | Sensor at x=8, y=7: closest beacon is at x=2, y=10 8 | Sensor at x=2, y=0: closest beacon is at x=2, y=10 9 | Sensor at x=0, y=11: closest beacon is at x=2, y=10 10 | Sensor at x=20, y=14: closest beacon is at x=25, y=17 11 | Sensor at x=17, y=20: closest beacon is at x=21, y=22 12 | Sensor at x=16, y=7: closest beacon is at x=15, y=3 13 | Sensor at x=14, y=3: closest beacon is at x=15, y=3 14 | Sensor at x=20, y=1: closest beacon is at x=15, y=3 15 | -------------------------------------------------------------------------------- /2022/in/16e.txt: -------------------------------------------------------------------------------- 1 | Valve AA has flow rate=0; tunnels lead to valves DD, II, BB 2 | Valve BB has flow rate=13; tunnels lead to valves CC, AA 3 | Valve CC has flow rate=2; tunnels lead to valves DD, BB 4 | Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE 5 | Valve EE has flow rate=3; tunnels lead to valves FF, DD 6 | Valve FF has flow rate=0; tunnels lead to valves EE, GG 7 | Valve GG has flow rate=0; tunnels lead to valves FF, HH 8 | Valve HH has flow rate=22; tunnel leads to valve GG 9 | Valve II has flow rate=0; tunnels lead to valves AA, JJ 10 | Valve JJ has flow rate=21; tunnel leads to valve II 11 | -------------------------------------------------------------------------------- /2022/in/17e.txt: -------------------------------------------------------------------------------- 1 | >>><<><>><<<>><>>><<<>>><<<><<<>><>><<>> 2 | -------------------------------------------------------------------------------- /2022/in/18e.txt: -------------------------------------------------------------------------------- 1 | 2,2,2 2 | 1,2,2 3 | 3,2,2 4 | 2,1,2 5 | 2,3,2 6 | 2,2,1 7 | 2,2,3 8 | 2,2,4 9 | 2,2,6 10 | 1,2,5 11 | 3,2,5 12 | 2,1,5 13 | 2,3,5 14 | -------------------------------------------------------------------------------- /2022/in/19e.txt: -------------------------------------------------------------------------------- 1 | Blueprint 1: Each ore robot costs 4 ore. Each clay robot costs 2 ore. Each obsidian robot costs 3 ore and 14 clay. Each geode robot costs 2 ore and 7 obsidian. 2 | Blueprint 2: Each ore robot costs 2 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 8 clay. Each geode robot costs 3 ore and 12 obsidian. 3 | -------------------------------------------------------------------------------- /2022/in/20e.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | -3 4 | 3 5 | -2 6 | 0 7 | 4 8 | -------------------------------------------------------------------------------- /2022/in/21e.txt: -------------------------------------------------------------------------------- 1 | root: pppw + sjmn 2 | dbpl: 5 3 | cczh: sllz + lgvd 4 | zczc: 2 5 | ptdq: humn - dvpt 6 | dvpt: 3 7 | lfqf: 4 8 | humn: 5 9 | ljgn: 2 10 | sjmn: drzm * dbpl 11 | sllz: 4 12 | pppw: cczh / lfqf 13 | lgvd: ljgn * ptdq 14 | drzm: hmdt - zczc 15 | hmdt: 32 16 | -------------------------------------------------------------------------------- /2022/in/22e.txt: -------------------------------------------------------------------------------- 1 | ...# 2 | .#.. 3 | #... 4 | .... 5 | ...#.......# 6 | ........#... 7 | ..#....#.... 8 | ..........#. 9 | ...#.... 10 | .....#.. 11 | .#...... 12 | ......#. 13 | 14 | 10R5L5R10L4R5L5 15 | -------------------------------------------------------------------------------- /2022/in/23e.txt: -------------------------------------------------------------------------------- 1 | ....#.. 2 | ..###.# 3 | #...#.# 4 | .#...## 5 | #.###.. 6 | ##.#.## 7 | .#..#.. 8 | -------------------------------------------------------------------------------- /2022/in/24e.txt: -------------------------------------------------------------------------------- 1 | #.###### 2 | #>>.<^<# 3 | #.<..<<# 4 | #>v.><># 5 | #<^v^^># 6 | ######.# 7 | -------------------------------------------------------------------------------- /2022/in/25e.txt: -------------------------------------------------------------------------------- 1 | 1=-0-2 2 | 12111 3 | 2=0= 4 | 21 5 | 2=01 6 | 111 7 | 20012 8 | 112 9 | 1=-1= 10 | 1-12 11 | 12 12 | 1= 13 | 122 14 | -------------------------------------------------------------------------------- /2023/01.apl: -------------------------------------------------------------------------------- 1 | w ← '/'(≠⊆⊢)'zero/one/two/three/four/five/six/seven/eight/nine' 2 | 3 | f ← ⊃⎕NGET'in/01.txt'1 4 | cali ← {10⊥∊1 ¯1↑¨⊂⍵~0} 5 | 6 | ⎕← +/ cali¨d←10|⎕D∘⍳¨f 7 | ⎕← +/ cali¨d⌈(⍳10)⌈.×w∘.⍷f 8 | -------------------------------------------------------------------------------- /2023/02.apl: -------------------------------------------------------------------------------- 1 | f ← ⊃⎕NGET'in/02.txt'1 2 | d ← 'rgb'∘.{⌈/⍎¨('(\d+) ',⍺)⎕S'\1'⊢⍵}f 3 | 4 | ⎕← +/1+⍸12 13 14∧.≥d 5 | ⎕← +/×⌿d 6 | -------------------------------------------------------------------------------- /2023/03.apl: -------------------------------------------------------------------------------- 1 | m ← ↑⊃⎕NGET'in/03.txt'1 2 | d ← m∊⎕D 3 | num ← 0,⍎¨⊃,/d⊆¨⍥↓m 4 | C ← ,{⊂∪(∊⍵)~0}⌺3 3⊢d×(⍴d)⍴+\,2i←⍺⍳⊂n←{⌽⍉F⍵}⍣4⊢⍵: S⊃⍺[i+((≢⍺)-i)|1e9-1+i] ⋄ (⍺,⊂n) ∇ n } f 6 | -------------------------------------------------------------------------------- /2023/15.apl: -------------------------------------------------------------------------------- 1 | H ← (256|17×+)/∘⌽0,⎕UCS 2 | N C ← ⍬⍬ 3 | ⎕← +/{ 4 | '-'∊⍵: H⍵⊣C/⍨←N/⍨←m←N≢¨⊂¯1↓⍵ 5 | n ← ⊂¯2↓⍵ 6 | n∊N: H⍵⊣C[N⍳n]←⍎⊃⌽⍵ 7 | H⍵ ⊣ N,←n ⊣ C,←⍎⊃⌽⍵ 8 | }¨','(≠⊆⊢)⊃⊃⎕NGET'in/15.txt'1 9 | 10 | ⎕← +/C{(⍺+1)+.×(⍵×1+⍳≢⍵)}⌸⍨H¨N 11 | -------------------------------------------------------------------------------- /2023/16.apl: -------------------------------------------------------------------------------- 1 | f ← ('#',⌽∘⍉)⍣4⊢↑⊃⎕NGET'in/16.txt'1 2 | next ← { 3 | x y dx dy ← ⍵ 4 | x y+←dx dy-1 5 | c←f[y;x] 6 | c='.': ⊂x y dx dy 7 | c='|': (x y 1 2)(x y 1 0) 8 | c='-': (x y 2 1)(x y 0 1) 9 | c='\': ⊂x y dy dx 10 | c='/': ⊂x y,2-dy dx 11 | ⍬ 12 | } 13 | solve←{ 14 | seen ← 0⍴⍨(⍴f),3 3 15 | { 16 | 0=≢⍵: +/∊∨/∨/seen 17 | n ← ⊃,/next¨⍵ 18 | n≡⍬: ∇n 19 | n/⍨ ← ~seen[n] 20 | seen[n]←1 21 | ∇n 22 | }⍵ 23 | } 24 | ⎕← solve ⊂0 1 2 1 25 | h w←⍴f 26 | p ← ⊃,/{(0 ⍵ 2 1)((w-1) ⍵ 0 1)}¨⍳h 27 | p,← ⊃,/{(⍵ 0 1 2)(⍵ (h-1) 1 0)}¨⍳w 28 | ⎕← ⌈/{solve⊂⍵}¨p 29 | -------------------------------------------------------------------------------- /2023/17.apl: -------------------------------------------------------------------------------- 1 | f ← ⎕D⍳↑⊃⎕NGET'in/17.txt'1 2 | D ← w 1(-w←≢f)¯1 3 | 4 | ⍝ M[n;dir;xy] is cost to xy in n steps from dir (D,R,U,L) 5 | M ← ↑(⍳11)∘.{,(⊖⍉)⍣⍵⊢99⍪⍨⍣⍺⊢⍺+⌿1↓(⌽⍉)⍣⍵⊢f} ⍳4 6 | 7 | F ← {best ← (≢,f)4⍴⌊/⍬ ⋄ ⍵{ 8 | cost pos dir ← ⍵ ⋄ steps ← ⍺⍺ 9 | 0=≢cost: ⌊/⊢⌿best ⍝ return best score once done 10 | dir ← 4|dir∘.+1 3 ⍝ new (orthogonal) directions 11 | val ← pos{M[steps;⍵;⍺]}¨↓dir ⍝ cost to get move 12 | cost ← ,1 0 2⍉↑cost+val ⍝ total cost 13 | pos ← ,pos+[1]steps∘.×D[dir] ⍝ for new positions 14 | dir ← ∊(≢steps)/⊂dir ⍝ in new directions 15 | cost pos dir /⍨←⊂99≠,1 0 2⍉↑val ⍝ remove positions outside bounds 16 | cost pos dir ⌷⍨←⊂⊂⍋cost ⍝ sort by cost 17 | cost pos dir /⍨←⊂≠pos,⍥⍪dir ⍝ remove duplicates 18 | m ← cost]\d+):([ARa-z]+),' '([ARa-z]+)}' 12 | dst ← '←{r←0⋄w←⍵⋄' '∆←∧@\1⋄r+←\3 w ∆⍨M←v\2⋄w ∆⍨←~M⋄' 'r+\1 w}' 13 | (⍎src⎕R dst)¨flows 14 | ⎕← in 4 4e3⍴1 15 | -------------------------------------------------------------------------------- /2023/20.apl: -------------------------------------------------------------------------------- 1 | F ← ' ->,'∘((~∊⍨)⊆⊢)¨⊃⎕NGET'in/20.txt'1 2 | N ← ('%&'~⍨⊃)¨F 3 | G ← (N⍳1↓⊢)¨F 4 | types ← 'b%&'⍳(⊃⊃)¨F 5 | rx ← ≢G 6 | flips ← rx/0 7 | conj ← 0@(⊃,/G,¨¨⍳rx)⊢(2/1+rx)⍴1 8 | rxI ← ⍸0=conj[conj[rx;]⍳0;] 9 | rxC ← 0×rxI 10 | counts ← 0 0 11 | 12 | press ← { 13 | 0=≢⍵: ⍺⍺+1 14 | source cur pulse ← ⊃⍵ 15 | counts[pulse]+← 1 16 | rx=cur: ∇1↓⍵ 17 | 0=t←types[cur]: ∇1↓⍵,{cur,⍵,pulse}¨cur⊃G 18 | 1 1≡t pulse: ∇1↓⍵ 19 | 1=t: ∇1↓⍵,(cur⊃G){cur,⍺,⍵}¨flips[cur]←~flips[cur] 20 | conj[cur;source] ← pulse 21 | rxC ⌈← (1=conj[cur;rxI])×⍺⍺×rx∊cur⊃G 22 | ∇1↓⍵,(cur⊃G){cur,⍺,⍵}¨pulse ← ~∧/conj[cur;] 23 | } 24 | start ← ⊂¯1(types⍳0)0 25 | ⎕← { 26 | n ← ⍵ press start 27 | 1e3=⍵: ∇n⊣ ⎕←×/counts 28 | ∧/×rxC: ×/rxC 29 | ∇n 30 | } 1 31 | -------------------------------------------------------------------------------- /2023/21.apl: -------------------------------------------------------------------------------- 1 | M ← '#.S'⍳↑⊃⎕NGET'in/21.txt'1 2 | start ← ,1 0J1+.×⊃⍸M=2 3 | M ← 1 0J1+.×⍉↑⍸×M 4 | result ← ⍬ 5 | a b c ← { 6 | ⍺←1 7 | next/⍨← M∊⍨1 0J1+.×131|9 11∘.○next←∪,1 ¯1 0J1 0J¯1∘.+⍵ 8 | 64=⍺: (⍺+1) ∇ next ⊣ ⎕←≢next 9 | 65=131|⍺: (⍺+1) ∇ next ⊣ result,←≢next 10 | ⍺>3×131: result 11 | (⍺+1) ∇ next 12 | } start 13 | n ← ⌊26501365 ÷ 131 14 | ⎕← a+n×(b-a)+(n-1)×(a+c-2×b)÷2 15 | -------------------------------------------------------------------------------- /2023/22.apl: -------------------------------------------------------------------------------- 1 | X Y Z ← ↓↓1 2 0⍉ ↑2 3∘⍴¨ {⍵[⍋2⊃¨⍵]} (⍎¨∊∘⎕D⊆⊢)¨⊃⎕NGET'in/22.txt'1 2 | to ← ⊃+(⍳1--/) 3 | pos ← X(,∘.,⍥to)¨Y ⍝ X,Y positions for each brick 4 | drop ← { 5 | ⍺ ← ¯1 ⋄ skip ← ⍺ ⋄ peaks ← 10 10⍴0 6 | (⍳≢⍵){ ⍺=skip: ⍵ ⋄ n⊣peaks[⍺⊃pos] ← ⊢/n←⍵-dz←0⌈⍵[0]-1+⌈/peaks[⍺⊃pos] }¨⍵ 7 | } 8 | falls ← {+/Z≢¨⍵ drop Z}¨⍳≢Z ← drop Z 9 | ⎕← +/falls=0 10 | ⎕← +/falls 11 | -------------------------------------------------------------------------------- /2023/23.apl: -------------------------------------------------------------------------------- 1 | M ← '#^<>v.'⍳↑⊃⎕NGET'in/23.txt'1 2 | D,← ⊂D←(-w)¯1 1(w←≢M) 3 | end start ← 0 1 4 | 5 | longest ← { 6 | ⍝ wgraph with only junctions 7 | graph costs ← ↓⍉↑{ 8 | ↓⍉↑{⍵/⍨×≢¨⍵}⍵{⍬≡⍵: ⍬ ⋄ ⍵∊N: (N⍳⍵),≢⍺ ⋄ (⍺,⍵)∇(⍵⊃graph)~⍺}¨⍵⊃graph 9 | }¨N←end start,⍸2<≢¨graph←⍵ 10 | 11 | ⍝ remove edges going back to start along outer walls 12 | outer ← ⍸4>≢¨graph 13 | graph costs ← { 14 | n ← ∪∊outer∘∩¨graph[⍵] 15 | n≡⍬: graph costs 16 | mask ← ~graph[n]∊¨⊂⍵ 17 | graph[n] ← mask/¨graph[n] 18 | costs[n] ← mask/¨costs[n] 19 | ∇∪n 20 | }start 21 | 22 | ⍬ { end=⍵: +/costs[⍺]⊃⍨¨graph[⍺]⍳¨1↓⍺,end 23 | ⍬≡next←(⍵⊃graph)~⍺: 0 24 | ⌈/(⍺,⍵)∘∇¨next 25 | } start 26 | } 27 | 28 | ⎕← longest {N⍳N∩∊⍵+D[⍵⊃,M-1]}¨N←¯1⌽⍸×,M 29 | ⎕← longest {N⍳N∩∊⍵+⊢/D}¨N←¯1⌽⍸×,M 30 | -------------------------------------------------------------------------------- /2023/24.apl: -------------------------------------------------------------------------------- 1 | f ← ↑{⍎¨⍵⊆⍨⍵∊'-',⎕D}¨⊃⎕NGET'in/24.txt'1 2 | S E ← ⊃(7 27)(2e14 4e14)[10<≢f] 3 | 4 | ⎕← +/{ 5 | x y dx dy ← f[⍺;0 1 3 4] 6 | u v du dv ← f[⍵;0 1 3 4] 7 | =/dy dv×du dx: 0 8 | s ← (-/dv du×x y-u v) ÷ -/dy dx×du dv 9 | t ← (-/dy dx×u v-x y) ÷ -/dv du×dx dy 10 | cx cy ← x y+dx dy×s 11 | cx cy E E s t ×.> S S cx cy 0 0 12 | }/¨⍸∘.<⍨⍳≢f 13 | 14 | solve ← { 15 | dx dy dz vx vy vz ← ⍺-⍵ 16 | A ← 3 6⍴0 (-vz) vy 0 (-dz) dy vz 0 (-vx) dz 0 (-dx) (-vy) vx 0 (-dy) dx 0 17 | B ← (-/⍵[1 2]×⍵[5 4]) - (-/⍺[1 2]×⍺[5 4]) 18 | B,← (-/⍵[2 0]×⍵[3 5]) - (-/⍺[2 0]×⍺[3 5]) 19 | B,← (-/⍵[0 1]×⍵[4 3]) - (-/⍺[0 1]×⍺[4 3]) 20 | A B 21 | } 22 | A B ← ⊃⍪¨/{f[0;] solve f[⍵;]}¨1 2 23 | ⎕← +/3↑(⌹A)+.×B 24 | -------------------------------------------------------------------------------- /2023/25.apl: -------------------------------------------------------------------------------- 1 | f ← {⍵⊆⍨⍵∊⎕C⎕A}¨⊃⎕NGET'in/25.txt'1 2 | V ← ∪⊃,/f 3 | E ← ↑⊃,/(⊃,¨1↓⊢)¨V∘⍳¨f 4 | ⍝ Karger's algorithm 5 | ⎕← ×/{≢⍵}⌸ {⍺{2=≢∪⍺:⍺⋄u v←⍺[E[⊃⍵;]]⋄(⍺⌈(u⌈v)×(⍺=u)∨⍺=v)∇1↓⍵}?⍨≢E}⍣{3=+/≠/⍺[E]}⍨⍳≢V 6 | -------------------------------------------------------------------------------- /2023/README.md: -------------------------------------------------------------------------------- 1 | # AoC 2023 2 | 3 | Done in APL. 4 | -------------------------------------------------------------------------------- /2023/in/01e.txt: -------------------------------------------------------------------------------- 1 | two1nine 2 | eightwothree 3 | abcone2threexyz 4 | xtwone3four 5 | 4nineeightseven2 6 | zoneight234 7 | 7pqrstsixteen 8 | -------------------------------------------------------------------------------- /2023/in/02e.txt: -------------------------------------------------------------------------------- 1 | Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green 2 | Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue 3 | Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red 4 | Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red 5 | Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green 6 | -------------------------------------------------------------------------------- /2023/in/03e.txt: -------------------------------------------------------------------------------- 1 | 467..114.. 2 | ...*...... 3 | ..35..633. 4 | ......#... 5 | 617*...... 6 | .....+.58. 7 | ..592..... 8 | ......755. 9 | ...$.*.... 10 | .664.598.. 11 | -------------------------------------------------------------------------------- /2023/in/04e.txt: -------------------------------------------------------------------------------- 1 | Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 2 | Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 3 | Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 4 | Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 5 | Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 6 | Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11 7 | -------------------------------------------------------------------------------- /2023/in/05e.txt: -------------------------------------------------------------------------------- 1 | seeds: 79 14 55 13 2 | 3 | seed-to-soil map: 4 | 50 98 2 5 | 52 50 48 6 | 7 | soil-to-fertilizer map: 8 | 0 15 37 9 | 37 52 2 10 | 39 0 15 11 | 12 | fertilizer-to-water map: 13 | 49 53 8 14 | 0 11 42 15 | 42 0 7 16 | 57 7 4 17 | 18 | water-to-light map: 19 | 88 18 7 20 | 18 25 70 21 | 22 | light-to-temperature map: 23 | 45 77 23 24 | 81 45 19 25 | 68 64 13 26 | 27 | temperature-to-humidity map: 28 | 0 69 1 29 | 1 0 69 30 | 31 | humidity-to-location map: 32 | 60 56 37 33 | 56 93 4 34 | -------------------------------------------------------------------------------- /2023/in/06.txt: -------------------------------------------------------------------------------- 1 | Time: 55 82 64 90 2 | Distance: 246 1441 1012 1111 3 | -------------------------------------------------------------------------------- /2023/in/06e.txt: -------------------------------------------------------------------------------- 1 | Time: 7 15 30 2 | Distance: 9 40 200 3 | -------------------------------------------------------------------------------- /2023/in/07e.txt: -------------------------------------------------------------------------------- 1 | 32T3K 765 2 | T55J5 684 3 | KK677 28 4 | KTJJT 220 5 | QQQJA 483 6 | -------------------------------------------------------------------------------- /2023/in/08e.txt: -------------------------------------------------------------------------------- 1 | LR 2 | 3 | AAA = (11B, XXX) 4 | 11B = (XXX, ZZZ) 5 | ZZZ = (11B, XXX) 6 | 22A = (22B, XXX) 7 | 22B = (22C, 22C) 8 | 22C = (22Z, 22Z) 9 | 22Z = (22B, 22B) 10 | XXX = (XXX, XXX) 11 | -------------------------------------------------------------------------------- /2023/in/09e.txt: -------------------------------------------------------------------------------- 1 | 0 3 6 9 12 15 2 | 1 3 6 10 15 21 3 | 10 13 16 21 30 45 4 | -------------------------------------------------------------------------------- /2023/in/10e.txt: -------------------------------------------------------------------------------- 1 | .......... 2 | .S------7. 3 | .|F----7|. 4 | .||....||. 5 | .||....||. 6 | .|L-7F-J|. 7 | .|..||..|. 8 | .L--JL--J. 9 | .......... 10 | -------------------------------------------------------------------------------- /2023/in/11e.txt: -------------------------------------------------------------------------------- 1 | ...#...... 2 | .......#.. 3 | #......... 4 | .......... 5 | ......#... 6 | .#........ 7 | .........# 8 | .......... 9 | .......#.. 10 | #...#..... 11 | -------------------------------------------------------------------------------- /2023/in/12e.txt: -------------------------------------------------------------------------------- 1 | ???.### 1,1,3 2 | .??..??...?##. 1,1,3 3 | ?#?#?#?#?#?#?#? 1,3,1,6 4 | ????.#...#... 4,1,1 5 | ????.######..#####. 1,6,5 6 | ?###???????? 3,2,1 7 | -------------------------------------------------------------------------------- /2023/in/13e.txt: -------------------------------------------------------------------------------- 1 | #.##..##. 2 | ..#.##.#. 3 | ##......# 4 | ##......# 5 | ..#.##.#. 6 | ..##..##. 7 | #.#.##.#. 8 | 9 | #...##..# 10 | #....#..# 11 | ..##..### 12 | #####.##. 13 | #####.##. 14 | ..##..### 15 | #....#..# 16 | -------------------------------------------------------------------------------- /2023/in/14e.txt: -------------------------------------------------------------------------------- 1 | O....#.... 2 | O.OO#....# 3 | .....##... 4 | OO.#O....O 5 | .O.....O#. 6 | O.#..O.#.# 7 | ..O..#O..O 8 | .......O.. 9 | #....###.. 10 | #OO..#.... 11 | -------------------------------------------------------------------------------- /2023/in/15e.txt: -------------------------------------------------------------------------------- 1 | rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7 2 | -------------------------------------------------------------------------------- /2023/in/16e.txt: -------------------------------------------------------------------------------- 1 | .|...\.... 2 | |.-.\..... 3 | .....|-... 4 | ........|. 5 | .......... 6 | .........\ 7 | ..../.\\.. 8 | .-.-/..|.. 9 | .|....-|.\ 10 | ..//.|.... 11 | -------------------------------------------------------------------------------- /2023/in/17e.txt: -------------------------------------------------------------------------------- 1 | 2413432311323 2 | 3215453535623 3 | 3255245654254 4 | 3446585845452 5 | 4546657867536 6 | 1438598798454 7 | 4457876987766 8 | 3637877979653 9 | 4654967986887 10 | 4564679986453 11 | 1224686865563 12 | 2546548887735 13 | 4322674655533 14 | -------------------------------------------------------------------------------- /2023/in/18e.txt: -------------------------------------------------------------------------------- 1 | R 6 (#70c710) 2 | D 5 (#0dc571) 3 | L 2 (#5713f0) 4 | D 2 (#d2c081) 5 | R 2 (#59c680) 6 | D 2 (#411b91) 7 | L 5 (#8ceee2) 8 | U 2 (#caa173) 9 | L 1 (#1b58a2) 10 | U 2 (#caa171) 11 | R 2 (#7807d2) 12 | U 3 (#a77fa3) 13 | L 2 (#015232) 14 | U 2 (#7a21e3) 15 | -------------------------------------------------------------------------------- /2023/in/19e.txt: -------------------------------------------------------------------------------- 1 | px{a<2006:qkq,m>2090:A,rfg} 2 | pv{a>1716:R,A} 3 | lnx{m>1548:A,A} 4 | rfg{s<537:gd,x>2440:R,A} 5 | qs{s>3448:A,lnx} 6 | qkq{x<1416:A,crn} 7 | crn{x>2662:A,R} 8 | in{s<1351:px,qqz} 9 | qqz{s>2770:qs,m<1801:hdj,R} 10 | gd{a>3333:R,R} 11 | hdj{m>838:A,pv} 12 | 13 | {x=787,m=2655,a=1222,s=2876} 14 | {x=1679,m=44,a=2067,s=496} 15 | {x=2036,m=264,a=79,s=2244} 16 | {x=2461,m=1339,a=466,s=291} 17 | {x=2127,m=1623,a=2188,s=1013} 18 | -------------------------------------------------------------------------------- /2023/in/20.txt: -------------------------------------------------------------------------------- 1 | %jr -> mq, xn 2 | %zl -> tz, cm 3 | &lh -> nr 4 | %hx -> jx, tz 5 | %cm -> tz, ls 6 | &fk -> nr 7 | broadcaster -> sj, pf, kh, cn 8 | %gz -> mq, lq 9 | %gb -> xf, kr 10 | %zc -> rq 11 | %ln -> qj, xf 12 | %gq -> pp 13 | %fb -> xf 14 | %pf -> tg, nv 15 | %bc -> cf 16 | &tz -> cn, fk, ls 17 | %cq -> fb, xf 18 | %rq -> tg, dx 19 | %km -> gq 20 | &mq -> gq, xn, fv, km, lh, xv, sj 21 | %zp -> mq, xv 22 | %jx -> tz, np 23 | &tg -> mm, rp, zc, pf, bc 24 | %cv -> sq, xf 25 | %nv -> ht, tg 26 | %sq -> gb 27 | %kr -> ln 28 | %dk -> cv 29 | %xn -> zp 30 | %sx -> xf, cq 31 | %zt -> tz, fq 32 | %dx -> tg, qn 33 | &ff -> nr 34 | %bn -> hx, tz 35 | %fj -> zt, tz 36 | %ht -> rr, tg 37 | %fq -> tz, bn 38 | %kh -> dk, xf 39 | %sj -> mq, fv 40 | %vm -> zl, tz 41 | &mm -> nr 42 | %rp -> bc 43 | %fh -> sx 44 | %ls -> fj 45 | %xz -> mq, gz 46 | %fv -> km 47 | &nr -> rx 48 | %lq -> mq 49 | %xv -> xz 50 | %cn -> tz, vm 51 | %pp -> jr, mq 52 | %hn -> tg 53 | %qn -> hn, tg 54 | %rr -> rp, tg 55 | %cf -> tg, zc 56 | %qj -> fh, xf 57 | &xf -> sq, dk, fh, ff, kh, kr 58 | %np -> tz 59 | -------------------------------------------------------------------------------- /2023/in/20e.txt: -------------------------------------------------------------------------------- 1 | broadcaster -> a 2 | %a -> inv, con 3 | &inv -> b 4 | %b -> con 5 | &con -> rx 6 | -------------------------------------------------------------------------------- /2023/in/21e.txt: -------------------------------------------------------------------------------- 1 | ........... 2 | .....###.#. 3 | .###.##..#. 4 | ..#.#...#.. 5 | ....#.#.... 6 | .##..S####. 7 | .##..#...#. 8 | .......##.. 9 | .##.#.####. 10 | .##..##.##. 11 | ........... 12 | -------------------------------------------------------------------------------- /2023/in/22e.txt: -------------------------------------------------------------------------------- 1 | 1,0,1~1,2,1 2 | 0,0,2~2,0,2 3 | 0,2,3~2,2,3 4 | 0,0,4~0,2,4 5 | 2,0,5~2,2,5 6 | 0,1,6~2,1,6 7 | 1,1,8~1,1,9 8 | -------------------------------------------------------------------------------- /2023/in/23e.txt: -------------------------------------------------------------------------------- 1 | #.##################### 2 | #.......#########...### 3 | #######.#########.#.### 4 | ###.....#.>.>.###.#.### 5 | ###v#####.#v#.###.#.### 6 | ###.>...#.#.#.....#...# 7 | ###v###.#.#.#########.# 8 | ###...#.#.#.......#...# 9 | #####.#.#.#######.#.### 10 | #.....#.#.#.......#...# 11 | #.#####.#.#.#########v# 12 | #.#...#...#...###...>.# 13 | #.#.#v#######v###.###v# 14 | #...#.>.#...>.>.#.###.# 15 | #####v#.#.###v#.#.###.# 16 | #.....#...#...#.#.#...# 17 | #.#########.###.#.#.### 18 | #...###...#...#...#.### 19 | ###.###.#.###v#####v### 20 | #...#...#.#.>.>.#.>.### 21 | #.###.###.#.###.#.#v### 22 | #.....###...###...#...# 23 | #####################.# 24 | -------------------------------------------------------------------------------- /2023/in/24e.txt: -------------------------------------------------------------------------------- 1 | 19, 13, 30 @ -2, 1, -2 2 | 18, 19, 22 @ -1, -1, -2 3 | 20, 25, 34 @ -2, -2, -4 4 | 12, 31, 28 @ -1, -2, -1 5 | 20, 19, 15 @ 1, -5, -3 6 | -------------------------------------------------------------------------------- /2023/in/25e.txt: -------------------------------------------------------------------------------- 1 | jqt: rhn xhk nvd 2 | rsh: frs pzl lsr 3 | xhk: hfx 4 | cmg: qnr nvd lhk bvb 5 | rhn: xhk bvb hfx 6 | bvb: xhk hfx 7 | pzl: lsr hfx nvd 8 | qnr: nvd 9 | ntq: jqt hfx bvb xhk 10 | nvd: lhk 11 | lsr: lhk 12 | rzs: qnr cmg lsr rsh 13 | frs: qnr lhk lsr 14 | -------------------------------------------------------------------------------- /2024/01.apl: -------------------------------------------------------------------------------- 1 | a b ← ↓⍉↑⍎¨⊃⎕NGET'in/01.txt'1 2 | 3 | ⎕← +/|a-⍥{⍵[⍋⍵]}b 4 | ⎕← +/a×+/a∘.=b 5 | -------------------------------------------------------------------------------- /2024/02.apl: -------------------------------------------------------------------------------- 1 | n ← ⍎¨⊃⎕NGET'in/02.txt'1 2 | 3 | check ← {(1=≢∪×d)∧∧/1 2 3∊⍨|d←2-/⍵}¨ 4 | 5 | ⎕← +/check n 6 | ⎕← +/{∨/check(↓∘.≠⍨⍳⍴⍵)/¨⊂⍵}¨n 7 | -------------------------------------------------------------------------------- /2024/03.apl: -------------------------------------------------------------------------------- 1 | n ← ∊⊃⎕NGET'in/03.txt'1 2 | 3 | r ← 'mul\((\d+),(\d+)\)' 4 | ⎕← +/r ⎕S {×/⍵.Match(⍎¨∊⊆⊣)⎕D}n 5 | 6 | r ← '(mul\((\d+),(\d+)\)|do\(\)|don''t\(\))' 7 | e ← 1 8 | ⎕← +/r ⎕S { 9 | c←⍵.Match[2] 10 | '('=c: 0⊣e∘←1 11 | 'n'=c: 0⊣e∘←0 12 | e××/⍵.Match(⍎¨∊⊆⊣)⎕D 13 | }n 14 | -------------------------------------------------------------------------------- /2024/04.apl: -------------------------------------------------------------------------------- 1 | n ← ↑⊃⎕NGET'in/04.txt'1 2 | di ← {⍵⊂⍤⊢⌸⍥,⍨+/↑⍳⍴⍵} 3 | 4 | ⎕← +/∊'XMAS' 'SAMX' ∘.⍷ (⍉n)n,↑∘di¨(⌽n)n 5 | ⎕← +/∊{∧/∨⌿'SAM' 'MAS'∘.≡0 0∘⍉¨⍵(⌽⍵)}⌺3 3⊢n 6 | -------------------------------------------------------------------------------- /2024/05.apl: -------------------------------------------------------------------------------- 1 | rules pages ← (×∘≢¨⊆⊢)⊃⎕NGET'in/05.txt'1 2 | rules ← rules(⍎¨∊⊆⊣)¨⊂⎕D 3 | fixed ← {⍵[⍒rules∊⍨∘.,⍨⍵]}¨pages ← ⍎¨pages 4 | mid ← {⍵⊃⍨⌊.5×≢⍵}¨fixed 5 | ⎕← +/mid/⍨fixed≡¨pages 6 | ⎕← +/mid/⍨fixed≢¨pages 7 | -------------------------------------------------------------------------------- /2024/06.apl: -------------------------------------------------------------------------------- 1 | map ← '.#^'⍳↑⊃⎕NGET'in/06.txt'1 2 | dir ← (¯1 0)(0 1)(1 0)(0 ¯1) 3 | saw ← 0⍴⍨4,⍴map ⋄ start ← ⊃⍸2=map 4 | walk ← { ⍺←0 5 | saw[⊂⍺,⍵]: 0 6 | saw[⊂⍺,⍵] ← 1 7 | nxt ← ⍵+⍺⊃dir 8 | ∨/∊(nxt=¯1)(nxt=⍴⍺⍺): +/,∨⌿saw 9 | 1=⍺⍺[⊂nxt]: (4|1+⍺)∇⍵ 10 | ⍺∇nxt 11 | } 12 | ⎕← map walk start 13 | ⍝ part 2 takes ~30s 14 | ⎕← +/0={saw×←0 ⋄ (1@(⊂⍵)⊢map) walk start}¨⍸∨⌿saw 15 | -------------------------------------------------------------------------------- /2024/07.apl: -------------------------------------------------------------------------------- 1 | n ← {⍎¨⍵⊆⍨⍵∊⎕D}¨⊃⎕NGET'in/07.txt'1 2 | 3 | clean ← {⍵/⍨⍵=0⌈⌊⍵}∪ 4 | valid ← {0∊⊃(clean⍺⍺)/1⌽⍵} 5 | solve ← {+/⊃¨n/⍨⍺⍺valid¨⍵} 6 | ⎕← { (⍵~0)(-,÷)⍺ } solve n 7 | ⎕← {((⍵~0)(-,÷)⍺),(⍵-⍺)÷10*1+⌊10⍟⍺} solve n 8 | -------------------------------------------------------------------------------- /2024/08.apl: -------------------------------------------------------------------------------- 1 | m ← ↑⊃⎕NGET'in/08.txt'1 2 | ⎕← ≢⊃∪/{(⍸m≠⍵)∩ ,∘.{ ⍺ + ⍺-⍵}⍨⍸m=⍵}¨(∪∊m)~'.' 3 | ⎕← ≢⊃∪/{(⍸m≠0)∩⊃∪/,∘.{(⊂⍺)+¨(⍳⌈/⍴m)×⊂⍺-⍵}⍨⍸m=⍵}¨(∪∊m)~'.' 4 | -------------------------------------------------------------------------------- /2024/09.apl: -------------------------------------------------------------------------------- 1 | d ← ⎕D⍳⊃⊃⎕NGET'in/09.txt'1 2 | n ← d/⌈.5×i×2|i←1+⍳≢d 3 | gs gi ← d(0,¯1↓+\d)/⍨¨⊂(≢d)⍴0 1 4 | sig ← {(0⌈⍵-1)+.×⍳⍴⍵} 5 | 6 | ⎕← sig (0+.⊃i: 0 13 | n[i] ← 0 14 | n[gi[g]+⍳s] ← ⍵+1 15 | ⊢gi[g]+← gs[g]-← s 16 | }¨⌽⍳⌈/n 17 | -------------------------------------------------------------------------------- /2024/10.apl: -------------------------------------------------------------------------------- 1 | p ← ⎕D⍸⍤=¨⊂↑⊃⎕NGET'in/10.txt'1 2 | d ← (0 1)(1 0)(0 ¯1)(¯1 0) 3 | t ← 1 { 10=⍺: ⍵ ⋄ (⍺+1)∇(,⍵∘.+d)∩⍺⊃p }¨⊂¨0⊃p 4 | 5 | ⎕← +/≢∘∪¨t 6 | ⎕← +/≢ ¨t 7 | -------------------------------------------------------------------------------- /2024/11.apl: -------------------------------------------------------------------------------- 1 | n ← ⍪⍎⊃⊃⎕NGET'in/11.txt'1 2 | blink ← { ⍵=0: 1 ⋄ 2|d←1+⌊10⍟⍵: 2024×⍵ ⋄ (⌊⍵÷m),⍵|⍨m←10*d÷2 } 3 | run ← { n{⍺,+/⍵}⌸⍥∊(⊢/⍵)+0×n←blink¨⊣/⍵ } 4 | 5 | ⎕← +/⊢/run⍣25⊢n,1 6 | ⎕← +/⊢/run⍣75⊢n,1 7 | -------------------------------------------------------------------------------- /2024/12.apl: -------------------------------------------------------------------------------- 1 | G ← ↑⊃⎕NGET'in/12.txt'1 2 | 3 | a ← ,G{(G=⍺)/¨⍵}⍥(⊢,¨{⊂(,⍵)/⍨9⍴⍳2}⌺3 3) (⍴G)⍴p←⍳⍴,G 4 | p ← {p[⍵]}⍣≡p⊣ 2{p[⍵]←⍺}⍥({p[⍵]}⍣≡)/¨a 5 | 6 | C ← (3 0 1)(1 2 5)(5 8 7)(7 6 3) 7 | area ← {≢⍵}⌸p 8 | peri ← p{+/⍵}⌸5-≢¨a 9 | side ← p{+/⍵}⌸,{+/0 2 5∊⍨2⊥¨⍵[1;1]=C{⍵[⍺]}¨⊂,⍵}⌺3 3⊢(⍴G)⍴1+p 10 | 11 | ⎕← area+.×peri 12 | ⎕← area+.×side 13 | -------------------------------------------------------------------------------- /2024/13.apl: -------------------------------------------------------------------------------- 1 | n ← {{(¯2↑⍵)(⍉2 2⍴4↑⍵)}⍎¨⎕D(∊⍨⊆⊢)∊⍵}¨(×∘≢¨⊆⊢)⊃⎕NGET'in/13.txt'1 2 | 3 | solve ← {3 1+.×⍵×⍵≡⌊⍵}⌹ 4 | ⎕← +/solve/¨n 5 | ⎕← +/solve/¨n+⊂1e13 0 6 | -------------------------------------------------------------------------------- /2024/14.apl: -------------------------------------------------------------------------------- 1 | p v ← 1 0 1 0⊂↑{⍎¨⍵⊆⍨⍵∊'-',⎕D}¨⊃⎕NGET'in/14.txt'1 2 | 3 | m ← 101 103⍴0 ⍝ ⋄ m ← 11 7⍴0 4 | d ← p⍴⍥⍴m 5 | 6 | ⎕← ×/{ ∊⍵{⊂(0∊⍺)↓≢⍵}⌸⍨×⍵-⌊d÷2 } d|p+v×100 7 | ⎕← ⍸{ 1∊10⌊⌿1@(↓d|p+v×⍵)⊢m }¨⍳1e4 8 | -------------------------------------------------------------------------------- /2024/15.apl: -------------------------------------------------------------------------------- 1 | g m ← {⍵⊆⍨×≢¨⍵}⊃⎕NGET'in/15.txt'1 2 | M ← 0j1*'>v<^'⍳∊m 3 | 4 | w b r ← '#O@'∘.{+⌿0j1×@0⍉↑⍸⍺=⍵}⊂↑g 5 | B ← b 6 | next ← { ⍵+b[⍸b∊m]+←⍺×~∨/w∊⍺+m←⍺{⍵∪b∩⍺+⍵}⍣≡⍵ } 7 | ⎕← +/100 1+.×11 9∘.○b⊣ next/⌽r,M 8 | 9 | w b r ← {⍵+9○⍵}¨w B r 10 | w,← 1+w 11 | next ← { ⍵+b[⍸b∊m]+←⍺×~∨/w∊⍺+m←⍺{⍵∪m,1+m←b∩⍺+⍵,⍵-1}⍣≡⍵ } 12 | ⎕← +/100 1+.×11 9∘.○b⊣ next/⌽r,M 13 | -------------------------------------------------------------------------------- /2024/16.apl: -------------------------------------------------------------------------------- 1 | D ← ≢g ← ↑⊃⎕NGET'in/16.txt'1 2 | G ← ,g ⋄ S E ← G⍳'SE' ⋄ M ← 1e6 3 | step ← { M@(⍸G='#')⊢⍵⌊(1+¯1(-D)1D⊖⍤2⊢⍵)⌊1e3+⊃⌊/1 3⌽¨⊂⍵ } 4 | P ← step⍣≡0@(⊂S 0)⍉(4,≢G)⍴M 5 | ⎕← B ← ⌊/P[E;] 6 | 7 | Q ← 2⌽step⍣≡0@(⊂E,⍸2⌽B=P[E;])⍉(4,≢G)⍴M 8 | ⎕← +/∨/B=P+Q 9 | -------------------------------------------------------------------------------- /2024/17.apl: -------------------------------------------------------------------------------- 1 | a ← ⊃n ← ⍎¨⎕D(∊⍨⊆⊢)∊⊃⎕NGET'in/17.txt'1 2 | n ← 3↓n 3 | xor ← 2⊥(≠/2(⊥⍣¯1),) 4 | run ← ⍬ { ⍝ :: pc (out ∇∇) 0 1 2 3 A B C 5 | ⍺ ← 0 6 | ⍺≥≢n: ⍺⍺ 7 | op ← ⍺⊃n 8 | combo ← ⍵[n[⍺+1]] 9 | op=0: (⍺+2)∇(⌊⍵[4]÷2*combo)@4⊢⍵ 10 | op=1: (⍺+2)∇(⍵[5] xor n[⍺+1])@5⊢⍵ 11 | op=2: (⍺+2)∇(8|combo)@5⊢⍵ 12 | op=3: ⍵∇⍨(2 0+⍺,n[⍺+1])[⍵[4]≠0] 13 | op=4: (⍺+2)∇(⍵[5] xor ⍵[6])@5⊢⍵ 14 | op=5: ⍵((⍺⍺,8|combo)∇∇)⍨⍺+2 15 | op=6: (⍺+2)∇(⌊⍵[4]÷2*combo)@5⊢⍵ 16 | op=7: (⍺+2)∇(⌊⍵[4]÷2*combo)@6⊢⍵ 17 | 'unknown op'op 18 | } 19 | native ← { ⍝ :: ∇A 20 | ⍺ ← ⍬ 21 | 0=⍵: ⍺ 22 | b ← n[3] xor 8|⍵ 23 | ⍝ |--- depending on input, could be n[7] 24 | (⍺,8|n[9] xor b xor ⌊⍵÷2*b) ∇ ⌊⍵÷2*3 25 | } 26 | show ← {⊃{⍺,',',⍵}/⎕D[⍵]} 27 | ⎕← show run 0 1 2 3 a 0 0 28 | ⎕← show native a 29 | 30 | ⎕← { 31 | 0=≢⍵: 'no solution' 32 | n≡⍥≢⊃⍵: ⌊/8⊥¨⍵ 33 | next ← ,⍵∘.,⍳8 34 | ∇ next/⍨{⍵≡¨⊂(-≢⊃⍵)↑n}native¨8⊥¨next 35 | } 1+⍳7 36 | -------------------------------------------------------------------------------- /2024/18.apl: -------------------------------------------------------------------------------- 1 | 'bsearch'⎕CY'dfns' 2 | 3 | n ← ⍎¨f←⊃⎕NGET'in/18.txt'1 4 | D ← 71 ⋄ N ← 1024 ⍝ ⋄ D ← 7 ⋄ N ← 12 5 | G ← ,{⊂¯1~⍨(,⍵-1)/⍨9⍴⍳2}⌺3 3⊢0@(N↑n)⊢D D⍴1+⍳D×D 6 | skip ← D 1+.×⍉↑N↓n 7 | 8 | bfs ← {0{ 9 | ⍺≡⍬: 0 ⋄ ¯1≠⊢/⍵: ⊢/⍵ 10 | next ← (⊃∪/G[⍺])∩⍸⍵=¯1 11 | next ∇ (1+⌈/⍵)@next⊢⍵ 12 | }¯2@⍵⊢¯1+0=⍳≢G} 13 | 14 | ⎕← bfs ⍬ 15 | ⎕← f⊃⍨¯1+N+{0=bfs ⍵↑skip} bsearch 0,≢skip 16 | -------------------------------------------------------------------------------- /2024/19.apl: -------------------------------------------------------------------------------- 1 | T ← 'wubrg'(∊⍨⊆⊢)⊃f ← ⊃⎕NGET'in/19.txt'1 2 | 3 | K ← ,⊂'' ⋄ V ← ,1 ⋄ S ← ∪≢¨T 4 | c ← { 5 | (⊂⍵)∊K: ((K⍳⊂⍵)⊃V) 6 | n ← ≢¨T∩S↑¨⊂⍵ 7 | 0=≢n: 0⊣K V,∘⊂← ⍵ 0 8 | v ← +/∇¨n↓¨⊂⍵ 9 | K V,∘⊂← ⍵ v 10 | v 11 | }¨2↓f 12 | ⎕← +/×c 13 | ⎕← +/c 14 | -------------------------------------------------------------------------------- /2024/20.apl: -------------------------------------------------------------------------------- 1 | w ← ⊃⌽⍴g ← ↑⊃⎕NGET'in/20.txt'1 2 | g ← ,g ⋄ p1 ← 100 ⋄ p2 ← 100 ⍝ ⋄ p1 ← 2 ⋄ p2 ← 50 3 | p ← {⍵∪n/⍨'#'≠g[n←w¯1(-w)1+⊢/⍵]}⍣≡g⍳'S' 4 | d ← ⊃+/(|∘.-⍨)¨↓⍉↑⍸w w⍴1@p⊢0⊣¨g 5 | s ← ∘.-⍨⍋p 6 | ⎕← +/∊(2=d)∧p1≤s-d 7 | ⎕← +/∊(21>d)∧p2≤s-d 8 | -------------------------------------------------------------------------------- /2024/21.apl: -------------------------------------------------------------------------------- 1 | n ← ⊃⎕NGET'in/21.txt'1 2 | N ← ⍎¨n/⍨¨n∊¨⊂⎕D 3 | C ← 6 3⍴'789456123 0A ^a' 4 | 5 | step ← { 6 | s t ← ⍺⍵(⊃∘⍸=)¨⊂C 7 | d ← t-s 8 | v h ← (|d)/¨(1+×d)⊃¨'^.v' '<.>' 9 | flip ← ((⊢/0>d) ∨ ' '=C[⊂0 1⊃¨t s]) ∧ ' '≠C[⊂0 1⊃¨s t] 10 | flip: h,v,'a' 11 | v,h,'a' 12 | } 13 | run ← { ⌽↓⍉n{⍺,+/⍵}⌸⍥(⊃,/)⍺+0⊣¨¨n←2step/¨'aA'[1=≢⍵],¨⍵ } 14 | len ← {⍺+.×≢¨⍵} 15 | ⎕← N+.×len/¨(⊃run/)¨⍣3⊢(1,⊂∘⊂)¨n 16 | ⎕← N+.×len/¨(⊃run/)¨⍣26⊢(1,⊂∘⊂)¨n 17 | -------------------------------------------------------------------------------- /2024/22.apl: -------------------------------------------------------------------------------- 1 | N ← ⍎¨⊃⎕NGET'in/22.txt'1 2 | P ← ⍉⍪10|N 3 | next ← { 4 | n ← ⍵ 5 | n≠← 6⊖0@(⍳6)⊢n 6 | n≠← 0@(⍳5)⊢¯5⊖n 7 | n≠← 11⊖0@(⍳11)⊢n 8 | P⍪← 10|2⊥n 9 | n 10 | } 11 | 12 | ⎕← +/2⊥next⍣2000⊢(24/2)⊤N 13 | 14 | S ← ↓4,/2-⍨/⍉P 15 | M ← ∊≠¨S 16 | N ← ,⍉4↓P 17 | ⎕← ⌈/ (⊃,/S) {+/⍵}⌸⍥(M/⊢) N 18 | -------------------------------------------------------------------------------- /2024/23.apl: -------------------------------------------------------------------------------- 1 | c ← '-'(≠⊆⊢)¨⊃⎕NGET'in/23.txt'1 2 | V ← ∪⊃,/c 3 | E ← (⊢∨⍉)1@(V∘⍳¨c)⊢(2/⍴V)⍴0 4 | show ← ⊃{⍺,',',⍵}/ 5 | 6 | ⎕← ≢∪{⍵[⍋⍵]}¨{⊃,/{(⊂⍵),¨⍸∧⌿E[,⍵;]}¨⍵}⍣2⊢⍸'t'=⊃¨V 7 | ⎕← show {⍵[⍋⍵]} V[⊃C[(⊢⍳⌈/)≢¨C←{⍵,n↑⍨×≢n←⍸∧⌿E[,⍵;]}⍣≡¨⍳⍴V]] 8 | -------------------------------------------------------------------------------- /2024/24.apl: -------------------------------------------------------------------------------- 1 | v w ← (×∘≢¨⊆⊢)⊃⎕NGET'in/24.txt'1 2 | join ← ⊃{⍺,',',⍵}/ 3 | a op b _ c ← ↓⍉↑' '(≠⊆⊢)¨w 4 | ops ← 'XOR' 'AND' 'OR' 5 | 6 | {⍎∊(3↑⍵)(⊢/⍵),¨'←⍨'}¨v 7 | ⍎¨c,¨'←{('∘,¨a,¨'≠∧∨'[ops⍳op],¨b,¨⊂')⍵}' 8 | ⎕← 2⊥⍎'(',(join{⍵[⍒⍵]}c/⍨'z'=⊃¨c),')⍬' 9 | 10 | xor and or ← ↓ops∘.≡op 11 | M ← xor∧~∨⌿(⊃¨↑a b c)∊'xyz' 12 | M∨← (~xor)∧('z'=⊣/↑c)∧'z45'∘≢¨c 13 | M∨← and∧(c∊⊃,/xor∘/¨a b)∧⊃⍱/a b∊¨⊂⊂'x00' 14 | M∨← xor∧(c∊⊃,/or∘/¨a b)∧⊃⍱/a b∊¨⊂⊂'x00' 15 | ⎕← join{⍵[⍋⍵]} M/c 16 | 17 | -------------------------------------------------------------------------------- /2024/25.apl: -------------------------------------------------------------------------------- 1 | ⎕← .5×+/,∘.(∧.⍲)⍨∊¨(×∘≢¨⊆⊢)'#'=⊃⎕NGET'in/25.txt'1 2 | -------------------------------------------------------------------------------- /2024/in/01e.txt: -------------------------------------------------------------------------------- 1 | 3 4 2 | 4 3 3 | 2 5 4 | 1 3 5 | 3 9 6 | 3 3 -------------------------------------------------------------------------------- /2024/in/02e.txt: -------------------------------------------------------------------------------- 1 | 7 6 4 2 1 2 | 1 2 7 8 9 3 | 9 7 6 2 1 4 | 1 3 2 4 5 5 | 8 6 4 4 1 6 | 1 3 6 7 9 -------------------------------------------------------------------------------- /2024/in/03e.txt: -------------------------------------------------------------------------------- 1 | xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5)) -------------------------------------------------------------------------------- /2024/in/04e.txt: -------------------------------------------------------------------------------- 1 | MMMSXXMASM 2 | MSAMXMSMSA 3 | AMXSXMAAMM 4 | MSAMASMSMX 5 | XMASAMXAMM 6 | XXAMMXXAMA 7 | SMSMSASXSS 8 | SAXAMASAAA 9 | MAMMMXMMMM 10 | MXMXAXMASX -------------------------------------------------------------------------------- /2024/in/05e.txt: -------------------------------------------------------------------------------- 1 | 47|53 2 | 97|13 3 | 97|61 4 | 97|47 5 | 75|29 6 | 61|13 7 | 75|53 8 | 29|13 9 | 97|29 10 | 53|29 11 | 61|53 12 | 97|53 13 | 61|29 14 | 47|13 15 | 75|47 16 | 97|75 17 | 47|61 18 | 75|61 19 | 47|29 20 | 75|13 21 | 53|13 22 | 23 | 75,47,61,53,29 24 | 97,61,53,29,13 25 | 75,29,13 26 | 75,97,47,61,53 27 | 61,13,29 28 | 97,13,75,29,47 -------------------------------------------------------------------------------- /2024/in/06e.txt: -------------------------------------------------------------------------------- 1 | ....#..... 2 | .........# 3 | .......... 4 | ..#....... 5 | .......#.. 6 | .......... 7 | .#..^..... 8 | ........#. 9 | #......... 10 | ......#... -------------------------------------------------------------------------------- /2024/in/07e.txt: -------------------------------------------------------------------------------- 1 | 190: 10 19 2 | 3267: 81 40 27 3 | 83: 17 5 4 | 156: 15 6 5 | 7290: 6 8 6 15 6 | 161011: 16 10 13 7 | 192: 17 8 14 8 | 21037: 9 7 18 13 9 | 292: 11 6 16 20 -------------------------------------------------------------------------------- /2024/in/08e.txt: -------------------------------------------------------------------------------- 1 | ............ 2 | ........0... 3 | .....0...... 4 | .......0.... 5 | ....0....... 6 | ......A..... 7 | ............ 8 | ............ 9 | ........A... 10 | .........A.. 11 | ............ 12 | ............ -------------------------------------------------------------------------------- /2024/in/09e.txt: -------------------------------------------------------------------------------- 1 | 2333133121414131402 -------------------------------------------------------------------------------- /2024/in/10e.txt: -------------------------------------------------------------------------------- 1 | 89010123 2 | 78121874 3 | 87430965 4 | 96549874 5 | 45678903 6 | 32019012 7 | 01329801 8 | 10456732 -------------------------------------------------------------------------------- /2024/in/11.txt: -------------------------------------------------------------------------------- 1 | 0 5601550 3914 852 50706 68 6 645371 2 | -------------------------------------------------------------------------------- /2024/in/11e.txt: -------------------------------------------------------------------------------- 1 | 125 17 -------------------------------------------------------------------------------- /2024/in/12e.txt: -------------------------------------------------------------------------------- 1 | AAAA 2 | BBCD 3 | BBCC 4 | EEEC -------------------------------------------------------------------------------- /2024/in/13e.txt: -------------------------------------------------------------------------------- 1 | Button A: X+94, Y+34 2 | Button B: X+22, Y+67 3 | Prize: X=8400, Y=5400 4 | 5 | Button A: X+26, Y+66 6 | Button B: X+67, Y+21 7 | Prize: X=12748, Y=12176 8 | 9 | Button A: X+17, Y+86 10 | Button B: X+84, Y+37 11 | Prize: X=7870, Y=6450 12 | 13 | Button A: X+69, Y+23 14 | Button B: X+27, Y+71 15 | Prize: X=18641, Y=10279 -------------------------------------------------------------------------------- /2024/in/14e.txt: -------------------------------------------------------------------------------- 1 | p=0,4 v=3,-3 2 | p=6,3 v=-1,-3 3 | p=10,3 v=-1,2 4 | p=2,0 v=2,-1 5 | p=0,0 v=1,3 6 | p=3,0 v=-2,-2 7 | p=7,6 v=-1,-3 8 | p=3,0 v=-1,-2 9 | p=9,3 v=2,3 10 | p=7,3 v=-1,2 11 | p=2,4 v=2,-3 12 | p=9,5 v=-3,-3 -------------------------------------------------------------------------------- /2024/in/15e.txt: -------------------------------------------------------------------------------- 1 | ########## 2 | #..O..O.O# 3 | #......O.# 4 | #.OO..O.O# 5 | #..O@..O.# 6 | #O#..O...# 7 | #O..O..O.# 8 | #.OO.O.OO# 9 | #....O...# 10 | ########## 11 | 12 | ^v>^vv^v>v<>v^v<<><>>v^v^>^<<<><^ 13 | vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<^<^^>>>^<>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^v^^<^^vv< 15 | <>^^^^>>>v^<>vvv^>^^^vv^^>v<^^^^v<>^>vvvv><>>v^<<^^^^^ 16 | ^><^><>>><>^^<<^^v>>><^^>v>>>^v><>^v><<<>vvvv>^<><<>^>< 17 | ^>><>^v<><^vvv<^^<><^v<<<><<<^^<^>>^<<<^>>^v^>>^v>vv>^<<^v<>><<><<>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^ 19 | <><^^>^^^<>^vv<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<> 20 | ^^>vv<^v^v^<>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<>< 21 | v^^>>><<^^<>>^v^v^<<>^<^v^v><^<<<><<^vv>>v>v^<<^ -------------------------------------------------------------------------------- /2024/in/16e.txt: -------------------------------------------------------------------------------- 1 | ############### 2 | #.......#....E# 3 | #.#.###.#.###.# 4 | #.....#.#...#.# 5 | #.###.#####.#.# 6 | #.#.#.......#.# 7 | #.#.#####.###.# 8 | #...........#.# 9 | ###.#.#####.#.# 10 | #...#.....#.#.# 11 | #.#.#.###.#.#.# 12 | #.....#...#.#.# 13 | #.###.#.#.#.#.# 14 | #S..#.....#...# 15 | ############### -------------------------------------------------------------------------------- /2024/in/17.txt: -------------------------------------------------------------------------------- 1 | Register A: 51342988 2 | Register B: 0 3 | Register C: 0 4 | 5 | Program: 2,4,1,3,7,5,4,0,1,3,0,3,5,5,3,0 6 | -------------------------------------------------------------------------------- /2024/in/18e.txt: -------------------------------------------------------------------------------- 1 | 5,4 2 | 4,2 3 | 4,5 4 | 3,0 5 | 2,1 6 | 6,3 7 | 2,4 8 | 1,5 9 | 0,6 10 | 3,3 11 | 2,6 12 | 5,1 13 | 1,2 14 | 5,5 15 | 2,5 16 | 6,5 17 | 1,4 18 | 0,4 19 | 6,4 20 | 1,1 21 | 6,1 22 | 1,0 23 | 0,5 24 | 1,6 25 | 2,0 -------------------------------------------------------------------------------- /2024/in/19e.txt: -------------------------------------------------------------------------------- 1 | r, wr, b, g, bwu, rb, gb, br 2 | 3 | brwrr 4 | bggr 5 | gbbr 6 | rrbgbr 7 | ubwu 8 | bwurrg 9 | brgr 10 | bbrgwb -------------------------------------------------------------------------------- /2024/in/20e.txt: -------------------------------------------------------------------------------- 1 | ############### 2 | #...#...#.....# 3 | #.#.#.#.#.###.# 4 | #S#...#.#.#...# 5 | #######.#.#.### 6 | #######.#.#...# 7 | #######.#.###.# 8 | ###..E#...#...# 9 | ###.#######.### 10 | #...###...#...# 11 | #.#####.#.###.# 12 | #.#...#.#.#...# 13 | #.#.#.#.#.#.### 14 | #...#...#...### 15 | ############### -------------------------------------------------------------------------------- /2024/in/21.txt: -------------------------------------------------------------------------------- 1 | 480A 2 | 143A 3 | 983A 4 | 382A 5 | 974A 6 | -------------------------------------------------------------------------------- /2024/in/21e.txt: -------------------------------------------------------------------------------- 1 | 029A 2 | 980A 3 | 179A 4 | 456A 5 | 379A -------------------------------------------------------------------------------- /2024/in/22e.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | 3 4 | 2024 -------------------------------------------------------------------------------- /2024/in/23e.txt: -------------------------------------------------------------------------------- 1 | kh-tc 2 | qp-kh 3 | de-cg 4 | ka-co 5 | yn-aq 6 | qp-ub 7 | cg-tb 8 | vc-aq 9 | tb-ka 10 | wh-tc 11 | yn-cg 12 | kh-ub 13 | ta-co 14 | de-co 15 | tc-td 16 | tb-wq 17 | wh-td 18 | ta-ka 19 | td-qp 20 | aq-cg 21 | wq-ub 22 | ub-vc 23 | de-ta 24 | wq-aq 25 | wq-vc 26 | wh-yn 27 | ka-de 28 | kh-ta 29 | co-tc 30 | wh-qp 31 | tb-vc 32 | td-yn -------------------------------------------------------------------------------- /2024/in/24e.txt: -------------------------------------------------------------------------------- 1 | x00: 1 2 | x01: 0 3 | x02: 1 4 | x03: 1 5 | x04: 0 6 | y00: 1 7 | y01: 1 8 | y02: 1 9 | y03: 1 10 | y04: 1 11 | 12 | ntg XOR fgs -> mjb 13 | y02 OR x01 -> tnw 14 | kwq OR kpj -> z05 15 | x00 OR x03 -> fst 16 | tgd XOR rvg -> z01 17 | vdt OR tnw -> bfw 18 | bfw AND frj -> z10 19 | ffh OR nrd -> bqk 20 | y00 AND y03 -> djm 21 | y03 OR y00 -> psh 22 | bqk OR frj -> z08 23 | tnw OR fst -> frj 24 | gnj AND tgd -> z11 25 | bfw XOR mjb -> z00 26 | x03 OR x00 -> vdt 27 | gnj AND wpb -> z02 28 | x04 AND y00 -> kjc 29 | djm OR pbm -> qhw 30 | nrd AND vdt -> hwm 31 | kjc AND fst -> rvg 32 | y04 OR y02 -> fgs 33 | y01 AND x02 -> pbm 34 | ntg OR kjc -> kwq 35 | psh XOR fgs -> tgd 36 | qhw XOR tgd -> z09 37 | pbm OR djm -> kpj 38 | x03 XOR y03 -> ffh 39 | x00 XOR y04 -> ntg 40 | bfw OR bqk -> z06 41 | nrd XOR fgs -> wpb 42 | frj XOR qhw -> z04 43 | bqk OR frj -> z07 44 | y03 OR x01 -> nrd 45 | hwm AND bqk -> z03 46 | tgd XOR rvg -> z12 47 | tnw OR pbm -> gnj -------------------------------------------------------------------------------- /2024/in/25e.txt: -------------------------------------------------------------------------------- 1 | ##### 2 | .#### 3 | .#### 4 | .#### 5 | .#.#. 6 | .#... 7 | ..... 8 | 9 | ##### 10 | ##.## 11 | .#.## 12 | ...## 13 | ...#. 14 | ...#. 15 | ..... 16 | 17 | ..... 18 | #.... 19 | #.... 20 | #...# 21 | #.#.# 22 | #.### 23 | ##### 24 | 25 | ..... 26 | ..... 27 | #.#.. 28 | ###.. 29 | ###.# 30 | ###.# 31 | ##### 32 | 33 | ..... 34 | ..... 35 | ..... 36 | #.... 37 | #.#.. 38 | #.#.# 39 | ##### -------------------------------------------------------------------------------- /everybody.codes/2024/01.apl: -------------------------------------------------------------------------------- 1 | parse ← {(⍺÷⍨≢f)⍺⍴f←'AB C D'⍳⊃⊃⎕NGET('in/',⍵,'.txt')1} 2 | solve ← {+/(+/6|⍵)+{⍵×⍵-1}+/6≠⍵} 3 | 4 | ⎕← solve 1parse'01_1' 5 | ⎕← solve 2parse'01_2' 6 | ⎕← solve 3parse'01_3' 7 | -------------------------------------------------------------------------------- /everybody.codes/2024/in/01_1.txt: -------------------------------------------------------------------------------- 1 | AABCBBCACBCBCCBCABACBBBABBCBBCBCBAAACBBBBBBABCACBCACBCCBACBBCBBCCBABCCCBCBBCABACAAACAABCCBCBBCCBACAACACCCACACABBAAAABBBACBAAABABACACAACAAAAACABAABABCBACBBCCCABACCCBCACCBBBBBACBCBACAACCAABCBABABCCCBCCCACACCACBCCCBCBBABCACCCCBAACABBBCCBACAACBABBBCBAAABBBABBBCAABBACBBCCAABBCACBBBABAACCAAABBAABBAACAABABCBAABBBBBBCCBACCCBACAAACAAAAABAACABAACACCCABABACBACBCBBBBCACABABBACACABCCBCBCBCCACABAABACACACBAABCCABCACCABCCBCCBCACCCBCCACABACCBABCCBAABBBABBBBACABCCCAABBBBACABCCBBCBAABBBCCABCABBABCBABCCABCCACCAABBBABCBAABCABACBAAABCCABABCBBBCCAACABCBCCCACCCABABBACABAACABAACCCCBBACBBAABCBBCCCABCABABABACBCBBBCABBABACBBABBBBCCBCAABCAABACAACCCBCBAABBCCCCCBABCABACAAABABAABACBBACBCACCBABBBBCBCBABABACBABBBAABAAABABACCACCCBAAACBCABBABCCCBBBAABCBAAACBBBACCACCBCBBAAAABCACAAACBCBCCAAACACBABBAAACCCBBBCCCBBBAACCAAACBACACAABAAABCABCCCCCBABBBAABCAACACABBBBCCBCBCBCCBCAABCBBCCABBCCCBCCCCACBBCAAAABACBBCABABBBBBCCABBACCACCAAACCBBCCCACCAABACACCABBACBBBBCCCCCCBCCAACAAABCBABABCBBCABAACCCBBCACACCBABAAACBACACAACCCBBBACAAACCABCBC -------------------------------------------------------------------------------- /everybody.codes/2024/in/01_1e.txt: -------------------------------------------------------------------------------- 1 | ABBAC 2 | -------------------------------------------------------------------------------- /everybody.codes/2024/in/01_2e.txt: -------------------------------------------------------------------------------- 1 | AxBCDDCAxD -------------------------------------------------------------------------------- /everybody.codes/2024/in/01_3e.txt: -------------------------------------------------------------------------------- 1 | xBxAAABCDxCC --------------------------------------------------------------------------------