Skip to content

Commit

Permalink
Create part2.fth
Browse files Browse the repository at this point in the history
  • Loading branch information
Iamlel authored Feb 8, 2023
1 parent 76d90a1 commit 61151a1
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions day09/part2.fth
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
include ./util.fth

: hash-pair ( x y -- h )
swap 1 lshift dup 0 < if negate 1 - then
swap 1 lshift dup 0 < if negate 1 - then
2dup >= if over dup * + + else dup * + then ;

: normalize dup abs 2 = if 2 / then ;

act-new value locations
: execute-instructions ( rope-addr max fname-addr fname-len -- )
r/w open-file throw >r \ open file and throw fp onto rstack
begin pad over r@ read-line throw \ iterate through file
while
rot >r pad tuck dup c@ \ get the instruction
80 > -2 * 1 - \ get the sign of the direction
-rot 2 + swap 2 - 0 here 2swap >number throw 2drop \ get the amount of instruction
rot c@ dup 76 = swap 82 = or 1 + cells \ get direction
r@ 2swap 0 do \ loop instruction number of times
dup 2over + +! \ move rope by 1
over 9 0 do
2 cells + >r \ get next rope knot
r@ 2 cells - @ r@ @ - \ head x - tail x
r@ cell - @ r@ cell + @ - \ head y - tail y

2dup abs 1 > swap abs 1 > or if \ check the distance
normalize swap normalize \ divide by 2 if its -2 or 2
r@ +! r@ cell + +! \ increment by displacement vector
else 2drop then r>
loop drop
swap dup 18 cells + @ over 19 cells + @ hash-pair \ hash the tail
dup locations act-insert swap \ insert value into avl tree
loop 2drop drop r> swap
repeat r> close-file throw 2drop drop ;

: main
20 cells dup allocate throw tuck swap erase \ create rope
10 s" input.txt" execute-instructions \ run instructions
locations act-length@ . \ print the length
;

: return 10 emit bye ;

main
return

0 comments on commit 61151a1

Please sign in to comment.