Commit 0a859081 authored by Peter Fidelman's avatar Peter Fidelman
Browse files

Days 1-3

parents
variable last
: read-num ( -- n n? ) 0 0 pad pad 10 accept >number nip nip not ;
: ?deeper ( n -- f ) dup last @ > swap last ! ;
: get-first-depth ( -- ) read-num drop last ! ;
: count-deepers ( n -- ) begin read-num while ?deeper if 1+ then repeat drop ;
get-first-depth 0 count-deepers ." Answer:" .
\ No newline at end of file
: addrs ( a -- a a' a'' ) dup cell + dup cell + ;
: slide ( a -- n' ) addrs dup @ >r over @ swap ! swap @ swap ! r> ;
: mkwindow
create ( -- ) 0 , 0 , 0 ,
does> ( n [a] -- ?deeper ) dup >r slide swap dup r> ! < ;
mkwindow >w>
: read-num ( -- n n? ) 0 0 pad pad 10 accept >number nip nip not ;
: first-window ( -- ) 3 0 do read-num drop >w> drop loop ;
: count-deepers ( n -- n ) begin read-num while >w> if 1+ then repeat drop ;
first-window 0 count-deepers ." Answer:" .
\ No newline at end of file
: get-n ( -- n ) 0 0 bl parse >number 2drop drop ;
variable horiz 0 horiz !
variable depth 0 depth !
: forward ( "N" -- ) get-n horiz +! ;
: up ( "N" -- ) get-n negate depth +! ;
: down ( "N" -- ) get-n depth +! ;
: report ( -- ) horiz @ depth @ * ;
\ Then, paste your input into the repl and type "report".
\ Data is code.
\ No newline at end of file
: get-n ( -- n ) 0 0 bl parse >number 2drop drop ;
variable horiz 0 horiz !
variable depth 0 depth !
variable aim 0 aim !
: forward ( "N" -- ) get-n dup horiz +! aim @ * depth +! ;
: up ( "N" -- ) get-n negate aim +! ;
: down ( "N" -- ) get-n aim +! ;
: report ( -- ) horiz @ depth @ * ;
\ No newline at end of file
variable n 0 n !
variable bit 0 bit !
: pops ( -- ) create here 12 cells dup allot 0 fill
does> ( -- a ) bit @ cells + 1 bit +! ;
pops p
: do-cr ( -- ) bit @ 0= if r> drop else 0 bit ! 1 n +! then ;
: go ( -- ) begin key dup 10 = if drop do-cr else 48 - p +! then again ;
: gamma ( -- g ) 0 12 0 do 1 << p @ 2* n @ > 1 and or loop ;
: delta ( g -- g d ) dup invert 1 12 << 1- and ;
go gamma delta *
\ No newline at end of file
\ Fill PAD with raw chars read from problem input, with 12 char stride.
\ # of chars read: "#c".
variable #c
\ 5 constant stride
12 constant stride
: key>pad ( a n -- a' )
dup 32 = if drop pad - #c ! rdrop exit then
dup 10 = if drop exit then
over c! 1+ ;
: keys>pad ( -- ) pad begin key key>pad again ;
: buf ( -- a ) pad #c @ + ;
: pad>buf ( -- ) pad buf #c @ cmove> ;
: stride-exec ( a #bit xt -- n )
>r
over + ( a a' ) swap #c @ + swap ( amax a' )
begin 2dup > while
dup r@ execute stride +
repeat
2drop rdrop ;
variable digit-count 2 cells allot
: strip-sign ( n -- n') cell 8 * 1- 1 swap << invert and ;
: c-to-idx ( c -- i ) 48 - strip-sign 2 min ;
: clear-counts ( -- ) digit-count 3 cells 0 fill ;
: count-digit ( c -- ) c-to-idx cells digit-count + 1 swap +! ;
: (most-common-step) ( a -- ) c@ count-digit ;
: (most-common-end) ( -- n ) digit-count 2@ >= 1 and ;
: most-common ( a #bit -- n )
clear-counts
['] (most-common-step) stride-exec (most-common-end) ;
: least-common ( a #bit -- n ) most-common 1 xor ;
variable bit-offset
variable savior-char
create last-saved stride allot
: (nuke) ( a -- ) bit-offset @ - stride bl fill ;
: (save) ( a -- ) bit-offset @ - last-saved stride cmove> ;
: (nuke-step) ( a -- ) dup c@ savior-char @ = if (save) else (nuke) then ;
: nuke ( a #bit savior-bit -- )
48 + savior-char !
dup bit-offset !
['] (nuke-step) stride-exec ;
: ox ( -- ) pad>buf buf stride 0 do dup i 2dup most-common nuke loop drop ;
: co ( -- ) pad>buf buf stride 0 do dup i 2dup least-common nuke loop drop ;
: ans>num ( -- n ) 0 0 last-saved stride >number 2drop drop ;
: go ( -- ) keys>pad binary ox ans>num co ans>num decimal * ." Answer:" . ;
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment