AoC 2019


#1

https://adventofcode.com/2019

Day1

)copy loaddata
a←LoadTEXT 'input.txt'
a←{⍎⍵}¨¯1↓a
d1←{+/(⌊⍵÷3)-2}
d2←{
     f←{r←d1 ⍵ ⋄ r≤0:⍺ ⋄ (⍺+r)∇ r}
     +/(0 f⊢)¨⍵
 }

Day2

d2←'1,0,...'
d2←⎕JSON '[',d2,']'
∇R←q3 prog;⎕IO;mem;c;op;r1;r2;r3
 ⎕IO←0
 mem←prog
 mem[1]←12
 mem[2]←2
 c←0
LOOP:
 op←mem[c]
 →(1 2 99=op)/ADD MUL HALT
ADD:op←'+'
 →COM
MUL:op←'×'
COM:
 r1←mem[c+1]
 r2←mem[c+2]
 r3←mem[c+3]
 mem[r3]←⍎op,'/mem[r1 r2]'
 c+←4
 →LOOP
HALT:
 R←mem[0]
∇
⍝ Part 2 is easy once you discover the relation between input and output is polynomial

#2

day3

q1

:Namespace Day3
⍝ === VARIABLES ===

...
⍝ === End of variables definition ===

(⎕IO ⎕ML ⎕WX)←1 1 3

 draw←{
     walk←{
         f←{d n←⍵
             d≡'R':n 0
             d≡'U':0 n
             d≡'L':(-n)0
             d≡'D':0(-n)}
         ⍺+f ⍵
     }
     worker←{
         0=≢⍵:⍺⍺
         W((⍺⍺,⊂W)∇∇)1↓⍵⊣W←⍺ walk⊃⍵
     }
     0 0((⊂0 0)worker)⍵
 }

 expand←{
     e←{A B←⍵ ⋄ (K/A)(⌽⍣(⊃~K),)¨(⊃-∘(⍳∘|××)-/),(~K←A=B)/↑A B}
     ⊃,/e¨((1 0⍴⍨⍴)⊂⊢)((⌽+⊢)0(@1)(⍴⍵)⍴1)/⍵

 parse←{(⊃,(⍎1↓⊢))¨(','≠⍵)⊆⍵}

 q1←{
     l1 l2←⍵
     f←expand∘draw∘parse
     ⌊/(+/|)¨I/⍨(I←f l1)∊f l2
 }

:EndNamespace 

q2

q2←{
     l1 l2←⍵
     f←expand∘draw∘parse
     s←{(⍸K)[⍒(K←⍵∊⍺)/⍵]}
     ⌊/(f l2)(s+s⍨)f l1
 }

#3

day4

q1←{
     ⍴(((∧⌿2≤⌿⊢)∧(∨⌿2=⌿⊢))(6⍴10)⊤⍵)/⍵
 }
q2←{
     ⍴⍵/⍨(∨/{0 1 0≡⍵}⌺3)¨↓⍉2=⌿(6⍴10)⊤⍵
 }

#4

Day5 和 Day2 类似,略去。


#5

Day6, 用 dfs。 直接贴 q2

test←{
     c1←1 2 3 4 5 2 6 4 5 7 8 8 9
     c2←2 3 4 5 10 6 11 9 7 8 12 13 14
     dfs←{⊃∇⍨/⌽(⊂⍺ ⍺⍺ ⍵),⍺ ⍵⍵ ⍵}
     next←{
         f depth id←⍵
         c←(id=c1)/c2
         p←(id=c2)/c1
         {id,(depth+1),⍵}¨{(f≠⍵)/⍵}c,p
     }
     acc←{
         f depth id←⍵
         14=id:⍺+depth
         ⍺
     }
     (0(acc dfs next)0 0 13)-2
 }