• Victor@lemmy.world
    link
    fedilink
    arrow-up
    13
    ·
    1 day ago

    Doing this year in OCaml again. It’s so freaking great. It’s like the language is built for this kind of linear data processing. Loving it.

      • Victor@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        21 hours ago

        I’ve seen some J code, yeah, it’s pretty freaking terse indeed!

        One drawback with extreme terseness I suppose is that if I come back to a program 6 months later, I’m probably not going to understand much at a glance, so to speak. 😅

        I’d be excited to see your solutions for this year though!

        • brennesel@discuss.tchncs.de
          link
          fedilink
          arrow-up
          1
          ·
          edit-2
          10 hours ago

          That’s true. The more concise the solution, the harder it is to understand and remember. Even if you came up with it yourself. That’s why I’ve included explanations for my solutions below.

          On Day 1, a list of numbers had to be sorted by how frequently each number appeared. The three most frequent numbers should then be multiplied together.

          In awk, this is quite simple:
          awk '{s[$1]++}END{for(i in s)print s[i],i}' in | sort -nr | awk -vp=1 'NR<4{p*=$2}END{print p}'
          (in is the file containing the numerical values)

          • Create an array with the number of values
          • Print the number of values and the values themselves
          • Sort in descending order by frequency
          • Filter the first 3 values
          • Multiply them together

          In J, it’s much easier if you pass the values as a list:
          */3{.(~.\:#/.~) in

          • #/.~ displays the frequency of the numbers
          • ~. displays a list of the different values
          • \: sorts the values in the list on the right in descending order and displays the values from the list on the left
          • 3{. takes the first 3 elements of the list
          • */ multiplies all elements of the list

          Part 2 of Day 1 is trivial, since you just need to reverse the sorting order.
          So use sort -n instead of sort -nr for the awk solution, and /: instead of \: for the J solution.

          Day 2 is already a tiny bit more interesting. The task was to calculate the distance for a defined sequence of colors.

          Here’s how I solved it in awk:
          echo "groovyyoyo" | fold -w1 | awk -vl=1 '{i=index("roygbv",$1);s+=(i-l+5)%6+1;l=i}END{print s}'

          • For line-by-line processing in awk, the string is split into lines using fold -w1
          • Determine the index of the current character
          • Subtract the distance from the last character (l starts at 1)
          • Modulo 6 accounts for the fact that the sequence starts with r again after v; adding 5 prevents negative values
          • Add 1 at the end so that the values range from 1 to 6 instead of 0 to 5
          • All values are added up continuously, and the sum is printed at the end

          In J, the same algorithm can be used and looks like this:
          +/1+6|5+(}.-}:)0,'roygbv'i. in

          • 'roygbv' i. determines the index (starting at 0)
          • 0, adds the initial value 0 to the front
          • (}.-}:) is the difference between two consecutive list elements
          • 5+ adds 5 to each element
          • 6| calculates modulo 6 for each element
          • 1+ adds 1 to each element
          • +/ sums all elements

          Part 2 has the greatest potential for optimization so far. The goal is to get the greatest possible distance.

          First, I counted how often each letter appeared and subtracted the distance only at the transitions between colors:
          echo "roygbv" | fold -w1 | awk -vl=1 '{x[index("rvbgyo",$1)]++}END{for(i in x){s+=(6*x[i]-i+l);l=i}print s}'

          • In the array, I counted how often each letter appears
          • One trick is that I get the distance directly by reversing the list of color values and using the index
          • Then I take advantage of the fact that the for loop outputs the indices in ascending order, so that I can start with r for the longest distance and then work through the colors from the end
          • For each color, I calculate the number count * 6 minus the distance to the previous color to determine the color transitions
          • Finally, print the sum

          Then I realized that, in the end, it doesn’t matter at all how many different colors there are. The only thing that matters is which one is farthest from r. So you just need to subtract that maximum distance, and otherwise you can always assume a distance of 6 for each element.

          That makes it even easier:
          echo "groovyyoyo" | fold -w1 | awk '{i=index("rvbgyo",$1)-1;m=i>m?i:m}END{print 6*NR-m}'

          • m=i>m?i:m determines the maximum index (starting at 0)
          • 6 * NR - m calculates 6 times the number of numbers in the list minus the maximum distance from r

          The same procedure is even shorter in J:
          ((6*#)-[:>./'rvbgyo'&i.) in
          • >./ determines the maximum index
          • [: in J ensures that the function on the left-hand side receives the same input, i.e. the number list
          • 6*# determines the number of elements in the list and multiplies it by 6
          • Finally, the difference between the two values is calculated

          I can hardly imagine a shorter solution.

          But now I’m curious how your solutions look like in OCaml.