this post was submitted on 01 Dec 2024
2 points (100.0% liked)

Advent of Code

295 readers
1 users here now

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

https://adventofcode.com

founded 2 years ago
MODERATORS
2
2024 / DAY 01 - Solutions (self.adventofcode)
submitted 1 month ago* (last edited 1 month ago) by isti115 to c/adventofcode
 

Feel free to share your solutions or browse others' for inspiration! Please tag your comments with the language you solved in to make it easier to search for specific languages in case we happen to get more comments than expected. πŸ˜€ I really hope that we can create some discussion here to liven up this community!

top 2 comments
sorted by: hot top controversial new old
[–] isti115 2 points 1 month ago* (last edited 1 month ago)

#FSharp https://github.com/isti115/advent-of-code/tree/master/2024/day-01

I usually choose to learn a new language each year using these great little puzzles, this time it's FSharp. This naturally means that my solutions will be poorly written at first, since I'm not yet familiar with the language and make do with what I have. For example I'm pretty sure that there should be a better way to parse today's input instead of this monstrosity:

let pairs = lines |> Seq.map (fun l -> Regex.Matches(l, "(\d+)\s+(\d+)"))

let numpairs =
    pairs |> Seq.map (Seq.head >> _.Groups >> Seq.tail >> Seq.map (_.Value >> int))

let numtups = numpairs |> Seq.map (fun p -> (Seq.head p, p |> Seq.tail |> Seq.head))

~~Also, I was pretty surprised that I couldn't find the unzip function for sequences. I would've expected that to be present after using some other functional languages, such as Haskell and Scala. πŸ€”~~

Edit: Scratch that, I just need to convert the sequence into a list first... πŸ€¦β€β™‚οΈ It actually makes complete sense. https://fsharp.github.io/fsharp-core-docs/reference/fsharp-collections-listmodule.html#unzip Also, I have managed to clean up the parsing a little bit.

[–] mykl 1 points 1 month ago

For entertainment purposes only, I'll be trying a solution in Uiua each day until it all gets too much for me...

$ 4   3
$ 2   5
$ 1   3
$ 3   9
$ 3   3
βŠœβˆ˜βŠΈβ‰ @\n     # Partition at \n.
⊜(β†βˆ΅β‹•)βŠΈβ‰ @\s # Partition at space, parse ints, sort.
/+/(⌡-)     # Get abs differences, sum.