Paste: AoC 2022 day 5

Author: xr
Mode: factor
Date: Mon, 5 Dec 2022 14:21:39
Plain Text |
───────┬──────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: aoc2022-5.factor
───────┼──────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ ! Copyright (C) 2022 Your name.
   2   │ ! See http://factorcode.org/license.txt for BSD license.
   3   │ USING: accessors splitting aoc math.matrices sequences kernel
   4   │  peg.parsers peg.search ;
   5   │
   6   │ IN: aoc2022-5
   7   │
   8   │ : split-input ( seqstring -- seqstring seqstring )
   9   │   { "" } split1 ;
  10   │
  11   │ : not-space ( x -- ? ) 32 = not ;
  12   │
  13   │ : parse-crates ( seqstring -- seqseq )
  14   │   transpose
  15   │   ! Use numbers as guide for the columns
  16   │   [ last not-space ] filter
  17   │   ! clean columns from empty spaces and strip guide
  18   │   [ [ not-space ] filter but-last ] map ;
  19   │
  20   │ TUPLE: inst { move read-only } { from read-only } { to read-only } ;
  21   │
  22   │ C: <inst> inst
  23   │
  24   │ : parse-instruction ( seqstring -- seqvec )
  25   │   [ integer-parser search first3 <inst> ] map ;
  26   │
  27   │ : parse-input ( seqstr -- seqses seqinst )
  28   │   split-input
  29   │   [ parse-crates ] [ parse-instruction ] bi*
  30   │ ;

Annotation: include part1 and part2

Author: xr
Mode: factor
Date: Mon, 5 Dec 2022 19:23:42
Plain Text |
! Copyright (C) 2022 Your name.
! See http://factorcode.org/license.txt for BSD license.
USING: strings vectors accessors splitting aoc math math.matrices sequences kernel
 peg.parsers peg.search ;

IN: aoc2022-5

: split-input ( seqstring -- seqstring seqstring )
  { "" } split1 ;

: not-space ( x -- ? ) 32 = not ;

: parse-crates ( seqstring -- seqseq )
  transpose
  ! Use numbers as guide for the columns
  [ last not-space ] filter
  ! clean columns from empty spaces and strip guide
  [ [ not-space ] filter but-last >vector ] map
;

! parse crates as vectors, requires a reversal or anti transpose

TUPLE: inst { move read-only } { from read-only } { to read-only } ;

C: <inst> inst

: parse-instruction ( seqstring -- seqvec )
  [ integer-parser search first3 <inst> ] map ;

: parse-input ( seqstr -- seqses seqinst )
  split-input
  [ parse-crates ] [ parse-instruction ] bi*
;

! multistack/kstack operations

: popk ( i seq -- elt )
  nth pop ;

: pushk ( elt i seq -- )
  nth push ;

: movek ( i j seq -- )
  [ nip popk ] 2keep pushk ;

: nmovek ( n i j seq -- )
  '[ _ _ _ movek ] times ;

: inst>nmovek ( inst seq -- )
  [ [ move>> ] [ from>> 1 - ] [ to>> 1 - ] tri ] dip
  nmovek ;

: tops ( seq -- seq )
  [ pop ] map ;

: part1 ( -- x )
  "resource:work/aoc2022-5/input.txt" load-file
  parse-input
  swap
  [ reverse ] map
  [ '[ _ inst>nmovek ] each ] keep
  tops
  >string
;

! 9001 operations

: popk2 ( n i seq -- elts )
  [ swap cut ] change-nth ;

: pushk2 ( elts i seq -- )
  [ append ] change-nth ;

: movek2 ( n i j seq -- )
  [ nip popk2 ] 2keep pushk2 ;

: inst>nmovek2 ( inst seq -- )
  [ [ move>> ] [ from>> 1 - ] [ to>> 1 - ] tri ] dip
  movek2 ;

: tops2 ( seq -- seq )
  [ first ] map ;

: part2 ( -- x )
  "resource:work/aoc2022-5/input.txt" load-file
  parse-input
  swap
  [ '[ _ inst>nmovek2 ] each ] keep
  tops2
  >string
;

Annotation: Use same function for part1 and part2

Author: xr
Mode: factor
Date: Mon, 5 Dec 2022 19:49:33
Plain Text |
! Copyright (C) 2022 Your name.
! See http://factorcode.org/license.txt for BSD license.
USING: strings vectors accessors splitting aoc math math.matrices sequences kernel
 peg.parsers peg.search ;

IN: aoc2022-5

: split-input ( seqstring -- seqstring seqstring )
  { "" } split1 ;

: not-space ( x -- ? ) 32 = not ;

: parse-crates ( seqstring -- seqseq )
  transpose
  ! Use numbers as guide for the columns
  [ last not-space ] filter
  ! clean columns from empty spaces and strip guide
  [ [ not-space ] filter but-last >vector ] map
;

! parse crates as vectors, requires a reversal or anti transpose

TUPLE: inst { move read-only } { from read-only } { to read-only } ;

C: <inst> inst

: parse-instruction ( seqstring -- seqvec )
  [ integer-parser search first3 <inst> ] map ;

: parse-input ( seqstr -- seqses seqinst )
  split-input
  [ parse-crates ] [ parse-instruction ] bi*
  swap
;

: popk ( n i seq -- elts )
  [ swap cut ] change-nth ;

: pushk ( elts i seq -- )
  [ append ] change-nth ;

: movek ( n i j seq quot -- )
  '[ nip popk @ ] 2keep pushk ; inline

: inst>movek ( inst seq quot -- )
  [ [ move>> ] [ from>> 1 - ] [ to>> 1 - ] tri ] 2dip
  movek ; inline

: tops ( seq -- seq )
  [ first ] map ;

: part1 ( -- x )
  "resource:work/aoc2022-5/input.txt" load-file
  parse-input
  [ '[ _ [ reverse ] inst>movek ] each ] keep
  tops
  >string
;

: part2 ( -- x )
  "resource:work/aoc2022-5/input.txt" load-file
  parse-input
  [ '[ _ [ ] inst>movek ] each ] keep
  tops
  >string
;

New Annotation

Summary:
Author:
Mode:
Body: