-
Notifications
You must be signed in to change notification settings - Fork 0
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
7 changed files
with
212 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package y2015.day06 | ||
|
||
import utils.Point | ||
import utils.getInputFile | ||
import utils.pointsInArea | ||
|
||
fun main() { | ||
println("Part one: " + Day06.solvePartOne()) | ||
println("Part two: " + Day06.solvePartTwo()) | ||
} | ||
|
||
object Day06 { | ||
|
||
private val regex = """^(turn on|turn off|toggle) (\d+),(\d+) through (\d+),(\d+)$""".toRegex() | ||
private val input = getInputFile(this::class.java.packageName) | ||
.readLines() | ||
.map { | ||
val (operation, aX, aY, bX, bY) = regex.find(it)!!.destructured | ||
Triple(Operation.fromValue(operation), Point(aX.toInt(), aY.toInt()), Point(bX.toInt(), bY.toInt())) | ||
} | ||
|
||
fun solvePartOne(): Int { | ||
val lights = mutableMapOf<Point, Boolean>() | ||
|
||
input.forEach { (operation, tl, br) -> | ||
pointsInArea(tl, br).forEach { point -> | ||
lights[point] = when (operation) { | ||
Operation.ON -> true | ||
Operation.OFF -> false | ||
Operation.TOGGLE -> !lights.getOrDefault(point, false) | ||
} | ||
} | ||
} | ||
|
||
return lights.values.count { it } | ||
} | ||
|
||
fun solvePartTwo(): Int { | ||
val lights = mutableMapOf<Point, Int>() | ||
|
||
input.forEach { (operation, tl, br) -> | ||
pointsInArea(tl, br).forEach { point -> | ||
lights[point] = (lights.getOrDefault(point, 0) + when (operation) { | ||
Operation.ON -> 1 | ||
Operation.OFF -> -1 | ||
Operation.TOGGLE -> 2 | ||
}).coerceAtLeast(0) | ||
} | ||
} | ||
|
||
return lights.values.sum() | ||
} | ||
|
||
enum class Operation { | ||
ON, OFF, TOGGLE; | ||
|
||
companion object { | ||
fun fromValue(value: String) = when (value) { | ||
"turn on" -> ON | ||
"turn off" -> OFF | ||
"toggle" -> TOGGLE | ||
else -> throw IllegalArgumentException("Unknown operation $value") | ||
} | ||
} | ||
} | ||
} |
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package y2015.day07 | ||
|
||
import utils.getInputFile | ||
|
||
fun main() { | ||
println("Part one: " + Day07.solvePartOne()) | ||
println("Part two: " + Day07.solvePartTwo()) | ||
} | ||
|
||
object Day07 { | ||
|
||
private val input: Map<String, Operation> = getInputFile(this::class.java.packageName, example = false) | ||
.readLines() | ||
.map { it.split(" -> ") } | ||
.associate { (ins, out) -> | ||
out to when { | ||
"AND" in ins -> { | ||
val (l, r) = ins.split(" AND ") | ||
Operation.And(l, r, out) | ||
} | ||
"OR" in ins -> { | ||
val (l, r) = ins.split(" OR ") | ||
Operation.Or(l, r, out) | ||
} | ||
"LSHIFT" in ins -> { | ||
val (l, r) = ins.split(" LSHIFT ") | ||
Operation.LShift(l, r.toInt(), out) | ||
} | ||
"RSHIFT" in ins -> { | ||
val (l, r) = ins.split(" RSHIFT ") | ||
Operation.RShift(l, r.toInt(), out) | ||
} | ||
"NOT" in ins -> { | ||
val x = ins.removePrefix("NOT ") | ||
Operation.Not(x, out) | ||
} | ||
else -> Operation.Set(ins, out) | ||
} | ||
} | ||
private val wires = mutableMapOf<String, Int>() | ||
|
||
fun solvePartOne(): Int = get("a") | ||
|
||
fun solvePartTwo(): Int { | ||
val p1 = solvePartOne() | ||
wires.clear() | ||
wires["b"] = p1 | ||
return get("a") | ||
} | ||
|
||
private fun get(k: String): Int { | ||
return k.toIntOrNull() ?: wires[k] ?: run { | ||
val v = when (val ins = input[k]!!) { | ||
is Operation.And -> get(ins.a) and get(ins.b) | ||
is Operation.Or -> get(ins.a) or get(ins.b) | ||
is Operation.LShift -> get(ins.a) shl ins.b | ||
is Operation.RShift -> get(ins.a) shr ins.b | ||
is Operation.Not -> 65536 + get(ins.a).inv() | ||
is Operation.Set -> get(ins.v) | ||
} | ||
wires[k] = v | ||
v | ||
} | ||
} | ||
|
||
sealed interface Operation { | ||
data class And( | ||
val a: String, | ||
val b: String, | ||
val out: String, | ||
) : Operation | ||
|
||
data class Or( | ||
val a: String, | ||
val b: String, | ||
val out: String, | ||
) : Operation | ||
|
||
data class LShift( | ||
val a: String, | ||
val b: Int, | ||
val out: String, | ||
) : Operation | ||
|
||
data class RShift( | ||
val a: String, | ||
val b: Int, | ||
val out: String, | ||
) : Operation | ||
|
||
data class Not( | ||
val a: String, | ||
val out: String, | ||
) : Operation | ||
|
||
data class Set( | ||
val v: String, | ||
val out: String, | ||
) : Operation | ||
} | ||
} |
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package y2015.day06 | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
|
||
internal class Day06Test { | ||
@Test | ||
fun solvePartOne() { | ||
assertEquals(377891, Day06.solvePartOne()) | ||
} | ||
|
||
@Test | ||
fun solvePartTwo() { | ||
assertEquals(14110788, Day06.solvePartTwo()) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package y2015.day07 | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
|
||
internal class Day07Test { | ||
@Test | ||
fun solvePartOne() { | ||
assertEquals(46065, Day07.solvePartOne()) | ||
} | ||
|
||
@Test | ||
fun solvePartTwo() { | ||
assertEquals(14134, Day07.solvePartTwo()) | ||
} | ||
} |