-
Notifications
You must be signed in to change notification settings - Fork 4
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
3 changed files
with
48 additions
and
2 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
17 changes: 16 additions & 1 deletion
17
2024/kotlin/src/main/kotlin/com/tymscar/day07/part1/part1.kt
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 |
---|---|---|
@@ -1,5 +1,20 @@ | ||
package com.tymscar.day07.part1 | ||
|
||
private fun isValid(solution: Long, equation: List<Long>): Boolean { | ||
if (equation.isEmpty()) return solution == 0L | ||
val sumSolution = isValid(solution - equation.first(), equation.drop(1)) | ||
val multSolution = | ||
if (solution % equation.first() == 0L) isValid(solution / equation.first(), equation.drop(1)) else false | ||
return sumSolution || multSolution | ||
} | ||
|
||
fun solve(input: String): String { | ||
return input | ||
var equations = input.lines().map { | ||
Regex("""(\d+)""").findAll(it).map { it.value.toLong() }.toList() | ||
} | ||
|
||
return equations | ||
.filter { isValid(it.first(), it.drop(1).reversed()) } | ||
.sumOf { it.first() } | ||
.toString() | ||
} |
31 changes: 30 additions & 1 deletion
31
2024/kotlin/src/main/kotlin/com/tymscar/day07/part2/part2.kt
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 |
---|---|---|
@@ -1,5 +1,34 @@ | ||
package com.tymscar.day07.part2 | ||
|
||
import kotlin.math.floor | ||
import kotlin.math.log10 | ||
import kotlin.math.pow | ||
|
||
private fun deconcat(log: Long, splinter: Long): Long? { | ||
val splinterSizeInHundreds = 10.0.pow(floor(log10(splinter.toDouble())) + 1).toLong() | ||
val afterDeconcat: Long = log / splinterSizeInHundreds | ||
return if ((afterDeconcat * splinterSizeInHundreds + splinter) == log) afterDeconcat else null | ||
} | ||
|
||
private fun isValid(solution: Long?, equation: List<Long>): Boolean { | ||
if (solution == null) return false | ||
if (equation.isEmpty()) return solution == 0L | ||
val remainingEq = equation.drop(1) | ||
val sumSolution = isValid(solution - equation.first(), remainingEq) | ||
val multSolution = | ||
if (solution % equation.first() == 0L) isValid(solution / equation.first(), remainingEq) else false | ||
val concatSolution = | ||
isValid(deconcat(solution, equation.first()), remainingEq) | ||
return sumSolution || multSolution || concatSolution | ||
} | ||
|
||
fun solve(input: String): String { | ||
return input | ||
var equations = input.lines().map { | ||
Regex("""(\d+)""").findAll(it).map { it.value.toLong() }.toList() | ||
} | ||
|
||
return equations | ||
.filter { isValid(it.first(), it.drop(1).reversed()) } | ||
.sumOf { it.first() } | ||
.toString() | ||
} |