r/excel • 647 • Dec 02 '25

Discussion Advent of Code 2025 Day 2

It's back. Only 12 days of puzzles this year.

Today's puzzle "Gift Shop" link below.

https://adventofcode.com/2025/day/2

Three requests on posting answers:

Please try blacking out / marking as spoiler with at least your formula solutions so people don't get hints at how to solve the problems unless they want to see them.

The creator of Advent of Code requests you DO NOT share your puzzle input publicly to prevent others from cloning the site where a lot of work goes into producing these challenges.

There is no requirement on how you figure out your solution (many will be trying to do it in one formula, possibly including me) besides please do not share any ChatGPT/AI generated answers as this is a challenge for humans.

7 Upvotes

18 comments sorted by

4

u/Anonymous1378 1542 Dec 02 '25 edited Dec 03 '25

Part one:

=LET(_a,F31,

_b,--TEXTSPLIT(_a,"-",","),

SUM(MAP(CHOOSECOLS(_b,2),CHOOSECOLS(_b,1),LAMBDA(v,w,LET(_d,""&SEQUENCE(v-w+1,,w),

_e,FILTER(_d,REPT(LEFT(_d,ROUNDUP(LEN(_d)/2,0)),2)=_d,0),SUM(--_e))))))

Part two:

=LET(_a,F31,

_b,--TEXTSPLIT(_a,"-",","),

SUM(MAP(CHOOSECOLS(_b,2),CHOOSECOLS(_b,1),LAMBDA(v,w,LET(_d,""&SEQUENCE(v-w+1,,w),

_e,FILTER(_d,BYROW(--REPT(LEFT(_d,ROUNDUP(LEN(_d)/SEQUENCE(,MAX(LEN(_d))-1,2),0)),SEQUENCE(,MAX(LEN(_d))-1,2))=_d),SUM),0),SUM(--_e))))))

EDIT (slight optimization of repeated part):

=LET(_a,F31,

_b,--TEXTSPLIT(_a,"-",","),

SUM(MAP(CHOOSECOLS(_b,2),CHOOSECOLS(_b,1),LAMBDA(v,w,

LET(_d,""&SEQUENCE(v-w+1,,w),_e,SEQUENCE(,MAX(LEN(_d))-1,2),

SUM(--FILTER(_d,BYROW(--(REPT(LEFT(_d,ROUNDUP(LEN(_d)/_e,0)),_e)=_d),SUM),0)))))))

2

u/Downtown-Economics26 647 Dec 02 '25

Ooooh can't wait to solve myself today so I can click, I must be missing some maths just seeing the length of your answers.

1

u/Downtown-Economics26 647 Dec 02 '25

Ahhhh, I for the life of me couldn't figure out how to generate a SEQUENCE for each range.

1

u/Anonymous1378 1542 Dec 02 '25

What did you do to solve the example without doing so...?

1

u/Downtown-Economics26 647 Dec 02 '25

I spread out the start and finishes of the ranges across columns then used a formula per column to generate the sequences, then summed up the invalid numbers in each column... for whatever reason my solution wasn't working on the input so I messed up something in identifying invalids in my formula, I believe.

3

u/khosrua 16 Dec 03 '25

me stuck on that step

1

u/khosrua 16 Dec 03 '25

Oooh, I see all the spill works as a part of the MAP formula, but they do not spill into cells properl,y so I couldn't check any of the intermediary steps with the range as the input, only with single value as inputs. I was so confused #CALC! errors

1

u/jhandros Dec 02 '25

For Part 2; result = 0

1

u/Anonymous1378 1542 Dec 03 '25

If my part 1 gave a non-zero answer for you, I'd assume you did something wrong...? You could PM me your puzzle input, in the event you have some strange edge case not covered by my approach.

1

u/jhandros Dec 03 '25

No, I don't have any proposal. Your 2nd DAF is correct.

1

u/khosrua 16 Dec 03 '25

Thank you, i learnt a lot about dynamic range and some regex today. but now if you would excuse me, i need a nap

2

u/Downtown-Economics26 647 Dec 02 '25

Was able to get the answer the example for Part 1 with formulas, but kept getting wrong answer for input. Will probably have to revisit with VBA later today.

2

u/Downtown-Economics26 647 Dec 02 '25

VBA answers today as I wasn't as clever as u/Anonymous1378.

Sub AOC2025D02P01()

Dim rs As LongLong
Dim rf As LongLong
Dim ans As LongLong

i = Range("a1")
ilen = Len(i) - Len(Replace(i, ",", ""))

For ir = 0 To ilen
istring = Split(i, ",")(ir)
rs = Split(istring, "-")(0) * 1
rf = Split(istring, "-")(1) * 1
    For n = rs To rf
    nlen = Len(n)
    mp = nlen / 2
    If nlen Mod 2 = 0 Then
    ise = True
    Else
    ise = False
    End If
        If ise = True And Left(n, mp) = Right(n, mp) Then
        ans = ans + n
        End If
    Next n
Next ir

Debug.Print ans

End Sub

Part 2

Sub AOC2025D02P02()

Dim rs As LongLong
Dim rf As LongLong
Dim ans As LongLong

i = Range("a1")
ilen = Len(i) - Len(Replace(i, ",", ""))

For ir = 0 To ilen
istring = Split(i, ",")(ir)
rs = Split(istring, "-")(0) * 1
rf = Split(istring, "-")(1) * 1
    For n = rs To rf
    ns = CStr(n)
    nlen = Len(n)
    mp = Fix(nlen / 2)
        For d = 1 To mp
        ds = Left(ns, d)
        cleft = Len(Replace(ns, ds, ""))
            If cleft = 0 Then
            ans = ans + n
            Exit For
            End If
        Next d
    Next n
Next ir

Debug.Print ans
    
End Sub

4

u/ziadam 7 Dec 03 '25 edited Dec 03 '25

This formula works for both parts. Expects the input in A1.

=LET(
  r,TEXTSPLIT(A1,"-",","), 
  MAP({"$";"+$"},LAMBDA(z,
    SUM(MAP(TAKE(r,,1),TAKE(r,,-1),LAMBDA(a,b,LET(
      c,SEQUENCE(b-a+1,,a),
      SUMPRODUCT(c,--REGEXTEST(c,"^(.+)\1"&z))
    ))))
  ))
)

3

u/PaulieThePolarBear 1927 Dec 03 '25

Part 1

=LET(
a, A1,
b, TEXTSPLIT(a, ","),
c, REDUCE(0, b, LAMBDA(x,y, VSTACK(x, LET(
ca, SEQUENCE(TEXTAFTER(y, "-")-TEXTBEFORE(y, "-")+1,,TEXTBEFORE(y, "-")),
cb, FILTER(ca, ISEVEN(LEN(ca))*(SUBSTITUTE(ca, LEFT(ca, LEN(ca)/2),"")=""),0),
cb
)))),
d, SUM(c),
d)

Part 2

=LET(
a, A1,
b, TEXTSPLIT(a, ","),
c, REDUCE(0, b, LAMBDA(x,y, VSTACK(x, LET(
ca, SEQUENCE(TEXTAFTER(y, "-")-TEXTBEFORE(y, "-")+1,,TEXTBEFORE(y, "-")),
cb, FILTER(ca, (LEN(ca)>1)*MAP(ca, LAMBDA(m, OR(SUBSTITUTE(m, LEFT(m,SEQUENCE(,ROUNDUP(LEN(m)/2,0))),"")=""))),0),
cb
)))),
d, SUM(c),
d)

2

u/SheepiCagio 1 Dec 03 '25

I did Day 2 in a couple of ways:

P1 - 1:
=SUM(MAP(TEXTSPLIT(A2;",");LAMBDA(x;LET(
s;--TEXTBEFORE(x;"-");
e;--TEXTAFTER(x;"-");
rng;SEQUENCE(e-s+1;;s);
SUM(MAP(rng;LAMBDA(a;LET(l;LEN(a);IF(ISODD(l);0;IF(LEFT(a;l/2)=RIGHT(a;l/2);--a))))))))))

P1-2:

=SUM(MAP(TEXTSPLIT(A2;",");LAMBDA(x;
LET(s;--TEXTBEFORE(x;"-");
e;--TEXTAFTER(x;"-");
rng;SEQUENCE(e-s+1;;s);
SUM( MAP(rng;LAMBDA(x;
REGEXTEST(x;"^(\d+)\1$")))*rng)))))

P1-3:

=SUM(MAP(TEXTSPLIT(A2;",");LAMBDA(x;LET(
s;TEXTBEFORE(x;"-");
e;TEXTAFTER(x;"-");
rng;SEQUENCE(e-s+1;;s);
SUM(FILTER(rng;ISNUMBER(XMATCH(""&rng;REPT(SEQUENCE(99999);2)));0))))))

P2-1:

