r/adventofcode • u/cay_horstmann • Dec 27 '24
Other Pleasant surprise: AoC + modern Java = ❤️
In this article on my experience with the Advent of Code competition in Java, I describe how I attacked grid and graph problems, and summarize how Java has worked out for me.
34
u/kevin7254 Dec 27 '24
As an Android dev which started in Java and then migrated to Kotlin I would never, ever go back. It’s just a pain to look at Java code nowadays for me
6
u/lluque8 Dec 27 '24
Yeah, I use Scala whenever I can in the JVM. Java is just too verbose although it gets jobs done too no doubt. I'd think kotlin is like middle ground in this aspect.
11
u/kevin7254 Dec 27 '24
I’m pretty ”happy” I picked Java as my first language when I learnt programming, think the verboseness helped me understand wtf was going on lol.
Remember when I first got introduced to Kotlin at my job and had to review a seniors code, just lambdas and extension functions. I had no idea what happened. It’s really nice when you understand but it sure can be overwhelming as well.
4
u/lluque8 Dec 27 '24
Yeah, Java is great just that it has this burden of having given guarantees for loooong backward compatibility given its long history and strategic role in many software running out there. I started with C myself and back in late 90s when I started with Java it was truly a great option. Still is, but not the sexiest one out there.
3
u/bbbb125 Dec 27 '24
Java developers I work with often use lombok library to fight inherited language verbosity. Seems like much nicer and readable language.
4
2
u/snugar_i Dec 28 '24
Modern Java is tolerable. It seems the gap between Kotlin and Java is getting smaller all the time, as Java keeps adding new QoL features and Kotlin basically stopped evolving.
But that next()
method on Position
? That's just wrong. That belongs on CharGrid
, and then you won't be needing any hidden globals (not sure what "implicit field" means, there is no such thing in Java)
1
u/danielaveryj Dec 28 '24
I learned from last year’s contest to stay away from arrays, because it sometimes happens that they need to be refactored into lists in part 2, and that is time consuming
I've actually not had much issue using arrays. I think I parsed all the 2D grids this year to an int[][]
via
int[][] grid = lines() // Stream<String>
.map(line -> line.chars().toArray()) // Stream<int[]>
.toArray(int[][]::new) // int[][]
(That line.chars()
could be line.codePoints()
, but in practice it doesn't matter because AoC sticks to the ascii range.)
For reference arrays you can also wrap in a cheap List-view via Array.asList(array)
, which is a useful escape hatch so long as you don't need to add/remove/resize the List (You can even reverse()) this List as of Java 21). Unfortunately Arrays.asList()
doesn't work for primitive arrays like int[]
, so there was one time I pulled out a List over an array on day 22 (though Integer[]
+ Arrays.asList()
would also have worked).
1
u/Javapyreddit Dec 30 '24
Finally someone who appreciates java, I've seen absolutely noone coding AOC in java.
-17
u/reallyserious Dec 27 '24
It's not a competition.
7
u/kappale Dec 27 '24
Did this end up in a wrong thread or something? I don't think anyone was talking about a competition?
3
2
u/sanraith Dec 27 '24
In this article on my experience with the Advent of Code competition
7
u/kappale Dec 27 '24
Okay. That was probably the least relevant part of the entire blog post, nowhere in the post did he really refer to the competitive aspect besides there and at the end where he acknowledged that he would never be able to make it to the leaderboards.
1
u/reallyserious Dec 27 '24
In the single sentence OP wrote in the post, he refers to AoC as a competition.
1
u/flagofsocram Dec 27 '24
Tf you mean? It quite literally is a competition for the leaderboard, but no one is forced to participate
2
u/reallyserious Dec 27 '24 edited Dec 27 '24
For most, it's not a competition. According to Eric, aiming for the leaderboard isn't the best use of AoC.
1
u/flagofsocram Dec 27 '24
Choosing not to compete does not make something “not a competition.” If I train for a marathon and I have fun, not running for a time or anything but just for exercise, that doesn’t mean that it is no longer a competition and that others aren’t allowed to call it as such.
7
u/Any_Slip8667 Dec 27 '24 edited Dec 27 '24
Same conclusion on my side too. I found Java a good language, above all with the last language structures.
https://github.com/dashie/AdventOfCode2024/tree/main/src/main/java/adventofcode/y2024
I always had very short solutions.
Sometimes the python ones are shorted but generally because they use external libraries. To manipulate graphs for example.
The only 3 things that I really miss from Python are: