Skip to content

Commit 6cd55d7

Browse files
committed
revert: reasoable day 7
1 parent b07fc58 commit 6cd55d7

File tree

1 file changed

+20
-60
lines changed
  • src/main/kotlin/dev/mtib/aoc/aoc24/days

1 file changed

+20
-60
lines changed

src/main/kotlin/dev/mtib/aoc/aoc24/days/Day7.kt

Lines changed: 20 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,99 +6,59 @@ import kotlinx.coroutines.*
66
import kotlinx.coroutines.channels.Channel
77
import kotlinx.coroutines.channels.SendChannel
88
import kotlinx.coroutines.selects.select
9+
import java.math.BigDecimal
910
import java.math.BigInteger
11+
import kotlin.math.log10
1012

1113
object Day7: AocDay(2024, 7) {
12-
13-
private suspend fun bruteForce(goal: BigInteger, parts: List<BigInteger>, current: BigInteger? = null, withPart2: Boolean = false, scope: CoroutineScope, channel: SendChannel<Unit>? = null): Boolean {
14+
private suspend fun bruteForce(goal: BigInteger, parts: List<BigInteger>, current: BigInteger? = null, withPart2: Boolean = false): Boolean {
1415
if (current == null) {
15-
val channel = Channel<Unit>(Channel.BUFFERED)
16-
val work = scope.launch {
17-
bruteForce(
18-
goal = goal,
19-
parts = parts.subList(1, parts.size),
20-
current = parts[0],
21-
withPart2 = withPart2,
22-
scope = scope,
23-
channel = channel
24-
)
25-
}
26-
return select<Boolean> {
27-
channel.onReceive {
28-
work.cancel()
29-
true
30-
}
31-
work.onJoin {
32-
false
33-
}
34-
}
16+
return bruteForce(
17+
goal = goal,
18+
parts = parts.subList(1, parts.size),
19+
current = parts[0],
20+
withPart2 = withPart2,
21+
)
3522
}
3623

3724
if (current > goal) {
3825
return false
3926
}
4027

4128
if (parts.isEmpty()) {
42-
if (current == goal) {
43-
channel?.send(Unit)
44-
}
4529
return current == goal
4630
}
4731

48-
val jobs = buildSet<Deferred<Boolean>> {
49-
add(scope.async { bruteForce(goal, parts.subList(1, parts.size), current + parts[0], withPart2 = withPart2, scope = scope, channel = channel)})
50-
add(scope.async { bruteForce(goal, parts.subList(1, parts.size), current * parts[0], withPart2 = withPart2, scope = scope, channel = channel)})
51-
if (withPart2){
52-
add(scope.async { bruteForce(goal, parts.subList(1, parts.size), (current.toString() + parts[0].toString()).toBigInteger(), withPart2 = true, scope = scope, channel = channel)})
53-
}
54-
}.toMutableSet()
55-
56-
while (jobs.isNotEmpty()) {
57-
select {
58-
jobs.forEach { job ->
59-
job.onAwait { result ->
60-
jobs.remove(job)
61-
if (result) {
62-
channel?.send(Unit)
63-
}
64-
result
65-
}
66-
}
67-
}.let {
68-
if (it) {
69-
jobs.forEach { it.cancel() }
70-
return true
71-
}
72-
}
73-
}
74-
75-
return false
32+
return bruteForce(goal, parts.subList(1, parts.size), current * parts[0], withPart2 = withPart2) ||
33+
(withPart2 && bruteForce(goal, parts.subList(1, parts.size), current * BigInteger.TEN.pow(log10(parts[0].toDouble()).toInt() + 1) + parts[0], withPart2 = true)) ||
34+
bruteForce(goal, parts.subList(1, parts.size), current + parts[0], withPart2 = withPart2)
7635
}
7736

78-
private val parseRegex = Regex("""^(\d+):(?: (\d+))*""")
79-
override suspend fun part1(): Any = coroutineScope {
80-
inputLinesList.chunkedParMap(inputLinesList.size / cpu) {lines ->
37+
override suspend fun part1(): Any {
38+
val matching = inputLinesList.chunkedParMap(inputLinesList.size / cpu) {lines ->
8139
lines.map {
8240
val colon = it.indexOf(':')
8341
val goal = it.substring(0, colon).toBigInteger()
8442
val parts = it.substring(colon + 2).split(" ").map { it.toBigInteger() }
8543
goal to parts
86-
}.filter { bruteForce(it.first, it.second, scope = this@coroutineScope) }
44+
}.filter { bruteForce(it.first, it.second) }
8745
.map { it.first }
8846
.fold (BigInteger.ZERO) { acc, i -> acc + i }
8947
}.fold(BigInteger.ZERO) { acc, i -> acc + i }
48+
return matching
9049
}
9150

92-
override suspend fun part2() = coroutineScope {
93-
inputLinesList.chunkedParMap(inputLinesList.size / cpu) {lines ->
51+
override suspend fun part2(): Any {
52+
val matching = inputLinesList.chunkedParMap(inputLinesList.size / cpu) {lines ->
9453
lines.map {
9554
val colon = it.indexOf(':')
9655
val goal = it.substring(0, colon).toBigInteger()
9756
val parts = it.substring(colon + 2).split(" ").map { it.toBigInteger() }
9857
goal to parts
99-
}.filter { bruteForce(it.first, it.second, withPart2 = true, scope = this@coroutineScope) }
58+
}.filter { bruteForce(it.first, it.second, withPart2 = true) }
10059
.map { it.first }
10160
.fold (BigInteger.ZERO) { acc, i -> acc + i }
10261
}.fold(BigInteger.ZERO) { acc, i -> acc + i }
62+
return matching
10363
}
10464
}

0 commit comments

Comments
 (0)