aoc2021/day2.retro

51 lines
1.1 KiB
Plaintext

# Day 2: Dive!
## part 1
This one doesn't seem too difficult. Parsing a text file line-by-line and updating 2 variables based on that.
~~~
'depth var
'hpos var
~~~
The `read-move` word is used for every command in the file.
~~~
:read-move (s-) ASCII:SPACE s:split/char
'forward [ s:trim s:to-number @hpos + !hpos ] s:case
'up [ s:trim s:to-number @depth swap - !depth ] s:case
'down [ s:trim s:to-number @depth + !depth ] s:case ;
~~~
And run this on the whole file, printing the results:
~~~
'input2 [ read-move ] file:for-each-line
@depth @hpos * n:put ASCII:LF c:put
~~~
## part 2
basically like part 1 but we need a new variable and to change some of the move-reading logic.
~~~
#0 !depth
#0 !hpos
'aim var
~~~
~~~
:read-aim (s-) ASCII:SPACE s:split/char
'forward [ s:trim s:to-number dup @hpos + !hpos @aim * @depth + !depth ] s:case
'up [ s:trim s:to-number @aim swap - !aim ] s:case
'down [ s:trim s:to-number @aim + !aim ] s:case ;
~~~
And run this on the whole file, printing the results:
~~~
'input2 [ read-aim ] file:for-each-line
@depth @hpos * n:put ASCII:LF c:put
~~~