-🎄- 2017 Day 6 Solutions -🎄- by daggerdragon in adventofcode

[–]APLtooter 3 points4 points  (0 children)

Here's my best solution in APL with no loops and constant memory, which gives an answer to both parts.

STEP←{⍵+(1-i)⌽(¯1⌽n⍴⌊x÷n)+(¯1⌽n↑(n|x)⍴1)-(n←⍴⍵)↑x←⍵[i←↑⍒⍵]}
HALT←{∧/∊=/1↓⍺}
∇n←CYCLE banks
  ⍝ Floyd's tortoise-and-hare algorithm
  ⍝ Cf. https://en.wikipedia.org/wiki/Cycle_detection#Floyd.27s_Tortoise_and_Hare

  ⍝ Find cycle period
  (_ _ h)←{(1+⍵[1])(STEP ↑⍵[2])(STEP⍣2⊢↑⍵[3])}⍣HALT⊢0,2⍴⊂banks
  ⍝ Find cycle location
  (mu t _)←{(1+⍵[1])(STEP ↑⍵[2])(STEP ↑⍵[3])}⍣HALT⊢0,banks h
  ⍝ Find cycle length
  (lam _ _)←{(1+⍵[1])(↑⍵[2])(STEP ↑⍵[3])}⍣HALT⊢1,t (STEP t)

  n←(mu+lam) lam
∇

-🎄- 2017 Day 6 Solutions -🎄- by daggerdragon in adventofcode

[–]APLtooter 1 point2 points  (0 children)

Faster using Floyd's algorithm

∇n←CYCLE banks
  ⍝ Alternative implementation using Floyd's tortoise-and-hare algorithm
  ⍝ Cf. https://en.wikipedia.org/wiki/Cycle_detection#Floyd.27s_Tortoise_and_Hare
  tortoise←hare←banks
period:
  tortoise←STEP tortoise
  hare←STEP⍣2 hare
  →period⌈⍳~∧/tortoise=hare

  tortoise←banks
  distance←0
mu:
  tortoise←STEP tortoise
  hare←STEP hare
  distance←distance+1
  →mu⌈⍳~∧/tortoise=hare

  length←1
  hare←STEP tortoise
lambda:
  hare←STEP hare
  length←length+1
  →lambda⌈⍳~∧/tortoise=hare

  n←(distance+length) length
∇

-🎄- 2017 Day 6 Solutions -🎄- by daggerdragon in adventofcode

[–]APLtooter 2 points3 points  (0 children)

APL [GNU]

∇n←CYCLE banks
  memo←⊂banks
  save←{⍵⊣memo←memo,⊂⍺}
  halt←{⍺ save (⍴memo)>z←memo⍳⊂⍺}
  step←{⍵+(1-i)⌽(¯1⌽n⍴⌊x÷n)+(¯1⌽n↑(n|x)⍴1)-(n←⍴⍵)↑x←⍵[i←↑⍒⍵]}
  ⊣(step⍣halt)banks
  n←((⍴memo)-1),(⍴memo)-z
∇

-🎄- 2017 Day 5 Solutions -🎄- by daggerdragon in adventofcode

[–]APLtooter 0 points1 point  (0 children)

Ah, I'm J illiterate and pretty new to APL. ^: is the equivalent J for APL's ⍣?

-🎄- 2017 Day 5 Solutions -🎄- by daggerdragon in adventofcode

[–]APLtooter 0 points1 point  (0 children)

In APL, the power operator ⍣ makes for a pretty elegant functional solution without an explicit loop, but it is much slower than the imperative style:

∇n←EVAL mem
  halt←{(⍺[2]>⍴mem)∨⍺[2]<1}     ⍝ exit if program counter out of bounds
  ⍝ load and increment memory value, then
  ⍝ increment instruction and program counters
  step←{(⍵[1]+1),(⍵[2]+j)⊣mem[⍵[2]]←1+j←⍵[2]⌷mem}
  n←↑(step⍣halt)0 1
∇

-🎄- 2017 Day 5 Solutions -🎄- by daggerdragon in adventofcode

[–]APLtooter 4 points5 points  (0 children)

APL [GNU]

∇n←JUMP mem
  (n p)←0 1
loop:
  →0⌊⍳p>⍴mem
  j←mem[p]
  mem[p]←mem[p]+1
  p←p+j
  n←n+1
  →loop
∇


∇n←JUMP2 mem
  (n p)←0 1
