Skip to content

Instantly share code, notes, and snippets.

@xpqz
Last active December 9, 2022 17:56
Show Gist options
  • Select an option

  • Save xpqz/4e3e8729e883aa593aa387140531a824 to your computer and use it in GitHub Desktop.

Select an option

Save xpqz/4e3e8729e883aa593aa387140531a824 to your computer and use it in GitHub Desktop.
day9
Read data, separate into vectors for direction and magnitude
d' '()¨⎕nget'd/9'1
mag¨1ddir'URDL'0d
Convert to complex offsets of the magnitude in specified direction
moves1 0J1 ¯1 0J¯1[dir]×mag
Expand the 'gaps' -- 0 4 means 0 1 2 3 4 etc. This gives the path of
the head knot
headpath2{+(-×)×|-}/+\0,moves
Helper func: if tail is more than 1 step either vertically or horizontally,
move diagonally. If within 1 step in both, remain. Otherwise, move 1 step
towards head.
g{1.5>||-:+0j1×11 9-}
First tail: follow the head
tailp{a0{0=:aa,vgv1}}
tailpathtailp headpath
tailpath part1
part2: 10-tails -- actually rather easy: repeatedly do part1, but
with the previous tail as the new head.
paths headpath_{paths,ptailp p}9headpath
Only look at the _actual_ tail positions.
1+9paths part2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment