From 1fc1ac6b2bca89c4595abfbe9960aec375e7ebd1 Mon Sep 17 00:00:00 2001 From: crc Date: Thu, 2 Dec 2021 16:46:43 +0000 Subject: [PATCH] advent of code, day 2 FossilOrigin-Name: 9eb8ac12ee6e66bbb29ee03c38002a36a993e9f4f674919a572b1e7baadba2ca --- RELEASE-NOTES | 6 ++++ example/advent-of-code-2021-day-2.retro | 42 +++++++++++++++++++++++++ example/select.retro | 11 +++++++ 3 files changed, 59 insertions(+) create mode 100644 example/advent-of-code-2021-day-2.retro create mode 100644 example/select.retro diff --git a/RELEASE-NOTES b/RELEASE-NOTES index ca1a24d..b1d22c5 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -16,4 +16,10 @@ # Documentation + # Other + +# Examples + +- advent of code, 2021 (day 1, 2) +- select.retro diff --git a/example/advent-of-code-2021-day-2.retro b/example/advent-of-code-2021-day-2.retro new file mode 100644 index 0000000..8a5cc3e --- /dev/null +++ b/example/advent-of-code-2021-day-2.retro @@ -0,0 +1,42 @@ +As with day 1, the first part of this is easy. I'm "parsing" +the input data by using `s:tokenize` and then unpacking the +two element array. Convert the numeric part to a number, then +a simple set of `s:case`s to process things. + +~~~ +:a:unpack [ ] a:for-each ; +:parse ASCII:SPACE s:tokenize a:unpack s:to-number swap ; + +:process + parse + 'forward [ + ] s:case + 'down [ swap [ + ] dip ] s:case + 'up [ swap [ - ] dip ] s:case + drop-pair ; + +(depth,horizontal) +#0 #0 'day2.input [ process ] file:for-each-line +* n:put nl +~~~ + +The second half is modestly more difficult. As I'm minimizing +any use of variables, this is a little messier than I'd prefer, +but it's still pretty straightforward. + +I'm reusing the "parsing" part from the first half. + +My stack values are: aim depth horizontal + +~~~ +:process + parse + 'forward [ [ + ] [ 'abcd 'acbda reorder * + swap ] bi ] s:case + 'down [ [ rot ] dip + rot rot ] s:case + 'up [ [ rot ] dip - rot rot ] s:case + drop-pair ; + +(aim,depth,horizontal) +#0 #0 #0 'day2.input [ process ] file:for-each-line + +* n:put nl drop (discard_the_aim_field) +~~~ diff --git a/example/select.retro b/example/select.retro new file mode 100644 index 0000000..98b490b --- /dev/null +++ b/example/select.retro @@ -0,0 +1,11 @@ +Select will discard one of two values based on a passed +flag. + +~~~ +:select (abf-) &drop &nip choose ; +~~~ + +``` +#100 #200 TRUE select n:put nl +#100 #200 FALSE select n:put nl +```