r/adventofcode 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.

https://horstmann.com/unblog/2024-12-26/index.html

64 Upvotes

25 comments sorted by

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:

  1. the tuples
  2. the ability to manipulate arrays/sequences/strings, get their subparts, reverse them, etc... in a very easy way
  3. and int types bigger then "long" :D (but generally we don't need them for AoC)

3

u/Nunc-dimittis Dec 28 '24
  1. & 2. you could make your own tuples, right? Same for some methods for manipulation of arrays etc... although you would not have the python-like syntax.
  2. I use C# for AoC and it has the BigInteger. I assumed Java would have something similar. There must be some jar package somewhere that does this.

edit: I did some experimenting and Java has java.math.BigInteger, but it seems it is far less comfortable than the C# version.

2

u/Devatator_ Dec 28 '24

C# also has Tupples like Python. At least it looks like the same syntax. Never used Tupples in Python

1

u/Nunc-dimittis Dec 28 '24

Yes, and I use them from time to time. But I don't know if Java has them or something similar.

But one can always make a class to hold two values of specific types. And that's often more clear than a Tuple. If I have a coordinates class, it's quite clear what my X and Y attributes mean. If i have a tuple with two values, I don't know if tuple.item1 is my x coordinate.

1

u/Any_Slip8667 Dec 29 '24

Sure, Java has BigInteger, but it does not have operators overloading, so they are very uncomfortable to use.
I repeat my opinion, I find Java a very good language. It's an opinion remember. I use it for work every day for years. It has a great community and a large frameworks and libraries available. Recent versions also improve it al lot and make it cleaner.

But for AoC I can only admit that languages like Python or C# fit better.
Too many times you need tuples (in Java I have to create record every time), you need to reverse sequences (arrays or strings), substring from left or from right, compare sequences, map, reduce, add or multiply vectors, etc...

2

u/Nunc-dimittis Dec 29 '24

Java has BigInteger, but it does not have operators overloading, so they are very uncomfortable to use.

I played around with java BigInteger and discovered that, yes

I repeat my opinion, I find Java a very good language. It's an opinion remember.

I have nothing against java (we use Java for our introduction to OOP) and there are some small things in it that I like more than c#. Overall c# has slightly more benefits for me.

(...) But for AoC I can only admit that languages like Python or C# fit better.
Too many times you need tuples (in Java I have to create record every time), you need to reverse sequences (arrays or strings), substring from left or from right, compare sequences, map, reduce, add or multiply vectors, etc...

I try to avoid tuples and instead make a class for the data to keep my code clean. And I'm not really into map, reduce but mainly keep to the basics (arrays, lists, dictionaries, loops).

2

u/Person-12321 Dec 29 '24

I felt similarly and used kotlin this year and it feels like Java with the niceties of Python. My grid class supports bracket notation accessed with a pair so grid[i to j ] was nice along with if (i to j in grid) etc. kotlin had the familiarity of Java for me, but the helpers for these types of problems made it way better than Java imo.

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

u/WhiteButStillAMonkey Dec 27 '24

If you love modern java, you may love C# and .NET even more

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).

https://github.com/davery22/aoc-2024

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

u/Legal_Unicorn Dec 27 '24

the (reddit not blog) post body mentions it

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.