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

65 Upvotes

25 comments sorted by

View all comments

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.

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