8000 Simplify 2018 day 7 part 2 tickTime · sim642/adventofcode@276dccb · GitHub
[go: up one dir, main page]

Skip to content

Commit 276dccb

Browse files
committed
Simplify 2018 day 7 part 2 tickTime
1 parent cff276d commit 276dccb

File tree

1 file changed

+8
-10
lines changed
  • src/main/scala/eu/sim642/adventofcode2018

1 file changed

+8
-10
lines changed

src/main/scala/eu/sim642/adventofcode2018/Day7.scala

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,16 @@ object Day7 {
1717

1818
def parallelTopologicalSort(reqs: Requirements, workerCount: Int = 5, baseStepTime: Int = 60): Int = {
1919

20-
case class Work(step: Step, timeLeft: Int)
20+
case class Work(step: Step, timeLeft: Int) {
21+
def ticked(time: Int): Work = copy(timeLeft = timeLeft - time)
22+
}
2123

2224
def tickTime(reqs: Requirements, works: Set[Work]) = {
23-
works.foldLeft((reqs, Set.empty[Work]))({
24-
case ((reqs, works), Work(step, timeLeft)) =>
25-
if (timeLeft == 0) {
26-
val restReqs = reqs.filterKeys(_ != step)
27-
(restReqs, works)
28-
}
29-
else
30-
(reqs, works + Work(step, timeLeft - 1))
31-
})
25+
val (endWorks, tickWorks) = works.partition(_.timeLeft == 0)
26+
val endSteps = endWorks.map(_.step)
27+
val restReqs = reqs -- endSteps
28+
val tickedWorks = tickWorks.map(_.ticked(1))
29+
(restReqs, tickedWorks)
3230
}
3331

3432
def pickWork(reqs: Requirements, works: Set[Work]) = {

0 commit comments

Comments
 (0)
0