Skip to content

Instantly share code, notes, and snippets.

@selfsame
Last active December 7, 2017 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save selfsame/520145c0631e21570c69a579f4b436db to your computer and use it in GitHub Desktop.
Save selfsame/520145c0631e21570c69a579f4b436db to your computer and use it in GitHub Desktop.

Day 1

{+/×2=\2(()[1]) , (1),''} {,[1]} {-48} ⎕UCS

{+/×=()(()÷2)(,)} {-48} ⎕UCS

Day 2

part 1

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

part 2

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}(.÷)×({.})}¨

Day 3

http://adventofcode.com/2017/day/3

part 1

want to expand the index into vectors of turn coordinates

 {{,}{1 21 0}¨}¨4

spiral vector of offsets

      {{((1 ¯1)[1+~2|])×{,-1}(1 0)}¨ 4} 23

┌──────────┬───────────────────┬────────────────────────────┬─────────────────────────────────────┐
│┌───┬────┐│┌────┬────┬───┬───┐│┌───┬───┬───┬────┬────┬────┐│┌────┬────┬────┬────┬───┬───┬───┬───┐│
││1 00 ¯1│││¯1 0¯1 00 10 1│││1 01 01 00 ¯10 ¯10 ¯1│││¯1 0¯1 0¯1 0¯1 00 10 10 10 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

part 2

mutating a matrix

H50
M100 1000 
COORDS{H+}
GET{(COORDS )M}
SET{{M[[1];[2]]}(COORDS )}
NBRS9{.,}(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

Day 4

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  '
┌───┬──┬┬──┬──┬──┬──┬┬┐
│theaa││bbccddaa│││
└───┴──┴┴──┴──┴──┴──┴┴┘

if non single whitespace is in input, could remove with:

{(A¯11,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'

part 2

{{}{[]}¨(' ')} 'ab ba'

Day 5

http://adventofcode.com/2017/day/5

part1

Ridx RUN vec;cnt;offset
	cnt1
	LOOP:
	idxidx+{vec[]1+vec[]} idx
	idxidx-1
	Rcnt
	(idx<1)/0
	(idx>vec)/0
	cntcnt+1
	LOOP

part2

Ridx RUN2 vec;cnt;offset;mod
	cnt1
	LOOP:
	offsetvec[idx]
	mod(1-2×offset>2)
	idxidx+{vec[]mod+vec[]} idx
	idxidx-mod
	Rcnt
	(idx<1)/0
	(idx>vec)/0
	cntcnt+1
	LOOP

notes

 hack to get multiple statements into an inline function
{14,} 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

Rleft 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

Day 6

http://adventofcode.com/2017/day/6

BLOCKS5 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'
RRUN vec;cnt;seen;nvec
	cnt1
	seen(0)  needs an inital value?
	LOOP:
		nvec BALANCE vec
		Rcnt
		(nvec VMEMB seen)/0
		seenseen,nvec
		vecnvec
		cntcnt+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'
RRUN2 vec;cnt;seen;nvec
	cnt1
	seen(0)  needs an inital value?
	LOOP:
		nvec BALANCE vec
		Rcnt+1 - nvec VMEMBIDX seen
		(nvec VMEMB seen)/0
		seenseen,nvec
		vecnvec
		cntcnt+1
	LOOP


RUN2 0 2 7 0
RUN2 BLOCKS

Day 7

http://adventofcode.com/2017/day/7

part 1

 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"

part 2

 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
SOLUTION0

⎕EX 'WEIGH'
Rdata WEIGH id;node;w;children;cw
	nodedata GET id
	w{[2]} node
	children{[3]} node
	cw{DATA WEIGH }¨children
	 conditionally append a value
	SOLUTIONSOLUTION,(0={^/⎕IO=} cw)/(children, cw) 
	Rw + +/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

notes

]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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment