r/adventofcode • u/daggerdragon • Dec 02 '25
SOLUTION MEGATHREAD -❄️- 2025 Day 2 Solutions -❄️-
OUR USUAL ADMONITIONS
- You can find all of our customs, FAQs, axioms, and so forth in our community wiki.
AoC Community Fun 2025: R*d(dit) On*
24 HOURS outstanding until unlock!
Spotlight Upon Subr*ddit: /r/AVoid5
"Happy Christmas to all, and to all a good night!"
— a famous ballad by an author with an id that has far too many fifthglyphs for comfort
Promptly following this is a list waxing philosophical options for your inspiration:
- Pick a glyph and do not put it in your program. Avoiding fifthglyphs is traditional.
- Shrink your solution's fifthglyph count to null.
- Your script might supplant all Arabic symbols of 5 with Roman glyphs of "V" or mutatis mutandis.
- Thou shalt not apply functions nor annotations that solicit said taboo glyph.
- Thou shalt ambitiously accomplish avoiding AutoMod’s antagonism about ultrapost's mandatory programming variant tag >_>
Stipulation from your mods: As you affix a submission along with your solution, do tag it with [R*d(dit) On*!] so folks can find it without difficulty!
--- Day 2: Gift Shop ---
Post your script solution in this ultrapost.
- First, grok our full posting axioms in our community wiki.
- Affirm which jargon via which your solution talks to a CPU
- Format programs using four-taps-of-that-long-button Markdown syntax!
- Quick link to Topaz's Markdown (ab)using provisional script host should you want it for long program blocks.
- What is Topaz's
past*tool?
36
Upvotes
14
u/Andreasnl Dec 02 '25 edited Dec 03 '25
[LANGUAGE: Uiua]
Regex might have been smarter but this also works.
5
u/justjarvo Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Ruby]. I am not good at math, so string manipulation it is 😅 I wouldn't mind being pointed to some material to help me figure out how to approach this without all the string work.
5
u/aimada Dec 02 '25 edited Dec 03 '25
[LANGUAGE: Perl]
5
u/CCC_037 Dec 02 '25 edited Dec 02 '25
[Language: Rockstar]
[R*d(dit) On*!] - no Arabic numerals anywhere
One of the downsides of Rockstar is that your code is never short
part one
4
u/laughlorien Dec 02 '25 edited Dec 03 '25
[LANGUAGE: Pico-8 Lua]
The PICO-8 runtime has a single numeric datatype: 16:16 fixed-point signed decimals, which are not quite big enough to support arithmetic on the numbers in this problem (indeed, the solution to my own input is larger than a 32-bit int can hold, so we can't even be clever and cram everything in using the fractional part). So, unfortunately, I needed to implement an arbitrary-precision integer type to support arithmetic on values larger than 32,000. Thankfully, we only need addition and less-than-or-equal to solve this problem, so I put off complicated operations like subtraction and multiplication until later days.
I also discovered that the VM will crash with a confusing error message if a
flip()gets triggered while you're dispatching to a metamethod (or, at least,__tostringin particular), so it's important to occasionally callflip()yourself while doing heavy computation (ideally at something very close to 30fps, although I didn't bother to do any tuning with today's solution) to avoid that.solution cartridge: https://git.sr.ht/~nmh/advent-of-code/blob/trunk/pico8-2025/day2.p8
4
u/Cute-Document3286 Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Zig 0.15]
Should be well optimized (part one is ~1μs and part two is ~9μs) https://github.com/gabrielmougard/AoC-2025
4
u/Maximum_Expression Dec 02 '25
[LANGUAGE: Elixir]
Benchmarks:
Solution:
https://github.com/akolybelnikov/advent-of-code/blob/master/2025/elixir/lib/day02.ex
The huge memory print in Part 2 is a trade-off between the digit-based solution vs the slower string-based one I first tried (about 3 x slower) :(
3
u/ricbit Dec 02 '25
[LANGUAGE: Python]
Both parts can be made with regexp, for the first part you have "^(\d+)\1$" (capture some digits and have one copy of that at the end; for the second part "^(\d+)\1+$" (capture some digits and have at least one copy at the end).
3
u/-Enter-Name- Dec 02 '25 edited Dec 03 '25
[LANGUAGE: Python]
my [[unprofessional language removed]] expecting much larger ranges but at least part 1 is lightning fast (about half a millisecond)
code
3
u/lost_in_a_forest Dec 02 '25 edited Dec 03 '25
[Language: uiua]
Day 2 of learning uiua. Runs just about instantly, which the first versions did .. not.
paste
3
u/snicklys Dec 02 '25
[LANGUAGE: Python]
Solution
Good attempt, was overthinking it at first, albeit a brute force. Could someone direct me to a better solution? Will definitely look into this more.
3
u/Prof_Farnsworth1729 Dec 02 '25
[Language: Javascript]
[R*d(dit) On*!]
Initially I struggled to choose a glyph as most seemed either trivial or impossible (without resorting to JS level). Eventually I settled on an equals sign, which basically pushed me down a functional path.
Being stopped from using arrow functions also meant that that I had to use old-school functions which reminded me how much I love function hoisting.
And no this is not an efficent solution by any definition but I like how readable it is
3
u/Fenomorf Dec 02 '25 edited Dec 02 '25
[LANGUAGE: python]
newbie, don't yell at me
solution
3
u/gisikw Dec 03 '25 edited Dec 03 '25
[LANGUAGE: Forth]
A few low-hanging optimizations, but it's on the brute-forcey side. Runs quick enough though, and fun to get to learn a new language.
Parts 1 & 2
3
u/4HbQ Dec 03 '25 edited Dec 03 '25
[LANGUAGE: Python], [R*d(dit) On*!]
This is my submission that fits today's Community Fun constraint, fully avoiding this annoying fifth glyph of our ABC. Its basis is my original solution, I only had to adapt that top import function to allow working with a string of binary digits, and program a variation on a for-loop using string multiplication and a manual plusplus.
Nothing too outlandish or hard to grasp, I think!
2
u/guiambros Dec 02 '25 edited Dec 02 '25
[Language: Python]
I solved p2 first by mistake, and was puzzled why 565656 wasn't an invalid ID for p1. Once I realized the issue, it was a buy one get one free 😂
*EDIT: moved code to github.
Part 1 and 2
2
u/msschmitt Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Python 3]
Part 1. Part 2
This went fast.
No attempt at a fancy algorithmic solution, this is just brute force. For part 2, the strategy was extract the first 1 character sequence, duplicate it length of id times, then compare to the id. Then extract the first 2 character sequence, duplicate it length of id/2 times, compare to the id. And so on.
2
u/DeadlyRedCube Dec 02 '25 edited Dec 02 '25
[Language: C++]
Parts 1 & 2
Wanted to do this as ints instead of strings, so counted the number of decimal digits and then used division/modulo to test the digits. Not honestly sure if it's faster this way or if it would have been faster in string world, but it runs in ~156ms (single-threaded on a Core i7-8700K), which is an order of magnitude slower than I'd like ...I haven't figured out any optimizations yet.
(Edit: I tested a string version and it was slower (280ms) so that's good to know 😅)
Edit 2: I've created an updated version of Parts 1 and 2 that flips the problem inside-out and runs in 2.42ms instead of 156, which is much more what I was hoping for
Basically, instead of testing every value within each range (some of which are quite huge), it calculates the minimum/maximum repeated values for each range (for instance for a 2s repeat in the range `561562-594935` the minimum repeated value is `562562` and the maximum is `594594`) and then adds every found value from there (`562562, 563563, 564564,...`) to a set (to avoid duplicates in the part 2 solution)
This tests *way* fewer values than iterating over each value in the range and runs a lot faster. Yay!
2
u/Markavian Dec 02 '25
[LANGUAGE: JavaScript]
https://github.com/johnbeech/advent-of-code-2025/blob/main/solutions/day2/solution.js
Somewhat over engineered solution for Day 2. In the Part 1 solution I added lots of extra error validation code to check for non-numeric strings, which turned out to be unnecessary, so that all got deleted in the Part 2 solution. Rather than refactor, I just made it WET and smashed in a regex. Made one typo (
$1instead of\1for match replacements) ... and off to the product code verification races!2
u/Fyvaproldje Dec 02 '25 edited Dec 03 '25
[LANGUAGE: Python] [R*d(dit) On*!]
Th* bad glyph is call*d in my runnist, so I had to workaround that too
link
2
u/dblackwood_q Dec 02 '25
[LANGUAGE: R]
Feels good to have achieved the result using only my brain after yesterday's Part 2 defeat.
https://github.com/denbogdan/AoC_2025/blob/main/day_2/day_2.R
2
u/DevoidSloth Dec 02 '25
[Language: Python]
https://github.com/DevoidSloth/Advent-Of-Code-2025/tree/main/day2
Brute force solution. Somewhat slow but fun regardless!
2
u/simplegecko Dec 02 '25
[Language: Golang]
Brute forcing solution
New to challenges like this and Go in general, so brute forcing it is
2
u/Key_Pack_9630 Dec 02 '25 edited Dec 02 '25
[Language: C] Somewhat pleased with myself for figuring out how to do this without storing any numbers or iterating over IDs. Happy that I could use the Moebius function for inclusion/exclusion. #include <stdio.h>
2
u/5stripe Dec 02 '25
[LANGUAGE: Python]
Part 2
2
u/LokimsG Dec 02 '25
[LANGUAGE: Python]
https://github.com/locksmithg/AdventOfCode2025/blob/master/day2.py
Had a great idea to solve it using factors of the length of the number. Ran 4 times faster than my first solve.
2
u/lucariomaster2 Dec 02 '25
[Language: C++]
This code stinks, but it was a fun challenge! I'm placing the extra restriction on myself that I'm not allowed to use any standard libraries other than iostream and the occasional bit of data type conversion (int to char* using sprintf, mostly). Part 1 took about 1 second and part 2 about 3 seconds on my machine.
Part 1
Part 2
Strings utility
2
u/randomginger11 Dec 02 '25
[LANGUAGE: Python]
Nothing terribly special with my implementation, but should be fairly easy to understand.
The only little optimization I did for part 2 was to only check substrings of a length that is a factor of the length of the string being checked ... I think I wrote that right.
Basically if the string you're checking is 9 characters long, you only need to check if the 1st digit repeats, then if the first 3 digits repeat, and that's it.
I stored these factors in a small hard coded dict since I could see no numbers would have more than, say, 12 digits.
Code on Github
2
u/bnberg Dec 02 '25 edited Dec 03 '25
[LANGUAGE: go]
Well, i am not a developer, but i try making stuff in go from time to time! So here is my solution in Golang!
2
u/willkill07 Dec 03 '25 edited Dec 14 '25
[LANGUAGE: python-cupy] [R*d(dit) On*]
https://github.com/willkill07/AdventOfCode2025/blob/main/day02.py
Day 2 of GPU programming :)
An
avoid5utility library avoids "fifthglyph" (that symbol that sits amid "D" and "F" in our ABCs)2
u/No-Present8648 Dec 03 '25
[LANGUAGE: Swift]
https://github.com/rickyjonesus/advant_calendar-2025/blob/main/Day2/Sources/part-2.swift
2
u/doodlebug80085 Dec 03 '25 edited Dec 03 '25
[Language: Swift]
Not my best code (usually try to combine parts 1 and 2 but could not be bothered) but I got to use a computed property in part 1 which was cool! Semi-improved brute force in part2, so I'll take it. Today was a long day just glad I got it done lol.
Part 1
Part 2
2
u/atweddle Dec 03 '25
[LANGUAGE: Rust]
Part 1: Rust - 468 ns.
Part 2: Rust - 18 µs.
In part 2, for increasing numbers of digits to group by, I generated a multiplier of the form:
For 1 digit: 11, 111, ...
For 2 digits 101, 10101, ...
For 3 digits: 1001, 1001001, ...
And so on.
I generated this multiplier based on the number of digits in the starting number in the range, but then updated it for each extra length of number up to the ending number in the range.
Multiplying a group of digits by the multiplier will then repeat those digits the required number of times.
2
u/CrumblingYourSoul Dec 04 '25
[LANGUAGE: Zig]
Day 2
27
u/4HbQ Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Python] 6 lines.
Most of my solving time was spent relearning regex syntax. But boy is it nice here!
For part 1, just add up the numbers matching
^(\d+)\1$. Short explanation: we want to match the start of the string^, followed by a group of one or more digits(\d+), followed by that same group\1, followed by the end of the string$.For part 2, we match on
^(\d+)\1+$: mostly the same, but instead of matching\1exactly once, we'll match it one or more times using\1+.Putting it al together yields this:
4
u/wc_nomad Dec 02 '25 edited Dec 02 '25
[Language: Rust]
I took a bit of inspiration from others to make things more functional, but dam, Rust is pretty. here is the meat for part 2.
https://github.com/imcnaugh/AdventOfCode/blob/main/2025/day_2/src/main.rs
16
u/fenrock369 Dec 02 '25 edited Dec 02 '25
[LANGUAGE: rust]
No brute force, and showing p2 only (but p1 is similar maths)
This was fun, I immediately started playing around with ranges rather than regex, and glad I did as it proved to be some interesting maths. My idea was that we're trying to generate a number from smaller blocks such that:
Thus k is the d-length block, repeated r times. This can be broken down into a simple formula:
It's easiest to think of simple cases like "12" repeated twice in "1212", so 1212 = 12 * 100 + 12, where d=2, r=2.
This simplifies (it's a geometric progression) to:
i.e. every n with repeated blocks we want to find matches this formula.
We do have some constraints on k, which will help us find its range: k is a d-digit number bound between:
for example, with the range (1000,2000), and d=2 (for 2 digit lengths of blocks), k (from the formula above) is between 101 and (102 - 1), i.e. 10 and 99. That's kind of obvious from the range numbers, but we can narrow it down a lot more, as clearly 99 is way too high.
The next constraint is:
f is simple to calculate, it was given above as "(10{dr} - 1) / (10d - 1)"
So we just find the range for k from the 2 constraints we have, and loop over all d,r such that d*r <= digits of upper value, compute the "f" value to give us a very small range.
The additional complexity is there may be dupes (e.g. "111111", "111"x2, "11"x3, "1"x6), so we collect the candidates, unique them, and then sum them as required. part 1 is just a special case where we fix r=2, and the maths turns out a bit easier.
On my PC, the 2 solutions run in 21 microseconds.
15
u/badcop_ Dec 02 '25
[LANGUAGE: bash]
did some golfing today on stream, this is what we ended up with
8
u/jonathan_paulson Dec 02 '25
[LANGUAGE: Python]. My times: 00:02:38 / 00:03:57. Video of me solving.
Brute force again. Just try all possible numbers. In part 1, we try splitting each number in half, and in part 2 we try all factors of the number's length (and, in keeping with the brute force theme, compute those factors in the simplest way possible). This actually takes a few seconds for part 2, but that's tolerable.
8
u/MizardX Dec 02 '25
[LANGUAGE: Rust]
Github
Brute force and some modulo.
1.52ms / 3.11ms
7
u/K722003 Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Python] GitHub, simple regex with back reference. P2 is just adding one positive closure to the same P1 regex lmao.
Cleaned up code:
7
u/YenyaKas Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Perl]
Straightforward solution with backreferences and modern
for-cycle iterating over multiple values. Got caught by the example actually being wrapped to multiple lines, d'oh. My part 2 solution (part 1 differs only with the second regex having only\1instead of\1+):7
u/JustinHuPrime Dec 02 '25
[LANGUAGE: x86_64 assembly]
Part 1 exposed a gap in my library of commonly used function - I was missing an
sprintfequivalent. So I made one. And then a quick bit of string comparison CISCness and I could find out if the two halves of the string were the same. (To elaborate further, x86_64 has a whole bunch of surprisingly complex string-manipulation instructions that let you write loops to operate on strings pretty concisely. I pieced togetherstrncmpout ofloopeandcmpsb- although this isn't the way strncmp is usually implemented - that's with vectorized instructions that need a good amount of special case handling.)Part 2 was mildly scary, until I figured out how I could decompose the check into two nested loops and conditionals. Then it was just a mild mess of loop invariants and slightly unstructured control flow. It still involved CISCy string comparisons, though. I will admit that the control flow I come up with is somewhat hacky, although I suspect a compiler with good basic block layout and double-jump elimination would probably produce control flow just as tangled.
Part 1 runs in 35 milliseconds and part 2 runs in 47 milliseconds. Part 1 is 9,216 bytes and part 2 is 9,448 bytes when linked into an executable. I'm not sure exactly why it's so slow, but I suspect it's because the CISC string instructions are horribly expensive at the microcode level. I'm going to keep using them, though - they're just too convenient.
6
u/Boojum Dec 02 '25
[LANGUAGE: Python]
Much quicker for me to finish both parts compared to last night - no real edge cases to worry about. I did spend a little time at first verifying that the total length of the ranges was small enough to brute-force.
My main trick here is using
itertools.batched()to chunk the strings, and thenlen(set())==1to check if they're all identical.6
u/maneatingape Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Rust]
Solution
Simple but slow (40ms) brute force check of all ids.EDIT: Really neat numeric only solution that doesn't convert ids to strings. First we build a ordered set of invalid ids for each possible combination of digits length and repeat. For example for a 4 digit number there are 90 possible combinations of repeat 2 (1010 to 9999).
The we simply sum the ids that are contained within each range which is practically instant. Building the set takes most of the time (1ms).
EDIT 2: I had some time to think at work and came up with a better approach. I manually computed the ranges and overlap for all numbers up to 10 digits. This means that we don't need to de-duplicate using a
HashSet(as this has already been handled). These are then divided into 3 sets, (a) part 1, (b) part 2 and the overlap between (c) part 1 and part 2. The part two result is then a + b - c.Improves the time to just 1µs.
6
u/synack Dec 02 '25
[LANGUAGE: Ada] Part 1 Part 2
Spent 20 minutes fussing over off-by-one errors in part 2. Surely there's a better way...
6
u/blacai Dec 02 '25
[Language: F#]
Maybe not the most efficient one... but it does the job around ~0.5sec :)
https://github.com/blfuentes/AdventOfCode_AllYears/tree/main/AdventOfCode_2025/day02
6
u/Czh13 Dec 02 '25
[LANGUAGE: Python] 4 lines
Instead of relying solely on regex, I went for a different bit of Python magic:
itertools.batched. For part 2, add up the numbersiwhere for somen, the following is true:The batched function chops up the input id
iin chunks of sizen. Thensetgets rid of duplicates. If only a unique chunk remains, the number must be a repeat of that chunk.6
u/ivanjermakov Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Zig] Part 1: 11μs, Part 2: 13μs
Couple of tricks:
id % 11 == 0, yielding 11, 22, 33, etc. For part 2 it's a bit more involved, since we need to check for patterns of varying length. For my input, at most I needed three modulo divisors to catch all invalid IDs with same digit count. Another example, 123123 can be catched with divisor 1001, 222222 with divisor 10101, and so on.<=start. Then I just iterate in multiples of divisor until out of current range.6
u/azzal07 Dec 02 '25 edited Dec 02 '25
[LANGUAGE: awk]
6
u/Akari_Takai Dec 03 '25 edited Dec 03 '25
[LANGUAGE: Rust]
(Solution)
Oh, Advent of Code, how I missed you!
I tried to get the best big-O complexity here. For
nranges andmbeing the largest number in that range:O(n * log m)(runs in ~600 ns on my machine)O(n * log^3 m)(runs in ~5 µs on my machine)So even for arbitrarily large ranges, it's quite fast!
Part 1:
Doublets are exactly numbers of the form
z = m || m.If
mhasddigits:z = m * 10^d + m = m * (10^d + 1).So, for each possible
d(half the total length):mult = 10^d + 1valid seeds
min range[L, R]are:low = max(10^(d-1), ceil(L / mult))high = min(10^d - 1, floor(R / mult))mand multiply bymult.Complexity is effectively
O(#ranges * #digit_lengths).Part 2:
For total length
lenand periodp | lenwith2p ≤ len:k = len / prepeatsN = seed * multiplier(p, k)wheremultiplier(p, k) = 10^{0·p} + 10^{1·p} + ... + 10^{(k−1)·p}seedis ap-digit integer without leading zeros, and I find the allowedseedrange in[L, R]exactly like in Part 1, and store the arithmetic progression sums insum_by_period[p]However, we run into the issue of duplicates. To get the sums of numbers with primitive period exactly
p, we use Mobius inversion:Then I sum
primitive_sum_by_period[p]over all valid periodspfor thatlen.Complexity is effectively
O(#ranges * #digit_lengths * #divisors * #divisors).5
u/LtHummus Dec 02 '25
[Language: Clojure]
I'm continuing my quest to do each puzzle this year in a different language and I decided to do Clojure for tonight's because a couple friends of mine are huge fans. I've also never written Clojure in my life and I haven't written anything in any Lisp-like language since 2008. I skimmed some Clojure documentation this evening and got some pointers from said friends (mostly around threaded macros) and I got lucky that most of the challenge tonight was regular expressions which are (mostly!) the same in any language.
Clojure is super fascinating though and I'm definitely going to check it out some more after Advent of Code is over. What language will I use for tomorrow? Who knowssssssssssssss
here's some first-timer Clojure code
5
u/ComradeMorgoth Dec 02 '25
[LANGUAGE: Python]
I misread the part 1 and initially solved part 2 by luck...
Part 1:
Part 2:
5
u/dijotal Dec 02 '25
[LANGUAGE: Common Lisp]
By inspection, the max number of digits in any rangewas 10. Generate all "invalid" possibilities and check which ranges they're in -- just bookkeeping.
Part 2 runtime ~0.05 seconds on a Mac M3 laptop.
5
u/Radiadorineitor Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Dyalog APL]
Part 2 had me debugging for a while, but in the end I managed. I even used the new operator behind (⍛) added in v20
5
u/flwyd Dec 02 '25
[LANGUAGE: Z shell] (on GitHub)
Continuing my theme this year of “glue languages that might already be sitting on your computer” I opted for zsh when I saw that the puzzle would involve doing numeric comparisons and string manipulation on the same variable. When I started coding I aimed for “generate a series of substrings and repeat them” rather than “iterate through all numbers in the range and see if they’re invalid,” though I did spend some time on my phone on Caltrain and Muni figuring out whether I zsh could do backrefs in globs (answer: I think so, but not worth it). I ended up with a solution that worked for the sample but was too low on my actual input, so I spent tens of minutes manually inspecting the numbers I generated to identify miscounts.
In part 2 it felt easier to knock out an “iterate the whole range” solution (mostly because I wouldn’t have to think about “did I already add this one”), though that ended up with a lot of missing edge cases. That took a minute and a half to run, The final solution, which generates prefixes to repeat and stores matches in an associative array, takes slightly longer than a second (with all the back and forth between strings and integers I’m not surprised). I learned several things about zsh today, including the
repeatbuiltin. Didn’t try for the Red(dit) One challenge though, since the first thing my program needs to do isread.5
u/BxW_ Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Python]
I don't think there can be a better approach. The time complexity for each ID range is O(log10(last / first) * log10(last)). For example, here is the solution for 1-1e120
Part 1: 495495495495495495495495495495495495495495495495495495495495540950040950040950040950040950040950040950040950040950040949540950040950040950040950040950040950040950040950040950040950
Part 2: 495495495495495495500445990545000446485496000391040545995392965461386848479115459408315906484948416067500862646063014497011409626847601515085801560973681802001168626306656553137840
Ideas:
[first, last]that can only be divided into shards no. of same parts. The main idea is if first and last have the same no. of digits, and we defineshard_len = digits(first) / shards. Let shard_first and shard_last be the prefixes of first and last with length = shard_len. For example, first=442972, last=608499, shards=2, shard_len=3, shard_first=442, shard_last=608. We know that[shard_first+1, shard_last-1]will be invalid IDs in [first, last] when repeated shards no. of times, e.g., 443443, 444444, 445445, ..., 606606, 607607. We only need to confirm for the boundary ones. We can do it by finding if first <= (shard_first repeated shards no. of times) <= last, and similarly for shard_last. Now, suppose we have gotten the range [442, 607], and we want 442442+443443+...+607607. We can write it as:(442*10^(shard_len*1) + 442*10^(shard_len*0)) + (443*10^(shard_len*1) + 443*10^(shard_len*0)) + ... + (607*10^(shard_len*1) + 607*10^(shard_len*0)). This can be simplified as(442+443+...+607) * sum(10^(shard_len * i) for i in range(0, shards)).Edit: Silly bug in summing up the answers at silver += and gold += part.
5
u/RazarTuk Dec 02 '25 edited Dec 02 '25
[LANGUAGE: LOLCODE]
paste
Once again, I still need to do Part 2. But some backstory (which is vaguely more appropriate for yesterday's thread). Last year, day 13 was mathematical enough that I decided to attempt it in Intcode. And while my solution was way too inefficient to even attempt to run it on part 2, I still managed to implement Cramer's rule for a system of linear equations in 2 variables despite dealing with a RISC. This year, I decided to use LOLCODE as a "Who would ever expect that to show up in a solutions thread?" language. But I also discovered that it's actually surprisingly fully-featured, even if it's extremely obtuse about it, like how today's solution even includes a rudimentary array class, which tracks its own length. So now my goal is to keep using it for as long as possible.
EDIT: Oh, and have a link to the comment where I solved something in Intcode
EDIT: I have every reason to believe part 2 would have worked. (As in it at least worked on the sample input) However, it ate up so much memory on the real input that I had to force restart my laptop, so I just caved and used Java + regexes.
5
u/SheepiCagio Dec 02 '25
[LANGUAGE: Excel]
P1:
P2:
5
u/Silly_Emu4915 Dec 02 '25
[LANGUAGE: Python]
Part 1:
Part 2:
Cursed one-liners.
5
u/[deleted] Dec 02 '25 edited Dec 02 '25
[Language: Awk]
4
u/[deleted] Dec 02 '25
[LANGUAGE: Rust]
String parsing matters a lot in AoC from what I've seen, thats why I tend to stay away from C++ because Rust has basically everything I need (and more).
Part 1 (198144µs):
So the first part was easy and it took me about 5 minutes, I wrote a seperate function that converted the input as a string, chopped it in half and checked if both strings were identical.
Part 2 (630282µs):
I came up with the idea fairly quickly, however, it took some time for me to implement since I also wanted to learn the functionality of Rust. All in all it was a fun problem!
Solutions: https://github.com/Elyrial/AdventOfCode/blob/main/src/solutions/year2025/day02.rs
4
u/munchler Dec 02 '25 edited Dec 02 '25
[LANGUAGE: F#]
For part 2, I got a little confused about a repeated chunk size of 1 being invalid (e.g. 999) vs. the entire string in a single chunk being valid (e.g. 1234). It's funny the things that can send me into a spiral.
4
u/yieldtoben Dec 02 '25 edited Dec 02 '25
[LANGUAGE: PHP]
PHP 8.5.0 paste
Execution time: 0.2035 seconds
Peak memory: 0.4976 MiB
MacBook Pro (16-inch, 2023)
M2 Pro / 16GB unified memory
4
u/AtomPhys Dec 02 '25
[LANGUAGE: q]
Very clunky and brute forced, but will iterate on it at a later time. Went for string comparison, but likely there's a less hungry solution
4
u/dellfm Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Google Sheets]
This is only for Part1, I don't know if I'm going to do Part 2 honestly because this one is already slow. It lags so much on my laptop with just this part
Edit: After thinking about it for a bit, I could also add an IF check to see if start and stop's length are both odd, and if so skip the query.
4
u/Ok-Builder-2348 Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Python]
Part 1
Part 2
Took a while to craft the non-brute force solution for part 2. The idea was as follows:
Overall, was fun to see the whole solution come together.
4
u/voidhawk42 Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Dyalog APL]
Late to the party this year, but having fun! :)
This solution doesn't determine repeating groups through string parsing - instead, we do some math to generate a matrix of all possible repeating group values in order, where each row corresponds to how many repeating digits there are (2 repeating, 3 repeating, etc.). We only have to generate these up to some constraints determined by the largest number in the input. So if the largest number is
9899037441(10 digits), we know we only need to generate a 9 row matrix (2-10 repeating digits) with however many columns we need to get to that maximum.Given that, for each range in the problem input we can use binary search (interval index) to determine which of our generated numbers are in range. Part 1 does this with just the first row of the matrix (2 repeating digits), part 2 does it with a vector of the unique matrix values. Runs in about half a second on my Macbook.
4
u/janiczek Dec 02 '25
[LANGUAGE: awk]
4
u/kekqqq Dec 02 '25
[LANGUAGE: SQL]
part1
part2
4
u/0rac1e Dec 02 '25
[Language: J]
Brute force... like almost everybody else, it seems.
4
u/pdxbuckets Dec 02 '25 edited Dec 03 '25
[Language: Rust; Kotlin]
It took me a while and it's not short or elegant code. I'm relatively happy with the performance. 40μs for Rust and 8ms for Kotlin (277μs warmed up).
I rewrote my Rust solution to use two custom Iterators. The first cycles through all the invalid numbers in the range that share the same length (my parse splits ranges so that the low and the high are the same length). But it can only manage one kind of slice at a time. The other composes two versions of the first iterator with different slice lengths to facilitate deduping without having to sort or hash. Saved a whopping 5μs over my original version.
4
u/Carighan Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Kotlin] 44 lines
This was a fun one to utterly brute-force. I got to learn that Kotlin has a
regionMatchesfor part 1, and while I could have done both parts with regexes, the chunk-repeat idea was kinda cute as a brainfart so I did that instead.4
u/JWinslow23 Dec 02 '25
[LANGUAGE: Python]
Solution writeup
Code (GitHub)
My first thought was to use
itertools.batched, and that's what was in my code once I hit "Submit". But if you really think about it, this is fundamentally about a digit string matching a pattern (namely, "repeats itself twice (or more)")...so to filter the product IDs down to the ones we need to sum, we can just use regexes!Part 1 pattern (
^(.+)\1$), with the sample dataPart 2 pattern (
^(.+)\1+$), with the sample data(Side note: My time was 9:05 today. I didn't beat yesterday, but I still beat every other one of my previous times.)
4
u/RudeGuy2000 Dec 02 '25
[LANGUAGE: Racket]
https://raw.githubusercontent.com/chrg127/advent-of-code/refs/heads/master/2025/day2.rkt
for some reason i REALLY wanted to use call/cc today. (and i do not know regex)
4
u/lboshuizen Dec 02 '25
[Language: F#]
Git
Generate candidates first was a major speed up, distinct in part2 did the job
4
u/Diligent-Bus-1192 Dec 02 '25
[LANGUAGE: R]
4
u/Hath995 Dec 02 '25
[LANGUAGE: Dafny]
https://github.com/hath995/dafny-aoc-2025/blob/main/problems/2/Problem2.dfy
I accidentally figured out the correct predicate for part 2 first and then realized part 1 was simpler. Dafny's existential operator has made short work of several advent of code problems.
4
u/thesoundofbibimbap Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Python]
Day 2 solution:
https://github.com/olganowak/advent_of_code/blob/79e308ca19838e36020b0ff7e6d57eea5a0759d7/2025/02/solve.py
4
u/thedrj0nes Dec 02 '25
[LANGUAGE: InterSystems ObjectScript / MUMPS]
Part one is nice and easy, just split in two, see if it's the same, I kind of guessed where part 2 might go.
For part two, I had three solutions, each is single threaded and essentially brute force, the one shared is the quickest at <3.25 seconds to run.
Using 123123123 as example:
I could make it quicker by being a bit more elegant of a method. Maybe I will revisit after I complete the 12 puzzles if I find the time.
I could dispatch each range to a number of range processing jobs and go multi-thread, but I'm trying to avoid that until I really need it. Part of my plan is show "real-world" MUMPS code, not much of the legacy code-base I see in my day-to-day work is multi-threaded.
Day 2
4
u/woond3r Dec 02 '25
[Language: OCaml]
https://github.com/KacperKopiec/advent-of-code/blob/main/2025/day2/part2.ml
No Regex, pure numbers
4
u/sk01001011 Dec 02 '25
[LANGUAGE: Odin]
Part 1
I'm only pasting the first part because I did both parts brute force first, then realized something and tried on part 1.
Basically I realized that we can count the number of relevant ID's without going through numbers. Here's kinda the gist of it:
Let's say the range is 1000 - 2220. We find the first good ID which is 1010. Then we count how many 0101's we can stick in between 1010 - 2220, which is (2220-1010)/0101 = 11. (+ 1 edge) -> 1010, 1111, 1212, 1313, 1414, 1515, 1616, 1717, 1818, 1919, 2020, 2121.
Then the sum is
(1111 + 1212 + 1313 + ...)= (1111 + 1111 + 1111 + ...) + (0101 + 2*0101 + 3*0101 + ...)= (1111*count) + (0101)*(count*(count+1)/2)With this, for each range of numbers the amount of times we loop is the digit difference of range ends (/2). I think this was pretty cool.
4
u/Aggressive_Okra_6821 Dec 02 '25
[LANGUAGE: Python ]
part 1:
part 2:
4
u/marx38 Dec 02 '25
[Language: Lean]
https://github.com/mfornet/advent-of-code-2025/blob/main/AdventOfCode2025/Day2.lean
4
u/danvk Dec 02 '25
[Language: Haskell]
https://github.com/danvk/aoc2025/blob/main/y2025d02/Main.hs
I'm using this year's AoC to learn Haskell so feedback is much appreciated.
4
u/IlluminPhoenix Dec 02 '25
[LANGUAGE: Rust]
Was really fun to optimize. Brought both parts down to around 10μs, using some math properties.
Overall pretty clean solution, but had this bad edge case where I had to subtract the counts from the 6 repetition numbers since number like:
222222 would get counted on 2 reps (222_222) and 3 reps (22_22_22). Only happens at 6 digits with this input but with larger inputs it might happen more.
Fun one to solve!
solution (47 lines)
4
u/siddfinch Dec 02 '25
[LANGUAGE: Free Pascal]
Part 1 and Part 2
Over-documentation is the rule for this year. A bit slow on my Raspberry Pi III with 1Gb.
4
u/lojic-tech Dec 02 '25 edited Dec 02 '25
[Language: Python]
4
u/ssnoyes Dec 02 '25
[LANGUAGE: MySQL]
4
u/Odd-Blackberry-7236 Dec 02 '25 edited Dec 03 '25
[Language: Typescript]
The most readable Typescript solution out there:
4
u/Seffylocks Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Python]
I was getting ready to deal with Part 2 being something like "Find IDs with repeating digits ANYWHERE in the number (e.g. 925259)", but the actual thing was easier.
4
u/Far_Sweet_6070 Dec 02 '25
[LANGUAGE: Ajla]
https://www.ajla-lang.cz/downloads/examples/advent-2025/02.ajla
https://www.ajla-lang.cz/downloads/examples/advent-2025/02-2.ajla
4
u/dantose Dec 02 '25
[LANGUAGE: Powershell]
I struggled a but with variable types while trying to use a..b style expansion, eventually gave up on that and just looped it. The [long] actually isn't necessary.
Part 1
Part 2: Only change is a single '+' in the regex in the last line.
4
u/dzecniv Dec 02 '25
[LANGUAGE: Common Lisp]
part 2 was short… but naive with string manipulation and slow (20s), sigh.
src
4
u/kristianhassel Dec 02 '25
[LANGUAGE: Midio]
Midio is a visual language I'm building, and I decided to try it out for advent of code this year. Didn't have enough time to do this one "properly", so I ended up brute forcing it. Not super happy about that, but at least I got to stress test the interpreter a bit.
Code: https://github.com/midio/advent-of-code-25/blob/main/day2.midio
Screenshots in readme: https://github.com/midio/advent-of-code-25
4
u/srugh Dec 02 '25
[LANGUAGE: Ruby]
Shared parsing, other wise part 1 (compare halves of string) and part 2 (compare chunks to full string) both independently solved.
GitHub: Day-02 parts 1 and 2
4
u/sondr3_ Dec 02 '25
[LANGUAGE: Haskell]
Not very happy with my solution again today, it works but feels needlessly complicated and slow, but it is what it is. Ironically I have had a bug in my program where it fails on my actual input in part 2 but not the sample input both days. A bit frustrating.
And code
4
u/jaank80 Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Powershell]
Takes less than 5 seconds to execute.
4
u/light_switchy Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Dyalog APL]
Part 1:
Part 2:
4
u/Wrong-Signature-4778 Dec 02 '25
[LANGUAGE: Bash]
Part1
Part2
Not much of a difference between part1 and part2 due to using Regex. First part was a head-scratcher though. Mainly for these reasons:
grepon individual number in for loop is slower than printing all numbers andgrepover themseqgives scientific notation for bigger numbers (never encountered before)shellcheckwarnings took a while, but that is my own doing4
u/jhandros Dec 02 '25
[LANGUAGE: Python in Excel]
In A1: puzzle input
4
u/baboonbomba Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Elisp]
https://github.com/adibozzhanov/aoc2025/tree/main
I'm doing a polyglot challenge with a different language every day. I chose 12 languages and randomized their order. You can see all of them in the github readme. My last day will be bash...
Today was elisp. I hated every second of writing elisp. Don't write elisp.
Tomorrow is Elixir. I've never seen a single line of elixir code in my life. But surely it cannot be worse than elisp...
4
u/h2g2_researcher Dec 02 '25
[LANGUAGE: C++]
Problems like today's make me feel smart, as I'm able to throw some maths ability at it, instead of trying to brute force it. I tried to explain as much of the maths in the comments as possible, but the key realisation was that a key
ABABcan always be divided by101;ABCABCis always a multiple of1001, andABABABis always a multiple of10101. That way I could work out start and end ranges forKey / 1001(for example), and then use maths to sum all the values in that range using a few multiplies and adds, instead of looping over them. I can then get the sum of the actual keys back by multiplying by1001again.Part 2 was a little tricker because I had to avoid double-counting.
ABABABABwould be counted when I check for length 4 sequences AND length 2 sequences, and because my method has no idea what the actual bad IDs are (I don't think, at any point, any invalid ID is ever in a register) I had to tweak to account for that.There is some framework to parse the inputs, and I leave them as strings until the last possible moment so I don't have to mess around with
log10calls at any point.Solution is here.
Some bits not included in the paste:
utils::small_vectoris a vector with a small stack buffer I implemented several years back when an AoC was spending 96% of its time allocating and deallocating length 2 vectors on the stack.utils::split_string_at_firstdoes what it says, splitting at a delimiter. I have a bunch of useful string-manip functions, some of which have been put into the standard library by now.utils::to_valuewrapsfrom_charsbut gives me a return value which I prefer.AdventDayis an enum that comes from my framework, so I can tell whether I'm in a part 1 or part 2.utils::int_rangecan be used to iterate over0,1, etc... It has some nice features for setting a stride, or going backwards, or slightly unusual things which protect me from some sillier errors. But I like using it pointlessly too.AdventCheckis either anassertor an optimisation hint, depending on what mode I'm compiling in.AdventUnreachable()is now just a wrapper aroundstd::unreachable. But I've been doing AoC since before that existed in standard C++ so it used to be a bunch of#ifdefs around various compiler intrinsics.4
u/StafDehat2 Dec 03 '25
[LANGUAGE: BASH]
Pt2 solution. Pt1 is the same, but without the last "+" in the regex.
4
u/JWinslow23 Dec 03 '25
[LANGUAGE: Bespoke]
Code (GitHub Gist)
I decided to challenge myself, and I programmed this in my own esoteric programming language called Bespoke, where the word lengths decide the commands. To give myself an extra challenge, I made the program itself a description of the day's puzzle prompt. (But I don't think I'm up for the r/AvoidE challenge, sorry!)
The general idea behind the algorithm is one that some folks here have pointed out: the invalid IDs will be multiples of numbers like 111, 10101, 1001, 111111, etc. I found a way to systematize the generation of those numbers, and I test various ones against each possible ID number based on how many digits it has.
Here's a disclaimer, verbatim from my program (and yes, this is executable as code):
4
u/argentcorvid Dec 03 '25
[Language: Common Lisp]
I parsed the ranges to lists of start/end pairs, then looped through them (for both parts).
for part 1, I used LOG base 10 (rounded up) to get the number of digits, then used FLOOR with a divisor of 10^(#digits/2) to get one value that is the left half of the number and the second returned value (the remainder) is the right half of the number.
part 2 was made a lot easier (and slower, 2 whole seconds!) by converting each id number back into a string. I started with the longest "potential pattern" possible and decremented its length until it was one digit long. used loops 'thereis' clause to return true as soon as a match was found
4
u/Polaric_Spiral Dec 04 '25
[Language: TypeScript]
Advent of Node, Day 2
Regex goes brrrr
1
u/TreeSquirrles Dec 02 '25
[LANGUAGE: Python]
Don't feel too great about this one, because I didn't use any smart methods to check for repeated sections.
part 1 was sort of brute force, I think there might've been some mathy way to find repeated digits but whatever,
part 2 was even worse, b/c I used regex. The solution ended up being shorter than part 1.
solutions here: https://github.com/TreeSquirrles/Advent-of-Code/tree/main/2025/2
1
u/kolev Dec 02 '25 edited Dec 04 '25
[Language: Zig]
I'm learning Zig by solving these puzzles.
1
u/sebastianblaster Dec 02 '25
[LANGUAGE: nim]
https://github.com/Naitsabot/adventofcode2025/blob/main/day02/day02.nim
1
u/citizen-kong Dec 02 '25
[language: python]
My dumb solution: https://github.com/dannyfraser/advent-of-code-2025/blob/main/day-2/day-2.py
1
u/Original-Regular-957 Dec 02 '25
[LANGUAGE: Python ]
Part 1: https://github.com/UserGeorge24/aoc_25_python/blob/main/day_02_01.py
Part 2: https://github.com/UserGeorge24/aoc_25_python/blob/main/day_02_02.py
I stucked for a while at part 2, due to my code does not handle in a good way the single numbers… they are not invalid
1
u/a9sk Dec 02 '25
[LANGUAGE: Golang]
https://github.com/a9sk/adventofcode/blob/main/year-2025/day-02/main.go
1
u/Awkward-Seaweed-961 Dec 02 '25
[LANGUAGE: Python] This was suprisingly quick and painless(for me, not my old CS lecturers)
1
u/NxrmqL Dec 02 '25
[Language: Python]
Today's AoC taught me an important life skill: it's not wrong to ask questions or ask for help lol.
here's my code (and my sloppy first attempt on part 2): https://github.com/nxrmqlly/advent-of-code
1
u/Impressive_Ad6196 Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Kotlin]
Solution
I could reuse my cleaned up solution for both part1 & part2
Used a recursive algorithm. Basically since the longest length of my input was 10 digits long I used an array of divisors (2, 3, 5, 7) (PS: 7 was not required, still added it), the logic was if the length of the number is not divisible by a divisor, then we recursively try for the next divisor in the list.
If it is, we check if the sections are equal or not (Kotlin's chunked saved me a lot of work). If its not, we recursively try with the next divisor, if the sections are equal, we found the invalid id.
Starting with 2 parts as it covers many scenarios.
Wdyt?
1
u/Szeweq Dec 02 '25
[LANGUAGE: Rust]
Heavily optimized - `ilog10` function to the rescue!
https://github.com/szeweq/aoc2025/blob/main/day02/src/main.rs
1
u/PantsB Dec 02 '25 edited Dec 04 '25
[LANGUAGE:Meditech Advanced Technology] for the obscure language solve, runs in ~200 ms including translation and file io
https://pastecode.io/s/z4s2s69v
1
u/theMachine0094 Dec 03 '25 edited Dec 03 '25
[Language: Rust]
Didn't even try brute forcing. No fun in that. In the end it was very satisfying to realize that the numbers we're looking for are just multiples of "patterns" that look like
10101 * 23 = 232323etc. That makes the search very efficient. Curious if others came up with more efficient ways to search.3
u/Aspen138 Dec 02 '25
[LANGUAGE: Wolfram Mathematica]
3
u/psm_pm Dec 02 '25 edited Dec 02 '25
[LANGUAGE: TypeScript]
I don't even know how you wouldn't brute force this. Still only takes ~300ms for both solutions though, and I bet it could be optimized further. Loving JavaScript's labeled blocks
https://pastebin.com/WvWVMgix
Edit: optimized a bit by using math instead of string manipulation. https://pastebin.com/Zh5c5hYS
3
u/Rainbacon Dec 02 '25
[LANGUAGE: Haskell]
Simple brute force. Only had to check a few million things https://github.com/Rainbacon/aoc/blob/master/src/Problems2025/Problem2.hs
3
u/python-b5 Dec 02 '25
[LANGUAGE: Lily]
To be honest, I didn't really know what I was doing with this one. Still don't have a clue how to do it in a "smart" way. I just bruteforced it, and it takes a couple seconds to run, which I'm not super happy with, but I'll take the win for tonight. Maybe I'll come back to it in the morning and see if I can figure out a nicer solution. I'm doing a lot of string copying right now, which I think is the main source of overhead.
https://github.com/python-b5/advent-of-code-2025/blob/main/day_02.lily
3
u/Conceptizual Dec 02 '25
[Language: Python]
code
My part 1 solve time wasn't particularly interesting or noteworthy, but my part 1 -> part 2 time was fantastic! 00:18:26 00:19:11
(... Because I wrote a solution to part 2 as my part 1 solution, noticed I was getting the wrong answer on sample input, fixed it, ran it, got my star, read part two, and hit undo a bunch of times and re-ran it to get my part two solution haha)
3
u/darren Dec 02 '25 edited Dec 02 '25
[Language: Clojure]
Simple brute force with regex's doing the heavy lifting. Github
3
u/r_so9 Dec 02 '25
[LANGUAGE: F#] paste
Brute force generating the potential invalid IDs by repeating numbers from 1 to 10maxLength/2 - 1 and checking all ranges. Interesting bit: generating sequences with unfold
3
u/Pyr0Byt3 Dec 02 '25
[LANGUAGE: Go] [LANGUAGE: Golang]
https://github.com/mnml/aoc/blob/main/2025/02/1.go
3
u/1234abcdcba4321 Dec 02 '25
[LANGUAGE: JavaScript] paste
Part 2 solution that does something other than just iterating through each number one by one. That was what my original solution did, of course (it's not posted because it's the same as everyone else), but I wanted to try to make something better.
This still obviously doesn't work that well if you're to run it on ranges like
10000000000000-99999999999999, since it still has to iterate through each number in the first (n/2) digits. But it's at least faster than other approaches like "just try every invalid number and check if it's inside a range" when ranges are sparse as they are in the real inputs.There's almost certainly a way to skip that loop entirely, only checking the endpoints and filling in the middle values formulaically. But you need to do some deduplication stuff to handle numbers like
222222properly and I don't feel like thinking about it right now.3
u/_rabbitfarm_ Dec 02 '25
[Language: Prolog]
Part 1: https://adamcrussell.livejournal.com/62344.html
Part 2: https://adamcrussell.livejournal.com/62577.html
3
u/Smylers Dec 02 '25
[LANGUAGE: Vim button-hit commands]
[R*d(dit) On*!]Load input into Vim and push buttons as follows.
⟨^M⟩is big button to run a command.⟨^[⟩is top-unright button for stopping input-typing situation.@aloop: copy low IDs of pairs to own row. Banish rows that don't contain invalid IDs or-symbols. Banish pairs with low ID and high ID matching. Add 1 to low ID in all pairs, and go round again.+in gaps. Work out total. Put commands in@vfor tomorrow or following days. Part 1 solution shows in window.This runs good on illustration input (Go on — try it out!), but it's a bit slow on actual input.
For Part 2, just put a
+in/\v-|^(.+)\1$/so it is/\v-|^(.+)\1+$/.3
u/pedantic_git Dec 02 '25
[Language: Ruby]
It's brute force, sure, but I'm really happy with how elegant my solution is, thanks to regexps. It could be terser still but I wanted it to still be readable!
3
u/Jdup1n Dec 02 '25
[Language: R]
Github link
For part 1, I only generate number sequences where both endpoints have an even number of digits. Then, I check whether it is a repeating pattern.
For part 2, I remembered the theorem used for the 2017 AOC, where you can detect a pattern by concatenating the word to itself and looking for the pattern between the second and penultimate letters.
3
u/Delicious_Staff_4115 Dec 02 '25
[LANGUAGE: Python]
Solution
I think i took the opposite approach to most.
Instead of checking if a number in range is invalid, i generated the invalid numbers and checked if they were in range.
I guess it is probably a little faster in terms of performance if the ranges are very wide, but please correct me if i'm wrong.
3
u/mkinkela Dec 02 '25
[LANGUAGE: C++]
Link
3
u/bigboots50 Dec 02 '25 edited Dec 02 '25
[LANGUAGE: JavaScript]
3
u/ExitingBear Dec 02 '25
[LANGUAGE: R]
It's ugly, but it works: Day 2
After reading part 1, I knew that part 2 was going to be "any repeat works." That was not a helpful premonition.
3
u/YOM2_UB Dec 02 '25
[LANGUAGE: Python]
3
u/blackzver Dec 02 '25
[Language: Scala]
Both parts 1 and 2 combined. Using Scala 3 and ZIO / ZIO Streams... Repository with more details here.
3
u/sanraith Dec 02 '25
[LANGUAGE: C++]
Source is available on my github: Day02.h
I am finding patterns by repeatedly dividing and checking the remainder of the number:
3
u/dannybres Dec 02 '25
[LANGUAGE: MATLAB]
Simples - just implemented it. Nothing fancy. https://github.com/dannybres/Advent-of-Code/tree/main/2025/Day%2002
Also added a regex solution thanks again to u/JWinslow23 for the inspiration.
3
u/Naturage Dec 02 '25
[Language: R]
Code here!
It's a mess. But I was really hellbent of replacing my first solution of "just get the entire range into memory, it's a couple million numbers, check them later" with one that finds the list of sequential substrings and then remakes into full IDs, and the solution does work in 0.25s, so I got that going for me. Definitely room for further improvement, but not during work hours!
Also, shoutout to 7-digit numbers that need to be treated as shorter when in start of range and longer when in end to pick everything up.
3
u/Jadarma Dec 02 '25
[LANGUAGE: Kotlin]
Much easier than the first day, what a breeze. Thankfully the product ranges are fairly small and can be iterated through. Using regex feels like cheating! All you need is
^(\d+)\1$. For part two, we do the same thing but with\1+instead, which will handle retrying for different-sized groups auto-magically!AocKt Y2025D02
3
u/tgs14159 Dec 02 '25
[Language: Haskell]
Day02.hs
I didn't even think to use regex, just string manipulation
3
u/ndunnett Dec 02 '25
[Language: Rust]
Solution
Runs both parts in ~11 us
3
u/Bacon_omelette Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Python]
Brute force - for part 1 check even strings to see if halves match and for part 2 loop till half the string’s length and check if chunks repeat
Then we just loop through ranges and check each individual number
3
u/andrewsredditstuff Dec 02 '25
[LANGUAGE: C#]
I'm sure there's a neat way of doing this in RegEx; I'm equally sure I'm not going to try to work it out just now.
My brute force string juggling is acceptably fast and acceptably concise (if not actually acceptably readable).
github
3
u/Antique_Cup_7622 Dec 02 '25
[LANGUAGE: python]
3
u/TCH69 Dec 02 '25
[LANGUAGE: C]
Rebuilding IDs seems faster for me as I don't have to compare strings so yeah.
part 1 & 2, codeberg link
3
u/Inanis_bellator Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Python] I don't like using regex to solve these if i can help it so this is my solution using string manipulation :> (execution time both parts ~69 ms)
3
u/Zymophilus Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Bash]
Started the problem by tinkering in bash (and jq) and suddenly I had already solved the problem.
Use '^([0-9]+)\1+$' for the regex for part two.
3
u/stewie410 Dec 02 '25
[LANGUAGE: Bash]
I was really hoping there was a math-only solution I could come up with, but my attempts to be clever only lead me back to brute force, as I'm sure was not uncommon.
With a runtime on my puzzle input of 17.6 sec, I definitely should speed this up, though I'd need to return to my original idea...
3
u/MyAoCAccnt Dec 02 '25
[LANGUAGE: C#]
https://github.com/jsharp9009/AdventOfCode2025/blob/main/Day%202%20-%20Gift%20Shop/Program.cs
Slow but effective. For the first part, I only check numbers if they have an even number of digits. The odd number of digits will never match. I used Regex for part 2, constructing expressions for each digit in the first half only. If it doesn't match in the first half, it won't in the second.
There are plenty of ways to speed it up, but I woke up feeling like crap today so this is where I'm leaving it for now. Takes ~10 seconds to run.
3
u/Kitty_Sparkles Dec 02 '25
[LANGUAGE: TypeScript]
It’s not much, but it’s Fast Enough™ (350ms for both parts) and it’s simple:
3
u/Parzival_Perce Dec 02 '25
[LANGUAGE: Python]
Didn't post mine yet today because I told myself I'll make flow more readable later. But I'm busy now so oh well I'll just edit it later if I want.
3
u/greycat70 Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Tcl]
Part 1, part 2.
I used an optimization where I iterated over the possible invalid IDs (11, 22, 33, 44, etc.) instead of iterating over the ranges, because I assumed the input would have some stupidly large ranges. Part 2 made things a little trickier, but I used fundamentally the same algorithm. I added a "checked" array to avoid double-checking (e.g. 1111 could be checked as "1" repeated 4 times, or "11" repeated 2 times).
Tcl-specific detail: since I figured this might start to run into performance issues with large input ranges, I moved everything into procs. In Tcl, procs are byte-compiled, whereas loops that run in the "main" script outside of procs are not. So, putting it all into procs is a performance optimization as well as a tidying-up.
3
u/AdMaterial3670 Dec 02 '25 edited Dec 02 '25
[LANGUAGE: TypeScript]
Part I Solution: https://github.com/mikeTwoTimes/adventofcode_ts/blob/main/src/d2/p1.ts
This is probably my favorite AoC problem I've ever done! I can't remember the last time I did that much math for a solution lol. It is a shame I picked this years event to learn typescript D:
Edit: I broke up some of the math operations into some helpers for readabilities sake, the logic is the exact same tho. I also forgot to mention it has a runtime of 66 ms which I think is pretty decent considering the amount of math js is doing.
3
u/EverybodyLovesChaka Dec 02 '25
[LANGUAGE: Python]
https://pastebin.com/KyeZwmiN
Rather than check through the ranges, I generated the possible invalid IDs and checked to see if each one was in the ranges given. I don't know if that's quicker but for very large inputs it probably would be.
3
u/tcbrindle Dec 02 '25
[Language: C++23]
I originally wrote a brute force solution using sequence compositions, until I saw that other people were using regexes to do it. So now it's still brute force, but (considering it's C++) I think it's actually pretty lovely.
Github
3
u/graynk Dec 02 '25
[Language: Elixir]
Nothing fancy, normal brute force on strings with recursion.
https://github.com/graynk/aoc2025/blob/master/lib/day_two/aoc2.ex
3
u/dirk993 Dec 02 '25 edited Dec 02 '25
[Language: Dirc]
About Dirc
The code
Thought this one would be impossible already because it took so long on my ingame-computer. (Would've taken about 7 hours to run according to my calculations.) Had to resort to plain old C# for my first solution. Then I realised a more efficient math only solution would be possible. Implemented that one in C# first to prove it worked, and because it's easier to debug. Then ported it to Dirc.
I was using a hashset to filter out duplicates in C# but I realised that would be difficult to implement in Dirc so I used a regular array with a set size. Had to try out some values for the size and 200 turned out to be the lowest one that would work. A bit dirty but I didn't feel like implementing a hashset or a list considering that both: the runtime allocator isn't fully implemented yet and I don't have structs yet. Those are more important.
I'll look into how reasonable it is to add a feature to the compiler to compile to x86 assembly so I can run my code on my own real machine as well. That should speed up the rate the code runs by a factor of about 1000000 lol. That way it does take the fun out of running code on my self-made computer, but at least I still have the fun of using my own programming language, and at 1000000x speed.
3
u/FleyFawkes Dec 02 '25
[Language: Python]
The only difference between 1st an 2nd is regex pattern, capturing group in 1st is \1 which captures 1st group once where in 2nd task its \1+ which means capture it once or unlimited times.
3
u/SleepingInsomniac Dec 02 '25
[Language: Ruby]
Part 1
Part 2
3
u/fromtheinternettoyou Dec 02 '25 edited Dec 02 '25
[Language: Python]
The heavy lifting of day2. The first thing I tried worked well! Not fast, but get things done in about a sec.
3
u/mine49er Dec 02 '25
[Language: Python]
TIL about Python's string repeat operator :)
Simple brute force solution but only takes 1.3s on my 5800X
3
u/leuke-naam Dec 02 '25
[Language: Typst]
Takes roughly a minute to render a PDF on my laptop. Uncomment line 10 for part 1
3
u/ESP_Viper Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Python]
Not optimized or a fancy one-liner, but does the job :)
3
u/qwool1337 Dec 02 '25
[LANGUAGE: python]
i golfed both solutions in one expression each and both in 4 sloc of more pleasant code, part two being only about 1.5x slower than c++
the terse version:
3
u/daic0r Dec 02 '25
[LANGUAGE: Elixir]
Part 1: This took me entirely too long. I decided to reread the description at some point and realized the sequence had to appear exactly twice. That made things easier of course. Essentially I tried to do part 2 here already. I also optimized this by eliminating the parts of each range that could definitely not contribute to the list of invalid IDs, because it was taking quite a long time without it.
Part 2: I wanted to invent some clever sequence detection algorithm, but gave up on it eventually. I decided to use the simple solution of just keeping track of and increasing a sequence length variable and, if the length of the ID can be evenly divided by it, split the ID into parts of that length and then check if they're identical. Quite simple if you do it like that.
https://github.com/daic0r/advent_of_code_2025/blob/main/elixir/day2/day2.exs
3
u/Trick-Apple1289 Dec 02 '25
[LANGUAGE: C]
part 1
part 2
Well, certainly bit easier than yesterday imo, second part caused me problems, but that's mostly because C isn't really a great tool for parsing strings, 2 dumb 4 regex.
3
u/im_sofi Dec 02 '25 edited Dec 04 '25
[LANGUAGE: Ruby]
Absolutely got stuck on an assumption that 1-9 was valid numbers for part 2.
Part 1 and 2 are done by enforcing each rule and picking the lowest next number. Regex is only used for parsing. :)
https://codeberg.org/soupglasses/advent-of-code/src/branch/main/2025/day_02.rb
3
u/SpudPanda Dec 02 '25
[LANGUAGE: Rust]
Originally I made a solution that split the numbers into chunks using some modulo math and prime numbers. Looked up after that to see if there's a simpler trick to finding if a number is repeating, and sure enough there is!
If you take a string, concat itself to it, then remove the first and last characters, and the original string is still in there, you got yourself a repeating string. Neat.
Original Math solution
String trick solution
3
u/Duncanslutz Dec 02 '25
[LANGUAGE: Javascript]
Part 2: Pretty happy with this I thought the invalidation check was clever
3
u/CowImaginary3155 Dec 02 '25
[LANGUAGE: Scheme]
Solution for part1 and part2 in CHICKEN Scheme. Solve function in one line of code.
3
u/ash30342 Dec 02 '25
[Language: Java]
Day 2
Not a lot of time today.
For part 1 split the value in half, check if the first part is the same as the second part.
For part 2 I could not be bothered to find something really efficient, so I just generated String patterns with length 1 up to the half of the length of the ID and then checked if they were repeated. Still runs in about 0.3s.
3
u/Acc3ssViolation Dec 02 '25
[LANGUAGE: C#]
I always forget how RegEx works, and string comparisons are slowish, so I ended up with some modulos and divisions that do the trick for parts 1 and 2.
Part 1
Part 2
3
u/leDragonir Dec 02 '25
[Language: Haskell]
It has terrible performance, but it works... I might repeatedly check for things as well, I'm not really sure about this. In any case, fun little puzzle!
3
u/isaiahwarnke Dec 02 '25
[LANGUAGE: Python]
Prep
Parser output is a list of tuples of strings with the upper and lower bounds , for example
[('11', '22'), ('95', '115'), ('998', '1012'),... ('2121212118', '2121212124')]
Part 1
Part 2: loop structure is the same as part 1, but I break the more involved validity test into a separate function.
3
u/Stano95 Dec 02 '25 edited Dec 03 '25
[LANGUAGE: Haskell]
Code is on github
For part 1 I did this
next work out the range for your two digit numbers
For part 2 I took a slightly different approach
first to simplify things I will split my interval into many intervals
for each sub-interval I can find the relevant factors of the length of the number in that interval
After all that I was able to implement part 1 in terms of part 2 (I think my approach to part 2 is cleaner because of my interval splitting thing)
I do kind of think brute force might have been fine for this but where's the fun in that!
EDIT: I was confusing numbers with number of digits in my first bullet point
3
u/CutOnBumInBandHere9 Dec 02 '25 edited Dec 02 '25
[LANGUAGE: Python]
My brain is tired, so I went with just a simple brute force. I started off by using a regex, but it was slower than the
str(val)[: length // 2] * 2I ended up using.Here's a link to my full code including any imports, along with a tiny bit of text explaining my approach
3
u/ywgdana Dec 02 '25
[LANGUAGE: C]
Pined a bit for LINQ today for comparing the segments, but C is pretty so far.
Today's solution on github
3
u/solengol Dec 02 '25
[LANGUAGE: Go]
https://github.com/merlintree/aoc/blob/main/2025/day2.go
Part 1 ~62ms
Part 2 ~773ms
Brain isn't wrinkly enough to make it faster.
3
u/jhandros Dec 02 '25
[LANGUAGE: Python]
3
u/valhesh Dec 02 '25
[LANGUAGE: Clojure]
I learnt clojure and functional programming for AOC and it makes my brain happy.
I was going to use Go for today, but I just discovered that the regex package does not support backref (\1)
3
u/mvorber Dec 02 '25
[Language: F#] https://github.com/vorber/AOC2025/blob/main/day2.fs nothing fancy, just some straightforward checks, will brush it up later when I have more spare time :)
3
u/unwisedev Dec 02 '25
[LANGUAGE: Java]
Spent a few hours on this bad boy!
Zero string conversions, purely arithmetic. Readability could use some work :P
O(n) time O(1) space
https://github.com/Ernesto905/Yearly-Advent-of-code/blob/main/2025/day2/GiftShop.java
3
u/TinyPowerr Dec 03 '25
[LANGUAGE: Rust]
I tried to be as fast as possible and did everything with integer manipulation
Code for part 2
3
u/DevGod2020 Dec 03 '25
[LANGUAGE: JavaScript]
Bruteforcey but you know what, it gets the job done.
For leetcode players, if you've done problem 459 part 2 is pretty similar to that problem, just with numbers instead.
https://github.com/DEVGOD2020/competitiveProgramming/blob/main/AdventOfCode/problems/2025/day2/2025-day2.js
3
u/xHyroM Dec 03 '25
[LANGUAGE: Python]
https://github.com/xhyrom/aoc/blob/main/2025/02/solution.py