Advent of Code 2024 - Day 1 - Historian Hysteria

Поділитися
Вставка
  • Опубліковано 3 гру 2024

КОМЕНТАРІ • 4

  • @willzin-da-esfiha
    @willzin-da-esfiha 3 дні тому +2

    My first solution was pretty much like yours. The difference was that I used the abs function to not use if/else structure to sum the value.
    In the second solution I also removed the sort instructions but instead of looping for each number I've used a map to count appearances for each left list value in the right list.
    By the way, nice video!

  • @Max-rz8br
    @Max-rz8br 3 дні тому +1

    My solution was pretty similar. I just did two things differently:
    At Part 1 (6:41) you checked which of the two values in each list was the highest and then subtracted the highest value by the lowest. I personally subtracted the left value by the right value every time, and then I took the absolute value of that, in order to always get a positive number. It does save some lines of code, but your solution might be easier to understand, so I don't think there's an objectively best way.
    At Part 2 (9:49) I didn't have a 'count' variable for every loop. I did have the same double for loop approach, but whenever two values were the same, I just added that value directly to the 'res' variable. Again, I don't think it's objectively better, but my brain likes smaller code blocks.

  • @WayneBagguley
    @WayneBagguley 3 дні тому +1

    Given the two sorted lists, first and second, part 1 in Kotlin is:
    first.zip(second) { a, b -> abs(a - b) }.sum()
    Given the two unsorted lists, first and second, part 2 in Kotlin is:
    first.sumOf { f -> second.count { s -> f == s } * f }

    • @aaronperl
      @aaronperl 3 дні тому +1

      Ah, I was too tired this morning to check the documentation, but I had a feeling there had to be a zip() function. Otherwise this is very similar to my solutions.