aoc2024

Advent of Code 2024
Log | Files | Refs | README

Day1.scala (718B)


      1 def day1_pt1(input: Iterator[String]): Int = 
      2   val (col1, col2) = parse_input(input)
      3   val result = col1.sorted.zip(col2.sorted).map((x, y) => Math.abs(x - y)).reduce(_ + _)
      4   return result
      5 
      6 def day1_pt2(input: Iterator[String]): Int = 
      7   val (col1, col2) = parse_input(input)
      8   val result = col1
      9     .map(v1 => col2.count(v2 => v1 == v2) * v1)
     10     .reduce(_ + _)
     11   return result
     12 
     13 def parse_input(input: Iterator[String]): (List[Int],List[Int]) = 
     14   return input
     15     .flatMap(line => 
     16       val ll = line.split(" ").mapInPlace(x => x.strip()).filter(b => b.length() > 0)
     17       if ll.length > 0 then 
     18         Some((Integer.parseInt(ll(0)), Integer.parseInt(ll(1))))
     19       else
     20         None
     21     )
     22     .toList
     23     .unzip