{+/β΅Γ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.