• 32 Posts
  • 17 Comments
Joined 1 year ago
cake
Cake day: June 12th, 2023

help-circle
  • mykl@lemmy.worldOPMtoAdvent of Code@lemmy.worldDay 1 solutions
    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.


  • mykl@lemmy.worldOPMtoAdvent of Code@lemmy.worldDay 1 solutions
    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;
    















  • Haha, I bit the bullet and installed Clojure… and Calva… and Leiningrad… and eventually got it to run locally. Looks like it’s quite slow via the REPL, but I get similar times to you when I run it with plain old "clojure -M”. That looks like a nice REPL though. I might pick through your code a bit more and see if I can improve my solution before Sunday!


  • Thanks, I’m not that familiar with Clojure, but I can read it okay. How fast did that run for you? I tried it in an online Clojure editor and it took more than 10 seconds for me, which from memory is comparable with my solution before I started hacking in some terrible shortcuts…



  • Haha, “Nadvent of Code” was just a joke description for my revisiting of last year’s challenges. It stands for “Not Advent of Code” or “November Advent of Code” depending on how the mood takes me. There’s a link to last year’s overview page on each post, and to the specific day on my more recent posts. Or you can just look at my solutions and figure out how you’d rewrite them in Rust. Have fun.


  • I first started going through my solutions from last year because I was intrigued by the interesting new language Uiua (pronounced wee-wuh) which is described as a “general purpose, stack-based, array-oriented programming language with a focus on simplicity, beauty, and tacit code.” (think APL meets FORTH!) But I soon realised that although I could solve some of the problems in Uiua, for others I don’t think a day will be long enough!

    So it will be back to Dart this year with the goal of having every solution runnable in DartPad in well under one second, and ideally without having to search for hints online. I made it to day 19 last year before I got stumped; you’ll see more about that on Sunday!