Hello Clodsire, I finally got the first part of the first task done, do you want to see some FORTH code?

(My first idea was a weird Binary Coded Decimal - type version for the PDP-8 in assembly, but I gave up because I'm clearly too stupid for that, so I decided to use the OpenBoot FORTH environment on a Sun JavaStation instead)

Advent of Code, day 1, part 1 

variable lock
variable cnt
variable lines
here 5 allot constant buf
: init-lock d# 50 lock ! 0 cnt ! 0 lines ! ;
: dir key dup emit dup
52 ( R ) = if drop 1 else
4c ( L ) = if -1 else 0 then then ;
: getnum base @ decimal
buf 5 2dup 2dup blank expect -trailing $number
if abort" Number conv failed" then
swap base ! ;
: process init-lock
begin dir ?dup while 1 lines +!
getnum * d# 100 + lock @ + d# 100 mod
dup 0= if 1 cnt +! then
lock ! repeat ;
: results cr
." Lock at: " lock @ . cr
." Count: " cnt @ . cr
." Lines processed: " lines @ . ;

Advent of Code, day 1, part 2 

Only thing that needs to change is the process function, but it took me too long to get the details right:

: process init-lock
begin dir ?dup while 1 lines +!
getnum d# 100 /mod cnt +!
* lock @ +
dup d# 99 > over 1 < or lock @ and if 1 cnt +! then
d# 100 + d# 100 mod lock !
repeat ;

Advent of Code, day 1, part 2 

Sorry for the slightly weird indentation, too tired to figure out how to fix my Vim config

re: Advent of Code, day 1, part 2 

@vaporeon_ i didn't end up getting a working solution for part 2, good job!

re: Advent of Code, day 1, part 2, spoilers 

@wallhackio I assume you can't read FORTH, but logic is relatively simple

We do division with modulus by 100, and the division part can just be added to the count, because it's whole rotations by 100 dials

The modulus part, we treat it similar to part 1 in that we add it to the number on the lock, but now we also need to check whether the boundary was crossed

If the number on the lock would now be beyond the range 1-99, we have crossed or reached the boundary of 0 (99 > over 1 < or)... unless we were already at 0 in the previous step! That's what lock @ and (would be written in C as && lock) handles, took me too long to find that edge case...

Follow

re: Advent of Code, day 1, part 2, spoilers 

@vaporeon_ that edge case got me too! but something else was wrong after i accounted for it and i gave up haha

Sign in to participate in the conversation
📟🐱 GlitchCat

A small, community‐oriented Mastodon‐compatible Fediverse (GlitchSoc) instance managed as a joint venture between the cat and KIBI families.