https://kotlinlang.org logo
#advent-of-code
Title
# advent-of-code
d

daugian

11/15/2023, 12:01 PM
Does anybody have some good tips to read-up on to prepare for AoC using Kotlin? A nice Kotlin Collections / group theory / software algorithms with Kotlin resource maybe?
I’m going to start with the basics: https://kotlinlang.org/docs/collections-overview.html kodee walking
j

Jakub Gwóźdź

11/15/2023, 1:05 PM
frankly, there is no consistent algorithms+data structures library for Kotlin
d

Davio

11/15/2023, 1:25 PM
I just started writing my own when I first started and kept reusing and expanding it each year as needed. There is mostly some stuff to deal with 2D points (x, y) coordinates and matrices (arrays of arrays)
1
Kotlin itself has an amazing standard library for collections with a lot of useful extension functions and the trick is often knowing which one to use, i.e. something like windowed or partitioned
❤️ 1
d

daugian

11/15/2023, 1:30 PM
Yeah, looking forward to learn more about the Collections API!
Already dreading the inevitable shortest path puzzles this year 😰
In Python there are so many libraries for that
j

Jakub Gwóźdź

11/15/2023, 1:32 PM
I wonder if there will be CRT again. It's like 3 years since we saw it last time
d

Davio

11/15/2023, 1:33 PM
Well I have a generic A* lying around for such use cases
d

daugian

11/15/2023, 1:35 PM
Do you have that laying around somewhere? Or just written it yourself based on the algorithm?
d

Davio

11/15/2023, 1:36 PM
d

daugian

11/15/2023, 1:36 PM
I see Ray Wenderlich has a book on Data Structures & Algorithms in Kotlin
But yeah… I’ll probably learn the most by programming it myself based on that pseudo code Dave!
I’ll give it a go.
… but I’ll still dread it 😉
d

Davio

11/15/2023, 1:38 PM
Oh yeah, graphs are not my strong point either 😄
d

daugian

11/15/2023, 1:39 PM
+ there’s always a catch… it’s never pure A*, or CRT, or what have you.
d

Davio

11/15/2023, 1:40 PM
Nope, I remember one from last year where you had a grid with heights (like a relief map) and you could go up or down 1 step (but not more) and had to find the shortest path from a given point (maybe the lowest point) to the 'peak'
1
m

maiatoday

11/15/2023, 1:51 PM
also just for some general input parsing etc there is this old but good blog post
👌 2
❤️ 1
d

daugian

11/15/2023, 2:02 PM
Thanks Maia! Will definitely read!
k

Ksenia Shneyveys

11/15/2023, 2:44 PM
We’ve also recently released an Algorithmic Challenges in Kotlin plugin-based course, prepared by competitive programmers and experts in algorithms. Maybe this could help. And we’ve gathered previous years’ solutions here.
❤️ 3
k

Kroppeb

11/15/2023, 3:43 PM
I don't think there has been a single day where you needed A*, BFS or Dijkstra is usually enough.
I thought you meant "cathode ray tube" with crt and was confused why you mentioned it hasn't been needed in a long time 😅
j

Jakub Gwóźdź

11/15/2023, 3:46 PM
Hehe yes... Now you reminded me of
uses crt;
from turbo pascal 😁
But no, I meant Chinese remainder
k

Kroppeb

11/15/2023, 3:48 PM
I have a method called 'crt' in my util library that aids in the printing of list of lists of booleans as grid. I might need to rename that then
😆 1
j

Jakub Gwóźdź

11/15/2023, 3:49 PM
It was the puzzle about some encoding, right?
d

daugian

11/15/2023, 4:32 PM
Doing this in the project template’s
Utils.kt
file…
Copy code
fun String.readLines(): List<String> {
    croakIfUsingSampleInput()

    return File("src", this).readLines()
}

private fun String.croakIfUsingSampleInput() {
    if (contains("sample")) {
        "❌❌❌ 🧝 YOU ARE USING SAMPLE INPUT ⚠️".println()
    }
}
… since I lost some time on not finding the right answer because I used sample input, instead of the real input last year 😅
e

ephemient

11/15/2023, 10:19 PM
I always have my code as a
fun
in the
main
source set, independent of any inputs, with a
main()
function that runs it on real input (which is stored as a resource), and at least one
@Test
function in the
test
source set for each sample input
with
./gradlew --continuous test run
I get pretty quick feedback as I edit
👍 1
d

daugian

11/16/2023, 6:52 AM
Nice approach @ephemient!
j

Jakub Gwóźdź

11/16/2023, 7:16 AM
out of curiosity - how does --continuous work if you edit source while previous run is still going?
e

ephemient

11/16/2023, 7:48 AM
yes, it'll start another run right after the previous one
j

Jakub Gwóźdź

11/16/2023, 8:04 AM
but in case the when previous version goes into some infinity loop or something, you need to kill it manually?
e

ephemient

11/16/2023, 8:09 AM
yep. don't write infinite loops 😛
j

Jakub Gwóźdź

11/16/2023, 8:25 AM
(or add
@Timeout(600)
or something 🙂
d

Davio

11/20/2023, 9:10 AM
Spent some time generalizing my setup, so basically: I can start implementing a new day by just adding a package "dayX" with contents "DayX.kt" (actual implementation with a class which extends Day which has default methods to get the answers for part 1 and 2), "DayXTest" to have tests (yes this is also in src/main/kotlin instead of src/test/kotlin just to make it easier for this project), "X-example-N.txt" with some example input and "X.txt" with the actual input
n

Neil Banman

11/25/2023, 7:21 PM
> + there’s always a catch… it’s never pure A*, or CRT, or what have you. At least with the pathfinding algorithms, the standard versions usually work. The CS class adjacency matrix versions don't usually work because oftentimes you can't efficiently calculate all the edges in advance. But if you have a version that lets you generate edges on the fly through a function or lambda, you're good to go.
j

Jakub Gwóźdź

11/27/2023, 7:26 AM
Hi @ephemient - I’m playing with your idea of continuous gradle testing, seems useful, but did you manage to suppress the
* What went wrong:
and
* Try:
sections from gradle output? Say in case of error, I’d only want to see
Copy code
Day0Test > part1test() FAILED
    org.opentest4j.AssertionFailedError: expected: <333> but was: <334>

3 tests completed, 1 failed, 1 skipped

FAILURE: Build failed with an exception.
to minimize the screen clogging. Do you maybe handle it somehow?
(also I wanted to use the “rerun” capability of IntelliJ testing, which is basically the same as gradle continuous but with nicer UI, but it’s crap. I mean - it works well, great, but whenever there’s a build error, and of course there is a build error if the rerun catches you in the middle of editing, IntelliJ moves the caret to the place of syntax, and you continue typing there 😆 )
e

ephemient

11/27/2023, 7:31 AM
perhaps you can use
./gradlew -q
with customized test logging settings
but I always look at the html test report if there are any failures, ignoring the terminal spew
j

Jakub Gwóźdź

11/27/2023, 7:33 AM
ah ok, that’s one way to solve it. the report file placement does not change so it can be open in brower…
👍
7 Views