⎕IO←1 ]box on -style=max -trains=tree -fns=on ]rows on DAY1←⊃⊃⎕NGET'data/2017/01.txt'1 DATA←DAY1,1⌷DAY1 +/⍎¨DATA[⍸2=/DATA] ⍝ Part 1: 1341 +/⍎¨DAY1[⍸=/⍉↑(DAY1)(DAY1⊖⍨2÷⍨≢DAY1)] ⍝ Part 2: 1348 DAY2←16 16⍴⍎¨'\d+'⎕S'&'⊢⊃⎕NGET'data/2017/02.txt'1 +/(⌈/-⌊/)DAY2 ⍝ Part 1: 39126 Day2←{target←1↑⍸{≠/0=⍺(|,|⍨)⍵}/¨cmb←,⍵∘.,⍵⋄÷/pair[⍒pair←target⊃cmb]} +/Day2¨↓DAY2 ⍝ Part 2: 258 ⎕IO←0 DAY3←325489 Square←{r←⍵*0.5⋄⍵=2*⍨⌊r:r⋄0≠2|⌈r:⌈r⋄1+⌈r} ⍝ Smallest odd number < the square root, unless perfect square ]dinput Day3p1←{ val←⍵ width←Square ⍵ ⍝ The width of the square in which the value is found edge←⌊((width*2)-val)÷(width-1) ⍝ The side of the square: S E N W ← 0 1 2 3 pos←edge⊃,size,-size∘.,-size,size←⌊width÷2 ⍝ Grid coords of corner _←(edge⊃,¯1 0∘.,0 ¯1)∘{pos+←⍺⋄⍵-1}⍣{⍺=val} (width*2)-edge×width-1 ⍝ Sequence at the nearest corner, acw +/|¨pos ⍝ Manhattan distance to centre } Day3p1 DAY3 ⍝ 552 ]dinput Coords←{ ⍝ Generate all coordinate pairs for the circumference of a square ⍵ pos←(⌊⍵÷2),(-⌊⍵÷2)+1 E←(⊂pos)+0,¨⍳⍵-1 N←(¯1↑E)+(-⍳⍵),¨0 W←(¯1↑N)+0,¨-⍳⍵ S←(¯1↑W)+(⍳⍵),¨0 ∪E, N, W, S } Neighbours←{(⊂⍵)+(0 1)(1 1)(1 0)(1 ¯1)(0 ¯1)(¯1 ¯1)(¯1 0)(¯1 1)} ⍝ 8-neighbours ]dinput Day3p2←{ target←⍵ seen←,⊂0 0 ⋄ vals←,1 { ⍝ Move outwards one square at a time (i.e. odd numbers) found←{ ⍝ Walk circumference, adding up values from visible neighbours 0=≢⍵:0 pos←⊃1↑⍵ old←seen⍳Neighbours pos newVal←+/vals[((≢seen)>old)/old] newVal>target:newVal vals,←newVal seen,←⊂pos ∇1↓⍵ } Coords ⍵ found>0:found ∇⍵+2 } 3 } Day3p2 325489 ⍝ 330785 ⎕IO←1 DAY4←' '(≠⊆⊢)¨⊃⎕NGET'data/2017/04.txt'1 +/(∪≡⊢)¨DAY4 ⍝ Part 1: 325 +/(∪≡⊢)¨{⍵[⍋⍵]}¨¨DAY4 ⍝ Part 2: 119 ⎕IO←1 DAY5←⍎¨⊃⎕NGET'data/2017/05.txt'1 ]dinput Day5←{ instr←⍵ modifier←⍺⍺ 1 { ip←1↑⍵ jmp←ip⌷instr (ip+jmp)>≢instr:⍺ instr[ip]+←modifier jmp (⍺+1)∇ip+jmp } 1 } {1} Day5 DAY5 ⍝ Part 1: 372671 {⍵≥3:¯1⋄1} Day5 DAY5 ⍝ Part 2: 25608480 (takes a minute or so) ⎕IO←0 DAY6←2 8 8 5 4 2 3 1 5 5 1 2 15 13 5 14 ]dinput Redist←{ (0@m⊢⍵) { ⍝ Zero out the current bin 0=⊃⍵:⍺ ⍝ Return end state (1∘+@n⊢⍺)∇(⊃⍵-1),n←16|1+⊢/⍵ ⍝ Move one index on, mod size and add 1. Decrease value to distribute. } ⍵[m],m←(⊢⍳⌈/)⍵ ⍝ Value and index of the largest element } Day6p1←{seen←⊂⍵⋄e←Redist⍣{seen∊⍨⊂⍺:1⋄0⊣seen,←⊂⍺} ⍵⋄(⊂e),≢seen} ⍝ Stop if we've seen a state before. (elem seen)←Day6p1 DAY6 seen ⍝ 3156 Day6p2←{count←0⋄target←⍵⋄_←Redist⍣{count+←1⋄⍺≡target} ⍵⋄count} ⍝ Stop when we revisit end state of part 1. Day6p2 elem ⍝ Part 2: 1610 ⎕IO←1 'segs'⎕CY'dfns' DAY7←⊃⎕NGET'data/2017/07.txt'1 ]dinput Parse←{ ⍺←⍬ ⍬ ⍬ 0=≢⍵:⍺ items←'() ,->' segs ⊃1↑⍵ (⍺,¨(⊂1⊃items)(⍎2⊃items)(⊂2↓items))∇1↓⍵ } (nodes weights children)←Parse DAY7 ⊃nodes~{⊃,/,⊆¨⍵}⍣≡children ⍝ Part 1: xegshds ]dinput SubTreeWeight←{ ⍝ (nodes weights children) SubTreeWeight 'node' idx←(1⊃⍺)⍳⊂⍵ ⍝ Find the node's index 0=≢idx⊃3⊃⍺:idx⊃2⊃⍺ ⍝ If node has no children -- return the weight (idx⊃2⊃⍺)++/⍺∘∇¨idx⊃3⊃⍺ ⍝ Add own weight to sum of child tree weights recursively } ]dinput FindAnomaly←{ ⍝ Find first key where its child trees all have the same weight idx←(1⊃⍺)⍳⊂⍵ ch←idx⊃3⊃⍺ 0=≢ch:⍬ chw←⍺∘SubTreeWeight¨ch (1≥≢∘∪)chw:⍵ (idx) ⍝ Child tree weights all equal - we're done ⍺∇⊃ch[∊{∩/⍵}⌸chw] ⍝ Intersect-reduce over unique indexes only returns non-empty for singles. } ⊢ANOMALY←(nodes weights children) FindAnomaly 'xegshds' PARENT←⍸{ANOMALY[1]∊⍵}¨children TWEAK←|-/∪(nodes weights children)∘SubTreeWeight¨⊃children[PARENT] weights[2⊃ANOMALY]-TWEAK ⍝ Part 2: 299 ⎕IO←1 'segs'⎕CY'dfns' DAY8←⊃⎕NGET'data/2017/08.txt'1 DATA←' ' segs¨DAY8 ]dinput Day8←{ regs←∪{⊃,/,⊆¨⍵}⍣≡(↑⍵)[;1 5] R←regs∘⍳ ⍝ By binding an operator, the static 'regs' vector is hashed vals←0⍴⍨≢regs Reg←{vals[R ⊂,⍵]} Set←{vals[R ⊂,⍺]←⍵⋄⍬} inc←{⍺ Set (Reg ⍺)+⍎⍵} dec←{⍺ Set (Reg ⍺)-⍎⍵} 0 { 0=≢⍵:(⌈/vals) ⍺ max←⌈/vals,⍺ instr←1⊃⍵ rv←Reg 5⊃instr f←⍎2⊃instr ((,'>')≡6⊃instr)∧rv>⍎7⊃instr:max∇1↓⍵⊣(1⊃instr) f 3⊃instr ((,'<')≡6⊃instr)∧rv<⍎7⊃instr:max∇1↓⍵⊣(1⊃instr) f 3⊃instr ('=='≡6⊃instr)∧rv=⍎7⊃instr:max∇1↓⍵⊣(1⊃instr) f 3⊃instr ('>='≡6⊃instr)∧rv≥⍎7⊃instr:max∇1↓⍵⊣(1⊃instr) f 3⊃instr ('<='≡6⊃instr)∧rv≤⍎7⊃instr:max∇1↓⍵⊣(1⊃instr) f 3⊃instr ('!='≡6⊃instr)∧rv≠⍎7⊃instr:max∇1↓⍵⊣(1⊃instr) f 3⊃instr max∇1↓⍵ } ⍵ } Day8 DATA ⍝ Part 1: 3745 Part2: 4644 ⎕IO←1 DAY9←⊃⊃⎕NGET'data/2017/09.txt'1 ]dinput Day9←{ pos←0 str←⍵ Yield←{pos+←1⋄pos⊃str} Skip←{⍺←0⋄c←Yield⍬⋄c='>':⍺⋄c='!':⍺∇⍬⊣Yield⍬⋄(⍺+1)∇⍬} (0 0 0) { 3::1↓⍺ ⍝ Catch index error at end of input (depth total skipped)←⍺ c←Yield⍬ c='{':((1+depth),total,skipped)∇⍬ c='}':((¯1+depth),(total+depth),skipped)∇⍬ c='<':(depth,total,skipped+Skip⍬)∇⍬ c='!':⍺∇⍬⊣Yield⍬ ⍺∇⍬ }⍬ } Day9 DAY9 ⍝ Part 1: 10050 Part 2: 4482 ⎕IO←0 'iotag'⎕CY'dfns' DAY10←147 37 249 1 31 2 226 0 161 71 254 243 183 255 30 70 ]dinput Rot←{ (skip pos len)←⍵ 0=len:(skip+1) (256|pos+len+skip) ⍺ (skip+1) (256|pos+len+skip) (⊖@(256|pos iotag pos+len-1)⊢⍺) } Round←{0=≢⍵:⍺⋄((2⊃⍺) Rot (0⊃⍺) (1⊃⍺) (0⊃⍵))∇1↓⍵} ×/2↑2⊃(0 0,⊂⍳256)Round DAY10 ⍝ Part 1: 37230 ]dinput Knot←{ lengths←(⎕UCS¨' '⎕R','⍕⍵),17 31 73 47 23 sparse←(0 0,⊂⍳256) { 0=⍵:2⊃⍺ (⍺ Round lengths)∇⍵-1 } 64 ↓⍉{(⎕D,⎕A)[16⊥⍣¯1⊢⍵]} {2⊥⊃≠/(8⍴2)∘⊤¨⍵}¨↓16 16 ⍴ sparse } Knot DAY10 ⍝ 70b856a24d586194331398c7fcfa0aaf ⎕IO←1 DAY11←(∪⍳⊢)','(≠⊆⊢)⊃⊃⎕NGET'data/2017/11.txt'1 HexMHD←{⌊2÷⍨+/|⍵} ⍝ Manhattan distance on a hex grid DELTA←(¯1 0 1)(0 ¯1 1)(1 ¯1 0)(¯1 1 0)(1 0 ¯1)(0 1 ¯1) ⍝ SW S SE NW NE N -- order of index of first occurrance HexMHD ⊃+/⊖DELTA[DAY11] ⍝ Part 1: 643 ⌈/HexMHD¨+\DELTA[DAY11] ⍝ Part 2: 1471 ⎕IO←0 'segs'⎕CY'dfns' DAY12←1↓¨⍎¨¨' <->,'∘segs¨⊃⎕NGET'data/2017/12.txt'1 ]dinput BFS←{ ⍝ nodes BFS node graph←⍺ ⍬{ 0=≢⍵:⍺ node←1↑⍵ ⋄ queue←1↓⍵ ch←node⊃graph (⍺,node)∇(queue∪ch)~⍺ }⍵ } ≢DAY12 BFS 0 ⍝ Part 1: 175 ]dinput ConnectedComponents←{ graph←⍵ seen←⍬ 0 { root←⍵ root≥≢graph:⍺ root∊seen:⍺∇⍵+1 seen,←graph BFS root (⍺+1)∇⍵+1 } 0 } ConnectedComponents DAY12 ⍝ 213 ⎕IO←0 'segs'⎕CY'dfns' DAY13←⍎¨¨' :'∘segs¨⊃⎕NGET'data/2017/13.txt'1 MAX←⊃⊃1↑¯1↑DAY13 IDX←⊣/↑DAY13 LEN←⊢/↑DAY13 Pos←{cycle←⍺-1⋄1=2|⌊⍵÷cycle:cycle-cycle|⍵⋄cycle|⍵} ⍝ depth Pos pico - the index at pico, for given depth Day13p1←{fw←⍵⋄0{⍵≥≢fw:⍺⋄0=fw[⍵] Pos ⍵:(⍺+fw[⍵]×⍵)∇⍵+1⋄⍺∇⍵+1}0} +/Day13p1 LEN@IDX⊢(1+MAX)⍴0 ⍝ Part 1: 2384 ]dinput Day13p2←{ fw←⍵ 0 { ⍝ Current delay is ⍺ ⍵≥≢fw:⍺ 0=fw[⍵]:⍺∇⍵+1 ⍝ Skip gaps 0=fw[⍵] Pos ⍵+⍺:(⍺+1)∇0 ⍝ Caught! Increase delay and re-start from layer 0 ⍺∇⍵+1 ⍝ Still going: try the next layer } 0 ⍝ Current layer } Day13p2 LEN@IDX⊢(1+MAX)⍴0 ⍝ Part 2: 3921270 ⎕IO←0 'dec'⎕CY'dfns' ⍝ Hex to dec helper function DAY14←'jzgqcdpd' ADJ←↑{∊{(8⍴2)⊤⍵}¨dec¨Knot DAY14,'-',⍕⍵}¨⍳128 +/∊ADJ ⍝ Part 1: 8074 Bfs←{⍺←⍬⋄0=≢⍵:⍺⋄(⍺,1↑⍵)∇((1↓⍵)∪⍺⍺ 1↑⍵)~⍺} Components←{⍺←0⋄0=≢⍵:⍺⋄(⍺+1)∇(1↓⍵)~⍺⍺ Bfs⊢1↑⍵} ]dinput Connected←{ valid←({(⍵[0]≥0)∧(⍵[1]≥0)∧(⍵[0]<128)∧(⍵[1]<128)}¨n)/n←⍵+(¯1 0)(1 0)(0 1)(0 ¯1) (1=⍺[valid])/valid } ADJ∘Connected Components ⊢ ⍸ADJ ⍝ Part 2: 1212 Part1←{⊢⍺⍺.prev←2147483647|⍺⍺.prev×⍺⍺.fact} GA←({⍵⊣⍵.(prev fact)←679 16807}⎕NS'') Part1 GB←({⍵⊣⍵.(prev fact)←771 48271}⎕NS'') Part1 {⍺←0⋄0=⍵:⍺⋄(⍺+≡/{(16⍴2)⊤⍵}¨(GA⍬) (GB⍬))∇⍵-1} 40000000 ⍝ Part 1: 626 -- takes a good few mins to run Part2←{0=⍺⍺.multiple|⍺⍺.prev←2147483647|⍺⍺.prev×⍺⍺.fact:⍺⍺.prev⋄∇⍬} GA2←({⍵⊣⍵.(prev fact multiple)←679 16807 4}⎕NS'') Part2 GB2←({⍵⊣⍵.(prev fact multiple)←771 48271 8}⎕NS'') Part2 {⍺←0⋄0=⍵:⍺⋄(⍺+≡/{(16⍴2)⊤⍵}¨(GA2⍬) (GB2⍬))∇⍵-1} 5000000 ⍝ Part 2: 306 -- also takes a good few mins to run ⎕IO←0 'segs'⎕CY'dfns' DAY16←',' segs⊢⊃⊃⎕NGET'data/2017/16.txt'1 Spin←{(-⍺)⊖⍵} Exchange←{(a b)←⍺⋄⍵[b a]@a b⊢⍵} Pair←{(⍵⍳⍺)Exchange ⍵} ]dinput Day16←{ ⍺←'abcdefghijklmnop' 0=≢⍵:⍺ cmd←⊃1↑⍵ 's'=cmd[0]:((⍎1↓cmd) Spin ⍺)∇1↓⍵ 'x'=cmd[0]:((⊃1↓'/'⎕VFI 1↓cmd) Exchange ⍺)∇1↓⍵ ((∊'/'(≠⊆⊢)1↓cmd) Pair ⍺)∇1↓⍵ } Day16 DAY16 ⍝ Part 1: kbednhopmfcjilag 'abcdefghijklmnop' {0=⍵:⍺⋄(⍺ Day16 DAY16)∇⍵-1} 100 ⍝ Part 2: fbmcgdnjakpioelh ⎕IO←0 DAY17←356 Insert←{(v cur st)←⍵⋄c←1+(≢⍺)|cur+st⋄c≥≢⍺:c (⍺,v)⋄c ((c↑⍺),v,c↓⍺)} Day17p1←{2018=⍵:⍺⋄(cur buf)←⍺⋄(buf Insert ⍵ cur DAY17)∇⍵+1} (cur buf)←0 (,0)Day17p1 1 buf⌷⍨cur+1 ⍝ Part 1: 808 Day17p2←{(cur target st)←⍵⋄cur←1+st|cur+DAY17⋄1=cur:cur,st,st+1⋄cur,target,st+1} 1⊃Day17p2⍣50000000⊢0 ¯1 1 ⍝ Part 2: 47465686 -- takes a minute or so to run ⎕IO←1 DAY18←{6::⍵⋄⍎⍵}¨¨' '(≠⊆⊢)¨⊃⎕NGET'data/2017/18.txt'1 ]dinput Day18←{ mem←⍵ ri←{'abfips'⍳⍵} val←{(1=2|⎕DR)⍵:⍵⋄⍺[ri ⍵]} set←{(x y)←⍵⋄1∘+@7⊢(⍺ val y)@(ri x)⊢⍺} mul←{(x y)←⍵⋄1∘+@7⊢(⍺ val y)∘×@(ri x)⊢⍺} add←{(x y)←⍵⋄1∘+@7⊢(⍺ val y)∘+@(ri x)⊢⍺} mod←{(x y)←⍵⋄1∘+@7⊢(⍺ val y)∘|@(ri x)⊢⍺} rcv←{0≠⍺ val ⊃⍵:99∘+@7⊢⍺⋄1∘+@7⊢⍺} snd←{⍺ set 's' (⊃⍵)} jgz←{(x y)←⍵⋄(⍺ val x)>0:(⍺ val y)∘+@7⊢⍺⋄1∘+@7⊢⍺} { ip←¯1↑⍵ instr←ip⊃mem⋄op←⊃instr ⍵ (⍎op) 1↓instr }⍣{(¯1↑⍺)>≢mem}⍺ } 6⊃0 0 0 0 0 0 1 Day18 DAY18 ⍝ Part 1: 3423 MSG←{⍵⊣⍵.Queue←⍬ ⍬}⎕NS'' ]dinput Step←{ pid←⍺ mem←DAY18 ri←{'abfip'⍳⍵} val←{(1=2|⎕DR)⍵:⍵⋄⍺[ri ⍵]} set←{(x y)←⍵⋄1∘+@8⊢(⍺ val y)@(ri x)⊢⍺} mul←{(x y)←⍵⋄1∘+@8⊢(⍺ val y)∘×@(ri x)⊢⍺} add←{(x y)←⍵⋄1∘+@8⊢(⍺ val y)∘+@(ri x)⊢⍺} mod←{(x y)←⍵⋄1∘+@8⊢(⍺ val y)∘|@(ri x)⊢⍺} jgz←{(x y)←⍵⋄(⍺ val x)>0:(⍺ val y)∘+@8⊢⍺⋄1∘+@8⊢⍺} snd←{MSG.Queue[1+~pid],←⍺ val ⊃⍵⋄1∘+@7 8⊢⍺} ⍝ Add item to other's queue, and 1 to send-count and ip rcv←{ ⍝ Pull item from message queue data←⊃MSG.Queue[1+pid] 0=≢data:1@6⊢⍺ ⍝ Blocked! Ip stays unchanged. val←1⊃data MSG.Queue[1+pid]←⊂1↓data ⍝ Pop queue 0@6⊢⍺ set (⊃⍵) val ⍝ Set reg with value from queue, and make note that we're not blocked } ip←¯1↑⍵ ip>≢mem:⍵ ⍝ Already terminated instr←ip⊃mem⋄op←⊃instr ⍵ (⍎⊃instr) 1↓instr } Deadlocked←{(p0 p1)←⍵⋄(p0[6]=1)∧p1[6]=1} Terminated←{(p0 p1)←⍵⋄(p0[8]≥≢DAY18)∧p1[8]≥≢DAY18} Done←{(Deadlocked ⍵)∨Terminated ⍵} 7⊃2⊃{⊃↓Step/0 1,⍪⍵}⍣{Done ⍺} (0 0 0 0 0 0 0 1)(0 0 0 0 1 0 0 1) ⍝ 'p' reg holds process id -- Part 2: 7493 ⎕IO←1 DAY19←↑⊃⎕NGET'data/2017/19.txt'1 ]rows on ]dinput Turn←{ graph←⍺ (prev cur)←⍵ Valid←{({(⍵[1]>0)∧(⍵[2]>0)∧(⍵[1]≤≢graph)∧⍵[2]≤≢graph}¨⍵)/⍵} ⊃(' '≠graph[coords])/coords←Valid (⊂prev)~⍨{cur+⍵}¨(¯1 0)(1 0)(0 ¯1)(0 1) } ]dinput Next←{ (prev cur)←⍵ dir←v÷(2*∘÷⍨+.×⍨)v←cur-prev ⍝ Unit-vector in the direction of travel next←cur+dir ⍺[⊂cur]='+':cur (⍺ Turn prev cur) 1 ⍝ On a jct ⍺[⊂next]∊⎕A,⍺[⊂cur],'+':cur (next) 1 ⍝ Straight or coming to jct ⍺[⊂next]=' ':cur (cur) 0 ⍝ End of the track cur (next+dir) 2 ⍝ Underpass } ]dinput Day19←{ graph←⍺ (⍬ 1) { (letters count)←⍺ (prev cur delta)←graph Next ⍵ graph[⊂cur]='Y':(letters,'Y') (count+delta) graph[⊂cur]∊⎕A:((letters,graph[⊂cur]) (count+delta))∇prev (cur) (letters (count+delta))∇prev (cur) } ⍵ } ⊢START←DAY19[1;]⍳'|' DAY19 Day19 (0 START) (1 START) ⍝ Part 1: GPALMJSOY Part 2: 16204 ⎕IO←0 DAY20←⊖1 0 2⍉(9÷⍨≢DATA)3 3⍴DATA←⍎¨'-?\d+'⎕S'&'⊢⊃⎕NGET'data/2017/20.txt'1 ⍝ 3 x 1000 x 3: acc vel pos ⊃⍋+/|2⌷+⍀⍣500⊢DAY20 ⍝ Part 1: 170 Day20p2←{state←+⍀⍵⋄hist←{⍵ (2>≢⍵)}⌸2⌷state⋄state[;∊(hist[;1])/hist[;0];]} ≢2⌷Day20p2⍣500⊢DAY20 ⍝ Part 2: 571 ⎕IO←0 'segs'⎕CY'dfns' DAY21←⍉↑'/'⎕R''¨↓⍉↑' =>'∘segs¨⊃⎕NGET'data/2017/21.txt'1 Rot←{(⍵)(⌽⍉⍵)(⌽⊖⍵)(⊖⍉⍵)} Fold←{s←.5*⍨≢⍵⋄s s⍴⍵} Trn←{m←Fold ⍵⋄∪(Rot m),(Rot⌽m),(Rot⊖m)} Expand←{⍺←⍬⋄0=≢⍵:⍺⋄(k v)←⊃⍵⋄fv←Fold v⋄(⍺,{⍵ fv}¨Trn k)∇ 1↓⍵} Tile←{2=⍺:,⊢∘⊂⌺(2 2⍴2 2)⊢⍵⋄1 1↓⊢∘⊂⌺(2 2⍴3 3)⊢0⍪0⍪0,0,⍵} Day21←{s←2+2|≢⍵⋄tiles←,s Tile ⍵⋄dim←s÷⍨≢⍵⋄((⍴×⍴∘⊃)⍴0 2 1 3⍉↑)dim dim⍴{Find ⊂⍵}¨tiles} TABLE←Expand ↓DAY21 Find←(⊣/↑TABLE)∘{⊃⊢/⊃TABLE[⍺⍳⍵]} +/∊'#'=Day21⍣5⊢3 3⍴'.#...####' ⍝ Part 1: 162 +/∊'#'=Day21⍣18⊢3 3⍴'.#...####' ⍝ Part 2: 2264586 ⎕IO←0 Make←{s←(2÷⍨⍺)-2÷⍨≢⍵⋄ctr←⊂s s⋄1@(ctr+⍸⍵)⊢⍺ ⍺⍴0} ⍝ Center data in larger array of shape ⍺ ⍺ DAY22←429 Make 25 25⍴(∪⍳⊢)∊⊃⎕NGET'data/2017/22.txt'1 ]dinput Day22p1←{ m←⍺ start←⌊2÷⍨≢m { (row col infected count dir)←⍵ cur←m[row;col] m[row;col]←~cur newDir←cur {1=⍺:¯1∘⌽ ⍵⋄1∘⌽ ⍵} dir newPos←(⊃newDir)⌷(⊂row col)(-,+)(1 0)(0 1) (⊃newPos),(infected+~cur) (count-1) (newDir) }⍣⍵⊢start start 0 ⍵ (⍳4) } 2⊃DAY22 Day22p1 10000 ⍝ Part 1: 5261 ]dinput Day22p2←{ m←0 2[⍺] ⍝ Infected cells should have value 2 start←⌊2÷⍨≢m { (row col infected count dir)←⍵ cur←m[row;col] m[row;col]←cur⊃1 2 3 0 newDir←cur {0=⍺:1⌽⍵⋄1=⍺:⍵⋄2=⍺:¯1⌽⍵⋄¯2⌽⍵} dir newPos←(⊃newDir)⌷(⊂row col)(-,+)(1 0)(0 1) (⊃newPos),(infected+2=m[row;col]) (count-1) (newDir) }⍣⍵⊢start start 0 ⍵ (⍳4) } 2⊃DAY22 Day22p2 10000000 ⍝ Part 2: 2511927 -- takes around 90s to run ⎕IO←1 DAY23←{6::⍵⋄⍎⍵}¨¨' '(≠⊆⊢)¨⊃⎕NGET'data/2017/23.txt'1 ]dinput Day23←{ mem←⍵ ri←{'abcdefgh'⍳⍵} val←{(1=2|⎕DR)⍵:⍵⋄⊃⍺[ri ⍵]} set←{(x y)←⍵⋄1∘+@10⊢(⍺ val y)@(ri x)⊢⍺} mul←{(x y)←⍵⋄1∘+@9⊢⍺ set x,(⍺ val x)×⍺ val y} sub←{(x y)←⍵⋄⍺ set x,(⍺ val x)-⍺ val y} jnz←{(x y)←⍵⋄(⍺ val x)≠0:(⍺ val y)∘+@10⊢⍺⋄1∘+@10⊢⍺} { ip←¯1↑⍵ instr←ip⊃mem⋄op←⊃instr ⍵ (⍎op) 1↓instr }⍣{(¯1↑⍺)>≢mem}⍺ } 9⊃0 0 0 0 0 0 0 0 0 1 Day23 DAY23 ⍝ Part 1: 5929 ]dinput r←Day23p2 ;b;c;h;d;f;e;g;v (b c d e f g h)←107900 124900 0 0 0 0 0 :While 1 (f d)←1 2 :While 1 e←2 :If 0=d|b ⍝ L4 hack v←⌊b÷d :If (v≥e)∧v+/∊strongest strongest←bridge :EndIf :EndFor r←strongest +/∊DAY24 Day24p1 (⊂0 0) 0 ⍝ Part 1: 1511 ]dinput r←components Day24p2 args;path;pins;longest;c;bridge (path pins)←args longest←path :For c :In (components~path) Find pins bridge←(components~c)Day24p2 (⊂(path,⊂c)),c {r←⍺~⍵⋄0=≢r:⊃⍺⋄⊃r} pins ⍝ Handle when both pins are equal :If (≢bridge)>≢longest longest←bridge :ElseIf ((≢bridge)=≢longest)∧(+/∊bridge)>+/∊longest longest←bridge :EndIf :EndFor r←longest +/∊DAY24 Day24p2 (⊂0 0) 0 ⍝ Part 2: 1471 s←+/↑DAY24⋄n←1+⌈/∊DAY24⋄g←(⍳n)∘.∊DAY24⋄u←∘.≠⍨⍳≢DAY24 0{∨/c←⍵∧⍺⌷g:⌈/t+(-⍺-t←c/s)∇¨↓⍵∧⍤1⊢c⌿u⋄0}≡¨DAY24 STRENGTHS←+/↑DAY24 COUNT←1+⌈/∊DAY24 GRAPH←(⍳COUNT)∘.∊DAY24 INVIDM←∘.≠⍨⍳≢DAY24 ⍝ Inverted identity matrix used to exclude the current item ]dinput D24p1←{ candidates←⍵∧⍺⌷GRAPH t←candidates/STRENGTHS ∨/candidates:⌈/t+(-⍺-t)∇¨↓⍵∧⍤1⊢candidates⌿INVIDM 0 } 0 D24p1 ≡¨DAY24 ⎕IO←0 STATE←6 6⍴1 1 1 0 ¯1 2 1 ¯1 0 1 1 3 1 1 0 0 ¯1 4 1 1 0 0 1 1 1 ¯1 5 1 ¯1 2 1 1 3 1 1 0 ]dinput Day25←{ (pos state)←⍵ args←(0 3[pos⊃TAPE])+⍳3 (nv m ns)←STATE[state;args] TAPE[pos]←nv (pos+m) ns } TAPE←12919244⍴0⋄_←Day25⍣12919244⊢6459622 0⋄+/TAPE ⍝ 4287