8000 Optimize 2018 day 7 part 2 by making time steps of longer than 1 if p… · sim642/adventofcode@b5cd98f · GitHub
[go: up one dir, main page]

Skip to content

Commit b5cd98f

Browse files
committed
Optimize 2018 day 7 part 2 by making time steps of longer than 1 if possible
1 parent 3e4e4e1 commit b5cd98f

File tree

1 file changed

+11
-9
lines changed
  • src/main/scala/eu/sim642/adventofcode2018

1 file changed

+11
-9
lines changed

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@ object Day7 {
2424
}
2525

2626
object Work {
27-
def apply(step: Step): Work = Work(step, baseStepTime + (step - 'A' + 1) - 1)
27+
def apply(step: Step): Work = Work(step, baseStepTime + (step - 'A' + 1))
2828
}
2929

3030
def tickTime(reqs: Requirements, works: Set[Work]) = {
31-
val (endWorks, tickWorks) = works.partition(_.timeLeft == 0)
31+
val tick = works.map(_.timeLeft).min // tick until first work finishes, nothing interesting happens in between anyway
32+
val tickedWorks = works.map(_.ticked(tick))
33+
34+
val (endWorks, restWorks) = tickedWorks.partition(_.timeLeft == 0)
3235
val endSteps = endWorks.map(_.step)
3336
val restReqs = reqs -- endSteps
34-
val tickedWorks = tickWorks.map(_.ticked(1))
35-
(restReqs, tickedWorks)
37+
38+
(restReqs, restWorks, tick)
3639
}
3740

3841
def pickNewWorks(reqs: Requirements, works: Set[Work]): Set[Work] = {
@@ -48,13 +51,12 @@ object Day7 {
4851
}
4952

5053
def helper(reqs: Requirements, works: Set[Work]): Int = {
51-
val (reqs2, works2) = tickTime(reqs, works)
52-
val works3 = works2 ++ pickNewWorks(reqs2, works2)
53-
54-
if (reqs2.isEmpty && works3.isEmpty)
54+
if (reqs.isEmpty && works.isEmpty)
5555
return 0
5656

57-
1 + helper(reqs2, works3)
57+
val works2 = works ++ pickNewWorks(reqs, works)
58+
val (reqs2, works3, tick) = tickTime(reqs, works2)
59+
tick + helper(reqs2, works3)
5860
}
5961

6062
helper(reqs, Set.empty)

0 commit comments

Comments
 (0)
0