How was day one for everyone? I was surprised at how tricky it was to get the right answer for part two. Not the usual easy start I’ve seen in the past. I’d be interested to see any solutions here.

  • mykl@lemmy.worldOPM
    link
    fedilink
    arrow-up
    4
    ·
    edit-2
    11 months ago

    Dart solution

    Here’s my solution to start the ball rolling. In the end I incorporated my part 1 answer into the part 2 logic, and golfed the code quite a lot.

    import 'package:collection/collection.dart';
    
    var ds = '0123456789'.split('');
    var wds = 'one two three four five six seven eight nine'.split(' ');
    
    int s2d(String s) => s.length == 1 ? int.parse(s) : wds.indexOf(s) + 1;
    
    int value(String s, List digits) {
      var firsts = {for (var e in digits) s.indexOf(e): e}..remove(-1);
      var lasts = {for (var e in digits) s.lastIndexOf(e): e}..remove(-1);
      return s2d(firsts[firsts.keys.min]) * 10 + s2d(lasts[lasts.keys.max]);
    }
    
    part1(List lines) => lines.map((e) => value(e, ds)).sum;
    
    part2(List lines) => lines.map((e) => value(e, ds + wds)).sum;
    
  • hal9001@lemmy.world
    link
    fedilink
    arrow-up
    3
    ·
    11 months ago

    My clojure solution

    Part 2 was unexpected. Was not expecting that level of wrinkle on day 1 for sure.

    I saw a few things that were guessing we might have a lot more of these sorts of twists to help throw off the AI solutions 🤷‍♂️

  • Andy@programming.dev
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    11 months ago

    I’m not really happy with the repetition I have, but here’s my Factor solution:

    Here it is 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 .
    ;
    
  • Rooki@lemmy.world
    link
    fedilink
    English
    arrow-up
    1
    ·
    11 months ago

    Really cool idea! Just bad that it increases the submit time every failed attempt. I have to wait now 10 minutes on the 2nd question. + In what order?

    For example: oneight CAN be 1ight or on8

    Is it sorted by 1-9 or one - nine or first in word first served?

    I think this is my currently issue, and i cant go through and sadly my patience of submitting every try every 10 mins has ended and i just lost it.

    The delay is just so unnecessary…

    • mykl@lemmy.worldOPM
      link
      fedilink
      arrow-up
      4
      ·
      edit-2
      11 months ago

      It’s normally not so tricky on the first day, and the examples also normally do a much better job of guiding you away from wrong approaches so the increasing delay is intended to ensure you aren’t just spamming poorly tested answers.

      The task in part two could be better written as “find the first occurrence of a digit in the string (either as a digit or as a word), and then separately find the last occurrence of a digit (either as a digit or as a word again) then compose these two digits”. Thinking of it like that should help you identify where your problem lies.