Day 1: Dusty Shed

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

  • janAkali@programming.dev
    link
    fedilink
    arrow-up
    5
    ·
    2 days ago

    Nim

    Probably one of the easiest first days in AOC history.

    Solved with HashTable -> sorting -> sequence
    Runtime: 280 μs

    proc solve(input: string): AOCSolution[int, int] =
      var cnt: CountTable[int]
      for line in input.splitLines():
        cnt.inc parseInt line
      cnt.sort()
      let s = cnt.keys.toseq()
      result.part1 = s[0] * s[1] * s[2]
      result.part2 = s[^1] * s[^2] * s[^3]
    

    Full solution at Codeberg: solution.nim

    • CameronDev@programming.devOPM
      link
      fedilink
      arrow-up
      5
      ·
      2 days ago

      I thought you were exagerating about the easiness, because normally day 1 is easy, but being able to change a single character to solve pt2 was unusually easy. No real twist at all.

      • addie@feddit.uk
        link
        fedilink
        arrow-up
        5
        ·
        2 days ago

        My favourite AoC challenges are the ones where part 1 is relatively straightforward, but part 2 requires some algorithmic insight or forces you to learn something new in order to complete it in a reasonable amount of time.

        I think all the people in this thread who have responded so far will be coding enthusiasts, and this is just a warm-up. Yes, you can solve it by resorting the list in the opposite direction. Or you can be a bit more efficient and reuse the same list, which requires you to do reverse iteration and risk off-by-one errors, which we all had to learn once.

        Back when it was ‘four weeks of AoC’, I used to mentally divide up the weeks into:

        1. Iteration solutions
        2. Recursion
        3. Dynamic programming
        4. Crazy shit

        … and looking at the stats, the drop-off in completion rate for the first problem that needed recursion was quite stark. So yeah, let’s start off with a couple of puzzles for everyone.

        • CameronDev@programming.devOPM
          link
          fedilink
          arrow-up
          3
          ·
          2 days ago

          Definitely nice to have a warm up, but pt2 was just too easy IMO.

          I like the ones where pt 1 can be solved trivially, but in order to do pt2, the logic or approach needs to be completely flipped.

          Slapping memoisation on a trivial solution just feels a bit boring after a while, even if it is cool

    • addie@feddit.uk
      link
      fedilink
      arrow-up
      4
      ·
      2 days ago

      C++

      I’d have to check back through them, but they tend to be pretty easy on the first day; let the people that are using Excel get a win. 12 days again this year, so they should ramp up pretty quick - I actually preferred that last year, keep it short and sweet, no filler.

      Solved by counting the entries in a hash map, then sorting it. 90 microseconds.

      auto parts(const std::vector<int>& input) {
        auto count = boost::unordered_flat_map<int, int>{1000};
        for (const auto& entry : input)
          ++count[entry];
        auto sort = std::vector<std::pair<int, int>>{count.begin(), count.end()};
        std::sort(sort.begin(), sort.end(), [](auto& a, auto& b) {
          return a.second > b.second;
        });
        auto part1 = sort[0].first * sort[1].first * sort[2].first;
        auto size = sort.size();
        auto part2 =
          sort[size - 1].first * sort[size - 2].first * sort[size - 3].first;
        return std::pair{part1, part2};
      }
      
        • addie@feddit.uk
          link
          fedilink
          arrow-up
          3
          ·
          2 days ago

          It’s about fifty times faster, since it doesn’t make the specification-required mistakes in the standard one. (Specifically, iteration in constant time. The Boost one is ‘always faster’, but not constant. Constant time requires maintaining a separate linked list of all entries, which wastes time and memory.)

          For this; no, makes no difference. The problem space is so small that the standard maps are quite fast enough. Just force it habit to never use them.