Skip to content

Instantly share code, notes, and snippets.

@selfsame
Last active December 14, 2018 05:41
Show Gist options
  • 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
GRID1000 10000
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}}¨DIMSGRID1000 10000
part 2
DIMS{¨}¨{(1' #@,:x') }¨INPUT
initialize
SET00
GRID1000 10000
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
DIMS10DIMS
create the coord rect matrix, then record and check
{{WRITE ,SCORD }¨(1),¨INDGRID 1}¨DIMS 55 3 4 5 2
make an actual set
SETDSTNCT 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?!?
Rname WRITEFILE lines;handle;res
handle('w' ⎕FIO[3] name)
res({(,'') ⎕FIO[43] handle} ¨ lines)
⎕FIO[4] handle
Rres
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'
Rcnt RUN input
input(100'.'),input,(100'.')
LOOP:
cntcnt-1
input{{{'...',,'..'}{ MATCH input}¨()-5}} input
(0cnt)/LOOP
R+/{('#'=)×(¯101+())}input
20 RUN INPUT
part 2
⎕EX 'RUN'
CNT0
Rcnt RUN input
input(2000'.'),input,(2000'.')
LOOP:
cntcnt-1
input{{{'...',,'..'}{ MATCH input}¨()-5}} input
CNTCNT,+/{('#'=)×(¯2001+())}input
(0cnt)/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¨1PARTITIONED
partition pairs
PAIR{(+0=~2|)}
convert rest of partition into pairs of minute entries
{PAIR 6¨1}¨6PARTITIONED
IDSPANS{(GID ), PAIR 6¨1}¨PARTITIONED
find highest id number
/{1}¨IDSPANS
init a structure of id and 0-60 minute columns
RECORD3170 600
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
INPUT1¨⎕FIO[49]'data/2018.8.txt'
⎕EX 'FIND'
META0
Ridx FIND nodes;children;meta
childrennodes[idx]
metanodes[idx+1]
idxidx+2
LOOP:
(children=0)/RETURN
idx(idx FIND nodes)
childrenchildren-1
LOOP
RETURN:
METAMETA,nodes[(idx-1)+meta]
R(idx+meta)
1 FIND INPUT
+/META
PART 2
IDXOK{((())(>0))/}
⎕EX 'FINDB'
Ridx FINDB nodes;node;children;meta;cvals;value;mvals
childrennodes[idx]
metanodes[idx+1]
cvals0
idxidx+2
LOOP:
(children=0)/RETURN
node(idx FINDB nodes)
idx1node
cvalscvals,2node
childrenchildren-1
LOOP
RETURN:
mvalsnodes[(idx-1)+meta]
(0=cvals)/CHILDLESS
value+/cvals[cvals IDXOK mvals]
R(idx+meta),value
0
CHILDLESS:
R(idx+meta),+/mvals
21 FINDB INPUT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment