Skip to content

Commit

Permalink
Y2015 D6-7
Browse files Browse the repository at this point in the history
  • Loading branch information
hibob224 committed Dec 13, 2024
1 parent ade6e8d commit a24bc49
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/kotlin/utils/Point.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,16 @@ fun <T> Map<Point, T>.traverse(block: (Point, T?) -> Unit) {
}
}
}

fun pointsInArea(point1: Point, point2: Point): List<Point> {
val (minY, maxY) = listOf(point1.y, point2.y).sorted()
val (minX, maxX) = listOf(point1.x, point2.x).sorted()

return buildList {
(minY..maxY).forEach { y ->
(minX..maxX).forEach { x ->
add(Point(x, y))
}
}
}
}
66 changes: 66 additions & 0 deletions src/main/kotlin/y2015/day06/Day06.kt
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 added src/main/kotlin/y2015/day06/input.txt
Binary file not shown.
101 changes: 101 additions & 0 deletions src/main/kotlin/y2015/day07/Day07.kt
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 added src/main/kotlin/y2015/day07/input.txt
Binary file not shown.
16 changes: 16 additions & 0 deletions src/test/kotlin/y2015/day06/Day06Test.kt
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())
}
}
16 changes: 16 additions & 0 deletions src/test/kotlin/y2015/day07/Day07Test.kt
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())
}
}

0 comments on commit a24bc49

Please sign in to comment.