git.alexw.nyc home about git garden
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
\ utils
: skip-thru ( c -- ) begin in< over = until drop ; 
: array ( n -- ) ( i -- addr )
  here# swap cells allot does> swap cells + ; 
: sum ( a u -- ) 0 >r for @+  to+ V1 next V1 rdrop ; 

212 const num-cards

100       array winning-buf
num-cards 1+ array card-copies

: clear-winning 100 for 0 i winning-buf ! next ; \ semi wasteful (32-bit)
: a-points ( n -- n2 ) 1 swap lshift >> ; \ 2^(n-1)

0 value cur-card 

: add-copies ( n -- ) cur-card card-copies @ >r
  0 swap for2 cur-card i 1+ + dup num-cards > if break then 
  card-copies V1 1+ swap +! next rdrop to1+ cur-card ;

: check-wins ( n... -- n )
  0 >r begin scnt 0 <> while 
  winning-buf  @ if to1+ V1 then repeat V1 rdrop ;

0 value points
: Card check-wins dup a-points to+ points 
  add-copies clear-winning ':' skip-thru ;
: | scnt for winning-buf 1 swap ! next ;

f<< /aoc/input Card x: 

points . nl>  \ part 1
0 card-copies num-cards 1+ sum num-cards + . nl> \ part 2