This commit is contained in:
Alexander Gehrke 2021-12-07 16:03:54 +01:00
parent 38e907ddf6
commit 8ea208920a
3 changed files with 13 additions and 1 deletions

View file

@ -2,5 +2,15 @@ package de.qwertyuiop.aoc.`2021`
import de.qwertyuiop.aoc.lib.*
import cats.*, cats.implicits.given
import util.chaining.given
def day7(using InputSource): Unit = ???
def day7(using InputSource): Unit =
val positions = input(_.splitNN(",").map(_.toInt).toVector).head
def minCost(positions: Iterable[Int])(cost: (Int, Int) => Int) =
(positions.min to positions.max)
.map(target => (target, positions.map(start => cost(start, target)).sum))
.minBy(_._2)
minCost(positions)((s, t) => (s - t).abs).pipe(println)
minCost(positions)((s, t) => { val diff = (s - t).abs; diff * (diff + 1) / 2 }).pipe(println)