-
Notifications
You must be signed in to change notification settings - Fork 0
Add Kotlin base for Advent of Code 2024 #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add Kotlin base for Advent of Code 2024 #2
The head ref may contain hidden characters: "codex/ajouter-structure-et-d\u00E9pendances-pour-adventofcode-2024-en-k"
Conversation
WalkthroughThe changes introduce Kotlin support for Advent of Code 2024 solutions. A new abstract helper class standardizes input handling and execution for puzzle solvers. An initial Day 1 solution class and input files are added, and the build system is updated to support Kotlin sources and JVM 21. The README is updated accordingly. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant MainFunction
participant AoCHelper
participant D01
User->>MainFunction: Run main(args)
MainFunction->>AoCHelper: AoCHelper.main(args)
AoCHelper->>D01: Instantiate D01 via reflection
AoCHelper->>D01: Call run()
D01->>AoCHelper: getTestInputPath()
D01->>AoCHelper: readFile(path)
D01->>User: Print file contents
Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🔭 Outside diff range comments (1)
pom.xml (1)
89-89: 💡 Verification agent🧩 Analysis chain
Address the plugin resolution issue mentioned in PR description.
The PR description mentions a
PluginResolutionExceptionwhen runningmvn -q testdue to kotlin-maven-plugin download issues. This might be related to the Kotlin version or missing repository configuration.
🏁 Script executed:
#!/bin/bash # Description: Check Maven repository configuration and Kotlin plugin availability # Check if Maven can resolve the kotlin-maven-plugin mvn help:describe -DgroupId=org.jetbrains.kotlin -DartifactId=kotlin-maven-plugin -Dversion=1.9.0 # Check Maven settings and active repositories mvn help:effective-settings | grep -A 5 -B 5 "repository"Length of output: 345
#!/bin/bash set -e # Search for kotlin-maven-plugin in pom.xml rg -C2 -n "kotlin-maven-plugin" pom.xml # Check for <pluginRepositories> section rg -C2 -n "<pluginRepositories>" pom.xml # Check for <repositories> section rg -C2 -n "<repositories>" pom.xml
#!/bin/bash set -e # Check for pluginRepositories section in pom.xml rg -C2 -n "<pluginRepositories>" pom.xml # Check for repositories section in pom.xml rg -C2 -n "<repositories>" pom.xml
🏁 Script executed:
#!/usr/bin/env bash set -e # Check if <pluginRepositories> is declared rg -n "<pluginRepositories>" pom.xml || echo "No <pluginRepositories> section found in pom.xml" # Check if <repositories> is declared rg -n "<repositories>" pom.xml || echo "No <repositories> section found in pom.xml" # Verify kotlin-maven-plugin definition rg -n "<artifactId>kotlin-maven-plugin</artifactId>" pom.xml || echo "kotlin-maven-plugin not referenced in pom.xml"Length of output: 409
Add explicit pluginRepositories for Kotlin Maven Plugin resolution
It looks like your pom.xml defines the Kotlin plugin (line 15) but has no
<pluginRepositories>or<repositories>section—Maven falls back to central by default, but if your build environment overrides or mirrors central you’ll need to declare it explicitly. Please add something like:<project> … <pluginRepositories> <pluginRepository> <id>central</id> <url>https://repo.maven.apache.org/maven2</url> </pluginRepository> </pluginRepositories> <repositories> <repository> <id>central</id> <url>https://repo.maven.apache.org/maven2</url> </repository> </repositories> … </project>This ensures
kotlin-maven-plugin(version 1.9.0) can always be fetched.
🧹 Nitpick comments (3)
src/main/kotlin/fr/lmo/aoc2024/D01.kt (1)
6-6: Consider uncommenting the real input line.Once you have the actual puzzle input, uncomment this line to process the real input data alongside the test input.
src/main/kotlin/fr/lmo/aoc2024/AoCHelper.kt (2)
13-21: Fix the directory naming collision risk.The
directoryName()method usessimpleName.lowercase()which could cause collisions if class names differ only in case. Consider using the actual class name or a more robust naming strategy.-private fun directoryName(): String = this::class.java.simpleName.lowercase() +private fun directoryName(): String = this::class.java.simpleName.lowercase().let { name -> + if (name.startsWith("d") && name.length == 3) name else name +}
37-54: Improve error handling specificity and add constructor validation.The current exception handling catches all exceptions generically. Consider more specific error handling and validate that target classes have the expected no-arg constructor.
fun main(args: Array<String>) { if (args.isEmpty()) { println("Arg should contain classname") return } val className = args[0] try { val clazz = Class.forName("${AoCHelper::class.java.packageName}.$className") .asSubclass(AoCHelper::class.java) + + // Validate constructor exists + clazz.getDeclaredConstructor() + val instance = clazz.getDeclaredConstructor().newInstance() instance.run() - } catch (e: Exception) { + } catch (e: ClassNotFoundException) { + println("Class not found: ${AoCHelper::class.java.packageName}.$className") + } catch (e: NoSuchMethodException) { + println("Class $className must have a no-arg constructor") + } catch (e: Exception) { e.printStackTrace() } }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
README.md(1 hunks)pom.xml(2 hunks)src/main/kotlin/fr/lmo/aoc2024/AoCHelper.kt(1 hunks)src/main/kotlin/fr/lmo/aoc2024/D01.kt(1 hunks)src/main/resources/2024/d01/input-test.txt(1 hunks)src/main/resources/2024/d01/input.txt(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/main/kotlin/fr/lmo/aoc2024/D01.kt (1)
src/main/kotlin/fr/lmo/aoc2024/AoCHelper.kt (2)
readFile(30-30)getTestInputPath(26-26)
🔇 Additional comments (11)
src/main/resources/2024/d01/input.txt (1)
1-1: Appropriate placeholder for puzzle input.This TODO placeholder is the correct approach for scaffolding Advent of Code input files. The actual puzzle input will be added when working on day 1.
src/main/resources/2024/d01/input-test.txt (1)
1-1: Appropriate placeholder for test input.Consistent with the main input file, this TODO placeholder follows the correct scaffolding pattern for Advent of Code test input files.
README.md (1)
1-8: Well-structured documentation updates.The README updates appropriately reflect the repository's expanded scope to include Advent of Code 2024 with Kotlin support. The title change and useful links section improve clarity and navigation.
pom.xml (3)
26-28: Correct Kotlin source directory configuration.Adding
src/main/kotlinto the source directories properly enables Kotlin compilation alongside existing Java sources.
42-45: Good test source directory configuration.Explicitly configuring both Kotlin and Java test source directories ensures proper test compilation for mixed-language projects.
41-41:Details
❓ Verification inconclusive
Verify JVM target compatibility.
JVM target 21 is quite recent. Ensure your runtime environment supports Java 21, especially given the PR description mentions build failures.
🏁 Script executed:
#!/bin/bash # Description: Check if the Kotlin version supports JVM target 21 and verify current Java version compatibility # Check current Java version java -version # Check Maven Java version mvn -version | head -3Length of output: 313
🏁 Script executed:
#!/bin/bash # Inspect pom.xml for Kotlin plugin and Java version settings echo "== Kotlin Maven plugin configuration ==" rg -n '<artifactId>kotlin-maven-plugin' pom.xml -A5 echo echo "== Kotlin version property ==" rg -n '<kotlin.version>' pom.xml echo echo "== Java version property ==" rg -n '<java.version>' pom.xml echo echo "== Maven Compiler plugin configuration ==" rg -n '<artifactId>maven-compiler-plugin' pom.xml -A5 echo echo "== Compiler source/target settings ==" rg -n '<source>' pom.xml -A3 rg -n '<target>' pom.xml -A3Length of output: 1982
Confirm Java 21 support in build environment
Please verify that your local and CI environments are running Java 21 (or later) to match the following settings in pom.xml:
- Kotlin plugin’s
<jvmTarget>21</jvmTarget>- Maven Compiler plugin’s
<source>21</source>and<target>21</target>Our sandbox reported JDK 17 and could not run
mvn -version, so ensure all build agents have at least Java 21 installed. Otherwise, consider lowering the target to Java 17 to avoid compilation failures.src/main/kotlin/fr/lmo/aoc2024/D01.kt (2)
3-8: Well-structured solution class following the helper pattern.The class structure correctly extends AoCHelper and implements the run method. The approach of testing with test input first before switching to real input is a good Advent of Code practice.
10-12:Details
✅ Verification successful
Verify the main function delegation pattern.
The main function delegates to
AoCHelper.main(args)rather than directly instantiating and runningD01. Ensure this pattern correctly resolves to running the D01 solution.
🏁 Script executed:
#!/bin/bash # Description: Verify AoCHelper.main implementation to understand the delegation pattern # Search for AoCHelper main method implementation ast-grep --pattern $'companion object { $$$ fun main($_) { $$$ } $$$ }' # Alternative search for main method in AoCHelper rg -A 10 "fun main" src/main/kotlin/fr/lmo/aoc2024/AoCHelper.ktLength of output: 626
🏁 Script executed:
#!/bin/bash # Locate the D01 solution class rg -n "class D01" -n src/main/kotlin/fr/lmo/aoc2024 # Show the contents of D01.kt sed -n '1,200p' src/main/kotlin/fr/lmo/aoc2024/D01.kt # Locate the AoCHelper class definition rg -n "class AoCHelper" -n src/main/kotlin/fr/lmo/aoc2024/AoCHelper.kt # Display the beginning of AoCHelper.kt to inspect the abstract base and run() method sed -n '1,200p' src/main/kotlin/fr/lmo/aoc2024/AoCHelper.ktLength of output: 2257
Delegation pattern verified and correct.
ThemaininD01.kthands off toAoCHelper.main(args), which uses reflection to load the class named inargs[0]and invoke itsrun()method. Running with"D01"correctly executesD01.run(). No changes required.src/main/kotlin/fr/lmo/aoc2024/AoCHelper.kt (3)
1-5: LGTM: Clean package structure and appropriate imports.The package declaration and imports are well-organized and include only the necessary dependencies for file operations.
7-11: LGTM: Good use of abstract class with template method pattern.The abstract class design provides a clean template method pattern where subclasses implement puzzle-specific logic while inheriting common utility functionality.
30-35: LGTM: Well-implemented file reading utilities with proper resource management.The file reading methods are well-designed, and the
list()method correctly uses.use {}for proper resource management with streaming.
|
|
||
| private fun path(name: String): Path = | ||
| Paths.get("src", "main", "resources", "2024", directoryName(), fileName(name)) | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider using classpath resources for better JAR compatibility.
The current path construction uses filesystem paths which may not work when the application is packaged in a JAR file. Consider using classpath resources instead.
-private fun path(name: String): Path =
- Paths.get("src", "main", "resources", "2024", directoryName(), fileName(name))
+private fun path(name: String): Path {
+ val resourcePath = "/2024/${directoryName()}/${fileName(name)}"
+ return this::class.java.getResource(resourcePath)?.toURI()?.let { Paths.get(it) }
+ ?: throw IllegalStateException("Resource not found: $resourcePath")
+}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| private fun path(name: String): Path = | |
| Paths.get("src", "main", "resources", "2024", directoryName(), fileName(name)) | |
| private fun path(name: String): Path { | |
| val resourcePath = "/2024/${directoryName()}/${fileName(name)}" | |
| return this::class.java | |
| .getResource(resourcePath) | |
| ?.toURI() | |
| ?.let { Paths.get(it) } | |
| ?: throw IllegalStateException("Resource not found: $resourcePath") | |
| } |
🤖 Prompt for AI Agents
In src/main/kotlin/fr/lmo/aoc2024/AoCHelper.kt around lines 22 to 25, the method
constructs file system paths directly, which can fail when running from a JAR.
Refactor the path function to load resources using the class loader's
getResource or getResourceAsStream methods to access files from the classpath,
ensuring compatibility when packaged as a JAR.
| fun getTestInputPath(): Path = path("input-test") | ||
|
|
||
| fun getInputPath(): Path = path("input") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical bug: getTestInputPath() will return incorrect filename when test property is set.
The getTestInputPath() method calls path("input-test"), but the fileName() method will append "-test" again if the system property "test" is true, resulting in "input-test-test.txt" instead of "input-test.txt".
-fun getTestInputPath(): Path = path("input-test")
+fun getTestInputPath(): Path =
+ Paths.get("src", "main", "resources", "2024", directoryName(), "input-test.txt")Or create a separate method that bypasses the system property logic:
+private fun pathWithoutTestSuffix(name: String): Path =
+ Paths.get("src", "main", "resources", "2024", directoryName(), "$name.txt")
+
-fun getTestInputPath(): Path = path("input-test")
+fun getTestInputPath(): Path = pathWithoutTestSuffix("input-test")Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In src/main/kotlin/fr/lmo/aoc2024/AoCHelper.kt around lines 26 to 28, the
getTestInputPath() method calls path("input-test"), but since path() appends
"-test" when the "test" system property is true, this results in
"input-test-test.txt" instead of "input-test.txt". To fix this, create a
separate method that returns the path for "input-test" without applying the
"-test" suffix logic, bypassing the system property check, or adjust
getTestInputPath() to directly return the correct filename without double
appending "-test".
Summary
src/main/kotlinAoCHelperfor the 2024 editionTesting
mvn -q test(fails: PluginResolutionException - unable to download kotlin-maven-plugin)https://chatgpt.com/codex/tasks/task_e_6841baad38708326bff0314ec5f2f783
Summary by CodeRabbit
New Features
Documentation
Chores