Day 5: If You Give a Seed a Fertilizer
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)
- Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
🔒This post will be unlocked when there is a decent amount of submissions on the leaderboard to avoid cheating for top spots
🔓 Unlocked after 27 mins (current record for time, hard one today)
Scala3
kind of convoluted, but purely functional
import scala.collection.immutable.NumericRange.Exclusive import math.max import math.min extension [A] (l: List[A]) def chunk(pred: A => Boolean): List[List[A]] = def go(l: List[A], partial_acc: List[A], acc: List[List[A]]): List[List[A]] = l match case (h :: t) if pred(h) => go(t, List(), partial_acc.reverse :: acc) case (h :: t) => go(t, h :: partial_acc, acc) case _ => partial_acc.reverse :: acc go(l, List(), List()).reverse type R = Exclusive[Long] def intersectTranslate(r: R, c: R, t: Long): R = (t + max(r.start, c.start) - c.start) until (t + min(r.end, c.end) - c.start) case class MappingEntry(from: R, to: Long) case class Mapping(entries: List[MappingEntry], produces: String): def resolveRange(in: R): List[R] = entries.map(e => intersectTranslate(in, e.from, e.to)).filter(!_.isEmpty) def completeEntries(a: List[MappingEntry]): List[MappingEntry] = a ++ ((0L until 0L) +: a.map(_.from).sorted :+ (Long.MaxValue until Long.MaxValue)).sliding(2).flatMap{ case List(a, b) => Some(MappingEntry(a.end until b.start, a.end)); case _ => None}.toList def parse(a: List[String], init: List[Long] => List[R]): (List[R], Map[String, Mapping]) = def parseEntry(s: String): MappingEntry = s match case s"$end $start $range" => MappingEntry(start.toLong until start.toLong + range.toLong, end.toLong) a.chunk(_ == "") match case List(s"seeds: $seeds") :: maps => init(seeds.split(raw"\s+").map(_.toLong).toList) -> (maps.flatMap{ case s"$from-to-$to map:" :: entries => Some(from -> Mapping(completeEntries(entries.map(parseEntry)), to)); case _ => None }).toMap case _ => (List(), Map()).ensuring(false) def singletons(a: List[Long]): List[R] = a.map(s => s until s + 1) def paired(a: List[Long]): List[R] = a.grouped(2).flatMap{ case List(x, y) => Some(x until x+y); case _ => None }.toList def chase(d: (List[R], Map[String, Mapping]), initial: String, target: String) = val (init, m) = d def go(a: List[R], s: String): List[R] = if trace(s) == target then a else val x = m(s) go(a.flatMap(x.resolveRange), x.produces) go(trace(init), initial) def task1(a: List[String]): Long = chase(parse(a, singletons), "seed", "location").min.start def task2(a: List[String]): Long = chase(parse(a, paired), "seed", "location").min.start