uf-toys

toys and experiments with uf forth
git clone git://git.alexwennerberg.com/uf-toys
Log | Files | Refs | README

commit a8c0a6ca150f1c01258650dd245acfd1f1fc7a14
parent b0f196f38e6685d0f37f87ece6a28b12575f07ec
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sat,  1 Oct 2022 17:59:09 -0700

starting advent of code

Diffstat:
M.gitignore | 1+
Aaoc/01a.f | 8++++++++
Aaoc/01b.f | 11+++++++++++
Aaoc/02a.f | 16++++++++++++++++
Aaoc/02b.f | 18++++++++++++++++++
Aaoc/README.md | 11+++++++++++
Aaoc/dmath.f | 106+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aaoc/etc/getinput.sh | 3+++
Aaoc/utils.f | 1+
9 files changed, 175 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1 +1,2 @@ *.rom +input.txt diff --git a/aoc/01a.f b/aoc/01a.f @@ -0,0 +1,8 @@ + +variable last +: to-num ( a u -- n ) 0 -rot 0 do dup r@ + c@ h# 30 - rot 10 * + swap loop drop ; +: read-num ( -- n ) pad pad 10 accept to-num ; +: get-first-depth read-num last ! ; +: ?deeper dup last @ u> swap last ! ; +: count-deepers 1999 0 do read-num ?deeper if 1+ then loop ; +get-first-depth 0 count-deepers . cr bye diff --git a/aoc/01b.f b/aoc/01b.f @@ -0,0 +1,11 @@ +variable last +variable total +: to-num ( a u -- n ) 0 -rot 0 do dup r@ + c@ h# 30 - rot 10 * + swap loop drop ; +: read-num ( -- n ) pad pad 10 accept to-num ; +: 3dup 2 pick 2 pick 2 pick ; +: read-first-3 3 0 do read-num loop ; +: solve read-first-3 2000 3 do +3dup + + dup \ sum +last @ swap < 0<> negate total +! last ! +rot drop read-num loop total @ ; +solve . cr bye diff --git a/aoc/02a.f b/aoc/02a.f @@ -0,0 +1,16 @@ +\ doesnt need stdin, reads from file named "input.txt" +include dmath.f + +variable horiz 0 horiz ! +variable depth 0 depth ! + +: to-num ( a u -- n ) 0 -rot 0 do dup r@ + c@ h# 30 - rot 10 * + swap loop drop ; +: get-n bl parse to-num ; +: forward ( "N" -- ) get-n horiz +! ; +: down ( "N" -- ) get-n depth +! ; +: up ( "N" -- ) get-n negate depth +! ; +: solve horiz @ depth @ um* ; + +include input.txt + +solve d. cr bye diff --git a/aoc/02b.f b/aoc/02b.f @@ -0,0 +1,18 @@ +\ doesnt need stdin, reads from file named "input.txt" +\ copied mostly from peter to learn metaprogramming stuff +include dmath.f + +variable horiz 0 horiz ! +2variable depth 0 0 depth 2! +variable aim 0 aim ! + +: to-num ( a u -- n ) 0 -rot 0 do dup r@ + c@ h# 30 - rot 10 * + swap loop drop ; +: get-n bl parse to-num ; +: forward ( "N" -- ) get-n dup horiz +! aim @ * 0 depth 2@ d+ depth 2! ; +: down ( "N" -- ) get-n aim +! ; +: up ( "N" -- ) get-n negate aim +! ; +: solve depth 2@ horiz @ ; + +include input.txt + +solve .s cr . d. cr bye \ TODO figure out how to multiply these diff --git a/aoc/README.md b/aoc/README.md @@ -0,0 +1,11 @@ +Experimenting with aoc-2021. My plan is to do advent of code 2022 LIVE on twitch and upload the VODs to youtube. + +Many of these are "inspired" by peter fidelman's [solutions](https://gitlab.cs.washington.edu/fidelp/advent-of-code-2021) + +many of these won't run on the sample input + +Easiest way to run: + +``` +cat ##.f input.txt | uxncli [or uxnemu for solutions with visualizations] ufx.rom +``` diff --git a/aoc/dmath.f b/aoc/dmath.f @@ -0,0 +1,106 @@ +\ mostly taken from: +\ http://excamera.com/files/j1demo/docforth/nuc.fs.html + +: d= ( a b c d -- f ) + >r \ a b c + rot xor \ b a^c + swap r> xor \ a^c b^d + or 0= ; +: d+ ( augend . addend . -- sum . ) + rot + >r ( augend addend) + over + ( augend sum) + dup rot ( sum sum augend) + u< if ( sum) + r> 1+ + else + r> + then ; ( sum . ) +: s>d dup 0< if -1 else 0 then ; +: dnegate + invert swap invert swap + 1 0 d+ ; +: dabs ( d -- ud ) dup 0< if dnegate then ; +: d- dnegate d+ ; +: d< \ ( al ah bl bh -- flag ) + rot \ al bl bh ah + 2dup = + if + 2drop u< + else + 2nip > + then ; +: d0= or 0= ; +: d0< 0 0 d< ; +: d2* 2dup d+ ; +: d2/ dup 31 lshift >r 2/ swap 2/ r> or swap ; +: dmax 2over 2over d< if 2swap then 2drop ; +: dmin 2over 2over d< 0= if 2swap then 2drop ; + +variable scratch +: um* ( u1 u2 -- ud ) + scratch ! + 0 0 + 16 0 do + 2dup d+ + rot dup 0< if + 2* -rot + scratch @ 0 d+ + else + 2* -rot + then + loop + rot drop ; +: abssgn ( a b -- |a| |b| negf ) + 2dup xor 0< >r abs swap abs swap r> ; +: m* abssgn >r um* r> if dnegate then ; +: divstep + ( divisor dq hi ) + 2* + over 0< if 1+ then + swap 2* swap + rot ( dq hi divisor ) + 2dup < 0= if + tuck ( dq divisor hi divisor ) + - + swap ( dq hi divisor ) + rot 1+ ( hi divisor dq ) + rot ( divisor dq hi ) + else + -rot + then ; + + +: um/mod ( ud u1 -- u2 u3 ) + -rot 16 0 do divstep loop + rot drop swap ; +: sm/rem ( d n -- r q ) ( symmetric ) + over >r >r dabs r@ abs um/mod + r> r@ xor 0< if negate then r> 0< if >r negate r> then ; +: */mod >r m* r> sm/rem ; +: */ */mod nip ; +: t2* over >r >r d2* r> 2* r> 0< 1 and + ; + +variable divisor +: m*/mod + divisor ! + tuck um* 2swap um* ( hi. lo. ) + ( m0 h l m1 ) + swap >r 0 d+ r> ( m h l ) + -rot ( l m h ) + 32 0 do + t2* + dup divisor @ >= if + divisor @ - + rot 1+ -rot + then + loop ; +: m*/ m*/mod drop ; + +\ double printing +: (digit) ( u -- c ) 9 over < 7 and + [char] 0 + ; +: (d#) ( d1 -- d2 ) + 0 base @ um/mod >r base @ um/mod swap (digit) hold r> ; +: d#s ( d1 -- d2 ) begin (d#) 2dup or 0= until ; +: d#> ( d -- a u ) drop #> ; +: (d.) ( d -- a n ) 2dup dabs <# d#s 2swap nip sign d#> ; +: d. ( n -- ) (d.) type space ; diff --git a/aoc/etc/getinput.sh b/aoc/etc/getinput.sh @@ -0,0 +1,3 @@ +# usage: ./this [year] [day] +# set AOC_SESSION env var with cookie from browser console +curl -s -H "Cookie: session=$AOC_SESSION" https://adventofcode.com/$1/day/$2/input diff --git a/aoc/utils.f b/aoc/utils.f @@ -0,0 +1 @@ +