-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package day13 | ||
|
||
import scala.io.Source.fromFile | ||
import scala.util.chaining.* | ||
|
||
type Score = Map[String, Map[String, Int]] | ||
|
||
def parse(input: String) = input match | ||
case s"$from would gain $n happiness units by sitting next to $to." => | ||
from -> (to -> n.toInt) | ||
case s"$from would lose $n happiness units by sitting next to $to." => | ||
from -> (to -> -n.toInt) | ||
|
||
def parseToMap(xs: Iterable[String]) = xs | ||
.map(parse) | ||
.toSeq | ||
.groupBy(_._1) | ||
.view | ||
.mapValues(_.map(_._2).toMap) | ||
.toMap | ||
|
||
extension [A](xs: Seq[A]) | ||
def slidingCyclic(size: Int) = | ||
xs.concat(xs).sliding(size).take(xs.size) | ||
|
||
def permutationsCyclic = | ||
xs.drop(1).permutations.map(xs.head +: _) | ||
|
||
def score(comb: Seq[String])(implicit scores: Score) = | ||
comb | ||
.slidingCyclic(3) | ||
.collect { case Seq(left, center, right) => | ||
val f = scores.getOrElse(center, Map.empty) | ||
f.getOrElse(left, 0) + f.getOrElse(right, 0) | ||
} | ||
.sum | ||
|
||
def part1(scores: Score) = | ||
val people = scores.keys.toSeq | ||
val combination = people.permutationsCyclic.toSeq | ||
|
||
given Score = scores | ||
combination.map(score).max | ||
|
||
def part2(scores: Score) = | ||
val people = scores.keys.toSeq :+ "Me" | ||
val combination = people.permutationsCyclic.toSeq | ||
|
||
given Score = scores | ||
combination.map(score).max | ||
|
||
@main def main() = | ||
val scores: Score = fromFile(".cache/13.txt").getLines.toSeq.pipe(parseToMap) | ||
|
||
println(part1(scores)) | ||
println(part2(scores)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import scala.util.chaining.* | ||
import munit.FunSuite | ||
import day13.* | ||
|
||
class Day13Tests extends FunSuite: | ||
test("Part 1"): | ||
val scores = """ | ||
Alice would gain 54 happiness units by sitting next to Bob. | ||
Alice would lose 79 happiness units by sitting next to Carol. | ||
Alice would lose 2 happiness units by sitting next to David. | ||
Bob would gain 83 happiness units by sitting next to Alice. | ||
Bob would lose 7 happiness units by sitting next to Carol. | ||
Bob would lose 63 happiness units by sitting next to David. | ||
Carol would lose 62 happiness units by sitting next to Alice. | ||
Carol would gain 60 happiness units by sitting next to Bob. | ||
Carol would gain 55 happiness units by sitting next to David. | ||
David would gain 46 happiness units by sitting next to Alice. | ||
David would lose 7 happiness units by sitting next to Bob. | ||
David would gain 41 happiness units by sitting next to Carol. | ||
""".trim.split("\n").map(_.trim).toSeq.pipe(parseToMap) | ||
|
||
assertEquals(part1(scores), 330) |