Skip to content

Commit 8c4b352

Browse files
committed
Solved day24 part 2
1 parent a820e7b commit 8c4b352

File tree

8 files changed

+104
-75
lines changed

8 files changed

+104
-75
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ build/
3030

3131
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
3232
hs_err_pid*
33+
34+
# Helper files
35+
## Data export for python solver script
36+
day24/src/main/resources/result_part2.txt

README.md

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
Simple project for Advent of Code 2023.
44

55
Planned to be done primarily in [Kotlin](https://kotlinlang.org). Please beware of code quality issues, because these
6-
are early steps on Kotlin.
7-
8-
If there will be major issues in solving the tasks in Kotlin these tasks may be solved in JAVA 21.
6+
are early steps on Kotlin. Some tasks may be solved in other languages like Python or JAVA 21.
97

108
For more information see https://adventofcode.com [2023](https://adventofcode.com/2023)
119

@@ -20,36 +18,36 @@ To run all solutions, simply run `./gradlew run`. If you want to run the solutio
2018

2119
## Current status:
2220

23-
| Day | Part 1 | Part 2 |
24-
|---------|----------|----------|
25-
| 1 |||
26-
| 2 |||
27-
| 3 |||
28-
| 4 |||
29-
| 5 |||
30-
| 6 |||
31-
| 7 |||
32-
| 8 |||
33-
| 9 |||
34-
| 10 |||
35-
| 11 |||
36-
| 12 |||
37-
| 13 |||
38-
| 14 |||
39-
| 15 |||
40-
| 16 |||
41-
| 17 |||
42-
| 18 |||
43-
| 19 |||
44-
| 20 |||
45-
| 21 |||
46-
| 22 |||
47-
| 23 |||
48-
| 24 | | |
49-
| 25 |||
50-
| **SUM** | **23** | **23** |
51-
52-
Total: 46
21+
| Day | Part 1 | Part 2 |
22+
|---------|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|
23+
| 1 || |
24+
| 2 || |
25+
| 3 || |
26+
| 4 || |
27+
| 5 || |
28+
| 6 || |
29+
| 7 || |
30+
| 8 || |
31+
| 9 || |
32+
| 10 || |
33+
| 11 || |
34+
| 12 || |
35+
| 13 || |
36+
| 14 || |
37+
| 15 || |
38+
| 16 || |
39+
| 17 || |
40+
| 18 || |
41+
| 19 || |
42+
| 20 || |
43+
| 21 || |
44+
| 22 || |
45+
| 23 || |
46+
| 24 | | ⭐ <img src="https://s3.dualstack.us-east-2.amazonaws.com/pythondotorg-assets/media/community/logos/python-logo-only.png" width="10" height="12" alt="Python" /> |
47+
| 25 || |
48+
| **SUM** | **24** | **24** |
49+
50+
Total: 48
5351

5452
## Prior Years contribution:
5553
| 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.23.3'
7+
version '0.24.0'
88

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

day24/build.gradle

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
2+
import com.pswidersk.gradle.python.VenvTask
23

34
plugins {
45
id 'application'
56
id 'org.jetbrains.kotlin.jvm' version '1.9.22'
7+
id 'com.pswidersk.python-plugin' version '2.4.0'
68
}
79

810
dependencies {
@@ -50,3 +52,23 @@ jar {
5052
application {
5153
mainClass = 'de.havox_design.aoc2023.day24.MainClass'
5254
}
55+
56+
pythonPlugin {
57+
pythonVersion.set("3.12")
58+
installDir.set(file(rootProject.layout.buildDirectory.file("python")))
59+
}
60+
61+
afterEvaluate {
62+
def pipTask = tasks.register('pipInstall', VenvTask) {
63+
venvExec = "pip"
64+
args = ["install", "--isolated", "-r", "requirements.txt"]
65+
}
66+
67+
def solverTask = tasks.register('solvePart2', VenvTask) {
68+
workingDir = project.file('src/main/resources')
69+
args = [project.file("src/main/python/Day24.py").toString()]
70+
dependsOn(pipTask)
71+
}
72+
73+
compileKotlin.dependsOn(solverTask)
74+
}

day24/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sympy

day24/src/main/kotlin/de/havox_design/aoc2023/day24/Day24.kt

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@ class Day24(private var filename: String) {
88
private val ID_VELOCITY_Y = 4
99
private val ID_VELOCITY_Z = 5
1010

11-
fun solvePart1(minWindow: Long = 200000000000000L, maxWindow: Long = 400000000000000L): Long =
11+
fun solvePart1(minWindow: Long = 200000000000000L, maxWindow: Long = 400000000000000L): Int =
1212
processDay24(getResourceAsText(filename), minWindow, maxWindow)
13-
.first
1413

15-
fun solvePart2(minWindow: Long = 200000000000000L, maxWindow: Long = 400000000000000L): Long =
16-
processDay24(getResourceAsText(filename), minWindow, maxWindow)
17-
.second
14+
fun solvePart2(): String =
15+
getPythonResult("result_part2.txt")
1816

1917
private fun processDay24(
2018
input: List<String>,
2119
minWindow: Long = 200000000000000L,
2220
maxWindow: Long = 400000000000000L
23-
): Pair<Long, Long> {
21+
): Int {
2422
val windowRange = minWindow..maxWindow
2523

2624
val hailstones = input
@@ -37,18 +35,18 @@ class Day24(private var filename: String) {
3735
}
3836
.toList()
3937

40-
val result1 = hailstones
38+
return hailstones
4139
.mapIndexed { index, hailstone ->
4240
hailstones
4341
.drop(index + 1)
4442
.count { hailstone.collide2D(it, windowRange) }
4543
}
4644
.sum()
47-
.toLong()
48-
49-
return Pair(result1, 47L)
5045
}
5146

47+
private fun getPythonResult(path: String): String =
48+
getResourceAsText(path).last()
49+
5250
private fun getResourceAsText(path: String): List<String> =
5351
this.javaClass.classLoader.getResourceAsStream(path)!!.bufferedReader().readLines()
5452
}

day24/src/main/python/Day24.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import sympy
2+
3+
4+
def read_input():
5+
f = open('day24.txt', 'r')
6+
for line in f.readlines():
7+
p, v = line.split(' @ ')
8+
p = list(map(int, p.split(', ')))
9+
v = list(map(int, v.split(', ')))
10+
yield p, v
11+
12+
13+
if __name__ == "__main__":
14+
parsed = read_input()
15+
x = sympy.var('x')
16+
y = sympy.var('y')
17+
z = sympy.var('z')
18+
xv = sympy.var('xv')
19+
yv = sympy.var('yv')
20+
zv = sympy.var('zv')
21+
equations = []
22+
count = 0
23+
for point, velocity in parsed:
24+
count += 1
25+
t = sympy.var('t' + str(count))
26+
equations.append(sympy.Eq(x + t * xv, point[0] + velocity[0] * t))
27+
equations.append(sympy.Eq(y + t * yv, point[1] + velocity[1] * t))
28+
equations.append(sympy.Eq(z + t * zv, point[2] + velocity[2] * t))
29+
30+
if count > 5:
31+
break
32+
33+
solution = sympy.solve(equations)[0]
34+
part2 = solution[x] + solution[y] + solution[z]
35+
print(f"Solution for part 2 via Python: {part2}")
36+
with open("result_part2.txt", 'a') as out:
37+
print(part2, file=out)
Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,10 @@
11
package de.havox_design.aoc2023.day24
22

3-
import org.junit.jupiter.api.Assertions.assertEquals
43
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
94

105
class Day24Test {
116
@Test
127
fun testMainClass() {
138
MainClass.main(arrayOf())
149
}
15-
16-
@ParameterizedTest
17-
@MethodSource("getDataForTestSolvePart1")
18-
fun testSolvePart1(filename: String, expectedResult: Long, minWindow: Long, maxWindow: Long) =
19-
Day24(filename).solvePart1(minWindow, maxWindow).shouldBe(expectedResult)
20-
21-
@ParameterizedTest
22-
@MethodSource("getDataForTestSolvePart2")
23-
fun testSolvePart2(filename: String, expectedResult: Long) =
24-
Day24(filename).solvePart2().shouldBe(expectedResult)
25-
26-
companion object {
27-
@JvmStatic
28-
private fun getDataForTestSolvePart1(): Stream<Arguments> =
29-
Stream.of(
30-
Arguments.of("sample.txt", 2L, 7L, 27L)
31-
)
32-
33-
@JvmStatic
34-
private fun getDataForTestSolvePart2(): Stream<Arguments> =
35-
Stream.of(
36-
Arguments.of("sample.txt", 47L, 7L, 27L)
37-
)
38-
}
3910
}
40-
41-
private fun Long.shouldBe(expectation: Long) = assertEquals(expectation, this)

0 commit comments

Comments
 (0)