• 0 Posts
  • 35 Comments
Joined 2 years ago
cake
Cake day: June 13th, 2023

help-circle

  • Factor

    spoiler
    TUPLE: equation value numbers ;
    C: <equation> equation
    
    : get-input ( -- equations )
      "vocab:aoc-2024/07/input.txt" utf8 file-lines [
        split-words unclip but-last string>number
        swap [ string>number ] map <equation>
      ] map ;
    
    : possible-quotations ( funcs numbers -- quots )
      dup length 1 -
      swapd all-selections
      [ unclip swap ] dip
      [ zip concat ] with map
      swap '[ _ prefix >quotation ] map ;
    
    : possibly-true? ( funcs equation -- ? )
      [ numbers>> possible-quotations ] [ value>> ] bi
      '[ call( -- n ) _ = ] any? ;
    
    : solve ( funcs -- n )
      get-input
      [ possibly-true? ] with filter
      [ value>> ] map-sum ;
    
    : part1 ( -- n )
      { + * } solve ;
    
    : _|| ( m n -- mn )
      [ number>string ] bi@ append string>number ;
    
    : part2 ( -- n )
      { + * _|| } solve ;
    


  • Factor

    spoiler
    : get-input ( -- rows )
      "vocab:aoc-2024/06/input.txt" utf8 file-lines ;
    
    : all-locations ( rows -- pairs )
      dimension <coordinate-matrix> concat ;
    
    : guard-location ( rows -- pair )
      [ all-locations ] keep
      '[ _ matrix-nth "<>^v" in? ] find nip ;
    
    TUPLE: state location char ;
    C: <state> state
    
    : guard-state ( rows -- state )
      [ guard-location ]
      [ dupd matrix-nth ] bi <state> ;
    
    : faced-location ( state -- pair )
      [ char>> H{
        { CHAR: > { 0 1 } }
        { CHAR: v { 1 0 } }
        { CHAR: < { 0 -1 } }
        { CHAR: ^ { -1 0 } }
      } at ] [ location>> ] bi v+ ;
    
    : off-grid? ( rows location -- ? )
      [ dimension ] dip
      [ v<= vany? ] keep
      { 0 0 } v< vany? or ;
    
    : turn ( state -- state' )
      [ location>> ] [ char>> ] bi
      H{
        { CHAR: > CHAR: v }
        { CHAR: v CHAR: < }
        { CHAR: < CHAR: ^ }
        { CHAR: ^ CHAR: > }
      } at <state> ;
    
    : obstacle? ( rows location -- ? )
      swap matrix-nth CHAR: # = ;
    
    : guard-step ( rows state -- state' )
      swap over faced-location
      {
        { [ 2dup off-grid? ] [ 2nip f <state> ] }
        { [ [ obstacle? ] keep-under ] [ drop turn ] }
        [ swap char>> <state> ]
      } cond ;
    
    : walk-out ( rows state -- trail )
      [
        [ 2dup location>> off-grid? ] [
          dup location>> ,
          dupd guard-step
        ] until
      ] { } make 2nip ;
    
    : part1 ( -- n )
      get-input dup guard-state walk-out cardinality ;
    
    : (walk-loops?) ( visited rows state -- looped? )
      dupd guard-step
      2dup location>> off-grid? [ 3drop f ] [
        pick dupd in? [ 3drop t ] [
          pick dupd adjoin (walk-loops?)
        ] if
      ] if ;
    
    : walk-loops? ( rows -- looped? )
      dup guard-state
      [ HS{ } clone ] 2dip
      pick dupd adjoin (walk-loops?) ;
    
    : obstacle-candidates ( rows -- pairs )
      [ guard-location ]
      [ dup guard-state walk-out members ] bi remove ;
    
    : part2 ( -- n )
      get-input dup obstacle-candidates
      [ CHAR: # spin deep-clone [ matrix-set-nth ] keep walk-loops? ] with count ;
    

  • Factor

    : get-input ( -- rules updates )
      "vocab:aoc-2024/05/input.txt" utf8 file-lines
      { "" } split1
      "|" "," [ '[ [ _ split ] map ] ] bi@ bi* ;
    
    : relevant-rules ( rules update -- rules' )
      '[ [ _ in? ] all? ] filter ;
    
    : compliant? ( rules update -- ? )
      [ relevant-rules ] keep-under
      [ [ index* ] with map first2 < ] with all? ;
    
    : middle-number ( update -- n )
      dup length 2 /i nth-of string>number ;
    
    : part1 ( -- n )
      get-input
      [ compliant? ] with
      [ middle-number ] filter-map sum ;
    
    : compare-pages ( rules page1 page2 -- <=> )
      [ 2array relevant-rules ] keep-under
      [ drop +eq+ ] [ first index zero? +gt+ +lt+ ? ] if-empty ;
    
    : correct-update ( rules update -- update' )
      [ swapd compare-pages ] with sort-with ;
    
    : part2 ( -- n )
      get-input dupd
      [ compliant? ] with reject
      [ correct-update middle-number ] with map-sum ;
    

    on GitHub


  • Factor

    spoiler
    : get-input ( -- rows )
      "vocab:aoc-2024/04/input.txt" utf8 file-lines ;
    
    : verticals ( rows -- lines )
      [ dimension last [0..b) ] keep cols ;
    
    : slash-origins ( rows -- coords )
      dimension
      [ first [0..b) [ 0 2array ] map ] [
        first2 [ 1 - ] [ 1 (a..b] ] bi*
        [ 2array ] with map
      ] bi append ;
    
    : backslash-origins ( rows -- coords )
      dimension first2
      [ [0..b) [ 0 2array ] map ]
      [ 1 (a..b] [ 0 swap 2array ] map ] bi* append ;
    
    : slash ( rows origin -- line )
      first2
      [ 0 [a..b] ]
      [ pick dimension last [a..b) ] bi* zip
      swap matrix-nths ;
    
    : backslash ( rows origin -- line )
      [ dup dimension ] dip first2
      [ over first [a..b) ]
      [ pick last [a..b) ] bi* zip nip
      swap matrix-nths ;
    
    : slashes ( rows -- lines )
      dup slash-origins
      [ slash ] with map ;
    
    : backslashes ( rows -- lines )
      dup backslash-origins
      [ backslash ] with map ;
    
    : word-count ( line word -- n )
      dupd [ reverse ] dip
      '[ _ subseq-indices length ] bi@ + ;
    
    : part1 ( -- n )
      get-input
      { [ ] [ verticals ] [ slashes ] [ backslashes ] } cleave-array concat
      [ "XMAS" word-count ] map-sum ;
    
    : origin-adistances ( rows origins line-quot: ( rows origin -- line ) -- origin-adistances-assoc )
      with zip-with
      "MAS" "SAM" [ '[ [ _ subseq-indices ] map-values ] ] bi@ bi append
      harvest-values
      [ [ 1 + ] map ] map-values ; inline
    
    : a-coords ( origin-adistances coord-quot: ( adistance -- row-delta col-delta ) -- coords )
      '[ first2 [ @ 2array v+ ] with map ] map-concat ; inline
    
    : slash-a-coords ( rows -- coords )
      dup slash-origins [ slash ] origin-adistances
      [ [ 0 swap - ] keep ] a-coords ;
    
    : backslash-a-coords ( rows -- coords )
      dup backslash-origins [ backslash ] origin-adistances
      [ dup ] a-coords ;
    
    : part2 ( -- n )
      get-input [ slash-a-coords ] [ backslash-a-coords ] bi
      intersect length ;
    

    Better viewed on GitHub.



  • Factor

    : get-input ( -- corrupted-input )
      "vocab:aoc-2024/03/input.txt" utf8 file-contents ;
    
    : get-muls ( corrupted-input -- instructions )
      R/ mul\(\d+,\d+\)/ all-matching-subseqs ;
    
    : process-mul ( instruction -- n )
      R/ \d+/ all-matching-subseqs
      [ string>number ] map-product ;
    
    : solve ( corrupted-input -- n )
      get-muls [ process-mul ] map-sum ;
    
    : part1 ( -- n )
      get-input solve ;
    
    : part2 ( -- n )
      get-input
      R/ don't\(\)(.|\n)*?do\(\)/ split concat
      R/ don't\(\)(.|\n)*/ "" re-replace
      solve ;
    

  • Factor

    : get-input ( -- reports )
      "vocab:aoc-2024/02/input.txt" utf8 file-lines
      [ split-words [ string>number ] map ] map ;
    
    : slanted? ( report -- ? )
      { [ [ > ] monotonic? ] [ [ < ] monotonic? ] } || ;
    
    : gradual? ( report -- ? )
      [ - abs 1 3 between? ] monotonic? ;
    
    : safe? ( report -- ? )
      { [ slanted? ] [ gradual? ] } && ;
    
    : part1 ( -- n )
      get-input [ safe? ] count ;
    
    : fuzzy-reports ( report -- reports )
      dup length <iota> [ remove-nth-of ] with map ;
    
    : tolerable? ( report -- ? )
      { [ safe? ] [ fuzzy-reports [ safe? ] any? ] } || ;
    
    : part2 ( -- n )
      get-input [ tolerable? ] count ;
    







  • I’m not who you asked, and not a user of pkgx, but one of the reasons I prefer rtx (which supports asdf plugins) to asdf is that by default it doesn’t use shims, but updates the PATH instead.

    So for example, running which python will show me a clearly versioned python executable path, rather than a mysterious shim that represents a different realpath at different times.



  • Factor on github (with comments and imports):

    Note: lemmy mangles the double-ampersands and the less-than symbols.

    : card-key ( ch -- n ) "23456789TJQKA" index ;
    
    : five-kind?  ( hand -- ? ) cardinality 1 = ;
    : four-kind?  ( hand -- ? ) sorted-histogram last last 4 = ;
    : full-house? ( hand -- ? ) sorted-histogram { [ last last 3 = ] [ length 2 = ] } &amp;&amp; ;
    : three-kind? ( hand -- ? ) sorted-histogram { [ last last 3 = ] [ length 3 = ] } &amp;&amp; ;
    : two-pair?   ( hand -- ? ) sorted-histogram { [ last last 2 = ] [ length 3 = ] } &amp;&amp; ;
    : one-pair?   ( hand -- ? ) sorted-histogram { [ last last 2 = ] [ length 4 = ] } &amp;&amp; ;
    : high-card?  ( hand -- ? ) cardinality 5 = ;
    
    : type-key ( hand -- n )
      [ 0 ] dip
      { [ high-card? ] [ one-pair? ] [ two-pair? ] [ three-kind? ] [ full-house? ] [ four-kind? ] [ five-kind? ] }
      [ dup empty? ] [
        unclip pick swap call( h -- ? )
        [ drop f ] [ [ 1 + ] 2dip ] if
      ] until 2drop
    ;
    
    :: (hand-compare) ( hand1 hand2 type-key-quot card-key-quot -- &lt;=> )
      hand1 hand2 type-key-quot compare
      dup +eq+ = [
        drop hand1 hand2 [ card-key-quot compare ] { } 2map-as
        { +eq+ } without ?first
        dup [ drop +eq+ ] unless
      ] when
    ; inline
    
    : hand-compare ( hand1 hand2 -- &lt;=> ) [ type-key ] [ card-key ] (hand-compare) ;
    
    : input>hand-bids ( -- hand-bids )
      "vocab:aoc-2023/day07/input.txt" utf8 file-lines
      [ " " split1 string>number 2array ] map
    ;
    
    : solve ( hand-compare-quot -- )
      '[ [ first ] bi@ @ ] input>hand-bids swap sort-with
      [ 1 + swap last * ] map-index sum .
    ; inline
    
    : part1 ( -- ) [ hand-compare ] solve ;
    
    : card-key-wilds ( ch -- n ) "J23456789TQKA" index ;
    
    : type-key-wilds ( hand -- n )
      [ type-key ] [ "J" within length ] bi
      2array {
        { { 0 1 } [ 1 ] }
        { { 1 1 } [ 3 ] } { { 1 2 } [ 3 ] }
        { { 2 1 } [ 4 ] } { { 2 2 } [ 5 ] }
        { { 3 1 } [ 5 ] } { { 3 3 } [ 5 ] }
        { { 4 2 } [ 6 ] } { { 4 3 } [ 6 ] }
        { { 5 1 } [ 6 ] } { { 5 4 } [ 6 ] }
        [ first ]
      } case
    ;
    
    : hand-compare-wilds ( hand1 hand2 -- &lt;=> ) [ type-key-wilds ] [ card-key-wilds ] (hand-compare) ;
    
    : part2 ( -- ) [ hand-compare-wilds ] solve ;
    

  • Factor on github (with comments and imports):

    I didn’t use any math smarts.

    : input>data ( -- races )
      "vocab:aoc-2023/day06/input.txt" utf8 file-lines     
      [ ": " split harvest rest [ string>number ] map ] map
      first2 zip                                           
    ;
    
    : go ( press-ms total-time -- distance )
      over - *
    ;
    
    : beats-record? ( press-ms race -- ? )
      [ first go ] [ last ] bi >
    ;
    
    : ways-to-beat ( race -- n )
      dup first [1..b)          
      [                         
        over beats-record?      
      ] map [ ] count nip       
    ;
    
    : part1 ( -- )
      input>data [ ways-to-beat ] map-product .
    ;
    
    : input>big-race ( -- race )
      "vocab:aoc-2023/day06/input.txt" utf8 file-lines             
      [ ":" split1 nip " " without string>number ] map
    ;
    
    : part2 ( -- )
      input>big-race ways-to-beat .
    ;
    

  • Aside from the general conciseness, the “universal function call syntax” is my favorite aspect of nim.

    If you want to take chaining procedures to the next level, try a concatenative language like Factor (I have a day 4 solution in this thread – with no assignment to variables).

    I also suggest having a look at Roc if you want a functional programming adventure, which offers great chaining syntax, a very friendly community, and is in an exciting development phase.