Skip to content

Commit ec6d11a

Browse files
committed
Solved day 18 part 1
1 parent 21f7515 commit ec6d11a

File tree

11 files changed

+188
-26
lines changed

11 files changed

+188
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ To run all solutions, simply run `./gradlew run`. If you want to run the solutio
3636
| 15 |||
3737
| 16 <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/74/Kotlin_Icon.png/600px-Kotlin_Icon.png" width="10" height="10" alt="Kotlin" /> |||
3838
| 17 |||
39-
| 18 |||
39+
| 18 <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/74/Kotlin_Icon.png/600px-Kotlin_Icon.png" width="10" height="10" alt="Kotlin" /> |||
4040
| 19 |||
4141
| 20 |||
4242
| 21 |||

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44

55
// project meta data
66
group 'de.havox_design.aoc2015'
7-
version '0.17.1'
7+
version '0.17.2'
88

99
// Switch to gradle "all" distribution.
1010
wrapper {

day18/build.gradle

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,52 @@
1+
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
2+
3+
plugins {
4+
id 'application'
5+
id 'org.jetbrains.kotlin.jvm' version '1.9.21'
6+
}
7+
18
dependencies {
9+
implementation 'org.jetbrains.kotlin:kotlin-reflect'
10+
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
11+
12+
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.1'
13+
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
14+
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
15+
}
16+
17+
tasks.withType(Test).configureEach {
18+
useJUnitPlatform()
19+
20+
testLogging {
21+
exceptionFormat = TestExceptionFormat.FULL
22+
}
23+
24+
reports {
25+
junitXml.required = true
26+
html.required = true
27+
}
28+
29+
jacocoTestReport.dependsOn(it)
30+
}
31+
32+
jacocoTestReport {
33+
reports {
34+
html.required.set(true)
35+
xml.required.set(true)
36+
xml.outputLocation.set(file("${project.layout.buildDirectory}/jacoco/jacoco.xml"))
37+
}
38+
39+
rootProject.tasks.sonar.dependsOn(it)
40+
}
41+
42+
jar {
43+
manifest {
44+
attributes 'Implementation-Title': project.name
45+
attributes 'Implementation-Version': project.version
46+
attributes 'Main-Class': 'de.havox_design.aoc2015.day16.MainClass'
47+
}
48+
}
49+
50+
application {
51+
mainClass = 'de.havox_design.aoc2015.day16.MainClass'
252
}

day18/src/main/java/de/havox_design/aoc2015/day18/MainClass.java

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package de.havox_design.aoc2015.day18
2+
3+
typealias Point = Pair<Int, Int>
4+
5+
class BooleanGrid(val size: Int) : Grid {
6+
private val lights = mutableSetOf<Point>()
7+
8+
override fun turnOn(x: Int, y: Int) {
9+
lights += Point(x, y)
10+
}
11+
12+
override fun turnOff(x: Int, y: Int) {
13+
lights -= Point(x, y)
14+
}
15+
16+
override fun toggle(x: Int, y: Int) =
17+
Point(x, y).let { if (it in lights) lights -= it else lights += it }
18+
19+
override fun lights() = lights.size
20+
21+
fun on(x: Int, y: Int) = Point(x, y) in lights
22+
23+
fun next(): BooleanGrid {
24+
val new = BooleanGrid(size)
25+
for (x in 0 until size) for (y in 0 until size)
26+
if (on(x, y))
27+
when (neighbors(x, y)) {
28+
2, 3 -> new.turnOn(x, y)
29+
}
30+
else
31+
when (neighbors(x, y)) {
32+
3 -> new.turnOn(x, y)
33+
}
34+
return new
35+
}
36+
37+
fun neighbors(x: Int, y: Int): Int {
38+
var on = if (on(x, y)) -1 else 0
39+
for (dy in -1..1) for (dx in -1..1) {
40+
if (on(x + dx, y + dy)) on++
41+
}
42+
return on
43+
}
44+
45+
fun turnCornersOn(): BooleanGrid {
46+
turnOn(0, 0)
47+
turnOn(size - 1, 0)
48+
turnOn(0, size - 1)
49+
turnOn(size - 1, size - 1)
50+
return this
51+
}
52+
53+
companion object {
54+
fun from(lines: Collection<String>): BooleanGrid {
55+
val grid = BooleanGrid(lines.size)
56+
lines.forEachIndexed { y, row -> row.forEachIndexed { x, c -> if (c == '#') grid.turnOn(x, y) } }
57+
return grid
58+
}
59+
}
60+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package de.havox_design.aoc2015.day18
2+
3+
class GIFMatrix(private var filename: String) {
4+
private val data = readData()
5+
6+
fun processPart1(steps: Int=100): Int =
7+
(1..steps)
8+
.fold(data) { current, _ -> current.next() }
9+
.lights()
10+
11+
fun processPart2(steps: Int=100): Int =
12+
0
13+
14+
private fun readData(): BooleanGrid =
15+
BooleanGrid.from(getResourceAsText(filename))
16+
17+
private fun getResourceAsText(path: String): List<String> =
18+
this.javaClass.classLoader.getResourceAsStream(path)!!.bufferedReader().readLines()
19+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package de.havox_design.aoc2015.day18
2+
3+
interface Grid {
4+
fun turnOn(x: Int, y: Int)
5+
fun turnOff(x: Int, y: Int)
6+
fun toggle(x: Int, y: Int)
7+
fun lights(): Int
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package de.havox_design.aoc2015.day18
2+
3+
class MainClass {
4+
companion object {
5+
@JvmStatic
6+
fun main(args: Array<String>) {
7+
println("Solution 1: ${GIFMatrix("day18.txt").processPart1()}")
8+
println("Solution 2: ${GIFMatrix("day18.txt").processPart2()}")
9+
}
10+
}
11+
}

day18/src/test/java/de/havox_design/aoc2015/day18/Day18Test.java

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package de.havox_design.aoc2015.day18
2+
3+
import org.junit.jupiter.api.Assertions
4+
import org.junit.jupiter.api.Test
5+
import org.junit.jupiter.params.ParameterizedTest
6+
import org.junit.jupiter.params.provider.Arguments
7+
import org.junit.jupiter.params.provider.MethodSource
8+
import java.util.stream.Stream
9+
10+
class Day18Test {
11+
@Test
12+
fun testMainClass() =
13+
MainClass.Companion.main(arrayOf())
14+
15+
@ParameterizedTest
16+
@MethodSource("getDataForTestProcessPart1")
17+
fun testProcessPart1(filename: String, expectedResult: Int, steps: Int) =
18+
GIFMatrix(filename).processPart1(steps).shouldBe(expectedResult)
19+
20+
companion object {
21+
@JvmStatic
22+
private fun getDataForTestProcessPart1(): Stream<Arguments> =
23+
Stream.of(
24+
Arguments.of("part1sample.txt", 11, 1),
25+
Arguments.of("part1sample.txt", 8, 2),
26+
Arguments.of("part1sample.txt", 4, 3),
27+
Arguments.of("part1sample.txt", 4, 4)
28+
)
29+
}
30+
}
31+
32+
private fun Int.shouldBe(expectation: Int) = Assertions.assertEquals(expectation, this)

0 commit comments

Comments
 (0)