Skip to content

Instantly share code, notes, and snippets.

@selfsame
Last active December 14, 2018 05:41
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/f134236b964fe3c3966e8249dffad919 to your computer and use it in GitHub Desktop.
Save selfsame/f134236b964fe3c3966e8249dffad919 to your computer and use it in GitHub Desktop.
#day 1
+/INPUT
{1↑{((⍵⍳⍵)≠⍳⍴⍵)/⍵}+⍀0,1000000⍴⍵}INPUT
#day 2
⍝ part 1 dyalog
×/¨+/{0<+/⍉{{{(2=⍵),(3=⍵)}2⊃⍵}⍺,≢⍵}⌸⍵}¨
⍝ part 2
⍝ boolean if two vecs are one item off
'abc' {(¯1+⍴⍵)=+/⍺=⍵} 'abz'
⍝ need to select only matching letters here
'abc' {(1+(¯1+⍴⍵)=+/⍺=⍵)⌷0 (⍺=⍵)} 'abz'
⍝ Remove characters!!!
{(~⍵∊' ')/⍵}'1 2'
⍝ Our dydactic solution fn
'abc' {(1+(¯1+⍴⍵)=+/⍺=⍵)⌷0 ⍵[{(~⍵∊0)/⍵}(⍺=⍵)×⍳⍴⍵]} 'abz'
ONEOFF←{(1+(¯1+⍴⍵)=+/⍺=⍵)⌷0 ⍵[{(~⍵∊0)/⍵}(⍺=⍵)×⍳⍴⍵]}
⍝ now run permutations on each string
⊃1↑{(~⍵∊0)/⍵},{⍵ ∘.{(1+(¯1+⍴⍵)=+/⍺=⍵)⌷0 ⍵[{(~⍵∊0)/⍵}(⍺=⍵)×⍳⍴⍵]} ⍵} INPUT
]BOXING ¯29
⍝ load a file
INPUT←⎕FIO[49]'data/2018.3.txt'
# DAY 3
⍝ part 1
DIMS←{⊃1↓⍎¨⍵}¨{(1≠⍵∊' #@,:x') ⊂ ⍵}¨INPUT
GRID←1000 1000⍴0
⍝ matrix of indicies into the grid
{((2↓⍵)⍴⊂(2↑⍵))+⍳⍴(2↓⍵)⍴0} 3 4 5 2
⍝ inc a coord
{GRID[1⌷⍵;2⌷⍵]←1+GRID[1⌷⍵;2⌷⍵]}3 4
⍝ tally
{+/+/⍵>1}GRID
⍝ one liner
{+/+/⍵>1}GRID⋄{{GRID[1⌷⍵;2⌷⍵]←1+GRID[1⌷⍵;2⌷⍵]}¨{((2↓⍵)⍴⊂(2↑⍵))+⍳⍴(2↓⍵)⍴0}⊃⍵}¨DIMS⋄GRID←1000 1000⍴0
⍝ part 2
DIMS←{⊃⍎¨⍵}¨{(1≠⍵∊' #@,:x') ⊂ ⍵}¨INPUT
⍝ initialize
SET←0⍴0
GRID←1000 1000⍴0
⍝ set functions
HAS←{+/⍺∊⍵}
DSTNCT←{((⍵⍳⍵)=⍳⍴⍵)/⍵}
CONJ←{SET←(SET,⍵)}
⍝ IMPL
INDGRID←{((2↓⍵)⍴⊂(2↑⍵))+⍳⍴(2↓⍵)⍴0}
⍝ given an id and coords, if coord allready has id, add both to SET
⍝ then write new id to coords
WRITE←{GRID[2⌷⍵;3⌷⍵]←1⌷⍵} ⍝8 4 4
⍝ if loc != 0 CONJ loc value and new value
SCORD←{CONJ ⊃(((1⌷⍵) GRID[2⌷⍵;3⌷⍵]) 0)[((0=GRID[2⌷⍵;3⌷⍵])+1)]} ⍝6 4 3
DIMS←10↑DIMS
⍝ create the coord rect matrix, then record and check
⍴{{WRITE ⍵,SCORD ⍵}¨(1⌷⍵),¨INDGRID 1↓⍵}¨DIMS ⍝ 55 3 4 5 2
⍝ make an actual set
SET←DSTNCT SET
⍝ find id not in SET
+/{⍵×0=SET HAS ⍵}¨1↑¨DIMS
INPUT←{{⊃⍎¨⍵[2 3 5 6]}(1≠⍵∊'<>, ')⊂⍵}¨⎕FIO[49]'data/2018.10.txt'
⍝ project and find bounding box for each time step
PROJECT←{⍵[1 2]+⍺×⍵[3 4]}
⍝ find bounding box for vectors
BOUNDS←{⊃|(⌊/⍵)-(⌈/⍵)}
⍝ find time step with lowest bounds
{⍵[{{+/(⍳⍴⍵)×(⌊/⍵)=⍵}{+/BOUNDS ⍵ PROJECT¨INPUT}¨⍵}⍵]} 9700+⍳2000
⍝ 10274 turns out this is part 2 answer
⍝ write to suitably sized grid
OUT← 250 250⍴'.'
{OUT[⍵[1];⍵[2]]←'#'}¨(10274 PROJECT¨INPUT)
⍝ lets save a file?!?
∇R←name WRITEFILE lines;handle;res
handle←('w' ⎕FIO[3] name)
res←({(⍵,'') ⎕FIO[43] handle} ¨ lines)
⎕FIO[4] handle
R←res
⍝ secret codes show up ?!?
'joe.txt' WRITEFILE ,OUT
INPUT←'##..##..#.##.###....###.###.#.#.######.#.#.#.#.##.###.####..#.###...#######.####.##...#######.##..#'
PATS← '#..#.' '...##' '#.#.#' '.###.' '##.#.' '#...#' '#####' '..#.#' '###.#' '.####' '.#..#' '.##..' '#.#..' '.#...' '.#.##'
⍝ given an index compare a len 5 subvec to PATS
⍝ and return . or #
MATCH←{".#"[1++/0 ⍵[⍺+⍳5]∊PATS]}
⎕EX 'RUN'
∇R←cnt RUN input
input←(100⍴'.'),input,(100⍴'.')
LOOP:
cnt←cnt-1
input←{{{'...',⍵,'..'}{⍵ MATCH input}¨⍳(⍴⍵)-5}⍵} input
→(0≠cnt)/LOOP
R←+/{('#'=⍵)×(¯101+(⍳⍴⍵))}input
20 RUN INPUT
⍝ part 2
⎕EX 'RUN'
CNT←⍳0
∇R←cnt RUN input
input←(2000⍴'.'),input,(2000⍴'.')
LOOP:
cnt←cnt-1
input←{{{'...',⍵,'..'}{⍵ MATCH input}¨⍳(⍴⍵)-5}⍵} input
CNT←CNT,+/{('#'=⍵)×(¯2001+(⍳⍴⍵))}input
→(0≠cnt)/LOOP
R←+/{('#'=⍵)×(¯2001+(⍳⍴⍵))}input
200 RUN INPUT
1557+8×(50000000000-200)
]BOXING ¯29
⍝ load a file
INPUT←⎕FIO[49]'data/2018.4.txt'
PARSE←{{(⊃⍎¨5↑⍵), (5↓⍵)}{(1≠⍵∊' []-:#') ⊂⍵}⍵}
⍝ unified time stamp
WSTAMP←{(+/ (15891840 44640 1440 60 1) × 5↑⍵), ⍵}
⍝ sort
SORTED←{⍵[⍋({1⌷⍵}¨⍵)]} WSTAMP¨{PARSE ⍵}¨INPUT
⍝ part 1
⍝ partition every gaurd sequence
PARTITIONED←{({,⊃⍵}+⍀{(10=(⍴⍵))}¨⍵)⊂⍵} SORTED
⍝ get the guard id from a partition
GID←{⍎1 8⊃⍵}
GID¨1↑PARTITIONED
⍝ partition pairs
PAIR←{(+⍀0=~2|⍳⍴⍵)⊂⍵}
⍝ convert rest of partition into pairs of minute entries
{PAIR 6⊃¨1↓⍵}¨6↑PARTITIONED
IDSPANS←{(GID ⍵), PAIR 6⊃¨1↓⍵}¨PARTITIONED
⍝ find highest id number
⌈/{1⊃⍵}¨IDSPANS
⍝ init a structure of id and 0-60 minute columns
RECORD←3170 60⍴0
⍝ record id ranges
RANGE←{(⍵[1]-1)+⍳(⍵[2]-⍵[1])}
TALLY←{RECORD[⍺;⍵+1]←(1+RECORD[⍺;⍵+1])}
{(1⊃⍵) {⍺{⍺ TALLY ¨(RANGE ⍵)}¨⍵} 1↓⍵}¨IDSPANS
⍝ count total minutes for each row (guard)
⍝ find row that = max total, find row index
CANDIDATE←{+/(⍳⍴+/⍵)×(+/⍵)=⌈/+/⍵} RECORD
⍝ find most slept minute
MOSTMIN←{{⍵-1}+/(⍳⍴⍵)×⍵=⌈/⍵} {⍵[CANDIDATE;]} RECORD
MOSTMIN×CANDIDATE
⍝ part 2
⍝ now find the highest slept minute of anybody
CANDIDATE←{+/(⍳⍴⌈/⍵)×(⌈/⍵)=⌈/⌈/⍵} RECORD
MOSTMIN←{{⍵-1}+/(⍳⍴⍵)×⍵=⌈/⍵} {⍵[CANDIDATE;]} RECORD
MOSTMIN×CANDIDATE
]BOXING ¯29
INPUT←1⊃⍎¨⎕FIO[49]'data/2018.8.txt'
⎕EX 'FIND'
META←⍳0
∇R←idx FIND nodes;children;meta
children←nodes[idx]
meta←nodes[idx+1]
idx←idx+2
LOOP:
→(children=0)/RETURN
idx←(idx FIND nodes)
children←children-1
→LOOP
RETURN:
META←META,nodes[(idx-1)+⍳meta]
R←(idx+meta)
1 FIND INPUT
+/META
⍝ PART 2
IDXOK←{(((⍴⍺)≥⍵)∧(⍵>0))/⍵}
⎕EX 'FINDB'
∇R←idx FINDB nodes;node;children;meta;cvals;value;mvals
children←nodes[idx]
meta←nodes[idx+1]
cvals←⍳0
idx←idx+2
LOOP:
→(children=0)/RETURN
node←(idx FINDB nodes)
idx←1⌷node
cvals←cvals,2⌷node
children←children-1
→LOOP
RETURN:
mvals←nodes[(idx-1)+⍳meta]
→(0=⍴cvals)/CHILDLESS
value←+/cvals[cvals IDXOK mvals]
R←(idx+meta),value
→0
CHILDLESS:
R←(idx+meta),+/mvals
2⌷1 FINDB INPUT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment