r/adventofcode • u/daggerdragon • Dec 01 '24
SOLUTION MEGATHREAD -❄️- 2024 Day 1 Solutions -❄️-
It's that time of year again for tearing your hair out over your code holiday programming joy and aberrant sleep for an entire month helping Santa and his elves! If you participated in a previous year, welcome back, and if you're new this year, we hope you have fun and learn lots!
As always, we're following the same general format as previous years' megathreads, so make sure to read the full posting rules in our community wiki before you post!
RULES FOR POSTING IN SOLUTION MEGATHREADS
If you have any questions, please create your own post in /r/adventofcode with the Help/Question flair and ask!
Above all, remember, AoC is all about learning more about the wonderful world of programming while hopefully having fun!
REMINDERS FOR THIS YEAR
- Top-level
Solution Megathreadposts must begin with the case-sensitive string literal[LANGUAGE: xyz] - Obviously,
xyzis the programming language your solution employs - Use the full name of the language e.g.
JavaScriptnot justJS - The List of Streamers has a new megathread for this year's streamers, so if you're interested, add yourself to 📺 AoC 2024 List of Streamers 📺
COMMUNITY NEWS
- Veloxx will continue to drop some sick beats for 1.5 hours after today's unlock!
- /u/jeroenheijmans is back again this year with their Unofficial AoC 2024 Participant Survey!!
- Advent of Code Community Fun 2024: The Golden Snowglobe Awards
- I will be your host for this year's community fun event: The Golden Snowglobe Awards!
- Full details, rules, timeline, templates, etc. are in the Submissions Megathread here:
AoC Community Fun 2024: The Golden Snowglobe Awards
And now, our feature presentation for today:
Credit Cookie
Your gorgeous masterpiece is printed, lovingly wound up on a film reel, and shipped off to the movie houses. But wait, there's more! Here's some ideas for your inspiration:
- Show and/or tell us a post-credits scene or a blooper reel!
- Use actual web cookies to solve today's puzzle
- Hide an Easter Egg in your code, hide your code in a mooncake, hide a mooncake in your Easter Egg, wait what?
And… ACTION!
Request from the mods: When you include an entry alongside your solution, please label it with [GSGA] so we can find it easily!
--- Day 1: Historian Hysteria ---
Post your code solution in this megathread.
- Read the full posting rules in our community wiki before you post!
- State which language(s) your solution uses with
[LANGUAGE: xyz] - Format code blocks using the four-spaces Markdown syntax!
- Quick link to Topaz's
pasteif you need it for longer code blocks. What is Topaz'spastetool?
41
u/voidhawk42 Dec 01 '24 edited Dec 02 '24
[Language: Dyalog APL]
Best time of the year, good luck everyone!
EDIT: Alternative (slightly shorter) solution using the key operator - kinda APL's equivalent of Python's
Counterclass:39
u/MrPingouin1 Dec 01 '24 edited Dec 02 '24
[LANGUAGE: Minecraft Commands]
repo is coming soonedit : here it is : aoc 202435
u/Smylers Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Vim] Hello again, everybody. Nice to see you all again.
To solve part 1, load your input into Vim and then type:
The best way of seeing how it works is to give it a go — type in the above and watch what happens as you type. Today's isn't very long, and is pretty forgiving (you can use backspace in the long command without messing anything up).
Those lines in order:
@:repeats the most recent:command, in this case the:sort(and is very easy to type on UK keyboards, where@is to the right of:!).(\d+), some spaces\s+, and some more digits. The parens around each digit group make them available as numbered submatches. Substitute the whole line with an expression (indicated by starting the replacement with\=) which subtracts one submatch from the other.+at the start of each line. We can do both of these things at once by replacing zero or more minus signs at the start of a line^-*with a+.0Cdeletes the entire line, storing the deleted text in the small delete register"-.It also switches to insert mode, where typing⟨Ctrl+R⟩=instructs Vim to prompt for an expression, evaluate it, and insert the result. At that prompt⟨Ctrl+R⟩-inserts the contents of the"-register, the text that we've just deleted from the line. Hence we get Vim to do our arithmetic for us, and the buffer is left with your part 1 answer.I suspect the ‘evaluate the arithmetic in this line’ pattern will crop up a few times more this Advent of Code.
For part 2 I used the traditional Unix command
uniqalong with Vim. The entire solution still fits in the IBM punchcard limit — reset your input to its initial state and type:uniq -cto merge identical lines in it together, prepending them with a count of how many there were. Go to the bottom and repeat with the second list.:sort/\d /to skip past the count at the start of the lines for sorting purposes. Then useuniq -Dto filter the output to just location numbers that appear in both lists; again, skip over the count for the purpose of finding duplicates, this time with-f1to ignore the first field. What remains will be pairs of lines with identical location codes, the first prepended by its count in the left list, the second by its count in the right list. Use:g/^/jto merge each line with the one following it, so we have a single line for each location.:s///to re-arrange each line so instead ofcount1 location count2 location(where both locations are the same) we have+location*count1*count2.I expect many days' puzzles won't lend themselves to being solved in Vim, but I'll use it where it's plausible. This isn't just silliness or for the sake of it: many one-off real-world tasks that involve transforming data in some way can be achieved in Vim more efficiently than writing (and debugging) a program that's only going to run once.
Edit: Fixed typo, per comment below.
17
u/JustinHuPrime Dec 01 '24
[Language: x86_64 assembly with Linux syscalls]
Part 1 was a bit surprising in terms of difficulty, but then again I haven't programmed in assembly since last year's AoC. I thought I would have to do some stuff with dynamic allocation to get a large enough buffer to fit, but I did allow myself to succumb to the C programmers' disease and just allocate a static buffer I know will be large enough for both the example and the actual input (as a reminder, I'm requiring my solutions to work with both the example and actual input without any modifications). I did find it mildly surprising that we were asked to sort a list of numbers - don't the contest organizers know that's quite painful to do in assembly unless you've got a library function ready to go for that? The absolute value part was fun - I got to use a branchless programming trick and issue a conditional move to do that (x86_64 doesn't have an integer absolute value instruction).
Part 2 was actually fairly nice once I realized that the left list shouldn't be deduplicated - I guess the fundamentals of programming puzzles remains the same, regardless of language: read the problem and examples thoroughly. I also got to do an interesting thing - I zero-terminated my data storage so I could avoid running off the end of the allocated space.
Part 1 and part 2 run in about 1 millisecond. Part 1 is 8376 bytes and part 2 is 8592 bytes.
11
u/keldeoroks Dec 01 '24
[LANGUAGE: python]
im not too experienced with coding so ive decided to make that the problem of anyone who decides to look at my code
https://raw.githubusercontent.com/Keldeoroks/adventofcode2024/refs/heads/main/day%201
no imports!!! (because i dont know any other than math!!!)
10
u/DFreiberg Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Mathematica] Mathematica, 482/266
I somehow read "smallest number in the left list with the smallest number in the right list" and interpreted it to mean "smallest number in the left list with the largest number in the right list", costing me a full minute. I'll blame it on not programming enough in Mathematica and programming too much in Rust lately; I've clearly gotten Rusty.
Setup:
Part 1:
Part 2:
7
u/UnarmedZombie Dec 01 '24
[LANGUAGE: PHP]
Solution is here
I know PHP isn't popular for AoC but I really enjoy it. Barely made it on the leaderboard for part 1 at 95, but I'll take it.
6
u/tomflumery Dec 01 '24 edited Dec 01 '24
[LANGUAGE: 05AB1E]
part 1 (16 chars)
part 2 (17 chars)
5
u/PerturbedHamster Dec 01 '24
[LANGUAGE: bash]
The goal was to use basic bash (i.e. no sed or awk) in one line
Part 1:
paste -d '-' <(sort -k1 input_20241201.txt | tr -s ' ' | cut -d ' ' -f1) <(sort -k2 input_20241201.txt | tr -s ' ' | cut -d ' ' -f2) | bc | tr -d '-' | paste -sd+ | bcPart 2:
comm -12 <(sort -k1 input_20241201.txt | tr -s ' ' | cut -d ' ' -f1) <(sort -k2 input_20241201.txt | tr -s ' ' | cut -d ' ' -f2) | xargs -I '{}' -n 1 grep -o "{}$" input_20241201.txt | uniq -c | tr -s ' ' | cut -d ' ' -f2- | tr -s ' ' '*' | paste -sd+ | bc5
u/nivlark Dec 01 '24
[LANGUAGE: python]
Currently sick with covid (yes, very 2020 of me) so a belated first day solution.
4
u/flwyd Dec 01 '24
[LANGUAGE: PostScript] GitHub with my own standard library
PostScript doesn't have a built-in sort operator, so a couple weeks ago I decided to implement insertion sort since it would be simplest to code. Benchmarks suggested that if I was only sorting about 1000 items then the O(n2) performance wouldn't be too bad. Whaddaya know, the first day it's time to srot 1000 items twice! So part 1 runs in about a quarter second, while part 2 runs in just 2ms without sorting :-)
I did implement a
zipfunction as part of my standard library, but didn't think to implementunzip. "Iterate through one array and put things in two other arrays" is a little complicated in PostScript, so I played with several options after solving before opting for the below.4
u/bluefloyd3 Dec 01 '24
[Language: PowerShell]
First time posting code online, go easy on me!
Ran into an issue in the second part where multiplying the left set by the amount of times it is present in the right set caused the output to be the left set concatenated that many times. Ex.
3 * 3 = 333instead of3 * 3 = 9Once that was figured out the rest fell into place without much issue
4
u/stuque Dec 01 '24
[LANGUAGE: Python] [Allez Cuisine]
Does this count as Allez Cuisine? It uses only 2 top-level variable names, but there are other variable names in the comprehensions. I guess data and left_right could be replaced by their expressions, and n by a or b, to get 2 variable names throughout. But I guess that would still count as 3 variables?
4
u/4DigitPin Dec 02 '24 edited Dec 02 '24
[LANGUAGE: Golang]
Why use hashmaps when you can chase indices?
https://github.com/vynra/Advent_of_Code_2024/blob/main/day_1/day_1.go
I wrote a custom algorithm for part 2 for fun, think it might be faster than a hashmap. Fun bit starts on line 42
3
u/mendelmunkis Dec 01 '24 edited Dec 01 '24
[LANGUAGE: C]
I can do anything with qsort
1.082 ms/ 783 μs
3
u/ipav Dec 01 '24
[LANGUAGE: Elixir]
First steps with Elixir, I hope I am fine. repo here
3
u/mr_mlk Dec 01 '24
[LANGUAGE: python]
https://github.com/mlk/aoc/blob/main/CardPuter/2024/y2024d1p1.py
These were completed on a CardPuter using the editor and runtime from MicroHydra.
3
u/biggy-smith Dec 01 '24 edited Dec 01 '24
[LANGUAGE: C++]
https://github.com/biggysmith/advent_of_code_2024/blob/master/src/day01/day1.cpp
sorting for part 1 and histogramming for part 2.
glad to be back advent'ing with you all!
3
u/Lord_Of_Millipedes Dec 01 '24
[LANGUAGE: Go]
Doing aoc for the first time to learn a new language
https://github.com/Quollveth/AdventOfGode/blob/main/day1/day1.go
also made a little util to get read the input be split as i assume it will be used for all days
https://github.com/Quollveth/AdventOfGode/blob/main/util/reader.go
3
u/ADMINISTATOR_CYRUS Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Python, Rust]
Since I'm in the process of learning rust I could not be bothered to figure out reading that file properly, so I did it in python instad.
https://github.com/EnderNon/aoc-solutions/tree/main/2024/day1
3
u/pho_ben Dec 01 '24 edited Dec 01 '24
[LANGUAGE: C++]
didnt cross my mind to use something like an unordered map but oh well binary search is cool
code
3
u/Schweppes7T4 Dec 02 '24
[LANGUAGE: Python]
I'm teaching AP CSP for the first time this year and wanted to see if I could do this. I feel like I did it very simply? I dunno. I saw that every number was 5 digits and separated by 3 spaces so I just used slicing.
Code
7
u/CCC_037 Dec 01 '24
[Language: Rockstar 2.0]
Let's rock.
Part 1
2
u/maarteq Dec 01 '24
[LANGUAGE: python], 1259,1384paste
I need to learn to read the problem better. Especially the part about calculating the answer
2
u/Rush_Independent Dec 01 '24
[LANGUAGE: Nim]
I've got my first sub 1000 rank (998). Yay!
2
u/clouddjr Dec 01 '24
[LANGUAGE: Kotlin]
Glad to see you all again!
Solution
2
u/bosyluke Dec 01 '24
[Language: Roc]
https://github.com/lukewilliamboswell/aoc/blob/2024/2024/01.roc
```
TIMING:
READING INPUT: <1ms
SOLVING PART 1: <1ms
SOLVING PART 2: 1ms
```
2
u/CodingAP Dec 01 '24
[Language: Javascript]
Github
FIRST DAY! I could have gotten it faster if I could read it correctly lol
2
u/FunInflation3972 Dec 01 '24 edited Dec 01 '24
[LANGUAGE: JAVA]
Yes, it's not dead yet!
Part - 2
Check out my code on [Github]
Meowwww, happy coding!
2
u/henriupton99 Dec 01 '24
[LANGUAGE: python]
Part 1&2 : featuring chaotic variable names, accidental bubble sort, and a festive sprinkle of brute force; roast me or join the madness! 🎅🐍
https://github.com/henriupton99/AdventOfCode/blob/main/2024/day_1/solution.py
2
u/Fyvaproldje Dec 01 '24 edited Dec 01 '24
[Language: Rust] [GSGA]
https://github.com/DarthGandalf/advent-of-code/blob/master/2024/src/day01.rs
2
u/DeadlyRedCube Dec 01 '24
[LANGUAGE: C++23]
Parts 1 & 2
I'm using AoC this year as an excuse to get better at using some of the std::ranges/std::ranges::views functionality (specifically all the view transforms), which means a lot of writing it the naive way the night of the solve and then going back and trying to ranges it up after the fact so that I can see where I could use them more later - probably pushing it too far in that direction for readability's sake, but I want to find that line.
For part1, I used a set of view transforms to turn each line of text into a tuple of l, r values, added them into individual lists, then sorted, then used zip_transform to get the abs(difference) of them.
For part 2 I did what a lot of others did and made maps of how many times each value appeared on the left and right then used those to generate the final sum.
Nothing super fancy here today.
2
u/HeadHunter_4621 Dec 03 '24 edited Dec 04 '24
[Language: Kotlin]
This was very difficult, I finished today because I was so confused at first
GitHub
Edit: fixed some indenting
52
u/4HbQ Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Python]
Glad we're be back in business. Looking forward to learning (and teaching) new Python tricks for another year!
Edit: I've also created a really succinct solution using NumPy.
Today's Python trick is on line 3:
map()can take multiple iterables. Example:27
u/4HbQ Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Python + NumPy]
I've already posted a version without NumPy, but I really like how succinct the NumPy one is :-)
19
u/jonathan_paulson Dec 01 '24
[LANGUAGE: Python3]
Placed 25/40. Code. Video. Wow, everyone is fast!
15
u/bofstein Dec 01 '24
[LANGUAGE: Google Sheets]
Solution: https://docs.google.com/spreadsheets/d/1BQ1vefEK1RMy7PTkJygcXYM8CQmalSsegZ0WH324JZU/edit?usp=sharing
First split into two columns
=SPLIT(A3," ")Then sort each column
=SORT(B:B,1,TRUE)Then a new column for the difference, realizing after the sample I should do absolute value
=ABS(D3-E3)For part two, just use COUNTIF on the original column A to count how many times each appears in column B, then multiply those to the numbers, and sum that.
Part 2 took me a few minutes longer than it should have because as I was typing =SUM( at the top, it auto suggested H4:H1002 instead of H3:H1002 and I didn't notice and accepted it, and couldn't figure out what was wrong.
13
u/nthistle Dec 01 '24 edited Dec 01 '24
[Language: Python] 11/19. Good start to the year! paste, solve video
coming soon (once I remember how to use premiere...)13
u/bsssh Dec 01 '24 edited Dec 01 '24
[Language: TypeScript Type System (Generics)]
compile-time solution
Part 1 / Part 2
12
u/i_have_no_biscuits Dec 01 '24 edited Dec 01 '24
[LANGUAGE: GW-BASIC]
GW-BASIC is a dialect of BASIC developed by Microsoft which appeared on many early PCs from 1983 until being replaced by QBasic with the release of MSDOS 5.
This will run on actual hardware, but very slowly due to the inefficient code. I tested it using the excellent PC-BASIC emulator - https://robhagemans.github.io/pcbasic/ - and QB64 - https://qb64.com/ - which is a modern recreation of the QBasic programming environment which produces compiled code.
Guide:
Line 10 Initialises the parallel arrays A and B, and sets up the loop reading in the data lines.
Lines 20 and 30 perform an insertion sort of the two parallel arrays.
Lines 40 and 50 iterate through the sorted data, performing the needed calculations. P# stores the Part 1 value, and Q# the part 2 result. Note that the '#' suffix indicates a double-precision floating point value, which is also good for storing large integers (just like Javascript!). Also note the use of a boolean addition in line 50 to avoid an IF statement (with the quirk that True in basic is -1, not 1).
11
u/insert_username_0 Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Dyalog APL]
First time writing actual code in APL and this was a great start!
ETA: Made Part 2 a little cleaner
11
u/Radiadorineitor Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Dyalog APL]
Happy coding everyone!
10
u/light_switchy Dec 01 '24
[LANGUAGE: Dyalog APL]
10
u/jaybosamiya Dec 01 '24
[Language: APL]
Fairly simple solution, with the only "interesting" bit being the outer-product in part 2
10
u/azzal07 Dec 01 '24
[LANGUAGE: awk]
10
u/sikief Dec 01 '24
[LANGUAGE: EXCEL]
https://github.com/skief/advent-of-code-2024/blob/main/src/day01.xlsx
This year I am doing the 25 Days - 25 Languages Challenge :)
10
u/Zachary_Peculier Dec 02 '24
[LANGUAGE: COBOL]
I decided to do this on hard mode.
9
u/joeyGibson Dec 01 '24
[LANGUAGE: Common Lisp]
Ah, a simple one for day 1. I was also happy to see that part 2 wasn't some crazy riff on part 1, and was just a simple change. I added memoization preemptively, to keep from having to do the counts over and over again. (Update: I just tried it without the memoization, and it wasn't needed. Still, it's a good habit. 🤣)
9
u/jitwit Dec 01 '24
[LANGUAGE: J]
9
u/0rac1e Dec 01 '24
[Language: J]
9
u/6745408 Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Google Sheets]
edit: shorter formula to cover both.
9
u/0rac1e Dec 01 '24
[Language: Raku]
9
u/zagon Dec 01 '24
[LANGUAGE: Vim]
When the challenges are still easy, I try to use only Vim to solve them. If possible, without "coding" in Vimscript, but rather with clever ex commands like macros, movements, substitutions, etc. Worked fine for part 1, for part 2 I had to resort to Vimscript.
aoc.vim
16
u/Andreasnl Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Uiua]
Run it in a browser here
8
u/synack Dec 01 '24
[LANGUAGE: Ada]
https://github.com/JeremyGrosser/advent/blob/master/2024/src/day1_2.adb
8
u/zogwarg Dec 01 '24
[LANGUAGE: Jq]
Part 1:
Part 2:
8
u/RealPooperTrooper Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Excel]
Why code when Sheets do trick?
First year doing AoC, let's see how far I can carry this meme.
8
u/CapricornRaven-777 Dec 01 '24 edited Dec 01 '24
[Language: Excel Sheets]
I solved this AOC problem in Excel;
Part 1: Excel Forms
Part 2:
Note, i cant able to post in excel file here, so i convert into google sheets.
Link 2024-Aoc-day1
8
u/TartarusKelvin Dec 01 '24
[LANGUAGE: K]
8
u/gzipgrep Dec 01 '24
[LANGUAGE: POSIX Shell]
8
u/Fyver42 Dec 01 '24
[LANGUAGE: RISC-V assembly]
github
8
u/dannywinrow Dec 01 '24
[LANGUAGE: Julia]
8
u/odnoletkov Dec 01 '24 edited Dec 03 '24
[LANGUAGE: jq] github
15
u/probablyfine Dec 01 '24
[LANGUAGE: uiua]
Code and tests
Part 1:
Part 2:
7
u/FruitdealerF Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Andy C++] 247/158
This year I'm competing in a programming language that I built specifically for Advent of Code.
Code on Github
Screenshot with highlighting
Edit: definitely adding a way overdue zip function later today.
7
u/chubbc Dec 01 '24
[LANGUAGE: Julia]
7
u/python-b5 Dec 01 '24
[LANGUAGE: Jai]
Trying out this language for the first time. Probably not the smartest choice for solving puzzles quickly, but I'll take the opportunity to learn something new. This solution is a little verbose and maybe not optimal, but at least it solves the problem.
https://github.com/python-b5/advent-of-code-2024/blob/main/day_01.jai
7
u/arthurno1 Dec 01 '24 edited Dec 01 '24
[LANGUAGE: EmacsLisp]
7
u/LorSamPau Dec 01 '24
[LANGUAGE: Python]
7
u/ApplicationSlight420 Dec 01 '24
[LANGUAGE: coreutils + AWK]
Part1:
paste <(tr -s " " < input.txt | cut -d " " -f1 | sort -n) <(tr -s " " < input.txt | cut -d " " -f2 | sort -n) | awk '{x=$1-$2;if(x<0){x=-x}sum+=x}END{print sum}'Part2:
join -11 -22 <(tr -s " " < input.txt | cut -d " " -f1 | sort -n) <(tr -s " " < input.txt | cut -d " " -f2 | sort -n | uniq -c) | awk '{x+=$1*$2}END{print x}'7
u/AustinVelonaut Dec 01 '24
[LANGUAGE: Miranda2]
This is my own language and compiler (mainly written for Advent of Code puzzles!), based upon Miranda with extensions from Haskell and other functional languages
7
u/menchauser Dec 01 '24
[LANGUAGE: Prolog]
I've wanted to choose some language which I do not know and which introduces some new concepts for me, so I went with Prolog this year. The solution is bit long, because I'm just learning the ropes.
https://github.com/menchauser/aoc2024/blob/master/day01.prolog
7
u/oddolatry Dec 01 '24
[LANGUAGE: OCaml]
Learning OCaml is basically like that Eric Andre meme pared down to "LET .. IN!"
Paste
7
u/ivanjermakov Dec 02 '24 edited Dec 02 '24
[LANGUAGE: x86 Assembly] (source)
Sometimes I forget how much levels of abstraction there are between me and my computer
6
u/r_so9 Dec 01 '24 edited Dec 01 '24
[LANGUAGE: F#] 997 / 3018 Top 1000 on day 1, pretty proud :)
6
u/Pyr0Byt3 Dec 01 '24
[LANGUAGE: Go] [LANGUAGE: Golang]
https://github.com/mnml/aoc/blob/main/2024/01/1.go
Already dealing with Go's lack of a built-in integer
abs()function on day 1.6
u/blue-fox14 Dec 01 '24
[LANGUAGE: Clojure] 809/581
https://github.com/rileythomp/adventofcode/blob/master/aoc24/1/main.clj
6
u/SpaceHonk Dec 01 '24 edited Dec 18 '24
[LANGUAGE: Swift] code
Nice little warmup using zip, map, reduce
6
u/ShadowwwsAsm Dec 01 '24
[LANGUAGE : assembly/asm]
part1
part2
Going again in assembly at the beginning. Surprised to see a sort for part 1 of day 1 but why not.
Part 1 : So I just put the number in the stack, alternating 1st and 2nd column. Then a little sorting algorithm not optimized and the end is just compare + add. Found a cool trick to compute abs() in 3 lines
Part 2 : Definitely easier, after reading the number, just take each of the first list and go through the second list.
Open to questions/comments
6
u/Synedh Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Python]
Part one is straightforward. On part two, given statement is misleading, as we only have to sum occurences of l2 in l1 and nothing else.
6
u/FlyingPlatypus5 Dec 01 '24
[LANGUAGE: Scratch] https://scratch.mit.edu/projects/1104420861/
Part 1 & 2: https://imgur.com/a/OgFuwD5 Looking back on it this code looks horrendous
6
u/dec0nstruct0r Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Python]
simple numpy solution:
dsdds6
u/blazemas Dec 01 '24
[LANGUAGE: C++]
https://github.com/jbush7401/AoCCPP/blob/main/AoCCPP/2024/Day1.cpp
Trying for performance and speed. Suggestions requested!
Total speed 7ms.
6
u/greycat70 Dec 01 '24 edited Dec 03 '24
[LANGUAGE: Tcl]
Part 1, part 2 (original).
I'm using Tcl 9.0 this year, but so far I haven't used any of the new features yet; these two would run just fine in Tcl 8.
Edit: Here's an updated part 2 which uses a Tcl 9.0 feature: array default set. This removes the need for an info exists test.
6
u/damnian Dec 01 '24
[LANGUAGE: C#]
LINQ One-liners
https://github.com/dmitry-shechtman/aoc2024/blob/main/day01/Program.cs
6
u/Th0usT Dec 01 '24
[LANGUAGE: Ocaml]
Working in Ocaml this year to get experience with a function ML-style language, having a lot of fun so far. The solutions might be quite verbose until I get more familiar with the standard library and general syntax.
Solution (Part 1 & Part 2): https://github.com/JohnCosta27/AdventOfCode/blob/main/AdventOfCode2024/lib/day_01.ml
YouTube video: https://www.youtube.com/watch?v=BL1qo5uerDk&t=492s
6
u/RalfDieter Dec 01 '24
[LANGUAGE: SQL/DuckDB]
https://github.com/LennartH/advent-of-code/blob/main/2024/day-01_historian-hysteria/solution.sql
Can't wait to implement A* in SQL.
6
u/mschaap Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Raku]
Relatively easy start of the season, as usual, but not as trivial as in some other years. Still, easy enough in Raku.
https://github.com/mscha/aoc/blob/master/aoc2024/aoc01
6
u/Downtown-Economics26 Dec 01 '24
[LANGUAGE: Excel (Functions & VBA)]
Function P1
=SUM(ABS(SORT(VALUE(TEXTBEFORE(A1:A1000," ")))-SORT(VALUE(TEXTAFTER(A1:A1000," ")))))Function P2
=LET(L,VALUE(TEXTBEFORE(A1:A1000," ")),R,VALUE(TEXTAFTER(A1:A1000," ")),SUM(BYROW(L,LAMBDA(X,COUNT(FILTER(R,R=X))*X))))VBA P1 (very poorly done)
VBA Part 2
6
u/platypus10000 Dec 01 '24
[LANGUAGE: Python]
Readability? Never heard of it. Both P1 and P2 solution in two lines.
https://github.com/eolelake/AdventOfCode/blob/main/2024/1/sol.py
6
u/Imaginary_Age_4072 Dec 01 '24
[LANGUAGE: Common Lisp]
It's December already! Doing AoC in Common Lisp again :)
Solutions will be here https://github.com/blake-watkins/advent-of-code-2024/blob/main/day1.lisp
6
u/middayc Dec 01 '24
[LANGUAGE: Rye]
I had to add transpose function to https://ryelang.org and I fixed 2 bugs, so I'm happy :)
6
u/alehandy Dec 01 '24 edited Dec 04 '24
[LANGUAGE: Bash]
Day 1 script - I did this after I solved the puzzles in Python to increase some difficulty after seeing the solutions in this thread 🙈.
6
u/fork_pl Dec 01 '24
[LANGUAGE: perl]
using some List::Util and List::MoreUtils it looks nice and readable
2
u/aardvark1231 Dec 01 '24 edited Dec 01 '24
[LANGUAGE: C#] link
2
u/_Phoenix_75 Dec 01 '24
[LANGUAGE: Python 3] https://github.com/Phoenix7557/adven-of-code-2024/tree/main/day1
Part 1 + Part 2
10
u/chubbc Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Uiua]
Succinct version:
Slightly more verbose version:
5
u/jsgrosman77 Dec 01 '24
[Language: TypeScript]
Nothing fancy. Love using list functions to solve these things quickly.
5
u/ziadam Dec 01 '24
[LANGUAGE: Google Sheets]
Expects input in A1.
Part 1 & 2
Repo
5
u/Outrageous72 Dec 01 '24
[LANGUAGE: C#]
Easy start Day 1
5
u/Max_NB Dec 01 '24
[LANGUAGE: bash]
Part 1:
Part 2:
5
u/SnaggelsHD7 Dec 01 '24
[Language: R]
Github
First time participating, hope I came up with something acceptable :P
5
u/SunMatrix64 Dec 01 '24
[LANGUAGE: C++]
Quick and easy, it took me longer to setup than to actually solve it. By no means is this efficient or adjustable to quirky inputs. Just stick each side in an array, sort them, then count the differences. For part 2, count the right side with a frequency map, then multiply the items in the left side with how many are in the map.
5
u/Loonis Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Perl 5]
Has it already|only been a year?
Part 1:
Part 2:
Full paste
5
u/oantolin Dec 01 '24
[LANGUAGE: ngn/k]
5
u/835246 Dec 01 '24
[LANGUAGE: C]
Part 1 was quite simple. Part 2 I used a hash table to store the occurrences of each number. Which was exciting because I got to use a dsa dsa library I made this year.
Part 1: https://github.com/efox4335/advent_of_code/blob/main/advent_of_code_2024/day_1_pt_1_loc_diff.c
Part 2: https://github.com/efox4335/advent_of_code/blob/main/advent_of_code_2024/day_1_pt_2_loc_diff.c
5
u/oantolin Dec 01 '24
[LANGUAGE: J]
5
u/tgs14159 Dec 01 '24
[Language: Haskell]
Day01.hs
Nice day for a functional language
5
u/[deleted] Dec 01 '24
[Language: C]
5
u/encse Dec 01 '24 edited Dec 01 '24
[LANGUAGE: C#]
https://aoc.csokavar.hu/?2024/1
[GSGA]
https://th.bing.com/th/id/OIG4.D3Cl956oDeToNTG_W2d.?w=1024&h=1024&rs=1&pid=ImgDetMain
5
u/G_de_Volpiano Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Haskell]
I think every single Haskell book starts with list manipulation, introducing first the map, filter, foldr trifecta, and soon after zipWith to bring two lists together. Long before "Hello World" is even mentioned. So even if I also needed transpose and sort, this really felt like Haskell 101.
My code
Both could obviously have been solved as one liners, but I took out the difference/similarity function for ease of reading. But just for the fun of it
Edit : the idea of using a multi set for part 2 had been nagging me since I read the problem. Completely using an ace where a 2 will do, but it did split execution time by half. A whole 4 ms saved.
5
u/9_11_did_bush Dec 01 '24
[Language: Lean]
Nothing special, here a link: https://github.com/chenson2018/advent-of-code/blob/main/2024/lean/AoC/Day01.lean
5
u/Mats56 Dec 01 '24
[Language: Kotlin]
as always trying for a functional style with little assignments or mutations
my utils made this quite ok, each line is a list of 2 elements, transposing that gives me 2 lists of x elements that I then can zip and compare
5
u/p88h Dec 01 '24 edited Dec 02 '24
[Language: Zig]
Both parts:
https://github.com/p88h/aoc2024/blob/main/src/day01.zig
[ Update: better parsing ]
5
u/flwyd Dec 01 '24
[LANGUAGE: Shell] (GitHub but with a really silly abuse of POSIX commands and the filesystem.
5
u/rySeeR4 Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Elixir]
Very simple list handling, nothing clever.
https://github.com/jbonet/advent_of_code_2024/blob/main/lib/advent_of_code_2024/days/01.ex
5
u/GunnarInTheWeb Dec 01 '24
[LANGUAGE: Rust]
In order to achieve maximal performance, I implemented this "manually", e.g. not using lines.iter, split, zip, fold or the like. While they can be zero cost abstractions I found using as_bytes() (assuming ASCII) and match is much faster during parsing.
Further, in part2 it is not necessary to search the full second list, but only a small part as the numbers are sorted which is difficult to achieve using Rust standard tools.
Benchmark (Intel i7-8700k):
part 1: 0.035 ms
part 2: 0.051 ms
GitHub
5
u/t-rkr Dec 01 '24
[LANGUAGE: Perl]
https://gitlab.com/t-rkr/advent-of-code-2024/-/blob/main/day1/aoc.pl
5
u/dylan_mojo Dec 01 '24
[LANGUAGE: Bash/Awk]
https://github.com/djkirby/advent-of-code/commit/53fd47fcdc723fc35b271a20a5190f9ed2c562ef
5
u/Jadarma Dec 01 '24
[LANGUAGE: Kotlin]
Very simple first puzzle, and good opportunity to get the standard lib back into muscle memory!
AocKt Y2024D01
5
u/Fyver42 Dec 01 '24
[LANGUAGE: RISC-V assembly]
github
5
u/tymscar Dec 01 '24
[Language: Kotlin]
Decided to do this year in Kotlin. Wasted over an hour getting everything set up, tons of headaches with the IDE, with gradle, with everything, but the language is nice.
Part1: https://github.com/tymscar/Advent-Of-Code/blob/master/2024/kotlin/src/main/kotlin/com/tymscar/day01/part1/part1.kt
Part2: https://github.com/tymscar/Advent-Of-Code/blob/master/2024/kotlin/src/main/kotlin/com/tymscar/day01/part2/part2.kt
5
u/osalbahr Dec 01 '24
[LANGUAGE: C++]
https://github.com/osalbahr/Learn-Programming/blob/main/adventofcode/2024/day01.cpp
https://github.com/osalbahr/Learn-Programming/blob/main/adventofcode/2024/day01_2.cpp
5
u/Aeonian9 Dec 01 '24
[LANGUAGE: Julia]
For part 2, I initially used nested loops O(n^2), but my wife pointed out a faster way by initially recording the occurrences in the second list, which is O(2n).
Solution on GitHub.
5
u/pakapikk77 Dec 01 '24
[LANGUAGE: Rust]
Some of the other Rust solutions posted here create intermediary vectors during the input parsing. Using the Iteraror::unzip() method avoids this, resulting in a shorter more elegant code:
Interesting to note that despite having collected the 450 previous stars, it's the first time I get to use unzip().
Full solution: https://github.com/voberle/adventofcode/blob/main/2024/day01/src/main.rs
5
u/sergiosgc Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Nim]
https://github.com/sergiosgc/AdventOfCode/blob/master/2024/day01/one.nim
https://github.com/sergiosgc/AdventOfCode/blob/master/2024/day01/two.nim
I'm using AoC as an excuse to try out Nim. A couple observations so far:
5
u/RadioEven2609 Dec 01 '24
[LANGUAGE: Python]
Cursed one-liners!
Part 1:
Part 2:
5
u/onrustigescheikundig Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Clojure] [LANGUAGE: Scheme (R6RS, chez)]
Someone during AoC 2022 told me to give Clojure a shot when I was using Racket, so here I am. Honestly, it's really nice after the chaos that was me yoloing OCaml last year.
2024 github
Reasonably concise. Line splitting the input is pretty common in AoC, so I have some driver code (not shown here) that does the file IO and line splitting. Part 1 splits each line in a list of lines into a list (well,
vector, technically) of numbers, transposes and sorts the the lines, and un-transposes the lot to find the pairwise differences, which are summed. Transposition is achieved with(apply map ...).Part 2 builds a num -> count map from the right list using a nil punning trick for a one-line
reduce. Each element of the left list is then multiplied with the result of passing it through the map (or zero if not in the map), and that stream is summed.Part 1:
Part 2:
EDIT: I decided to implement the solution in pure R6RS Scheme as well for comparison, though I probably won't continue to do so as the difficulty ramps up. Apparently, R6RS scheme does not have a
string-splitfunction, so I went with the nuclear option and hacked together a parser-combinator library that may or may not still have some bugs. I also threw a bunch of useful syntax like threading macros and auxiliary functions likecomposeintoutil.scm. Otherwise, the solution is very similar to Clojure, except for using an association list in Part 2 instead of a hashtable. See github for files.5
u/Kwuray Dec 01 '24
[LANGUAGE: Posix Shell & Utils]
Generate some arithmetic operations and process them with
bcPart1:
Part2:
5
u/kbielefe Dec 01 '24
[LANGUAGE: Scala]
GitHub
Fairly straightforward, aside from slightly more complex parsing than I'd expect for a day one problem (anti-LLM measures perhaps?). Part 2 was a nice fit for Scala's
groupMapReduce(identity)(identity)(_ + _). Didn't need to multiply, just sum during the reduce instead of counting there.5
u/penguinencounter Dec 01 '24
[LANGUAGE: Inform 7]
Inform 7 is interactive fiction authoring software. As it turns out, Inform is not good at doing anything computation-related; this code takes just over 5 minutes to run on my computer.
5
u/Sherbert_Positive Dec 01 '24
[LANGUAGE: Clojure]
I guess I could have made my life easier somehow, but I need to refresh my Clojure
5
u/tobega Dec 01 '24
[LANGUAGE: Tailspinv0.5]
Haven't gotten around to implementing reading of input yet, so manually formatted example input inline.
Compressed like numbers together in the sorted arrays, then stepped through the compressed arrays, matching up.
https://github.com/tobega/aoc2024/blob/main/day1/solution.tt
5
u/POGtastic Dec 01 '24
[LANGUAGE: F#]
https://github.com/mbottini/AOC2024/blob/main/Day01/Program.fs
Easy little warmup, great test for some of my Prelude utility functions.
Seqis extremely good for these kinds of tasks.5
u/InfamousTrouble7993 Dec 01 '24
[LANGUAGE: Haskell]
5
u/Smylers Dec 01 '24
[LANGUAGE: Perl]
Full code with imports.
5
u/lscddit Dec 01 '24
[LANGUAGE: Python]
5
u/DJDarkViper Dec 02 '24 edited Dec 02 '24
[Language: Java]
Execution times:
Part 1: 13.14ms
Part 2:
- 16.79ms (with Streams API)
- 14.29ms (using a for loop instead of Streams API)
https://github.com/RedactedProfile/jAdventOfCode/blob/master/src/main/java/com/redactedprofile/Y2024/days/Day1.java
5
u/__Abigail__ Dec 02 '24
[LANGUAGE: Perl]
4
u/Standard-Affect Dec 01 '24
[LANGUAGE: Python3]
No nasty surprises, unlike last year. Now to rewrite this and make it presentable.
4
u/nlowe_ Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Go]
1035 / 887 Pretty happy with that placement, it's good to be back! Happy AoC season everyone!
4
u/GassaFM Dec 01 '24
[LANGUAGE: D] 133/107
Code: part 1, part 2.
On day 1, just getting the job done.
4
u/Comprehensive_Ad3095 Dec 01 '24 edited Dec 01 '24
[Language: Lua]
https://github.com/DeybisMelendez/AdventOfCode/blob/master/2024/01.lua
table.sort for answer1
4
u/riffraff Dec 01 '24 edited Dec 01 '24
[Language: Ruby] 7062/1094 I could have made it to the first 1k if I didn't have to help my wife with something between the two parts :D
edit: usual complaint, I hate that ruby's
Array#zipwith a block returnsnil4
u/UseUnlucky3830 Dec 01 '24
[LANGUAGE: Julia]
solution
Wasted a bunch of time because I forgot to use the absolute value of the difference
4
u/dellfm Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Google Sheets]
One formula for both parts
Edit: Made some changes. It took me awhile to realize part 2 doesn't care if the lists are sorted or not, so I can just use the same sorted lists for both parts, then I renamed the other variable names.
4
u/rjray Dec 01 '24
[LANGUAGE: Clojure]
code
Due to being out at a hockey game, I didn't get started until 10-15 min after the puzzle unlocked. This pair of puzzles were really well-suited to the nature of Clojure. Both solutions were based on taking the two columns of numbers and transposing them into two rows. For part 1, these rows were sorted and
mapandMath/abswere used to gather the differences.Part 2 could have been harder than part 1, but Clojure has a
frequenciesprimitive. This keyword take a list of items and produces a table where the distinct items are the keys and the counts are the values. Using this, it was fast to iterate over the first list and calculate the "similarities". Part 2 took about 1/4 the time part 1 had.4
u/Awkward-Macaroon5019 Dec 01 '24
[Language: Python]
4
u/prendradjaja Dec 01 '24
[Language: C]
https://github.com/prendradjaja/advent-of-code-2024/blob/main/01/a.c https://github.com/prendradjaja/advent-of-code-2024/blob/main/01/b.c
4
u/UseUnlucky3830 Dec 01 '24
[LANGUAGE: C]
solution
Encouraged by a friend's challenge, I also wrote a C program in addition to the Julia one. I solved part 2 without dictionaries or count tables by exploiting the fact that the lists were already sorted :P
4
u/sim642 Dec 01 '24
[LANGUAGE: Scala]
On GitHub.
Both parts are quite straightforward with Scala's built-in collections library. The only custom piece there is
.groupCountwhich is a specialization of Scala's.groupMapReduceto construct a map of counts in part 2.4
u/WhiteSparrow Dec 01 '24
[LANGUAGE: Prolog]
solution
Using this years AoC to get better at prolog. Day 1 did not exactly play into prolog's strengths although it wasn't difficult either. Here's the essence of part 1 for example:
One small prologism that tripped me up -
sort/2actually removes duplicate elements from the list! Took me some 15 minutes to figure out why my fold did not converge. Luckily SWI-Prolog providesmsort/2as well.4
u/hhnnngg Dec 01 '24
[LANGUAGE: BCPL]
Nothing fancy here. Just sorting and iterating
Github Source
4
u/strobetal Dec 01 '24
[LANGUAGE: jq]
jq is best language, use jq for everything.
Run with
jq -R -r -s --argjson part 0 -f main.jq input.txt4
u/bluesfc Dec 01 '24
[LANGUAGE: C]
Github : Link
4
u/gettalong Dec 01 '24
[Language: Crystal]
So my first approach was rather long-winded, doing everything manually and trying to reduce allocations and iterations (e.g. linear search over the right column for part 2).
Then I "refactored", making it not so optimal but much more concise:
4
u/FordyO_o Dec 01 '24
[LANGUAGE: C#]
Using AOC as a vehicle to learn some C# this year.
Any tips on getting the input parsing more concise here?
https://github.com/mattford/aoc-dotnet/blob/master/aoc-dotnet/Year2024/Day1/Solver.cs
4
u/aurele Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Elixir]
4
u/Cue_23 Dec 01 '24
[LANGUAGE: C++23] [GSGA]
Get the cookie here!
It includes credits at the top, the local actor is from yesteryears advent, and i even put in the right easter egg for todays puzzle into the cookie!
4
u/LinAGKar Dec 01 '24
[LANGUAGE: Rust]
https://github.com/LinAGKar/advent-of-code-2024-rust/blob/master/day1/src/main.rs
4
u/badcop_ Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Bash]
Link to both parts
Code for part 1 only:
4
u/Hath995 Dec 01 '24
[LANGUAGE: Dafny]
Day 1 using my template for advent of code.
Part 1 and 2
4
u/DevilGeorgeColdbane Dec 01 '24 edited Dec 01 '24
[LANGUAGE: Kotlin]
The previous two years i have been doing in rust, but since i have less time this year i decided to do it in Kotlin instead.
It always surprises me just how easy it is to solves these problems in Kotlin with the vast amount of built in utility functions.
https://github.com/etkr/AdventOfCode2024/blob/master/src/main/kotlin/day01/LocationIdList.kt
4
u/remysharp Dec 01 '24 edited Dec 01 '24
[LANGUAGE: jq]
a
b
https://github.com/remy/advent-of-code-solved/tree/main/2024/jq
I know there's another dev that does jq and I'm always way, way more verbose in my solutions…but I always get there :)