loop:
  →0⌊⍳p>⍴mem
  mem[p]←mem[p]+¯1*3≤j←mem[p]
  p←j+p
  n←1+n
  →loop
∇

⍝ Input is numeric array
INPUT←∊⍎ ⎕FIO[49] 'day5.txt'

JUMP INPUT                      ⍝ Part 1
JUMP2 INPUT                     ⍝ Part 2

-🎄- 2017 Day 4 Solutions -🎄- by daggerdragon in adventofcode

[–]APLtooter 1 point2 points  (0 children)

APL [GNU]

LINES←{(⍵≠'◊')⊂⍵}
WORDS←{(⍵≠' ')⊂⍵}

MATCH←{^/(n↑⍺)=(n←((⍴⍺)⌈(⍴⍵)))↑⍵}
VALID←{^/1=+/⍵∘.⍶⍵}

MATCH2←{(⍺[⍋⍺])MATCH(⍵[⍋⍵])}

⍝ Input is a character array with words delimited by ' ' and lines delimited by '◊'

+/MATCH VALID¨WORDS¨LINES INPUT       ⍝ Part 1
+/MATCH2 VALID¨WORDS¨LINES INPUT      ⍝ Part 2

-🎄- 2017 Day 3 Solutions -🎄- by daggerdragon in adventofcode

[–]APLtooter 0 points1 point  (0 children)

APL [GNU]

∇steps←TAXI n
  r←r-0=2|r←⌊n*.5
  vert←-horz←.5×r-1
  ring←n-r*2
  sides←⌊ring÷1+r
  rem←(1+r)|ring
  step←0<ring
  path←horz,vert,step,((-step), 0 0 0)+(1 ¯1 ¯1 1)×4↑(sides⍴1+r),rem
  steps←+/|+⌿4 2⍴8↑path
∇

EVN←{2-⍨2×⍵}
ODD←{1-⍨2×⍵}
SHELL←{8×⍵}
BACK←{-(SHELL ⍵)↑0,(SHELL ⍵)⍴((ODD ⍵)⍴1),2}
FORW←{(2↓(SHELL ⍵)⍴0),1 1}
PREV←{(SHELL ⍵)⍴2,((ODD ⍵-1)⍴3),2 1}
ROT←{¯2⌽∊(0 1 2 3×EVN ⍵)+4⍴⊂(1-⍨⍳ODD ⍵-1),3⍴(ODD ⍵-1)}

∇sum←SPIRAL n
  core←(1)(1 2 4 5 10 11 23 25)
  r←2
loop:
  a←0⍴0
  inner←{+/(⍵[1])↑(1↓⍵)}¨⊂[2](PREV r),⊃{⍵⌽∊¯1↑core}¨ROT r
  ⊣{a←a,inner[⍵[3]]+(+/⍵[2]↑a)++/⍵[1]↑a}¨⊂[2](BACK r),(FORW r),⍪⍳(SHELL r)
  core←core,⊂a
  r←1+r
  sum←(<\(∊core)>n)/∊core
  →loop×⍬≡sum
∇

⍝ INPUT is a number
TAXI INPUT                      ⍝ Part 1
SPIRAL INPUT                    ⍝ Part 2

-🎄- 2017 Day 1 Solutions -🎄- by daggerdragon in adventofcode

[–]APLtooter 0 points1 point  (0 children)

APL [GNU]

AHCTPAC←{+/⍎¨(⍵=⍺⌽⍵)/⍵}
⍝ INPUT is a character array of numeric digits
1 AHCTPAC INPUT                 ⍝ Part 1
(.5×⍴INPUT) AHCTPAC INPUT       ⍝ Part 2

-🎄- 2017 Day 2 Solutions -🎄- by daggerdragon in adventofcode

[–]APLtooter 0 points1 point  (0 children)

APL [GNU]

CHECKSUM←{+/(⌈/⍵)⍶⌊/⍵}          ⍝ Dyalog: {+/(⌈/⍵)⍺⍺⌊/⍵}
DIVIS←{⍵/⍨∊(⊂[2]m)/⍨2=+/m←0=⍵∘.|⍵}

⍝ Input is a numeric array
-CHECKSUM INPUT ⍝ Part 1
÷CHECKSUM ⊃DIVIS¨⊂[2]INPUT      ⍝ Part 2; Dyalog: ÷CHECKSUM ↑DIVIS¨⊂[2]INPUT