# Advent of Code 2024

**URL:** <https://fortran-lang.discourse.group/t/advent-of-code-2024/8923>\
**Category:** Announcements\
**Created:** [December 4, 2024, 10:37am UTC](https://fortran-lang.discourse.group/t/advent-of-code-2024/8923 "2024-12-04T10:37:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carltoffel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/carltoffel/32/1680_2.png) [@Carltoffel](https://fortran-lang.discourse.group/u/Carltoffel)\
**Post date:** [December 4, 2024, 10:37am UTC](https://fortran-lang.discourse.group/t/advent-of-code-2024/8923/1 "2024-12-04T10:37:25Z")

</div>

[https://adventofcode.com](https://adventofcode.com)

> Advent of Code is an annual set of Christmas-themed computer programming challenges that follow an Advent calendar.

Who wants to join this year? 🙂

I am solving the puzzles in Fortran because I want to evaluate them for my Fortran course in February. On the last day of my last class, I let the students solve any puzzle from AoC23 to test their Fortran skills. In the entire two weeks of the course, they had never worked as hard or as ambitiously as they did on that day. 😂

I removed some inactive members from the leaderboard, but feel free to (re-)join:  
Go to [Leaderboard - Advent of Code 2024](https://adventofcode.com/2024/leaderboard/private) and enter `1510956-4380811b`.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [December 4, 2024, 3:42pm UTC](https://fortran-lang.discourse.group/t/advent-of-code-2024/8923/2 "2024-12-04T15:42:55Z")

</div>

> [@Carltoffel](#):
>
> Go to [Leaderboard - Advent of Code 2024](https://adventofcode.com/2024/leaderboard/private) and enter `1510956-4380811b`.

Note: you have to be logged in _before_ you click this link; then you can enter the code. Took me a while to figure out.

---

<div class="post-metadata">

**Author:** ![jacobwilliams](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/jacobwilliams/32/10_2.png) [@jacobwilliams](https://fortran-lang.discourse.group/u/jacobwilliams)\
**Post date:** [December 6, 2024, 2:52am UTC](https://fortran-lang.discourse.group/t/advent-of-code-2024/8923/3 "2024-12-06T02:52:32Z")

</div>

Day 5: We meet again, my old friend bubble sort.

I have no remorse for what I did.

---

<div class="post-metadata">

**Author:** ![tyranids](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@tyranids](https://fortran-lang.discourse.group/u/tyranids)\
**Post date:** [December 6, 2024, 2:59am UTC](https://fortran-lang.discourse.group/t/advent-of-code-2024/8923/4 "2024-12-06T02:59:38Z")

</div>

I always like doing these. I also don’t mind reinventing the wheel so every year I get to write new file reading, string parsing, and sorting utilities!

---

<div class="post-metadata">

**Author:** ![jacobwilliams](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/jacobwilliams/32/10_2.png) [@jacobwilliams](https://fortran-lang.discourse.group/u/jacobwilliams)\
**Post date:** [December 6, 2024, 3:11am UTC](https://fortran-lang.discourse.group/t/advent-of-code-2024/8923/5 "2024-12-06T03:11:18Z")

</div>

I reuse a module a various helper routines for things like that. (although I also do end up reinventing some wheels)

---

<div class="post-metadata">

**Author:** ![Euler-37](https://avatars.discourse-cdn.com/v4/letter/e/b19c9b/32.png) [@Euler-37](https://fortran-lang.discourse.group/u/Euler-37)\
**Post date:** [December 6, 2024, 4:23am UTC](https://fortran-lang.discourse.group/t/advent-of-code-2024/8923/6 "2024-12-06T04:23:35Z")

</div>

I think it is a good way to test new fortran compilers, like flang-new,lfortran.  
Because the solution usually contains intrinsic procedures, file I/O and some fortran magic tricks.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [December 6, 2024, 5:26am UTC](https://fortran-lang.discourse.group/t/advent-of-code-2024/8923/7 "2024-12-06T05:26:50Z")

</div>

@Euler-37 indeed, I discovered about 5 bugs yesterday related to IO, but most of them are now fixed. 🙂

---

<div class="post-metadata">

**Author:** ![TheFibonacciEffect](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/thefibonaccieffect/32/5390_2.png) [@TheFibonacciEffect](https://fortran-lang.discourse.group/u/TheFibonacciEffect)\
**Post date:** [December 8, 2024, 12:21pm UTC](https://fortran-lang.discourse.group/t/advent-of-code-2024/8923/8 "2024-12-08T12:21:40Z")

</div>

Did someone find a good way to read the input files?

---

<div class="post-metadata">

**Author:** ![nshaffer](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/nshaffer/32/161_2.png) [@nshaffer](https://fortran-lang.discourse.group/u/nshaffer)\
**Post date:** [December 8, 2024, 9:45pm UTC](https://fortran-lang.discourse.group/t/advent-of-code-2024/8923/9 "2024-12-08T21:45:03Z")

</div>

There is no single best answer here. Some problems can be tackled line by line, in which case list-directed reads into a character variable or integer array can work well. Other times it will feel more natural to parse one character at a time. Yet other problems can be tackled in a very “matrix-y” way by reading the whole data into a 2D array.

If I had to recommend one most flexible way, I would say to work line-by-line. Read each line into a “big enough” character variable, parse it in whatever way makes sense for your solution approach, and stop when you hit the end of file (by checking the `iostat` of each read statement).

---

<div class="post-metadata">

**Author:** ![jacobwilliams](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/jacobwilliams/32/10_2.png) [@jacobwilliams](https://fortran-lang.discourse.group/u/jacobwilliams)\
**Post date:** [December 9, 2024, 2:49am UTC](https://fortran-lang.discourse.group/t/advent-of-code-2024/8923/10 "2024-12-09T02:49:48Z")

</div>

I have a [read\_line](https://github.com/jacobwilliams/AoC-2024/blob/master/src/aoc_utilities.F90#L849) function that I use that returns an allocatable character string. That, with a simple string class and various routines like `split`, `string_to_int`, etc. are usually all you need for these, without having to mess around with any format statements, or assume “big enough” character strings.

---

<div class="post-metadata">

**Author:** ![tyranids](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@tyranids](https://fortran-lang.discourse.group/u/tyranids)\
**Post date:** [December 9, 2024, 4:29am UTC](https://fortran-lang.discourse.group/t/advent-of-code-2024/8923/11 "2024-12-09T04:29:34Z")

</div>

As stated by others above, it depends on the problem. Some of them it’s best to read the line as text because you’re going to be searching for some pattern, perhaps across multiple lines, and other times you are dealing with more simple “there will be N integers, do something…”

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [December 9, 2024, 7:52am UTC](https://fortran-lang.discourse.group/t/advent-of-code-2024/8923/12 "2024-12-09T07:52:42Z")

</div>

The latest release of the Intel Fortran compiler supports [`split`](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2025-0/split.html) and [`tokenize`](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2025-0/tokenize.html) in case you want to give them a try.

---

<div class="post-metadata">

**Author:** ![urbanjost](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@urbanjost](https://fortran-lang.discourse.group/u/urbanjost)\
**Post date:** [December 9, 2024, 10:20am UTC](https://fortran-lang.discourse.group/t/advent-of-code-2024/8923/13 "2024-12-09T10:20:40Z")

</div>

I generally use fpm and  
[general-purpose-fortran](https://urbanjost.github.io/general-purpose-fortran/docs/man3.html).  
Some of the most common I/O related procedures that usually reduce the I/O to a few lines are

```plaintext
M_io:
   fileread read (ie. slurp) a file into a string array
   filebyte read (ie. slurp) a file into a character array
   get_next_char read from a file one character at a time
   getline read a line from specified LUN into allocatable string
   read_line read a line from specified LUN into allocatable string cleaning up input line
   read_table read a numeric table from a file

M_strings:
 CASE
   lower changes a string to lowercase over specified range
   upper changes a string to uppercase
 COMPARE
   glob compare given string for match to a pattern which may contain globbing wildcard characters
 EDITING
   join append CHARACTER variable array into a single CHARACTER variable with specified separator
   replace function replaces one substring for another in string
   substitute subroutine globally substitutes one substring for another in string
   transliterate replace characters from old set with new set
 TOKENS
   delim parse a string and store tokens into an array
   sep function to parse string into an array using specified delimiters
   slice parse string into an array using specified delimiters
   split parse string into an array using specified delimiters
   split2020 parse a string into tokens using proposed f2023 method
                use M_strings, only: split=>split2020
                use M_strings, only : tokenize=>split2020
 TYPE
   getvals read arbitrary number of REAL values from a character variable up to size of VALUES() array
   s2vs given a string representing numbers return a numeric array
   string_to_values read a string representing numbers into a numeric array
 WHITESPACE
   compact converts contiguous whitespace to a single character (or nothing)

```

Note that a copy of split2020 by @milancurcic is included, so if you do not have the standard  
split and tokenize you can use the USE statements shown to try them out as well. If you just  
want these functions and not the entire GPF add these lines to the fpm manifest file fpm.toml:

```toml
[dependencies]
M_strings = { git = "https://github.com/urbanjost/M_strings.git" }
M_io = { git = "https://github.com/urbanjost/M_io.git" }

```

the source contains complete man-pages as comments as well as the documentation described in the repositories. The various regular expression modules available via fpm can be very useful as well.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [December 9, 2024, 4:07pm UTC](https://fortran-lang.discourse.group/t/advent-of-code-2024/8923/14 "2024-12-09T16:07:12Z")

</div>

@jacobwilliams your `read_line` function looks like a great candidate for `stdlib` or a standalone `fpm` package. @urbanjost is your `read_line` from `M_io` equivalent? If so, then maybe `M_io` is such an fpm package. 🙂

---

<div class="post-metadata">

**Author:** ![urbanjost](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@urbanjost](https://fortran-lang.discourse.group/u/urbanjost)\
**Post date:** [December 9, 2024, 5:51pm UTC](https://fortran-lang.discourse.group/t/advent-of-code-2024/8923/15 "2024-12-09T17:51:34Z")

</div>

read\_line from @jacobwilliams and getline from M\_io are basically equivalent except a slight difference in that if an error occurs in getline the I/O message is returned as the value. the read\_line in M\_io is used in some specific codes that allow a backslash on the end to continue a long line, expands tabs, and removes DOS and GNU/Linux line terminators, etc. The General Purpose Fortran collection and the AOC module appear to have a good number of similiar functions, indicating such functionality might be good candidates for a shared library such as stdlib. Some is in stdlib already. But none of the GPF has a graphic as great as the one I see on the AOC site.

> **[GitHub - jacobwilliams/AoC-2024: Advent of Code 2024 with Modern Fortran](https://github.com/jacobwilliams/AoC-2024)**
>
> Advent of Code 2024 with Modern Fortran

😄 **Nice!**. The flames are just great.

---

<div class="post-metadata">

**Author:** ![tyranids](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@tyranids](https://fortran-lang.discourse.group/u/tyranids)\
**Post date:** [December 10, 2024, 4:51am UTC](https://fortran-lang.discourse.group/t/advent-of-code-2024/8923/16 "2024-12-10T04:51:15Z")

</div>

agreed that @jacobwilliams `read_line` is very good, but could someone verify that it works with both gfortran and ifort (or latest ifx)? I more or less copied it at work today and ifort was consistently getting stuck at the end of file. Without looking too much into it, seemed that the version of gfortran I had (4.something ancient on RHEL8) would stop reading at end of file for `advance='no'` and report `iostat_eof` but would change to `iostat=5001` if another `read` statement was encountered. The old version of ifort I had would just stay stuck at the `iostat_eof` forever and never report a bad read. Perhaps user error, but I didn’t see how a caller of `read_line` was supposed to know that a file was done other than getting a `status_ok = .false.`.

---

<div class="post-metadata">

**Author:** ![FedericoPerini](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/federicoperini/32/1750_2.png) [@FedericoPerini](https://fortran-lang.discourse.group/u/FedericoPerini)\
**Post date:** [December 10, 2024, 9:08am UTC](https://fortran-lang.discourse.group/t/advent-of-code-2024/8923/17 "2024-12-10T09:08:53Z")

</div>

> <https://github.com/jacobwilliams/AoC-2024/blob/2c203bfaa526bd3b2d23baae4ee37e29ce1190ac/src/aoc_utilities.F90#L155>

When was `size=` allowed in `inquire`!?  
That would have saved me from so much bloat over the decades 😭

It’s not even mentioned in my 2018 Metcalf-Reid-Cohen…

---

<div class="post-metadata">

**Author:** ![Carltoffel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/carltoffel/32/1680_2.png) [@Carltoffel](https://fortran-lang.discourse.group/u/Carltoffel)\
**Post date:** [December 10, 2024, 9:18am UTC](https://fortran-lang.discourse.group/t/advent-of-code-2024/8923/18 "2024-12-10T09:18:12Z")

</div>

> [@FedericoPerini](#):
>
> `inquire(file=filename, size=filesize)`

I didn’t know this, too. I just learned it exists when I asked ChatGPT about file reading a few days ago. 😅

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [December 10, 2024, 9:24am UTC](https://fortran-lang.discourse.group/t/advent-of-code-2024/8923/19 "2024-12-10T09:24:02Z")

</div>

Apparently in F2003 (9.9.1.29 in N1601 Draft document). It seems not to be present in F95 (9.6.1 Inquiry specifiers in N1191 doc).

So it has been there for quite a while already 🙂

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [December 10, 2024, 9:37am UTC](https://fortran-lang.discourse.group/t/advent-of-code-2024/8923/20 "2024-12-10T09:37:28Z")

</div>

> [@FedericoPerini](#):
>
> It’s not even mentioned in my 2018 Metcalf-Reid-Cohen…

It is. Section 12.6 The Inquire statement, page 277. 🙂

[Next page](https://fortran-lang.discourse.group/t/advent-of-code-2024/8923.md?page=2)
