Paste: factor day1

Author: Recruit
Mode: factor
Date: Thu, 2 Dec 2021 18:51:47
Plain Text |
USING: kernel shuffle io io.encodings.utf8 io.files prettyprint
math math.parser formatting arrays sequences splitting ;
IN: day1


! move 2nd and 3rd elements to the front of the stack
: move2-infront ( x y z -- z x y )
   2over [ 2drop ] 3dip
;
! get an index and a sequence and return the element at that index and the inmediate next one
: get-nth-elems ( i seq -- elemI elemI+1 )
   2dup
   nth
   move2-infront
   [ 1 + ] dip
   nth
;
: increase-count ( c i seq -- c+1 i seq )
   rot 1 + -rot
;
: increase-idx ( c i seq -- c i+1 seq )
   swap 1 + swap
;

: main ( -- )
   ! split and parse lines into a sequence of numbers
   "depths.txt" utf8 file-lines [ string>number ] map
   dup length
   1 - [ 0 0 ] 2dip [
      2dup
      get-nth-elems < [
         increase-count
      ] [ ] if
      increase-idx
   ] times
   2drop
   .
;

MAIN: main

New Annotation

Summary:
Author:
Mode:
Body: