Skip to content

Instantly share code, notes, and snippets.

@selfsame
Last active December 7, 2017 16:55
Show Gist options
  • 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 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

part 2

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

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  '
β”Œβ”€β”€β”€β”¬β”€β”€β”¬β”¬β”€β”€β”¬β”€β”€β”¬β”€β”€β”¬β”€β”€β”¬β”¬β”
β”‚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'

part 2

{{⍡≑βˆͺ⍡}{⍡[⍋⍡]}Β¨(' '≠⍡)βŠ‚β΅} 'ab ba'

Day 5

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
βˆ‡

notes

⍝ 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

Day 6

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

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
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

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