-
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?
The head ref may contain hidden characters: "codex/ajouter-structure-et-d\u00E9pendances-pour-adventofcode-2024-en-k"
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| # aoc2022 | ||
| # Advent of Code | ||
|
|
||
| https://adventofcode.com/2022 | ||
| This repository contains various Advent of Code solutions. Kotlin support is now available for the 2024 edition. | ||
|
|
||
| Useful links: | ||
|
|
||
| * [Advent of Code 2022](https://adventofcode.com/2022) | ||
| * [Advent of Code 2024](https://adventofcode.com/2024) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package fr.lmo.aoc2024 | ||
|
|
||
| import java.nio.file.Files | ||
| import java.nio.file.Path | ||
| import java.nio.file.Paths | ||
|
|
||
| /** | ||
| * Utility helper inspired by previous years to ease solving Advent of Code 2024 puzzles. | ||
| */ | ||
| abstract class AoCHelper { | ||
| abstract fun run() | ||
|
|
||
| private fun directoryName(): String = this::class.java.simpleName.lowercase() | ||
|
|
||
| private fun fileName(name: String): String { | ||
| var fileName = name | ||
| if (System.getProperty("test", "false").toBoolean()) { | ||
| fileName += "-test" | ||
| } | ||
| return "$fileName.txt" | ||
| } | ||
|
|
||
| private fun path(name: String): Path = | ||
| Paths.get("src", "main", "resources", "2024", directoryName(), fileName(name)) | ||
|
|
||
| fun getTestInputPath(): Path = path("input-test") | ||
|
|
||
| fun getInputPath(): Path = path("input") | ||
|
Comment on lines
+26
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 -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")
🤖 Prompt for AI Agents |
||
|
|
||
| fun readFile(path: Path): String = Files.readString(path) | ||
|
|
||
| fun lines(path: Path): List<String> = Files.readAllLines(path) | ||
|
|
||
| fun <T> list(path: Path, mapper: (String) -> T): List<T> = | ||
| Files.lines(path).use { it.map(mapper).toList() } | ||
|
|
||
| companion object { | ||
| @JvmStatic | ||
| 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) | ||
| val instance = clazz.getDeclaredConstructor().newInstance() | ||
| instance.run() | ||
| } catch (e: Exception) { | ||
| e.printStackTrace() | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package fr.lmo.aoc2024 | ||
|
|
||
| class D01 : AoCHelper() { | ||
| override fun run() { | ||
| println("Test: " + readFile(getTestInputPath())) | ||
| // println("Real: " + readFile(getInputPath())) | ||
| } | ||
| } | ||
|
|
||
| fun main(args: Array<String>) { | ||
| AoCHelper.main(args) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| TODO |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| TODO |
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.
📝 Committable suggestion
🤖 Prompt for AI Agents