Skip to content

Commit 102cb80

Browse files
committed
Solved day22 part 1
1 parent dfd3383 commit 102cb80

File tree

4 files changed

+122
-6
lines changed

4 files changed

+122
-6
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ To run all solutions, simply run `./gradlew run`. If you want to run the solutio
4343
| 19 |||
4444
| 20 |||
4545
| 21 |||
46-
| 22 | ||
46+
| 22 | ||
4747
| 23 |||
4848
| 24 |||
4949
| 25 |||
50-
| **SUM** | **21** | **21 ⭐** |
50+
| **SUM** | **22** | **21 ⭐** |
5151

52-
Total: 42
52+
Total: 43
5353

5454
## Prior Years contribution:
5555
| Year | Language(s) | Total Stars | Link to repository |

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.aoc2023'
7-
version '0.21.1'
7+
version '0.21.2'
88

99
// Switch to gradle "all" distribution.
1010
wrapper {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package de.havox_design.aoc2023.day22
2+
3+
data class Brick(val startPosition: Triple<Int, Int, Int>, val endPosition: Triple<Int, Int, Int>) {
4+
val fallen: Brick
5+
get() =
6+
Brick(
7+
Triple(startPosition.x(), startPosition.y(), startPosition.z() - 1),
8+
Triple(endPosition.x(), endPosition.y(), endPosition.z() - 1)
9+
)
10+
11+
val canFall: Boolean
12+
get() =
13+
canFall(Day22.getBricks())
14+
15+
fun contains(position: Triple<Int, Int, Int>): Boolean =
16+
position.x() >= startPosition.x() && position.x() <= endPosition.x() &&
17+
position.y() >= startPosition.y() && position.y() <= endPosition.y() &&
18+
position.z() >= startPosition.z() && position.z() <= endPosition.z()
19+
20+
fun intersect(other: Brick): Boolean {
21+
return when {
22+
other.startPosition.z() > endPosition.z() || other.endPosition.z() < startPosition.z() -> false
23+
else -> {
24+
for (x in other.startPosition.x()..other.endPosition.x()) {
25+
for (y in other.startPosition.y()..other.endPosition.y()) {
26+
for (z in other.startPosition.z()..other.endPosition.z()) {
27+
when {
28+
contains(Triple(x, y, z)) -> return true
29+
}
30+
}
31+
}
32+
}
33+
false
34+
}
35+
}
36+
}
37+
38+
fun canFall(bricks: ArrayList<Brick>): Boolean {
39+
val f = fallen
40+
41+
for (brick in bricks) {
42+
when {
43+
brick != this && brick.intersect(f) -> return false
44+
}
45+
}
46+
47+
return startPosition.third > 1
48+
}
49+
}
50+
51+
private fun Triple<Int, Any, Any>.x(): Int =
52+
this.first
53+
54+
private fun Triple<Any, Int, Any>.y(): Int =
55+
this.second
56+
57+
private fun Triple<Any, Any, Int>.z(): Int =
58+
this.third
Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,70 @@
11
package de.havox_design.aoc2023.day22
22

33
class Day22(private var filename: String) {
4-
fun solvePart1(): Long =
5-
5L
4+
private val ID_BRICK_END_X = 3
5+
private val ID_BRICK_END_Y = 4
6+
private val ID_BRICK_END_Z = 5
7+
private val ID_BRICK_START_X = 0
8+
private val ID_BRICK_START_Y = 1
9+
private val ID_BRICK_START_Z = 2
10+
11+
private val number = Regex("-?\\d+")
12+
13+
fun solvePart1(): Long {
14+
bricks.clear()
15+
16+
for (row in getResourceAsText(filename)) {
17+
val numbers = number
18+
.findAll(row)
19+
.map { it.value.toInt() }
20+
.toList()
21+
22+
bricks.add(
23+
Brick(
24+
Triple(numbers[ID_BRICK_START_X], numbers[ID_BRICK_START_Y], numbers[ID_BRICK_START_Z]),
25+
Triple(numbers[ID_BRICK_END_X], numbers[ID_BRICK_END_Y], numbers[ID_BRICK_END_Z])
26+
)
27+
)
28+
}
29+
30+
bricks
31+
.sortBy { it.startPosition.third }
32+
33+
for (i in 0..<bricks.size) {
34+
while (bricks[i].canFall) {
35+
bricks[i] = bricks[i].fallen
36+
}
37+
}
38+
39+
var countToBeAbleToBeDisintegrated = 0L
40+
41+
for (i in 0..<bricks.size) {
42+
val save = bricks[i]
43+
bricks[i] = Brick(
44+
Triple(-1, -1, -1),
45+
Triple(-1, -1, -1)
46+
)
47+
48+
if (bricks.all { !it.canFall }) {
49+
countToBeAbleToBeDisintegrated++
50+
}
51+
52+
bricks[i] = save
53+
}
54+
55+
return countToBeAbleToBeDisintegrated
56+
}
657

758
fun solvePart2(): Long =
859
0L
960

1061
private fun getResourceAsText(path: String): List<String> =
1162
this.javaClass.classLoader.getResourceAsStream(path)!!.bufferedReader().readLines()
63+
64+
companion object {
65+
private val bricks = ArrayList<Brick>()
66+
67+
fun getBricks(): ArrayList<Brick> =
68+
bricks
69+
}
1270
}

0 commit comments

Comments
 (0)