{+/⍵×2⌷⍉=\⍉2((⍴⍵)[1])⍴ ⍵, (1↓⍵),'⍝'} {⍵,⍵[1]} {⍵-48} ⎕UCS
{+/⍵×⍵=(⍴⍵)↑((⍴⍵)÷2)↓(⍵,⍵)} {⍵-48} ⎕UCS
The checksum for each row can be found with:
{⌈/⍵-⌊/⍵} 5 1 9 5
Having trouble applying that to a matrix of the input, maybe I need to convert to a vector of vectors?
split & each works
+/{⌈/⍵-⌊/⍵}¨↓ 2 4⍴ 5 1 9 5 1 2 3 4
sort a vector
{⍵[⍋⍵]} 5 9 2 8
boolean integer test
0=1⊤ 4.5
outer product
{⍵∘.÷⍵}5 9 2 8
discard diagonals
{{⍵∘.≠⍵}⍳⍴⍵}5 9 2 8
combined
{(0=1⊤ ⍵∘.÷⍵)×({⍵∘.≠⍵}⍳⍴⍵)}5 9 2 8
({⍵∘.,⍵}⍳⍴⍵)
solution
+/{+/+/{⍵×0=1⊤⍵}(⍵∘.÷⍵)×({⍵∘.≠⍵}⍳⍴⍵)}¨↓
http://adventofcode.com/2017/day/3
want to expand the index into vectors of turn coordinates
{{⍵,⍵}{1 2⍴1 0}¨⍳⍵}¨⍳4
spiral vector of offsets
{{((1 ¯1)[1+~2|⍵])×{⍵,⍵-1}⍵⍴(⊂1 0)}¨ ⍳4} 23
┌──────────┬───────────────────┬────────────────────────────┬─────────────────────────────────────┐
│┌───┬────┐│┌────┬────┬───┬───┐│┌───┬───┬───┬────┬────┬────┐│┌────┬────┬────┬────┬───┬───┬───┬───┐│
││1 0│0 ¯1│││¯1 0│¯1 0│0 1│0 1│││1 0│1 0│1 0│0 ¯1│0 ¯1│0 ¯1│││¯1 0│¯1 0│¯1 0│¯1 0│0 1│0 1│0 1│0 1││
│└───┴────┘│└────┴────┴───┴───┘│└───┴───┴───┴────┴────┴────┘│└────┴────┴────┴────┴───┴───┴───┴───┘│
└──────────┴───────────────────┴────────────────────────────┴─────────────────────────────────────┘
Then make sure there's enough offsets for the input, take the input and reduce into manhattan distance
solution
{+/⊃|+/(⍵-1)↑⊃,/{((1 ¯1)[1+~2|⍵])×{⍵,⍵-1}⍵⍴(⊂1 0)}¨ ⍳1000} 361527
mutating a matrix
H←50
M←100 100⍴0
COORDS←{H+⍵}
GET←{(COORDS ⍵)⌷M}
SET←{⍺{M[⍵[1];⍵[2]]←⍺}(COORDS ⍵)}
NBRS←9⍴{⍵∘.,⍵}(2 - ⍳3)
NSUM←{+/GET¨NBRS + ⊂⍵}
⍝ initial center value
1 SET 0 0
scan our spiral of offsets from part 1 into coordinates
SPIRAL←+\⊃,/{((1 ¯1)[1+~2|⍵])×{⍵,⍵-1}⍵⍴(⊂1 0)}¨ ⍳50
set the NSUM for each location
{(NSUM ⍵) SET ⍵}¨SPIRAL
finally get the first spiral nsum value above our input
361527 {⍵[1+1⌷{≢⍵}⌸(⍺ < ⍵)]} GET¨SPIRAL
⍝ 363010
⍝ this works with gnu-apl
⊃{(¯1+(⍵=0)⍳0)↓⍵}{⍵×(361527 < ⍵)} GET¨SPIRAL
http://adventofcode.com/2017/day/4
splitting a string into words:
modified this but it only works with dyalog, the only other example i could find was https://dfns.dyalog.com/c_words.htm
gnu word split
{(⍵≠' ') ⊂ ⍵}
{{1↓¨⍵⊂⍨⍵∊' '}' ',⍵} 'the aa bb cc dd aa '
┌───┬──┬┬──┬──┬──┬──┬┬┐
│the│aa││bb│cc│dd│aa│││
└───┴──┴┴──┴──┴──┴──┴┴┘
if non single whitespace is in input, could remove with:
{(A∨¯1↓1,A←⍵≠' ')/⍵}
solution (for a single line)
0=+/{1≠≢⍵}⌸{{1↓¨⍵⊂⍨⍵∊' '}' ',⍵} 'aa bb cc dd ee'
should work but wrong??
+/{0=+/{1≠≢⍵}⌸{{1↓¨⍵⊂⍨⍵∊' '}' ',⍵}⍵}¨LINES
{(⍴⍵)≥+/,2⌷¨{⍵∘.=⍵}⍵}{(⍵≠' ') ⊂ ⍵} 'aa bb cc dd ee'
from IRC
{⍵≡∪⍵} 'ab' 'ab'
{{⍵≡∪⍵}{⍵[⍋⍵]}¨(' '≠⍵)⊂⍵} 'ab ba'
http://adventofcode.com/2017/day/5
part1
∇R←idx RUN vec;cnt;offset
cnt←1
LOOP:
idx←idx+{vec[⍵]←1+vec[⍵]} idx
idx←idx-1
R←cnt
→(idx<1)/0
→(idx>⍴vec)/0
cnt←cnt+1
→LOOP
∇
part2
∇R←idx RUN2 vec;cnt;offset;mod
cnt←1
LOOP:
offset←vec[idx]
mod←(1-2×offset>2)
idx←idx+{vec[⍵]←mod+vec[⍵]} idx
idx←idx-mod
R←cnt
→(idx<1)/0
→(idx>⍴vec)/0
cnt←cnt+1
→LOOP
∇
⍝ hack to get multiple statements into an inline function
{1⌷4,⍵} 5
⍝ load a file
⎕FIO[49]'data/day5.txt'
⍝ control flow of some sort
{⎕ES(⍵>100)/'Too high'} 110
{⍎(⍵>100)/'⎕←''Too high'' ⋄ →0'} 110
⍝ klg ¦ → takes a line number within defined function (which can be given using a
⍝ ¦ label), there are some common idioms like →(condition)/label for
⍝ ¦ conditional jump
⍝ klg ¦ ah yes, and jumping to 0 returns
⍝ klg ¦ so something like R←A (F OP G) B;X;Y defines an operator OP that takes
⍝ ¦ functions F and G as its operands and A and B as arguments of derived
⍝ ¦ function, returns value R, and localizes two wariables X and Y
∇R←left TEST right
left + right
∇
⍝ klg ¦ in Dyalog and in NARS2000, however, the above line declares a function that
⍝ ¦ is strictly dyadic and it's an error to call it monadically (you use
⍝ ¦ barckets {left} TEST right to declare ambivalent one there)
⍝ expunge a binding
⎕EX 'RUN'
⍝ call stack during suspended function
)SI
⍝ klg ¦ if that's the case )SI will print something resembling call stack, ⎕LC will
⍝ ¦ give you current line number (and sometimes you can correct the error and
⍝ ¦ continue using →⎕LC), and local variables will be in scope
⍝ klg ¦ you can also escape to toplevel using → without arguments, or clear stack
⍝ ¦ using )SIC, then it should allow you to )ERASE that function
http://adventofcode.com/2017/day/6
BLOCKS←5 1 10 0 1 7 13 14 3 12 8 10 7 12 0 6
⍝ find greatest index
{⍵⍳⌈/⍵} 0 2 7 0
⍝ 3
⍝ vec, idx, blocks
⍝ greatest index, it's value, original vector with greatest index set to 0
CLEAR← {⍵ {(⊂⍺×⍵≠⍳⍴⍺),⍵, ⍺[⍵]} ⍵⍳⌈/⍵}
CLEAR 0 2 7 0
⍝ 0 2 0 0 3 7
⍝ redistribution:
⍝ given the starting index and block count, we want a vector like:
2 2 1 2
⍝ reshape vector of n ones into a matrix with padding
⍝ +⌿ reduces the columns
REDIST←{+⌿(⌈(⍺÷⍵))⍵⍴(⍺⍴1),⍺⍴0}
⍝ blocks FN ⍴
7 REDIST 4
⍝ 2 2 2 1
⍝ we also need to rotate the redist to match the cleared index
¯3 ⌽ 7 REDIST 4
⍝ 2 2 1 2
⍝ finally add with the cleared vector
0 2 0 0 + ¯3 ⌽ 7 REDIST 4
⍝ 2 4 1 2
BALANCE←{{(⊃⍵[1])+(⍵[2]ׯ1)⌽{⍵[1] REDIST ⍵[2]} {(⍵[3]) , ⍴⊃⍵[1]} ⍵} CLEAR ⍵}
BALANCE 0 2 7 0
⍝ 2 4 1 2
BALANCE 2 4 1 2
BALANCE 3 1 2 3
BALANCE 0 2 3 4
BALANCE 1 3 4 1
⍝ 2 4 1 2
⍝ iterate until the redistribution has been seen
⍝ vector equality
VEQ←{~0∊⍺=⍵}
1 2 VEQ 1 1
⍝ vector membership
⍝ why doesn't VEQ work like this?
3=1 2 3 4
0 0 1 0
VMEMB← {0≠+/(⊂⍺) {⍺ {~0∊⍺=⍵} ⍵}¨⍵}
1 2 VMEMB (1 2) (2 3)
⎕EX 'RUN'
∇R←RUN vec;cnt;seen;nvec
cnt←1
seen←(0) ⍝ needs an inital value?
LOOP:
nvec← BALANCE vec
R←cnt
→(nvec VMEMB seen)/0
seen←seen,⊂nvec
vec←nvec
cnt←cnt+1
→LOOP
∇
RUN 0 2 7 0
RUN BLOCKS
⍝ in part 2 we just need to find the interval of the loop
VMEMBIDX← {+/(⍳⍴⍵)× (⊂⍺) {⍺ {~0∊⍺=⍵} ⍵}¨⍵}
⎕EX 'RUN2'
∇R←RUN2 vec;cnt;seen;nvec
cnt←1
seen←(0) ⍝ needs an inital value?
LOOP:
nvec← BALANCE vec
R←cnt+1 - nvec VMEMBIDX seen
→(nvec VMEMB seen)/0
seen←seen,⊂nvec
vec←nvec
cnt←cnt+1
→LOOP
∇
RUN2 0 2 7 0
RUN2 BLOCKS
http://adventofcode.com/2017/day/7
⍝ first task is to read the input text into data
⍝ this will get us a list of line strings
LINES←⎕FIO[49] 'data/day7'
⍝ partition the parts we want
{(1≠⍵∊' ->,()') ⊂ ⍵} 'vpbdpfm (74) -> ndegtj, wnwxs'
⍝┏→━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
⍝┃"vpbdpfm" "74" "ndegtj" "wnwxs"┃
⍝┗∊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
⍝ eval the weight entry
⍝ TODO refactor, this is ugly
DATA←{{⍵[2],⍵[3],⍵[1]}{(⊂2↓⍵), ⍵[1], ⍎⊃⍵[2]}{(1≠⍵∊' ->,()') ⊂ ⍵} ⊃⍵}¨LINES
IDS←⊃,/{⍵[1]}¨DATA
CHILDREN←⊃,/{⊃⍵[3]}¨DATA
⍝ invert id membership in children, multiply by id index, get id
CHILDREN {⍵{⊃⍺[⍵]}+/(⍳⍴⍵)×1≠(⍵∊⍺)} IDS
⍝"azqje"
⍝ we need a way to look up data by the id
GET←{⊃⍺[(⊃,/{⍵[1]}¨⍺)⍳⊂⍵]}
⍝ getting children of an id
{⊃⍵[3]} DATA GET "azqje"
⍝┏→━━━━━━━━━━━━━━━━━━━━━━━┓
⍝┃"holcy" "fwbang" "inwmb"┃
⍝┗∊━━━━━━━━━━━━━━━━━━━━━━━┛
⍝ weigh recursion
⍝ return w +/weigh children
⍝ if child weights are not =, investigate
⍝ test member equality
{^/⎕IO=⍵⍳⍵} 12 12 10
⍝ empty list
SOLUTION←⍳0
⎕EX 'WEIGH'
∇R←data WEIGH id;node;w;children;cw
node←data GET id
w←{⊃⍵[2]} node
children←{⊃⍵[3]} node
cw←{DATA WEIGH ⍵}¨children
⍝ conditionally append a value
SOLUTION←SOLUTION,(0={^/⎕IO=⍵⍳⍵} cw)/(children, cw)
R←w + +/cw
∇
⍝ super slow
DATA WEIGH 'azqje'
⍝ from here we can figure out the solution in the repl
SOLUTION
⍝ nmhmw pknpuej rfkvap 1051 1051 1060
⍝ ghaxmrh vqxwlkh nzeqmqi lokmiua znypga vtpoo 15369 15369 15378 15369 15369 15369
⍝ holcy fwbang inwmb 145260 145260 145269
(1051-1060) + {⊃⍵[2]} DATA GET 'rfkvap'
⍝ 646
]BOXING ¯29
⍝ loke ¦ selfsame: You can render a single value using pretty-printing using n⎕CR
⍝ ¦ where n is a number describing the type of formatting. Useful ones to try it
⍝ ¦ 8 and 29. 29 is my favourite
⍝ loke ¦ You can also tell GNU APL to always render using pretty-printing, use
⍝ ¦ ]BOXING n for this purpose, where n again is the type of rendering.