Skip to content

Commit

Permalink
⚡ optimize day 4 slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
twentylemon committed Dec 6, 2023
1 parent 83172b0 commit c8bdffc
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/main/scala/org/lemon/advent/year2023/Day04.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.lemon.advent.year2023
private object Day04:

case class Card(id: Int, winning: Seq[Int], pulls: Seq[Int]):
lazy val matches = winning intersect pulls
val matches = (winning intersect pulls).length

def parseCard(line: String) =
def asIntSeq(nums: String) = nums.split("\\s+").map(_.trim).filter(_.nonEmpty).map(_.toInt)
Expand All @@ -14,16 +14,16 @@ private object Day04:
.map(parseCard)

def score(card: Card) =
if card.matches.isEmpty then 0 else math.pow(2, card.matches.length - 1).toInt
if card.matches == 0 then 0 else math.pow(2, card.matches - 1).toInt

def part1(input: String) = parse(input)
.map(score)
.sum

def winMe(pool: Iterable[Card], draws: Iterable[Card], depth: Int = 0): Int =
def copyWinnings(from: Card) = pool
.filter(_.id > from.id)
.filter(_.id <= from.id + from.matches.length)
.drop(from.id)
.take(from.matches)

val winnings = draws.flatMap(copyWinnings)
if winnings.isEmpty then 0 else winnings.size + winMe(pool, winnings, depth + 1)
Expand Down

0 comments on commit c8bdffc

Please sign in to comment.