• 24 Posts
  • 55 Comments
Joined 1 year ago
cake
Cake day: June 13th, 2023

help-circle




  • Woohoo! You didn't even mention: code blocks no longer mangle ampersands and less-than symbols!

    • Is there any way to force it to be recognized as a specific programming language?
    • What library does it use? I'd like to see which langs are supported.
    • Might it become possible to have it use our system/browser-preferred mono font?

    Anyway it looks great and is a much appreciated feature, thank you!



  • I don't know SP or how its shortcuts work, but did you check if you already have those shortcuts assigned in plasma's global shortcuts? The easiest way is to assign them to any plasma global shortcut and see if it tells you there's a conflict.

    If that's not it, can you trigger those SP actions with an external command? Then you could do it through plasma global shortcuts.




  • Factor on github (with comments and imports):

    ! hand: "A23A4"
    ! card: 'Q'
    ! hand-bid: { "A23A4" 220 }
    
    : 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 = ] } && ;
    : three-kind? ( hand -- ? ) sorted-histogram { [ last last 3 = ] [ length 3 = ] } && ;
    : two-pair?   ( hand -- ? ) sorted-histogram { [ last last 2 = ] [ length 3 = ] } && ;
    : one-pair?   ( hand -- ? ) sorted-histogram { [ last last 2 = ] [ length 4 = ] } && ;
    : 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 -- <=> )
      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 -- <=> ) [ 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 -- <=> ) [ 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 .
    ;
    

  • Factor on github (with comments and imports):

    : line>cards ( line -- winning-nums player-nums )
      ":|" split rest
      [
        [ CHAR: space = ] trim
        split-words harvest [ string>number ] map
      ] map first2
    ;
    
    : points ( winning-nums player-nums -- n )
      intersect length
      dup 0 > [ 1 - 2^ ] when
    ;
    
    : part1 ( -- )
      "vocab:aoc-2023/day04/input.txt" utf8 file-lines
      [ line>cards points ] map-sum .
    ;
    
    : follow-card ( i commons -- n )
      [ 1 ] 2dip
      2dup nth swapd
      over + (a..b]
      [ over follow-card ] map-sum
      nip +
    ;
    
    : part2 ( -- )
      "vocab:aoc-2023/day04/input.txt" utf8 file-lines
      [ line>cards intersect length ] map
      dup length  swap '[ _ follow-card ]
      map-sum .
    ;
    

  • Factor on github (with comments and imports):

    : symbol-indices ( line -- seq )
      [ ".0123456789" member? not ] find-all [ first ] map
    ;
    
    : num-spans ( line -- seq )
      >array [ over digit? [ nip ] [ 2drop f ] if ] map-index
      { f } split harvest
      [ [ first ] [ last ] bi 2array ] map
    ;
    
    : adjacent? ( num-span symbol-indices -- ? )
      swap [ first 1 - ] [ last 1 + ] bi [a,b]
      '[ _ interval-contains? ] any?
    ;
    
    : part-numbers ( line nearby-symbol-indices -- seq )
      [ dup num-spans ] dip
      '[ _ adjacent? ] filter
      swap '[ first2 1 + _ subseq string>number ] map
    ;
    
    : part1 ( -- )
      "vocab:aoc-2023/day03/input.txt" utf8 file-lines
      [ [ symbol-indices ] map ] keep
      [
        pick swap [ 1 - ?nth-of ] [ nth-of ] [ 1 + ?nth-of ] 2tri
        3append part-numbers sum
      ] map-index sum nip .
    ;
    
    : star-indices ( line -- seq )
      [ CHAR: * = ] find-all [ first ] map
    ;
    
    : gears ( line prev-line next-line -- seq-of-pairs )
      pick star-indices
      [ 1array '[ _ part-numbers ] [ 3dup ] dip tri@ 3append ]
      [ length 2 = ] map-filter [ 3drop ] dip
    ;
    
    : part2 ( -- )
      "vocab:aoc-2023/day03/input.txt" utf8 file-lines
      dup [
        pick swap [ 1 - ?nth-of ] [ 1 + ?nth-of ] 2bi
        gears [ product ] map-sum
      ] map-index sum nip .
    ;
    

  • Factor on github (with comments and imports):

    : known-color ( color-phrases regexp -- n )
      all-matching-subseqs [ 0 ] [
        [ split-words first string>number ] map-supremum
      ] if-empty
    ;
    
    : line>known-rgb ( str -- game-id known-rgb )
      ": " split1 [ split-words last string>number ] dip
      R/ \d+ red/ R/ \d+ green/ R/ \d+ blue/
      [ known-color ] tri-curry@ tri 3array
    ;
    
    : possible? ( known-rgb test-rgb -- ? )
      v<= [ ] all?
    ;
    
    : part1 ( -- )
      "vocab:aoc-2023/day02/input.txt" utf8 file-lines
      [ line>known-rgb 2array ]
      [ last { 12 13 14 } possible? ] map-filter
      [ first ] map-sum .
    ;
    
    : part2 ( -- )
      "vocab:aoc-2023/day02/input.txt" utf8 file-lines
      [ line>known-rgb nip product ] map-sum .
    ;
    

  • I feel ok about part 1, and just terrible about part 2.

    day01.factor on github (with comments and imports):

    : part1 ( -- )
      "vocab:aoc-2023/day01/input.txt" utf8 file-lines
      [
        [ [ digit? ] find nip ]
        [ [ digit? ] find-last nip ] bi
        2array string>number
      ] map-sum .
    ;
    
    MEMO: digit-words ( -- name-char-assoc )
      [ "123456789" [ dup char>name "-" split1 nip ,, ] each ] H{ } make
    ;
    
    : first-digit-char ( str -- num-char/f i/f )
      [ digit? ] find swap
    ;
    
    : last-digit-char ( str -- num-char/f i/f )
      [ digit? ] find-last swap
    ;
    
    : first-digit-word ( str -- num-char/f )
      [
        digit-words keys [
          2dup subseq-index
          dup [
            [ digit-words at ] dip
            ,,
          ] [ 2drop ] if
        ] each drop                           !
      ] H{ } make
      [ f ] [
        sort-keys first last
      ] if-assoc-empty
    ;
    
    : last-digit-word ( str -- num-char/f )
      reverse
      [
        digit-words keys [
          reverse
          2dup subseq-index
          dup [
            [ reverse digit-words at ] dip
            ,,
          ] [ 2drop ] if
        ] each drop                           !
      ] H{ } make
      [ f ] [
        sort-keys first last
      ] if-assoc-empty
    ;
    
    : first-digit ( str -- num-char )
      dup first-digit-char dup [
        pick 2dup swap head nip
        first-digit-word dup [
          [ 2drop ] dip
        ] [ 2drop ] if
        nip
      ] [
        2drop first-digit-word
      ] if
    ;
    
    : last-digit ( str -- num-char )
      dup last-digit-char dup [
        pick 2dup swap 1 + tail nip
        last-digit-word dup [
          [ 2drop ] dip
        ] [ 2drop ] if
        nip
      ] [
        2drop last-digit-word
      ] if
    ;
    
    : part2 ( -- )
      "vocab:aoc-2023/day01/input.txt" utf8 file-lines
      [ [ first-digit ] [ last-digit ] bi 2array string>number ] map-sum .
    ;
    









  • I would have liked to see a nice clear example at the top to help me decide if I really want to read about the language.


    EDIT: First sample:

    day := 1;
    import "advent-prelude.noul";
    
    puzzle_input := advent_input();
    
    submit! 1, puzzle_input split "\n\n" map ints map sum then max;
    submit! 2, puzzle_input split "\n\n" map ints map sum then sort then (_[-3:]) then sum;
    

    Looks not too different from what you might do in Factor:

    : totals ( -- seq )
      puzzle-input "\n\n" split-subseq [ split-lines [ string>number ] map-sum ] map
    ;
    
    : part1 ( -- ) totals supremum . ;
    
    : part2 ( -- ) totals sort 3 tail* sum . ;