this post was submitted on 05 Dec 2023
34 points (100.0% liked)

Advent Of Code

770 readers
1 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2023

Solution Threads

M T W T F S S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
34
submitted 11 months ago* (last edited 11 months ago) by Ategon to c/advent_of_code
 

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


πŸ”’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)

you are viewing a single comment's thread
view the rest of the comments
[–] cvttsd2si 3 points 11 months ago

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