=SUM(MAP(TEXTSPLIT(A2;",");LAMBDA(x;
LET(s;--TEXTBEFORE(x;"-");
e;--TEXTAFTER(x;"-");
rng;SEQUENCE(e-s+1;;s);
SUM( MAP(rng;LAMBDA(x;
LET(l;LEN(x);ans;OR(
IF(l>1;ROWS(UNIQUE(MID(x;SEQUENCE(l);1)))=1);
IF(l=9;ROWS(UNIQUE(MID(x;SEQUENCE(l/3;;;3);3)))=1);
IF(AND(ISEVEN(l);l>3);ROWS(UNIQUE(MID(x;SEQUENCE(l/2;;;2);2)))=1);
IF(AND(ISEVEN(l);l>3);ROWS(UNIQUE(MID(x;SEQUENCE(2;;;l/2);l/2)))=1));
ans)))*rng)))))

P2-2:
=SUM(MAP(TEXTSPLIT(A2;",");LAMBDA(x;
LET(s;--TEXTBEFORE(x;"-");
e;--TEXTAFTER(x;"-");
rng;SEQUENCE(e-s+1;;s);
SUM( MAP(rng;LAMBDA(x;
REGEXTEST(x;"^(\d+)\1+$")))*rng)))))

P2-3:
=VSTACK(TOCOL(REPT(SEQUENCE(10;;0);SEQUENCE(;9;2)));
TOCOL(REPT(SEQUENCE(89;;10);SEQUENCE(;4;2)));
TOCOL(REPT(SEQUENCE(899;;100);{2,3}));
REPT(SEQUENCE(8999;;1000);2);
REPT(SEQUENCE(89999;;10000);2))

To create a list of all invalid ids and then similar to P1-3, where $B$2 refers to list above:

=SUM(MAP(TEXTSPLIT(A2;",");LAMBDA(x;LET(
s;TEXTBEFORE(x;"-");
e;TEXTAFTER(x;"-");
rng;SEQUENCE(e-s+1;;s);
SUM(FILTER(rng;ISNUMBER(XMATCH(""&rng;$B$2));0))))))

1

u/Decronym Dec 02 '25 edited Dec 03 '25

Acronyms, initialisms, abbreviations, contractions, and other phrases which expand to something larger, that I've seen in this thread:

Fewer LettersMore Letters
ANDReturns TRUE if all of its arguments are TRUE
BYROWOffice 365+: Applies a LAMBDA to each row and returns an array of the results. For example, if the original array is 3 columns by 2 rows, the returned array is 1 column by 2 rows.
CHOOSECOLSOffice 365+: Returns the specified columns from an array
FILTEROffice 365+: Filters a range of data based on criteria you define
IFSpecifies a logical test to perform
ISEVENReturns TRUE if the number is even
ISNUMBERReturns TRUE if the value is a number
ISODDReturns TRUE if the number is odd
LAMBDAOffice 365+: Use a LAMBDA function to create custom, reusable functions and call them by a friendly name.
LEFTReturns the leftmost characters from a text value
LENReturns the number of characters in a text string
LETOffice 365+: Assigns names to calculation results to allow storing intermediate calculations, values, or defining names inside a formula
MAPOffice 365+: Returns an array formed by mapping each value in the array(s) to a new value by applying a LAMBDA to create a new value.
MAXReturns the maximum value in a list of arguments
MIDReturns a specific number of characters from a text string starting at the position you specify
ORReturns TRUE if any argument is TRUE
REDUCEOffice 365+: Reduces an array to an accumulated value by applying a LAMBDA to each value and returning the total value in the accumulator.
REGEXTESTDetermines whether any part of text matches the pattern
REPTRepeats text a given number of times
RIGHTReturns the rightmost characters from a text value
ROUNDUPRounds a number up, away from zero
ROWSReturns the number of rows in a reference
SEQUENCEOffice 365+: Generates a list of sequential numbers in an array, such as 1, 2, 3, 4
SUBSTITUTESubstitutes new text for old text in a text string
SUMAdds its arguments
SUMPRODUCTReturns the sum of the products of corresponding array components
TAKEOffice 365+: Returns a specified number of contiguous rows or columns from the start or end of an array
TEXTAFTEROffice 365+: Returns text that occurs after given character or string
TEXTBEFOREOffice 365+: Returns text that occurs before a given character or string
TEXTSPLITOffice 365+: Splits text strings by using column and row delimiters
TOCOLOffice 365+: Returns the array in a single column
UNIQUEOffice 365+: Returns a list of unique values in a list or range
VSTACKOffice 365+: Appends arrays vertically and in sequence to return a larger array
XMATCHOffice 365+: Returns the relative position of an item in an array or range of cells.

Decronym is now also available on Lemmy! Requests for support and new installations should be directed to the Contact address below.


Beep-boop, I am a helper bot. Please do not verify me as a solution.
[Thread #46433 for this sub, first seen 2nd Dec 2025, 09:15] [FAQ] [Full list] ^[Contact] [Source code]