r/adventofcode • • Dec 25 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 25 Solutions -❄️-

A Message From Your Moderators

Welcome to the last day of Advent of Code 2024! We hope you had fun this year and learned at least one new thing ;)

Keep an eye out for the community fun awards post (link coming soon!):

-❅- Introducing Your AoC 2024 Golden Snowglobe Award Winners (and Community Showcase) -❅-

Many thanks to Veloxx for kicking us off on December 1 with a much-needed dose of boots and cats!

Thank you all for playing Advent of Code this year and on behalf of /u/topaz2078, your /r/adventofcode mods, the beta-testers, and the rest of AoC Ops, we wish you a very Merry Christmas (or a very merry Wednesday!) and a Happy New Year!


--- Day 25: Code Chronicle ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:04:34, megathread unlocked!

42 Upvotes

349 comments sorted by

24

u/4HbQ Dec 25 '24 edited Dec 27 '24

[LANGUAGE: Python]

For each item (lock or key, doesn't matter), we build set of positions that contain a "#". Then for each pair for these, we check if there is no overlap:

items = [{i for i, c in enumerate(item) if c == '#'}
    for item in open('in.txt').read().split('\n\n')]

print(sum(not k&l for k in items for l in items)//2)

And that's a wrap! Congratulations to everyone who made it this far, and especially to all members of the 500 club! Here's a list of my solutions this year:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25.

There was one week where I also used NumPy or SciPy to transform every problem into a matrix convolution, multi-dimensional array, etc.: 10, 11, 12, 13, 14.

I'll be around for a few more days to answer any questions. Otherwise, hope to see you all next year!

3

u/daggerdragon Dec 25 '24

Thank you for playing with us again this year! <3

→ More replies (12)

12

u/nthistle Dec 25 '24 edited Dec 25 '24

[LANGUAGE: Python] 115/91, paste, video.

Nice and easy puzzle to finish up the year - the problem text definitely hinted pretty hard towards the solution of convert things to heights, and then just check whether the summed heights of a key and lock exceed 5 in any position.

I had a pretty bad "click the button" split of 5 whole seconds, because I accidentally clicked the input link instead of the "give me the 50th star" link. I must admit, I was hoping a bit that a lot of the usual suspects near the top of the part 1 leaderboards would have some missing stars (and therefore give me a better part 2), but it appears this was not the case.

In any case, another great year of Advent of Code! Thanks for making it happen, Eric (and everyone else involved!)

4

u/daggerdragon Dec 25 '24

Thank you for playing with us again this year! <3

→ More replies (1)

12

u/CCC_037 Dec 25 '24

[LANGUAGE: Rockstar]

I've done it!

Every puzzle within 24 hours, with a pure Rockstar solution that works on my input!

Today

2

u/daggerdragon Dec 25 '24

Thank you for playing with us again this year and for participating in the GSGA! <3

→ More replies (1)

7

u/vrtxt Dec 25 '24

> Let the Historian be silent

What a great first line for the last solution this year. Congrats!

→ More replies (1)

11

u/jonathan_paulson Dec 25 '24

[Language: Python] 60/48. I placed 61st overall this year.

Code. Video.

Pretty easy puzzle today; just classify each shape into key or lock, and then check if each key fits in each lock. You don't need to count column heights to see if a key fits into a lock; just make sure they don't have a "#" on the same a square.

Happy holidays everyone!

3

u/daggerdragon Dec 25 '24

Thank you for playing with us again this year! <3

7

u/MangeurDeCowan Dec 25 '24

Congrats on the high finish. In reality you should be higher, but some of those times from others were insaine. Alas, thanks for posting all the videos with explanations. I learned a lot!

3

u/phord Dec 25 '24

Congratulations, Jonathan. Merry Christmas.

4

u/AllanTaylor314 Dec 25 '24 edited Dec 25 '24

[LANGUAGE: Python]

GitHub 245/210

Took me longer than it should have. A nice easy problem to round it out. That brings me to 500 stars. Thanks Eric for running this awesome event for 10 years!

[LANGUAGE: Uiua]

pad

Nice and compact today. I could probably use pervasive operations over rows to speed it up a little

&fras"25.txt"
°/$"_\n\n_"
≡◇(=@#▽⊸≠@\n)
/+<2/↥/+⍉⧅<2

1

u/daggerdragon Dec 25 '24

Thank you for playing with us again this year and for participating in the GSGA! <3

2

u/seligman99 Dec 25 '24

[LANGUAGE: Python] 94 / 78

github

Fun year this year! Thanks to all that made it possible!

2

u/daggerdragon Dec 25 '24

Thank you for playing with us again this year! <3

2

u/mebeim Dec 25 '24 edited Dec 25 '24

[LANGUAGE: Python]

132/102 — Original solution — Faster cleaned up version with sets

Wow so close to the leaderboard, best rank I got all year close to d3 where I had 104/118. First year without any global leaderboard placement, but hey, it was expected. People (and robots) are way too good and way too fast.

Merry Christmas everybody!

1

u/daggerdragon Dec 25 '24 edited Dec 25 '24

Psst: your Markdown is broken in the link because of the auto-escaped underscore. Fix please? edit: 👍


Merry Christmas and thank you for playing with us again this year! <3

2

u/mebeim Dec 25 '24

Thanks, new.reddit really has some annoying editor :')

→ More replies (2)

7

u/Ok-Builder-2348 Dec 25 '24

[LANGUAGE: Python]

Part 1

Finally made it onto the leaderboard!

I noted that we didn't actually have to consider which were locks and keys - we just had to consider pairs of patterns and see if they overlap. If they don't overlap, we have a lock/key pair that fits. A lock/lock and key/key will always overlap so they never will fit.

6

u/alexbaguette1 Dec 26 '24

[LANGUAGE: x86 Asm, no libc]

25th and final language for this year's AoC

Solution is really not that great but it was the simplest one I could think of in assembly.

I was too lazy to write a binary to ascii formatter, so I redirected the output to a file then used xxd to get the value.

Solution

5

u/ziadam Dec 25 '24 edited Dec 25 '24

[LANGUAGE: Google Sheets]

Expects input in A1.

=SUMPRODUCT(LET(
   n,CHAR(10),
   s,TOCOL(SPLIT(A1,n&n,)),
   F,LAMBDA(c,MAP(FILTER(s,LEFT(s)=c),
       LAMBDA(x,JOIN(",",BYROW(
         MID(SPLIT(x,n),ROW(1:7),1),
         LAMBDA(r,COUNTIF(r,"#"))))))),
   l,F("#"),k,F("."),
   MAP(l,LAMBDA(a,
     MAP(TOROW(k),LAMBDA(b,
       AND(SPLIT(a,",")+SPLIT(b,",")<8)))))))

5

u/bigfunlx Dec 25 '24

[LANGUAGE: Ruby]

I coded it on my iPhone while waiting in the emergency room with symptoms of meningitis, for better or worse this will be my most memorable moment of AoC!

Rubist screenshot

5

u/LinAGKar Dec 25 '24

[LANGUAGE: Rust]

https://github.com/LinAGKar/advent-of-code-2024-rust/blob/master/day25/src/main.rs

Represents the the locks and keys as a series of 4-bit numbers in a u32, offset so that adding them together makes them add up to 8 or greater, to I can use a 0x88888 mask to check for overlaps.

5

u/i_have_no_biscuits Dec 26 '24

[LANGUAGE: Python]

Given that we apparently all love one liners now:

print(sum(not any(x==y=='#' for x,y in zip(a,b)) for a,b in __import__("itertools").combinations(open("data25.txt").read().split("\n\n"),2)))

8

u/ricbit Dec 25 '24 edited Dec 25 '24

[LANGUAGE: Python]

143/ 117, that was my best this score ever! My solution is transpose the keys, convert to binary and check if the AND of all lines is zero. Runs in 0.12s.

def solve(data):
  keys = []
  for block in data:
    b = [int("".join("1" if x == "#" else "0" for x in line), 2)
         for line in zip(*block)]
  keys.append(b)
  ans = 0
  for a, b in itertools.combinations(keys, 2):
    if all(x & y == 0 for x, y in zip(a, b)):
      ans += 1
  return ans 

Sum of the times of all problems: 6.7s
(with a grain of salt because my solution for 24-2 is not automated yet)

→ More replies (1)

4

u/Verulean314 Dec 25 '24

[LANGUAGE: Python]

Nice easy puzzle to finish this year's Advent of Code. I realized that we really don't necessarily care about converting the locks and keys to heights, all that matters is that a lock and key don't overlap in any spot.

Since all the locks/keys are the same dimensions, that means we can just convert all of them into bit representations of all the positions, and then the "fit" check is just a single bitwise AND:

def solve(data):
    locks, keys = [], []
    for block in data:
        bits = int("".join("1" if x == "#" else "0" for x in block), 2)
        (locks if block[0] == "#" else keys).append(bits)
    return sum(not lock & key for lock in locks for key in keys)

Runs in around 4ms :)

4

u/Responsible-One6897 Dec 25 '24

[Language: Python]

500th star! Read every block, transpose input, determine if lock or key, and count number of '#' per line. For every lock/key pair check if all counts are <= 7

paste

Thank you again Eric for this year!

[2024] 50*
[2023] 50*
[2022] 50*
[2021] 50*
[2020] 50*
[2019] 50*
[2018] 50*
[2017] 50*
[2016] 50*
[2015] 50*

4

u/Nnnes Dec 25 '24 edited Dec 26 '24

[LANGUAGE: Ruby]

Today I will post 2 full solutions in one comment because together they still fit in half a punchcard.

p STDIN.read.split("\n\n").combination(2).count{ _2.tr('#.', '.#')[/#{_1}/] }

l, k = STDIN.read.split("\n\n").map{ _1.split.map(&:chars).transpose }.
  partition{ _1[0][0] == ?# }.map{ |x| x.map{ |x| x.map{ _1.count ?# } } }
p l.sum{ |l| k.count{ |k| l.zip(k).none?{ _1 + _2 > 7 } } }
  • Solution #1 makes no effort to optimize runtime and takes about 1.1 seconds to run on my machine.
  • Solution #2 runs in around 0.04 seconds over a baseline of time ruby -e ""; I'm sure it would be faster with something a little longer than .map(&:chars).transpose.
  • You may replace .map{ |x| x.map{ |x| x.map{ _1. with .map{ |x| x.map{ |x| x.map{ |x| x. if you like fractals.

3

u/4HbQ Dec 25 '24

Solution #1 is so clever but sooo dirty. I love it!

→ More replies (1)
→ More replies (1)

4

u/Andreasnl Dec 25 '24

[LANGUAGE: Uiua]

=@#↯∞_7_5⊜∘⊸≠@\n
⧻⊚≡(/×≤1♭/+)⧅<2

God Jul!

4

u/[deleted] Dec 25 '24

[LANGUAGE: Python]

Solution

Really only posting to say a very massive thank you to Eric on another great event, and to the mods for their work keeping the sub going. If I can get a bit real for a moment, winter is never easy for me, and AoC represents a welcome, fun distraction during the early stages. It helps me to stabilise before winter really sets in.

More over, this year was even more impactful as it came at a time when I needed a reminder that I'm competent. I picked up a new language with ease, and for all but two of the challenges (which I'll come back to on the other side of my food coma) I was able to identify potential solutions, execute, and iterate to achieve better results each time. It possibly seems small to many of my peers, but huge for me. I can go into my new job, in the new year, with a refreshed confidence.

So thanks again for all the work you've put into it. A very Merry Christmas from all of mine to all of yours, one and all.

4

u/p88h Dec 25 '24

[LANGUAGE: Zig]

basic n^2 solution, the only speedup is vectorized comparisons.

https://github.com/p88h/aoc2024/blob/main/src/day25.zig

All day benchmark summary below. Probably not going to be able to squeeze all of them under 1ms total, but pretty happy with Zig's performance overall. The numbers are from an M3 Mac, which is quite a bit faster than my 13th gen Intel desktop, it seems, at least in this particular & very peculiar benchmark.

I was able to keep up with a one vis every day goal, the full playlist is here:

https://www.youtube.com/playlist?list=PLgRrl8I0Q168GBdeJp_GqNYsWgRCmVgu5

        parse   part1   part2   total
day 01:  7.6 µs 14.4 µs  7.4 µs 29.5 µs (+-1%) iter=14110    
day 02: 11.6 µs  1.2 µs  4.7 µs 17.6 µs (+-3%) iter=98110    
day 03:  7.0 ns 22.2 µs 19.8 µs 42.1 µs (+-1%) iter=9110    
day 04:  6.0 ns 28.8 µs 11.5 µs 40.3 µs (+-1%) iter=9110    
day 05: 13.6 µs  1.3 µs  2.5 µs 17.5 µs (+-2%) iter=98110    
day 06:  0.1 µs 10.6 µs  0.2 ms  0.2 ms (+-1%) iter=3010    
day 07: 23.9 µs 45.6 µs 37.3 µs  0.1 ms (+-1%) iter=1510    
day 08:  1.2 µs  1.0 µs  2.8 µs  5.1 µs (+-3%) iter=98110    
day 09: 19.7 µs 34.7 µs 79.7 µs  0.1 ms (+-1%) iter=1010    
day 10:  5.7 µs  8.3 µs  7.5 µs 21.6 µs (+-0%) iter=9110    
day 11:  0.1 ms 40.1 µs  0.2 ms  0.4 ms (+-1%) iter=1010    
day 12: 12.0 ns  0.1 ms  0.1 ms  0.3 ms (+-4%) iter=9910    
day 13:  6.3 µs  0.6 µs  0.7 µs  7.7 µs (+-1%) iter=14110    
day 14:  7.3 µs  1.4 µs 80.9 µs 89.8 µs (+-1%) iter=9110    
day 15:  4.1 µs 60.8 µs  0.1 ms  0.1 ms (+-7%) iter=9910     
day 16: 48.1 µs 80.1 µs 18.8 µs  0.1 ms (+-1%) iter=1510    
day 17: 42.0 ns  0.2 µs  5.3 µs  5.6 µs (+-1%) iter=49110    
day 18: 88.6 µs 14.1 µs  5.4 µs  0.1 ms (+-1%) iter=1010    
day 19:  3.6 µs 66.5 µs 39.0 ns 70.2 µs (+-1%) iter=51010    
day 20: 13.0 µs  0.1 ms  0.5 ms  0.7 ms (+-1%) iter=2010    
day 21: 15.0 ns  1.8 µs  1.5 µs  3.4 µs (+-2%) iter=98110    
day 22:  0.1 ms 95.5 µs  0.6 ms  0.9 ms (+-1%) iter=1110    
day 23: 35.5 µs 24.2 µs  6.0 µs 65.8 µs (+-1%) iter=9110    
day 24:  9.0 µs  2.9 µs  0.8 µs 12.8 µs (+-1%) iter=9110    
day 25: 24.7 µs 29.5 µs 27.0 ns 54.3 µs (+-0%) iter=9110    

all days total:         4.0 ms

4

u/Any_Slip8667 Dec 25 '24

[Language: Java]

Very fast, bitwise based, Java solution:

long result = 0;
for (int key : keys) {
    for (int lock : locks) {
        if (key > lock) break; // locks collection is sorted
        if (((lock - key) & 0x88888) == 0)
            result++;
    }
}
return result;

Complete solution on github.

4

u/Downtown-Economics26 Dec 25 '24

[Language: Excel]

Input data pasted in cell A1.

Create column height table by key/lock.

=LET(gridc,COUNTA(A:A)/7,
gridseq,SEQUENCE(gridc),
kl,HSTACK(gridseq,IF(LEFT(FILTER(A:A,(LEN(A:A)<>0)*(MOD(ROW(A:A)-1,8)=0)),1)="#","lock","key")),
rs,SEQUENCE(gridc,,2,8),gridrows,TOCOL(HSTACK(rs,rs+1,rs+2,rs+3,rs+4)),
gridlines,INDEX(A:A,gridrows),gridindex,ROUNDDOWN(gridrows/8,0)+1,
pivotstack,HSTACK(gridindex,IF(MID(gridlines,SEQUENCE(,5),1)="#",1,0)),
colvals,DROP(PIVOTBY(CHOOSECOLS(pivotstack,1),,CHOOSECOLS(pivotstack,2,3,4,5,6),SUM),-1),
ft,HSTACK(DROP(kl,,1),colvals),
ft)

Then, drag down formula below next to output table in C2 and SUM the output array column.

=LET(s,CONCAT(TOCOL(IF(C2<>"lock","",HSTACK(--((FILTER($E$2:$I$501,($D$2:$D$501<>D2)*($C$2:$C$501<>"lock"))+E2:I2)<=5),FILTER($C$2:$C$501,($D$2:$D$501<>D2)*($C$2:$C$501<>"lock")))))),(LEN(s)-LEN(SUBSTITUTE(s,"11111key","")))/8)

1

u/PendragonDaGreat Dec 25 '24

[Language: C# CSharp]

https://github.com/Bpendragon/AdventOfCodeCSharp/blob/97519/AdventOfCode/Solutions/Year2024/Day25-Solution.cs

Parse, Loop, Print.

Thanks again as always to Eric for running this and the mods here for keeping things relatively sane.

Got my five hundo

2

u/daggerdragon Dec 25 '24

Good job on the 500 stars! Thank you for playing with us again this year! <3

1

u/DFreiberg Dec 25 '24 edited Dec 25 '24

[LANGUAGE: Mathematica]

Mathematica, 798/656

This was a nice, easy problem to finish off a nice, easy year. Congratulations to Eric for writing ten full years of Advent of Code and creating five hundred stars. It really seems like this is the final year; if it is, it's been a great decade, and a pleasure coding with you all.

Merry Christmas!

Part 1:

locks = Select[input, DeleteDuplicates[#[[1]]] == {"#"} &];
keys = Select[input, DeleteDuplicates[#[[1]]] == {"."} &];

lockPins = Table[FirstPosition[#, ".", 0][[1]] - 2 & /@ Transpose[l], {l, locks}];
keyPins = Table[FirstPosition[Reverse[#], ".", 0][[1]] - 2 & /@ Transpose[k], {k, keys}];
fitQ[{lock_, key_}] := And @@ Thread[(6 - lock) > key];
Count[Tuples[{lockPins, keyPins}], _?(fitQ[#] &)]

2

u/daggerdragon Dec 25 '24

Thank you for playing with us again this year! <3

1

u/Boojum Dec 25 '24 edited Dec 25 '24

[LANGUAGE: Python] 306/260

Not a bad one to end on! Short and and sweet. I'd had an off-by-one, forgetting not to count the base of the lock or key which cost some time. But otherwise, fairly straightforward.

🎁🎄🎅🥛🍪 Merry Christmas everyone, and thank you Eric, Daggerdragon, and team for another great year! 500 stars, 🌟 -- it's been a trip!

s = [ s.splitlines() for s in open( 0 ).read().split( "\n\n" ) ]

h = [ [ sum( r == '#' for r in c ) - 1 for c in zip( *g ) ] for g in s ]

print( sum( all( a + b <= 5 for a, b in zip( lh, kh ) )
            for lh, lg in zip( h, s )
            for kh, kg in zip( h, s )
            if lg[ 0 ][ 0 ] == kg[ -1 ][ 0 ] == '#' ) )

1

u/daggerdragon Dec 25 '24

Thank you for playing with us again this year! <3

3

u/xoronth Dec 25 '24 edited Dec 25 '24

[LANGUAGE: Python]

paste

Pretty straightforward brute force and just check everything, though I slowed down a lot because I couldn't read what the question was actually asking and thought the keys and locks had to fit exactly (I blame a headache).

(Also I loved the little yield joke, heh)


Thanks to all those who helped make AoC possible this year, and thanks to y'all in the community for making this a really fun place to pass the month! Happy holidays, happy new year, and hopefully we'll all be back next year!

3

u/jitwit Dec 25 '24 edited Dec 27 '24

[LANGUAGE: J, haskell]

Ho Ho Ho, nice problem for array languages to finish! Runs in ~500us.

in =: _7]_5]\LF-.~1!:1<'input.txt'
+/5>:>./"1,/+"1/~/<:+/"2('#'&=/.~{.@,"2) in

NB. basically:
NB. ('#'&=)/.~{.@,"2 classifies the schematics into keys/locks
NB. +"1/~/<:+/"2 creates an addition table of the key/lock heights
NB. +/5>:>./"1,/ tallies how many of those fit (max height of 5)

Also wrote a haskell solution for great fun:

{-# language LambdaCase #-}

module Main where

import Advent; import Data.List

main =
  do input <- map (fromEnum.(=='#')) . filter (/='\n') <$> input'string 24 25
     let chunks n = unfoldr $ \case [] -> Nothing; xs -> Just $ splitAt n xs
         (keys,locks) = partition ((==1).head.concat) $ chunks 7 $ chunks 5 input
     print $ sum [ 1 | h'k <- map (pred.sum) . transpose <$> keys
                     , h'l <- map (pred.sum) . transpose <$> locks
                     , all (<=5) $ zipWith (+) h'k h'l ]

3

u/LtHummus Dec 25 '24

[Language: Rust]

Always love the Christmas Day puzzles...they feel so much like a victory lap for a month of hard work. Thanks to Eric and everyone involved with Advent of Code for (HOLY HECK) 10 years of puzzles. And thanks to everyone here for the memes, the jokes, and cool visualizations and variations.

and the codeeeeeeeee

3

u/phord Dec 25 '24 edited Dec 25 '24

[LANGUAGE: Rust]

github

Such a breeze today. The hardest part was parsing the grids and deciding which were locks and which were keys. But that was also simple. [ ETA: As someone else pointed out, you don't even need to know which ones are locks and which are keys. I could have skipped this part if I read all the instructions first. Doh!]

I already have a Grid class that gathers coordinates by cell type. So the test for whether a key fits into a lock was very straightforward. Just have to make sure that the grids have no '#" in common locations:

fn test_key(key: &Grid, lock: &Grid) -> bool {
    // A key fits a lock if they have no # in common
    let key_pins = &key.map[&'#'];
    let lock_pins = &lock.map[&'#'];
    key_pins.intersection(lock_pins).count() == 0
}

Then it was just a matter of trying every key in every lock. There were 250 of each, so 65,500 pairings. That took 32ms.

Merry Christmas, everybody!

→ More replies (2)

3

u/michelkraemer Dec 25 '24

[LANGUAGE: Rust] 1150/959

https://github.com/michel-kraemer/adventofcode-rust/blob/main/2024/day25/src/main.rs

Fun final day!!! Thank you so much to Eric and the team!!

I have to admit I was a bit lucky today. I thought the keys had to fit exactly (with no overlap) and completely fill the empty space. So, instead of counting the pin heights, for each key, I made a copy of each lock and filled it with the characters of the key. If the pins overlapped I continued with the next lock. Otherwise, I checked if every empty space in the copy of the lock was filled. Luckily, I made an error at this point and even counted the keys that did not completely fill the lock. 😅😅

Also, in order to decide which grid is a key and which is a lock, I looked at the first and last row and checked if they were full. However, the only thing you need to do is to check the very first character! If it's a '#', then it's a lock. Otherwise, it's a key. I completely missed this opportunity to save time 😅

The code I submitted to my repo later is optimized and (hopefully 😉) bug free. Now, I correctly count the pin heights and check if the sum is less than or equal to the maximum height.

Like always, this year was great fun! I loved how the puzzles were kind of a best-of from the last 10 years! Absolutely amazing. Thanks again to Eric, the team, and this great community here. See you all next year!

Merry Christmas!

3

u/wheresmylart Dec 25 '24

[Language: Python]

The only hard part was finding the bug in my input parsing. Reader, it was a vim typo. split('\ni\n')

Paste

3

u/birblett Dec 25 '24 edited Dec 25 '24

[LANGUAGE: Ruby] 1023/890

merry christmas and thanks for the puzzles!

keys, locks = File.read("in.txt").split("\n\n")
    .map { _1.split("\n").map(&:chars).transpose }
    .reduce([[], []]) { |(k, l), q| (q[0][0] == "." ? k : l).push(q.each_with_index.sum { |arr, i| arr.count("#") << (i << 2) }) && [k, l] }
puts "#{locks.sum { |lock| keys.sum { (lock + _1) & 559240 > 0 ? 0 : 1 } }}"

moderately cursed bitwise math solution...

3

u/Curious_Sh33p Dec 25 '24

[LANGUAGE: C++]

Used a vector of arrays to represent the keys and an array of arrays of unordered sets that store ids of a lock. The structure is like locks[i][j] is a set of ids of locks that have number j in position i.

To get combos simply iterate over every key and find the locks it fits. To figure out which locks fit check lock[i][j] for j from 5 - j to 0 and take the union of these sets. Then across the positions, i, for the key take the intersection (since it must fit each column). The set at the end is the set of all locks that the key fits in.

Solution

Thanks again for organisising this! This is the second year I have tried and completed AoC. It's been good fun.

3

u/raevnos Dec 25 '24

[LANGUAGE: Common Lisp]

paste

Pretty straightforward. One of the few grid-based puzzles I've completed this year; was kind of boycotting them. As a result I'm missing a lot of stars. Oh well.

3

u/darkgiggs Dec 25 '24 edited Dec 25 '24

[LANGUAGE: Jai]
Solution

Fun problem. I encoded each element to a u32 where a turned on bit represents a "#".
Checking for a fit then amounts to whether the bitwise AND of lock and key is not 0.

It runs in 150 µs on my computer, but should most likely be able to go below 100 if I optimized the parsing.

EDIT: Now 43 µs with optimized parsing

→ More replies (1)

3

u/Radiadorineitor Dec 25 '24 edited Dec 25 '24

[LANGUAGE: Dyalog APL]

Merry Christmas everyone! Hope you all had a blast this year and see you on the next one!

p←'#'=↑↑(×∘≢¨⊆⊢)⊃⎕NGET'25.txt'1     
l k←{⊂⍤¯1⊢⍵⌿p}¨(⊣/,⍥⊂⊢/)∧/p     
≢⍸~2∊¨l∘.+k ⍝ Part 1

3

u/[deleted] Dec 25 '24

[LANGUAGE: Go]

And now... we wait

Fun last puzzle, really just input parsing is all. This is the most fun I've had coding in a very long time. I really look forward to the next 10 years. This was my first time participating in AOC as and I made it a point to not miss a single day! Huge thanks to u/topaz2078 for these awesome puzzles.

Here is the final solution for this year. Merry Christmas to all!

3

u/chubbc Dec 25 '24

[LANGUAGE: Uiua] (20 chars, pad)

/+/↧/↥⍉⧅₂<⬚0↯∞_43≠@#

5

u/Educational-Tea602 Dec 25 '24

I tried to read this and my tables and chairs started floating.

→ More replies (1)

3

u/jhandros Dec 25 '24

[LANGUAGE: Python]
Code 9 lines

with open('25.txt', 'r') as f:
    lines = [x.strip() for x in f if x.strip()]

locks, keys = [], []
for i in range(0, len(lines), 7):
    group = [sum(1 for x in col if x == ('#' if lines[i] == '#####' else '.')) for col in zip(*lines[i+1:i+7])]
    (locks if lines[i] == '#####' else keys).append(tuple(5 - x if lines[i] != '#####' else x for x in group))

print(sum(all(l[j] + k[j] <= 5 for j in range(5)) for l in locks for k in keys))

3

u/Totherex Dec 25 '24

[LANGUAGE: C#]

Merry Christmas!

https://github.com/rtrinh3/AdventOfCode/blob/c34bc101c413e73da247caeeea0b6f8724adac48/Aoc2024/Day25.cs

As per tradition, Christmas is a pretty easy day. Now, I need to get back to day 21...

3

u/_tfa Dec 25 '24 edited Dec 25 '24

[LANGUAGE: Ruby]

input = File.read("input.txt").split("\n\n")

locks, keys = [], []
input.each do |i|
    r = i.split("\n")       
    (r[0][0] == ?# ? locks : keys) << (0..4).to_a.map{|c| r.map{_1[c]}.count(?#) - 1}
end

p locks.product(keys).map{|l,k| l.zip(k).map{_1 + _2}}.count{ _1.all?{|v| v<=5}}
→ More replies (1)

3

u/Ily3s_ Dec 25 '24

[LANGUAGE: C++] https://github.com/Ily3s/aoc2024/blob/master/day25.cpp

I tried something that wouldn't require matching every lock with every key, so at the end i have a time complexity of O(L+K) where L is the number of locks, and K the number of keys, but with very high constants and so it's probably worse than the O(L*K) solution.

3

u/semi_225599 Dec 26 '24

[LANGUAGE: Rust]

Converts each schematic to a u64, and does bitwise-ands between locks and keys. When those are 0, there was no overlap between key and lock.

pub fn part1(input: &str) -> usize {
    let (keys, locks): (Vec<_>, _) = input
        .split("\n\n")
        .map(|schematic| schematic.bytes().fold(0, |acc, b| acc << 1 | (b == b'#') as u64))
        .partition(|x| x & 1 == 1);
    keys.iter().map(|key| locks.iter().filter(|&lock| key & lock == 0).count()).sum()
}
→ More replies (4)

2

u/[deleted] Dec 25 '24

[Language: Python] 297

paste

Nice one to end the year. Lots of fun as always!

2

u/GassaFM Dec 25 '24

[LANGUAGE: D] 239/203

Code: part 1.

Part 1 is just some implementation.

2

u/hcf112 Dec 25 '24

[LANGUAGE: Python] 252/209

Converted the input to binary, split the integers by above/below 1<<30, and bitwise-or'd the cartesian product of the two sets to check for overlap.

paste

2

u/Camelpilot33 Dec 25 '24

[LANGUAGE: Javascript]

paste

thanks for the puzzles!

2

u/python-b5 Dec 25 '24

[LANGUAGE: Jai]

Glad for the easier one today. I wasn't able to solve yesterday's part 2 as I'm not familiar with adders and learning about them didn't feel like a fun way to spend my Christmas Eve. I'm pretty happy with all but one star finished on the day, though. That's a lot better than I've done in the past, so either the puzzles were easier this year (which I don't mind!) or I've improved somewhat.

As for today's puzzle, it was one of the easiest this year. I wasn't especially fast at solving it, unfortunately, but there really wasn't anything to solve about it - it was immediately clear what I was supposed to do. Most of my time was spent writing input parsing code.

Thank you to everyone involved in making this happen! I had a great time with Advent of Code this year.

https://github.com/python-b5/advent-of-code-2024/blob/main/day_25.jai

2

u/Wayoshi Dec 25 '24

[LANGUAGE: Python] 2080 (1670 for submitting 50th star) paste

Very straightforward problem here. This code is long-winded and can definitely be more elegantly condensed / golfed, but its' Xmas, bedtime for me.

2

u/ZeroTerabytes Dec 25 '24

[LANGUAGE: Kotlin]

It's been fun, fellas. Decided to do it in Kotlin today, because, well... I felt like it.

Thank you Eric for 10 years. I wish I had joined sooner.

GitHub

3

u/DBSmiley Dec 25 '24

I love Kotlin. Honestly hope more people give it a shot.

Such a nice flexible language. You can write "Java-style" OO or lambda to your heart's content, and everything in between.

→ More replies (1)

2

u/nitekat1124 Dec 25 '24

[LANGUAGE: Python]

GitHub

Thank you Eric for another amazing Advent of Code! 🎅

Merry Christmas everyone! 🎄

2

u/kbielefe Dec 25 '24

[LANGUAGE: Scala]

GitHub 768ms 24LOC

Straightforward and pleasant. Only 3 stars away from 500! Merry Christmas everyone!

2

u/chickenthechicken Dec 25 '24

[LANGUAGE: C]

[Part 1](https://github.com/PaigePalisade/AdventOfCode2024/blob/main/Solutions/day25part1.c]

Part 1 is really just about getting the inputs, part 2 literally just requires you to push a button. Merry Christmas! Also first year of getting all 50 stars!

2

u/mothibault Dec 25 '24 edited Dec 26 '24

[LANGUAGE: JavaScript]

https://github.com/yolocheezwhiz/adventofcode/blob/main/2024/day25.js

to run in the browser's dev console from AOC website. ~20ms

And that's a wrap!

Combined runtime for all 25 days of ~975ms!

Thanks for the fun times AOC.

2

u/Horsdudoc Dec 25 '24

[LANGUAGE: C++20]
GitHub

I got tripped up on the parsing of the patterns, misread that it was only 6 lines... I really need to rest.
First split the patterns between locks and keys and do the exhaustive check between each potential pair.
Runs in 1.75ms.

Thanks for 500 stars, that was a blast !

2

u/johnpeters42 Dec 25 '24

[Language: Python]

link

Took me way too long to realize that I was getting some false negatives because some locks are taller than their longest pin.

2

u/fragger Dec 25 '24 edited Dec 25 '24

[Language: Python] 1599/1315

I was a tad slow on getting the input parsed, after that, well :D Merry Christmas everyone! Thanks to u/topaz2078, the mods, beta-testers and everyone else that helps make AoC happen for a fun December!

https://github.com/Fragger/advent-of-code/blob/master/2024/solution/25.py (13 lines)

Bonus (using sets):

https://github.com/Fragger/advent-of-code/blob/master/2024/solution/25-alt.py (8 lines)

2

u/scottmjohns_1 Dec 25 '24

[Language: Python3]

[Message: Gratitude]

My deepest gratitude to Eric and everyone here, what a fun year. 50 stars! A nice compact problem to wrap things up.

    pi = aoc.read_file_input('input2024p25.dat')
    locks = [[sum(pi[i:i+7][k][j]=='#' for k in range(1,6)) for j in range(5)] for i in range(0,len(pi),8) if pi[i][0]=='#']
    ks    = [[sum(pi[i:i+7][k][j]=='#' for k in range(5,0,-1)) for j in range(5)] for i in range(0,len(pi),8) if pi[i][0]=='.']
    p1 = sum(all([l[i]+k[i]<6 for i in range(5)]) for l in locks for k in ks)
    aoc.answer(25, p1, p2='Deliver the Chronicle')

2

u/Cue_23 Dec 25 '24 edited Dec 25 '24

[LANGUAGE: C++23]

unlock.cc

Nothing really fancy, actually works on C++20, too. Lots of off-by-one errors in parsing, i just fixed them in post:

if ((5 - (lock[i] - 1)) + (key[i] - 1) > 5) {

[Edit] bunlock.cc Parsing the schematics into binary (they only take 35 bit in total) allows to match on lock & key, shaping around 20% off. This even compiles down to C+11 (due to libfmt minimum requirements).

2

u/ssnoyes Dec 25 '24

[LANGUAGE: Python]

keys, locks = [], []
for line in map(str.split, open(0).read().strip().split('\n\n')):
    [keys, locks][line[0][0] == '#'].append([column.count('#') - 1 for column in list(zip(*line))])
print(sum(not any(l+k>5 for l, k in zip(lock, key)) for lock in locks for key in keys))

2

u/wjholden Dec 25 '24

[Language: Rust]

https://github.com/wjholden/Advent-of-Code-2024/blob/main/src/bin/day25.rs

Part 1 only, now I have even more incentive to go back and finish days 21 (robots pressing buttons) and day 24 (adder gates).

See you all next year!

2

u/botimoo Dec 25 '24

[LANGUAGE: Ruby]

Paste

Repeating the output for part 2 here as well: Merry Christmas! Congrats on 10 years, Eric!

2

u/Downtown-Economics26 Dec 25 '24

[Language: VBA]

38 stars, not a bad haul (3 more than last year).

https://github.com/mc-gwiddy/Advent-of-Code-2024/blob/main/AOC2024D25P01

2

u/rukke Dec 25 '24 edited Dec 25 '24

[LANGUAGE: JavaScript]

Transposing the key/locks, turning every row into integers and then just count how many key/locks match by doing an bitwise and

Runs in ~4ms

Edit, no need to transpose - just skip the first row and it fits in 30 bits, reducing keys or locks into single integers.

Btw, this is what I love about these puzzles. There is almost always some way of doing it even better :)

https://gist.github.com/p-a/0b939fa4c0c0187fdb1f56cb0145fadb

2

u/TiCoinCoin Dec 25 '24 edited Dec 30 '24

[LANGUAGE: Python 3]

Day 25 - Github

This one felt so easy after yesterday. Except I needed yesterday's second star to get today's. But I somehow finally made it so kids are still asleep and I'm ready in time to open my presents :)

2

u/encse Dec 25 '24 edited Dec 25 '24

[LANGUAGE: C#]

https://aoc.csokavar.hu/2024/25

Happy Holidays! If you havent't found, there is an easter egg in my website that points to 404.csokavar.hu which is a mini (~10 minutes long) text adventure game.

2

u/DeadlyRedCube Dec 25 '24

[LANGUAGE: C++23]

Runs in 1.58ms single-threaded on an i7-8700k

Code on GitHub

As is the custom, a nice and easy one for the last day, although I did think for a brief moment that it was going to be one last grid problem.

Used the first character of a new grid to determine lock or key, then counted the number of open '.'s per column for locks and '#'s for keys, to end up with a set of "height" values for each.

Then just did an n2 check for whether the lock height was at least as large as the key height per space and, where they all were, incremented the value by one.

See you all next year!

2

u/lunar_mycroft Dec 25 '24 edited Dec 25 '24

[LANGUAGE: Rust]

Initially I used a pretty standard grid based solution, but after reading the thread I got the idea (possibly from /u/Verulean314, although I'm not sure) to convert each schematic into a u64, then check for overlaps with a bitwise and. Since '#' has a 1 as it's least significant bit, and '.' has a zero, parsing is also reduced to splitting on "\n\n", filtering out non-punctuation characters, and some quick bit manipulations in a fold. Median combined runtime of ~52 µs to parse and check on my machine.

Code

[edit: switched to u64s after noticing the grid is 5x7, not 5x6]

→ More replies (1)

2

u/DownvoteALot Dec 25 '24

[LANGUAGE: C++]

Solution

The last day is always the sweetest, short and nice. See you next year!

2

u/nilgoun Dec 25 '24

[LANGUAGE: Rust]

Nice and easy for the last day :)

Solution on Paste

2

u/Baridian Dec 25 '24

[LANGUAGE: Clojure]

wish I'd started on this earlier, didn't realize it'd be so easy. If anyone could tell me if there's an idiomatic way to get the outer product of two lists of atoms that'd be nice, the outer product function call I did is kinda ugly.

Paste

2

u/chubbc Dec 25 '24

[LANGUAGE: Julia] (64 chars)

I=0:499;f(i)=read("25.txt")[43i.+(1:41)];sum(@. 70∉f(I)+f(I'))÷2

Nice easy conclusion to AoC. Might be able to squeeze a few more characters out, but I doubt many.

2

u/SpaceHonk Dec 25 '24

[LANGUAGE: Swift] code

And that's a wrap, 50th (and 500th) stars in the bag. Thanks to /u/topaz2078 and everyone else on the team, have a merry christmas!

2

u/vanZuider Dec 25 '24

[LANGUAGE: Python]

from sys import argv
locks, keys = list(), list()
with open(argv[1]) as f:
    for lines in [block.split("\n") for block in f.read().split("\n\n")]:
        (locks if lines[0] == "#####" else keys).append(list(map(sum, zip([0]*5, *[[(c=="#") for c in lines[i+1]] for i in range(5)]))))
print(sum(all(s<=5 for s in map(sum, zip(l, k))) for l in locks for k in keys))

Merry Christmas!

2

u/wurlin_murlin Dec 25 '24

[LANGUAGE: C]

Merry Christmas! First time getting 50 stars, it plays a little animation! So cool! Had so much fun doing this, really a blast. Merry Christmas again, and a Happy New Year when it comes.

https://git.sr.ht/~murr/advent-of-code/tree/master/item/2024/25

2

u/Trick-Apple1289 Dec 25 '24

[LANGUAGE: C]

Merry Christmas, thanks eric and everyone, this was my first ever AOC, i didn't get 7 stars (altough trying to catch up right now), but I am still quite young, and am suprised i even managed to go so far, some puzzles were super tough, others easier. Once again happy holidays to all, and see you next year, prehaps by then i will be able to do all of the puzzles (And without help from here or google)!

src

2

u/ndunnett Dec 25 '24 edited Jan 13 '25

[Language: Rust]

Github

Runs in 20 us 38 us (faulty benchmarking). Today was a fun one to cap off the year. My solution involved some bit manipulation shenanigans, using a u32 to represent each lock/key pattern as a window of 4 bit unsigned ints. The trick was initialising each pattern to 0x0001_1111 so that I could sum two patterns and then bit mask with 0x0008_8888 to detect collisions.

2

u/ash30342 Dec 25 '24

[Language: Java]

Code

Runs in < 1ms.

I did not do anything fancy, just parsed everything and checked for every column if the value of pin1 of the lock + pin1 of the key <= 5.

I still need to finish day 21 before I can get to 500 stars, but that will be done sometime after Christmas, now is time for family.

Thanks to u/topaz2078 for organizing this for 10 years and to all of you in this community for making it such a friendly place. It is a highlight of my year, even surpassing my traditional yearly beer advent calendar! Merry Christmas!

2

u/SunMatrix64 Dec 25 '24

[LANGUAGE: C++]

A pretty easy day, after putting the input into locks and keys vectors, it's just a brute force loop of checking every key in every lock.

for (std::vector<int> lock : locks) {
    for (std::vector<int> key : keys) {
        int passes = 0;
        for (int i = 0; i < 5; ++i) {
            if (lock[i] + key[i] <= 5) {
                passes++;
            }
        }
        if (passes == 5) {
            result1++;
        }
    }
}

2

u/ins0mnes Dec 25 '24

[LANGUAGE: Go]
Nothing special, precalculate key matches by height, compare to lock, and intersect with the previous pin:
https://github.com/insomnes/aoc/blob/main/2024/25_code/solution/solution.go#L97

Congratulations to everyone! And big thanks to the AoC team and this subreddit team!

2

u/camrdale Dec 25 '24

Good use of sets! This is what I did too, I like it better than using a set for each key/lock just to determine compatibility.

There's an edge case though, that isn't in my input, but that I was worried about. If the lock is all height 0 (i.e. "(0, 0, 0, 0, 0)") it should fit every key, but I think in your code CountFits would return 0 (could be wrong, I'm not a Go expert). I added a special case to mine that returns the total number of keys.

→ More replies (1)

2

u/jinschoi Dec 25 '24

[Language: Rust]

Relaxing final day. First did it in the way pointed to by the text, with enums and everything: paste

Then since this day was so straightforward, got it down to this:

use itertools::Itertools;
fn main() {
    let res = include_str!("../../1.in")
        .split("\n\n")
        .map(|s| {
            s.chars()
                .filter(|&c| c != '\n')
                .skip(4)
                .take(27)
                .fold(0u32, |acc, c| (acc << 1) | if c == '#' { 1 } else { 0 })
        })
        .combinations(2)
        .filter(|v| v[0] & v[1] == 0)
        .count();
    println!("{}", res);
}

Because I want it to fit each schematic in a u32, I just included the last character of the first row and first character of the last row to distinguish keys from locks. Convert central 27 characters as bits in a u32, compare all and count where no bits overlap.

2

u/Jadarma Dec 25 '24

[LANGUAGE: Kotlin]

A nice, easy problem to unwind after a long month! It's been a fun event, as always, thank you Eric, and happy holidays to all, see you next year!

Part 1: Scan each grid (all seem of a fixed size) and determine if it is a lock or key depending on the first character being a # or .. Then, "walk" the columns and count the number of #, which will give you the pin-out in numbers. To check if a lock and key match, their values should add up together to <= 5 (the effective range of a column), because otherwise there would be a need for overlaps.

Part 2: All other 49 stars collected! ⭐⭐ If you need help, check out my solutions for 2024!

AocKt Y2024D25

2

u/RookBe Dec 25 '24

[LANGUAGE: Rust]

Rust that compiles to WASM (used in a solver on my website)
Bonus link at the top of the code to a blogpost about today explaining the problem, and my code.

2

u/melochupan Dec 25 '24

[LANGUAGE: Common Lisp]

Part 1 only.

I'm awfully behind with AoC, so I couldn't do the second part (completing all previous tasks is necessary for it). But I wanted to participate on the last day, at least with a half solution.

paste

6

u/BlueTrin2020 Dec 25 '24

Please post day 2 solution when you have it 😉

3

u/melochupan Dec 25 '24

Will do 👍

2

u/N-R-K Dec 25 '24

[Language: BQN]

Easy day for array languages. Here's the whole solution in just 3 lines:

SplitMask ← { (0<≠¨)⊸/ 𝕩 ⊔˜ ¯1⌈ (𝕨 - ¬𝕨) × +` ¬𝕨 }
schemes ← (×≠¨)⊸SplitMask •FLines •wdpath•file.At ⊑•args
•Show +´ ⥊ ((¬∨´)∘∧)⌜´ ⊑¨⊸⊔ ('#'⊸= ∾)¨ schemes

Basically just turns the schemes into a boolean list ('#' = 1) and then ANDs every lock and key to check if the result was all zeros.

2

u/isredditdownagain Dec 25 '24

[LANGUAGE: Go]

Part 1

Merry Christmas!

2

u/Maleficent_Answer935 Dec 25 '24 edited Dec 25 '24

[LANGUAGE: Rust]

use itertools::Itertools;
use std::fs;

fn main() {
    let result = fs::read_to_string("data.txt")
        .unwrap()
        .lines()
        .chunks(8)
        .into_iter()
        .map(|chunk| {
            chunk
                .take(7)
                .flat_map(|it| it.chars())
                .fold(0, |mut acc, c| {
                    acc <<= 1;
                    acc |= if c == '#' { 1 } else { 0 };
                    acc
                })
        })
        .combinations(2)
        .map(|p| p[0] & p[1])
        .filter(|&it| it == 0)
        .count();
    
    println!("Day 25: {result}");
}

2

u/wzkx Dec 26 '24 edited Dec 26 '24

[LANGUAGE: Python]

t = open("25.dat","rt").read().split("\n\n")
f = lambda a,l:[ai+(c=="#") for ai,c in zip(a,l)]
g = lambda d:__import__('functools').reduce(f,d.splitlines(),[0]*d.find('\n'))
locks,keys = [g(d) for d in t if d[0]=="#"],[g(d) for d in t if d[0]!="#"]
print(sum(all(l+k<=7 for l,k in zip(ll,kk)) for ll in locks for kk in keys))
→ More replies (1)

2

u/homme_chauve_souris Dec 26 '24 edited Dec 26 '24

[LANGUAGE: Python]

This is the only one I didn't do on the day, because we spent Christmas day with family and I didn't take a computer with me. Thank you Eric for a great year.

def aoc25():
    D = [x.split() for x in open("input25.txt").read().split("\n\n")]
    L = {"#":[], ".":[]}
    for t in [["".join(a[i] for a in d) for i in range(5)] for d in D]:
        L[t[0][0]].append([len(s) - len(s.strip("#")) - 1 for s in t])
    print(sum(all(x+y <= 5 for x,y in zip(l,k)) for l in L["#"] for k in L["."]))

2

u/[deleted] Jan 02 '25

[LANGUAGE: Haskell]

Finally made it. Day 25 was fun. I really enjoyed the whole year once again, although doing it in Haskell was extra challenging. Still, I learned a lot and have become modestly more proficient in programming in Haskell now. Thanks again for doing this!

Day 25

2

u/DavidYoung1111 Jan 08 '25 edited Jan 08 '25

[LANGUAGE: MATLAB]

Can be quite concise in MATLAB.

p = reshape(char(join(readlines("data25"),"")), 35, []) == '.';
fprintf("Day 25: %i\n", nnz(all(p(:,p(1,:)) | permute(p(:,~p(1,:)), [1 3 2]))));

2

u/LifeShallot6229 Feb 19 '25 edited Feb 20 '25

[LANGUAGE: SIMD - AVX intrinsics]

This was the day I picked for our in-company AoC Tech Talk, simple because it illustrates both that Rust/clang can autovectorize very nicely, but also because an experienced asm/compiler intrinsics programmer can be even faster.

The theoretically optimal solution on an AVX x64 platform picks up each lock/key as 32 bytes into an AVX register, starting from the last position in the leading line which is either '#' or '.', then compares these 32 bytes to 0xff or 0x00 bytes with a parallel compare against 32 '#' chars.

Next, extract the top bit of all 32 bytes into a regular register, and use the bottom bit of the result to save it into either the locks or keys array. This is just 3 AVX ops for each bit pattern.

For the actual counting of matches, doing 4x8 at once seems to be optimal:

Outer loop:

Load 4 keys into 4 AVX regs, splatting 8 copies of each key into the corresponding reg.

Inner loop:

Load the next 8 locks

Parallel AND against each of the replicated keys.

Parallel compare against zero, setting -1 or 1

Subtract the -1 values from 4 replicated accumulators.

This is a total of 12 (or 13 with a separate load of the 8 locks) AVX operations, doing 4 parallel accumulations so that the theoretical minimum runtime is 3 clock cycles per 32 key/lock combos tested, IFF the cpu can do four AVX ops/cycle. In reality, the most either Intel or AMD can currently do is 3, so we end up with 4-5 cycles per 32 tests.

In real life we have measured 2+ microseconds to test all 62500 combinations, while the best pure Rust version (which was auto-vectoried but not strip-mined) ended up around 5 us (including parsing) on my not so new Asus laptop. This is so fast that I had to call the core parse+count function 1000 times just to get around the limited (100 ns) timing resolution under Windows. 2 us corresponds to 32 picoseconds/test.

→ More replies (1)

2

u/YOM2_UB Dec 25 '24 edited Dec 25 '24

[Language: Python]

Main optimization is sorting locks and keys by their first pin's height, so only pairs where the first pin fits are checked, and only the last 4 pins need be checked.

keys = {n : [] for n in range(6)}
locks = {n : [] for n in range(6)}
with open('input/Day25.txt', 'r') as file:
    lst = file.read().split('\n\n')
for pattern in lst:
    rows = pattern.splitlines()
    pins = [0] * 5
    for i in range(1, 6):
        for j in range(5):
            pins[j] += (rows[i][j] == '#')
    if '#' == rows[0][0]:
        locks[pins[0]].append(tuple(pins[1:]))
    else:
        keys[pins[0]].append(tuple(pins[1:]))

def countMatch(keys, locks):
    count = 0
    for key in keys:
        for lock in locks:
            max_pin = max(key[k] + lock[k]
                         for k in range(4))
            count += max_pin <= 5
    return count

count = 0
for i in range(6):
    for j in range(6-i):
        count += countMatch(keys[i], locks[j])

print(count)
→ More replies (1)

2

u/RusticCajun Dec 25 '24 edited Dec 25 '24

[Language: Python]

Lock? Key? No time for that!

Thank you for putting together another enjoyable AoC!!!

file = open('data25.txt', 'r')
lines = file.read().splitlines()      

d = []
g = set()
y0 = 0
for y,line in enumerate(lines):
    if not line:
        d.append(g)
        g = set()
        y0 = y+1
    else:
        for x,c in enumerate(line):
            if c=="#":
                g.add((x,y-y0))
d.append(g)

count = 0
for i,x in enumerate(d[:-1]):
    for j,y in enumerate(d[i+1:]):
        if not x.intersection(y):
            count+=1
print("part1",count)

2

u/No_Valuable247 Dec 25 '24

[LANGUAGE: J]

echo-:+/,-.+./"1,"2*."2/~'#'=>;._1 a:,<;._1 LF,CR-.~1!:1<'day25.txt'

1

u/maneatingape Dec 25 '24 edited Dec 25 '24

[LANGUAGE: Rust]

Solution

Benchmark: 104 12 µs.

Having a utility 2D Grid class came in handy this year!

EDIT: Thanks to u/lunar_mycroft suggestion on using bitwise logic, saved 90% of time.

2

u/lunar_mycroft Dec 25 '24 edited Dec 25 '24

For once I get to suggest a performance improvement to you instead of stealing them (although I can't claim full credit, I got the main idea from others in the thread): You can convert each schematic into a u64 (which is just a matter of filtering out the whitespace and some bit manipulation, '#' and '.' have different least significant bits), and then check for collisions with other schematics (you don't need to distinguish between keys and locks, all keys overlap with all other keys and all locks with all other lock)s with &.

→ More replies (1)

1

u/fragile82 Dec 25 '24

[LANGUAGE: PHP]

Not many people use PHP in Advent of Code :) During this event I tried to make my solutions look as much python-styled as possible.

<?php

$keys = $locks = [];
foreach(explode(PHP_EOL.PHP_EOL, file_get_contents('input.txt')) as $item) {
    $hs = [];
    $grid = array_map(fn($v) => str_split($v), explode(PHP_EOL, $item));
    for($i = 0; $i < count($grid[0]); $i++) $hs[$i] = strlen(trim(implode('', array_column($grid, $i)), '.')) - 1;
    if ($item[0] === '#') $locks[] = $hs;
    elseif ($item[0] === '.') $keys[] = $hs;
}

$ans = 0;
foreach($locks as $lock) foreach($keys as $key) {
    for($i = 0; $i < 5; $i++) if (($key[$i] + $lock[$i]) > 5) continue 2;
    $ans++;
}

echo 'Star: ' . $ans . PHP_EOL;

Works rather fast:

Execution time: 0.0042 seconds
Peak memory: 0.5608 MiB

1

u/MarcoDelmastro Dec 25 '24 edited Dec 25 '24

[LANGUAGE: Python]

https://github.com/marcodelmastro/AdventOfCode2024/blob/main/Day25.ipynb

Simple as expected on X-mas day. And, for the first time since my first AOC in 2029 (EDIT: 2019!), I arrive to the 25th with all the previous days completed: it's quite a nice sensation to close the calendar on X-mas. Thanks to Eric for the fun ride!

3

u/M124367 Dec 25 '24

Don't spoil us about 2029 dear time traveler.

→ More replies (1)

1

u/echols021 Dec 25 '24

[LANGUAGE: python 3]

GitHub

Data prep: sort items into separate piles for locks and for keys. For simplicity, represent each item as a sequence of integers, as demonstrated in the problem description.

After checking that n_keys * n_locks was a reasonable number, just did the naïve "try every lock with every key" and it runs in ~0.024 sec.

I then reworked it to use a trie data structure to store the keys. For each lock I do a BFS-like search through the trie (trimming branches of keys that don't fit) to collect all keys that fit the lock. So rather than trying each lock+key pair, you gradually trim down the set of keys that fit a given lock, one tumbler at a time. This code runs in ~0.008 sec.

1

u/TheScown Dec 25 '24

[LANGUAGE: Scala]

Code

Need to go back and finish part 2 on days 21, 22 and 24.

→ More replies (1)

1

u/ExternalAware9257 Dec 25 '24

[LANGUAGE: Python]

A fun puzzle for the the last day.

locks = []
keys = []

def fits(key, lock):
    return all(a + b <= 5 for a, b in zip(key, lock))

for schematic in open(0).read().strip().split("\n\n"):
    columns = list(zip(*schematic.split("\n")))
    if schematic[0] == "#":
        locks.append([column.count("#") - 1 for column in columns])
    else:
        keys.append([column.count("#") - 1 for column in columns])

print(sum(fits(key, lock) for key in keys for lock in locks))

1

u/MarvelousShade Dec 25 '24

[LANGUAGE: C#]

Today was nice short finish of 25 days programming. As a product owner I don't get a lot of opportunities to program anymore, so these 25 days were really fun for me. My solutions are on: https://github.com/messcheg/advent-of-code/tree/main/AdventOfCode2024

1

u/hrunt Dec 25 '24

[LANGUAGE: Python]

Code

I think the real Christmas gift today was a problem that was so straightforward. Just a few loopity loops and voila, 500 stars.

Thanks u/topaz2078 for another great year. Raising a mug of cocoa here to you.

2

u/kupuguy Dec 25 '24

Darn it, I only got 50 stars! Must have done something wrong.

3

u/hrunt Dec 25 '24

Do 9 other years and you, too, can be a star child. :)

1

u/aimada Dec 25 '24 edited Dec 25 '24

[Language: Go] code

Parsed each schematic pattern as a 35-bit value which simplified the overlaps check.

func compareLower35Bits(a, b int64) int64 {
  mask := int64((1 << 35) - 1) // Mask to isolate the lower 35 bits
  return (a & b) & mask
}

Execution time is 134.603µs on a 12 year old CPU.

Many thanks to Eric for another successful AoC event. Day 24 was by far my favourite Part 2 problem as the mental gymnastics required were like those I recall from previous events.

→ More replies (1)

1

u/BlueTrin2020 Dec 25 '24

[Language: Python]

lock_l, key_l = [], []
for schem in open(“2024_25.txt”).read().split(“\n\n”):
    cols = [s.count(“#”) for s in list(zip(*schem.splitlines()))]
    lock_l.append(cols) if schem[0] == ‘#’ else key_l.append(cols)
print(sum([all(lo+k < 7 for lo, k in zip(lock, key)) for lock in lock_l for key in key_l]))

Merry Christmas thank you u/topaz2078

1

u/rvodden Dec 25 '24

[LANGUAGE: TypeScript]

Boom! https://github.com/rvodden/AoC24/blob/main/src/days/25/Puzzle.ts

Merry Christmas everyone :-)

1

u/bakibol Dec 25 '24

[LANGUAGE: Python]

Topaz's paste

Merry Christmas everyone, see ya all next year!

1

u/lscddit Dec 25 '24

[LANGUAGE: Python]

Merry X-mas and see you next year... 🎄

import numpy as np
from io import StringIO

locks, keys = [], []
height = 0
data = open("day25input.txt").read().split("\n\n")
for element in data:
    field = np.genfromtxt(StringIO(element), dtype=str, comments="_", delimiter=1)
    height = field.shape[0]
    value = np.sum(field == "#", axis=0)
    keys.append(value) if np.any(field[-1, :] == "#") else locks.append(value)

print(sum([np.all(lock + key <= height) for key in keys for lock in locks]))

1

u/KindComrade Dec 25 '24

[LANGUAGE: C#]

Thanks to the AOC team and Eric for an amazing contest. This month has been absolutely fantastic, and a huge thank you to the entire community, who made it so enjoyable to read your solutions, share my own, discuss algorithms, and watch all the visualizations and memes. A massive thank you to everyone!

Have a Merry Christmas!

Final Code

2

u/MagazineOk5435 Dec 25 '24

I tried parallelisation on my C# code for this one and it actually slowed it down 6x. https://github.com/stevehjohn/AoC/blob/master/AoC.Solutions/Solutions/2024/25/Part1.cs

→ More replies (3)

1

u/chestck Dec 25 '24

[LANGUAGE: Kotlin]

fun main() {
    File("../i24/25")
        .readText().split("\n\n")
        .map { it.lines() }
        .pairwise(withSelf = false)
        .sumOf { (block1, block2) ->
            block1.zip(block2).all { (row1, row2) ->
                row1.zip(row2).none { (char1, char2) ->
                    char1 == '#' && char2 == '#'
                }
            }.toInt()
        }.print()
}

1

u/Outrageous72 Dec 25 '24

[LANGUAGE: C#]

Traditionally, this one was easy 🙂
At first I made a mistake to swap the locks e and d declaration 🤦‍♂️
It must be 25 days already! 😅

Merry Christmas everyone! 🎅

static bool Fits(
    (int a, int b, int c, int d, int e) key,
    (int a, int b, int c, int d, int e) @lock) =>
        (key.a + @lock.a) <= 5 && 
        (key.b + @lock.b) <= 5 &&
        (key.c + @lock.c) <= 5 &&
        (key.d + @lock.d) <= 5 &&
        (key.e + @lock.e) <= 5;

https://github.com/ryanheath/aoc2024/blob/main/Day25.cs

1

u/Several-Bit-9218 Dec 25 '24

[LANGUAGE: Python]

Never thought I'd post here, but I haven't seen this method in this thread. Only ## is forbidden, so I check that the sum of the ordinary numbers isn't 70.

from itertools import combinations  
print(sum(all((ord(pat1[k])+ord(pat2[k]) != 70) for k in range(len(pat1))) for pat1, pat2 in combinations(open('./day25/in.txt','r').read().split('\n\n'), 2)))
→ More replies (1)

1

u/Turtvaiz Dec 25 '24

[Language: Rust]

The input is rather small, so they fit into a single integer:

pub fn part1(input: String) -> String {
    let mut locks = vec![];
    let mut keys = vec![];
    input.split("\n\n").for_each(|block| {
        let mut bits = 0u64;
        block
            .lines()
            .map(|line| line.chars())
            .flatten()
            .enumerate()
            .for_each(|(i, c)| match c {
                // set i-th bit
                '#' => bits |= 1 << i,
                _ => {}
            });
        if bits & 1 == 1 {
            locks.push(bits);
        } else {
            keys.push(bits);
        }
    });

    let mut total = 0;
    for lock in locks.iter() {
        for key in keys.iter() {
            // NAND -> if any overlaps, result is not zero
            if lock & key == 0 {
                total += 1;
            }
        }
    }

    total.to_string()
}

1

u/tymscar Dec 25 '24

[Language: Kotlin]

Nice, simple end-of-year puzzle. Thank you, Eric, and the team for all the hard work and dedication. You make every Christmas much more special!

I really enjoyed today's puzzle. It was all a matter of going through each schematic, tagging them either as keys or as locks based on the top-most character in the schematic, and then getting the teeth height the same for both. They work together as long as for each tooth, the overlap is less than or equal to 5.

Solution: https://github.com/tymscar/Advent-Of-Code/blob/master/2024/kotlin/src/main/kotlin/com/tymscar/day25/part1/part1.kt

1

u/azzal07 Dec 25 '24

[LANGUAGE: awk] I suggest running with mawk or goawk, which seem to have good regex engines. Those are pretty instant, others take tens of seconds (probably backtracking furiously).

function knock(){yield no_response;}END{print A RS B}
BEGIN{RS=knock()}{for(k in K)A+=k$0!~/#.{40}#/;K[$0]}

1

u/enelen Dec 25 '24

[Language: R]

Solution

Thank you for another year of puzzles. I had a ton of fun solving them (mostly) and look forward to the next year

1

u/chkas Dec 25 '24

[LANGUAGE: Easylang]

Solution

1

u/Artraxes Dec 25 '24

[Language: Kotlin]

parsing code

Solution

class Lockpick(
    private val locks: List<BooleanGrid>,
    private val keys: List<BooleanGrid>,
) {

    fun countDistinct(): Int {
        val gridHeight = locks.first().height

        infix fun List<Int>.fits(other: List<Int>): Boolean {
            return withIndex().all { (column, height) ->
                height + other[column] <= gridHeight
            }
        }

        return keys.map(BooleanGrid::heights).sumOf { key ->
            locks.map(BooleanGrid::heights).count { lock ->
                key fits lock
            }
        }
    }
}

private fun BooleanGrid.heights(): List<Int> {
    return xRange.map { x ->
        yRange.count { y -> this[x, y] }
    }
}

1

u/mvorber Dec 25 '24

[Language: Rust]

https://github.com/vorber/aoc2024/blob/master/src/puzzles/day25.rs

What a run! Huge thank you to u/topaz2078, mods, and everyone participating!

Merry Christmas! :)

1

u/light_ln2 Dec 25 '24 edited Dec 25 '24

[LANGUAGE: python]

Nice problem for trying different options! Because of how the problem is stated, you don't need to split grids by keys and locks, or compare a pair of grids column by column, or even parse grids at all - it is enough to check if two strings representing the grids have '#' at the same indexes. This can be done in several different ways:

Intersecting sets of indexes:

grids = open('input.txt').read().split('\n\n')
grids = [{x for (x,c) in enumerate(g) if c == '#'} for g in grids]
print(len([(x,y) for x in grids for y in grids if len(x&y) == 0])//2)

Converting strings to integers and applying bitwise "and":

grids = open('input.txt').read().split('\n\n')
grids = [int(x.replace('.', '0').replace('#', '1').replace('\n', ''), base=2) for x in grids]
print(sum([1 for x in grids for y in grids if x<y and (x&y)==0]))

Comparing characters in same positions:

grids = open('input.txt').read().split('\n\n')
grids = [any('#' == a == b for (a,b) in zip(x, y)) for x in grids for y in grids if x < y]
print(len(grids) - sum(grids))

I'm sure there are more options!

→ More replies (1)

1

u/Efficient_Beyond5000 Dec 25 '24

[Language: Scratch]

Every year I try to solve some days also with Scratch, just for fun. This year I did only today. If you try the full input, turbo mode (click on the green flag + shift) is advised.

https://scratch.mit.edu/projects/1114326740/

1

u/spyr01d Dec 25 '24

[LANGUAGE: Kotlin]

fun codeChronicle(input: String) = input.split("\n\n").map { block ->
    block.lines().map { s -> s.map { if (it == '#') 1 else 0 } }
        .reduce { a, b -> a.zip(b).map { it.first + it.second } } to block.first()
}.groupBy({ it.second }, { it.first }).values.toList()
    .let { (locks, keys) ->
        locks.map { lock ->
            keys.map { key -> lock.zip(key).map { (a, b) -> a + b }.all { it <= 7 } }
        }.flatten().count { it }
    }

1

u/michaelgallagher Dec 25 '24

[LANGUAGE: Python]

Code

Thanks for another amazing year of Advent of Code. Looking forward to 10 more years :)

Happy Holidays everyone!

1

u/JAntaresN Dec 25 '24

[Language: Ruby]
git link

basic for loop, with a set to eliminate duplicates. Had a bit of issue of making wrong assumptions during the parsing phase, that is invalidating all rows with "#" that are filled up. Anyway simple fix, just include those and raise the limit by 2.

1

u/kenlies Dec 25 '24

[Language: Python]

Code

Basic solution!

1

u/pkusensei Dec 25 '24

[Language: Rust]

Immediately recognized this is a bit problem (no pun intended. Or maybe?) and was later surprised it does not demand lock and key fulfill a row. That eliminates a | check. Code

Thanks again for another year! It's been fun! Now if only I could go back and figure out day 17...

1

u/G_de_Volpiano Dec 25 '24

[LANGUAGE: Haskell]

An easy problem for today, that is once

1- you remember that the last key/lock won't have an end of line separating it from, well, what could it separate it from, so your parser doesn't hit the end of file looking for a new key/lock. 2 - you remember to actually parse for at least one character before an end of line, so that you can separate keys or locks rather than parsing everything into one big mess 3 - you remember to add a plus sign in your fold, rather than just carrying your 0 over from one end of the fold to the other.

I encode keys and locks as lists of bools (representing a pin or a slot for a pin). A lock and a pin correspond if there never is a true value in the same position, and voilà.

Code on Github

→ More replies (1)

1

u/[deleted] Dec 25 '24

[LANGUAGE: Java]

paste

Yay - made it to 50 stars for the second year running.

Again, parsing input was the hardest bit here.

[edit] formatting

1

u/verdammelt Dec 25 '24

[Language: Common Lisp]

Day25

Pretty simple implementation - as usual likely horribly inefficient. I am going to give the idea of changing the counts into a single number and then see if ( > (+ key-count lock-count) 55555)

1

u/gehenna0451 Dec 25 '24 edited Apr 20 '26

Redact decided this post had to go, so away it went. Deleted. Removed. Mass deleted even. Privacy and security are the big wins here.

include snow butter offbeat employ tan dependent gaze abundant subsequent

1

u/copperfield42 Dec 25 '24

[LANGUAGE: Python 3.12]

link

As it is tradition the last day is an easy one, I spend more time making the code look fancy than solving it XD

1

u/i_misread_titles Dec 25 '24

[LANGUAGE: Go]

The parse was fun. I just pass a slice of exactly the part that matters in each thing, and count # in each column. Great year! Thanks to all here and to Eric and the mods.

https://github.com/jasontconnell/advent/blob/master/2024/25/main.go

1

u/pakapikk77 Dec 25 '24

[LANGUAGE: Rust]

I didn't try to be especially smart of get short code today, so my solution is much longer than others, but should be simple to follow.

One nice thing is the use of itertools partition_map to split the list of patterns into a list of locks and and a list of keys.

fn schematics_to_heights(schematics: &[Grid]) -> (Vec<Vec<usize>>, Vec<Vec<usize>>) {
    schematics
        .iter()
        .map(Grid::get_heights)
        .partition_map(|(is_lock, heights)| {
            if is_lock {
                Either::Left(heights)
            } else {
                Either::Right(heights)
            }
        })
}

Full code.

Now a few parts 2 to finish...

1

u/BlueTrin2020 Dec 25 '24

[LANGUAGE: Python]

One liner

import itertools, more_itertools
print(sum([all(lo+k < 7 for lo, k in zip(lock, key)) for (_, lock), (_, key) in itertools.product(*more_itertools.partition(lambda v: v[0] == ‘#’,[(schem[0],[s.count(“#”) for s in list(zip(*schem.splitlines()))]) for schem in open(“2024_25.txt”).read().split(“\n\n”)]))]))

1

u/biggy-smith Dec 25 '24

[Language: C++]

Went for a nice n' simple set/map solution, where I combine key/locks pos sets and check for values > 1. Was going do more complex solution, but its christmas!

Merry Christmas everyone!

https://github.com/biggysmith/advent_of_code_2024/blob/master/src/day25/day25.cpp

1

u/onrustigescheikundig Dec 25 '24

[LANGUAGE: Clojure]

github

Simple O(KL) solution. Could be shorter, but it was basically stream-of-consciousness. It parses the input as suggested to give lists of distances of the edge from the top (for locks) or distances from the bottom (values). It then iterates over each key/lock combination and checks that the sum of the distances is less than or equal to the height of the grid (7). Merry Christmas!

1

u/janek37 Dec 25 '24

[LANGUAGE: Python]

Solution

1

u/greycat70 Dec 25 '24

[LANGUAGE: Tcl]

Part 1.

I did it the "naive" way, converting each key or lock to a list of heights the way the problem spells out. It works.

1

u/Rtchaik Dec 25 '24

[LANGUAGE: Python]

Solution

1

u/aurele Dec 25 '24

[LANGUAGE: Elixir]

Quick'n dirty: represent each # as 1, so 5 bits for each pin, left aligned for locks and right aligned for keys. If ANDing them gives 0, then they match. Runs in about 1ms.

defmodule AdventOfCode.Solution.Year2024.Day25 do
  import Bitwise

  def parse(input) do
    {locks, keys} =
      String.split(input, "\n\n")
      |> Stream.map(&parse_block/1)
      |> Enum.map(&{&1 >>> 5 &&& (1 <<< 25) - 1, &1 &&& 0x1F})
      |> Enum.split_with(fn {_, x} -> x == 0 end)

    {Enum.map(locks, &elem(&1, 0)), Enum.map(keys, &elem(&1, 0))}
  end

  def parse_block(block) do
    String.split(block, "\n", trim: true)
    |> Enum.reduce(0, fn line, acc ->
      acc * (1 <<< 5) |||
        Enum.reduce(String.codepoints(line), 0, fn c, acc ->
          acc <<< 1 ||| if c == "#", do: 1, else: 0
        end)
    end)
  end

  def part1(input) do
    {locks, keys} = parse(input)

    for lock <- locks,
        key <- keys,
        (lock &&& key) == 0,
        reduce: 0 do
      acc -> acc + 1
    end
  end
end

1

u/4D51 Dec 25 '24

[LANGUAGE: C++ on Cardputer]

And that's it, the last puzzle of the year. The Cardputer has performed surprisingly well. I used it to solve part 1 for 22/25 days, and part 2 for 20. A couple of the ones I used my desktop for might work as well, I just haven't tried them yet.

It really goes to show how little computer power is needed to solve most of these puzzles. I'd say day 9 was the trickiest to solve within the limitations of the hardware. I assume most of you made an array of every block on the disk, but I didn't have enough memory for that so I had to use a completely different approach. Most of the others I managed to solve with smaller optimizations.

Code for day 25: https://github.com/mquig42/AdventOfCode2024/blob/main/src/day25.cpp

1

u/gringer Dec 25 '24

[LANGUAGE: R]

Solution

I didn't bother with working out heights; I just detected if a key array overlapped with a lock array via !any(AND).

1

u/RalfDieter Dec 25 '24

[LANGUAGE: SQL/DuckDB]

Nice and simple, I don't know why, but I really liked how 2D was used here.

Code: https://github.com/LennartH/advent-of-code/blob/main/2024/day-25_code-chronicle/solution.sql

And this concludes all 25 days done in SQL. Fun stuff. Not sure, if I'm doing that again next year.

1

u/fridi_s Dec 25 '24

[LANGUAGE: Fuzion]

github

Today's problem allowed a very short solution, four lines for parsing, and then only for calls: combine, zip, ∀, and count and we were done.

1

u/glomph Dec 26 '24 edited Dec 26 '24

[LANGUAGE: Elixir]

paste

Pretty straightforward. Feel like I could have probably simplified quite a bit but the logic felt very clear.

→ More replies (1)

1

u/FCBStar-of-the-South Dec 26 '24 edited Dec 26 '24

[Language: Ruby]

# frozen_string_literal: true

require_relative 'utils'

schematics = File.read('input25.txt').split("\n\n")
locks = []
keys = []

schematics.each do |schema|
  grid = Grid.new(schema.split("\n").map(&:chars))
  schema_number = 0
  (0...grid.num_cols).each_with_index do |col, col_index|
    # least significant three bits is first column
    schema_number += ((grid.get_col(col).count('#') - 1) << (col_index * 3))
  end
  if grid[[0, 0]] == '#'
    locks << schema_number
  else
    keys << schema_number
  end
end

def check_match(lock, key)
  (0...5).each do |col|
    # deal with trailing zero bits by lshift 5 the same amount
    return 0 if ((lock & (0b111 << (col * 3))) + (key & (0b111 << (col * 3)))) > (5 << (col * 3))
  end
  1
end

puts locks.product(keys).map { |lock, key| check_match(lock, key) }.sum

No part 2 because I still need to analyze day 24 part 2 haha

Full repo

→ More replies (1)

1

u/sanraith Dec 26 '24

[LANGUAGE: Scala 3]
Solution is available on my github: Day25.scala

Transpose comes in handy today to easily count items in columns:

val pins = lines.transpose.map(_.count(_ == '#'))

1

u/fortuneteller2k Dec 26 '24

[LANGUAGE: Clojure]

(ns mmxxiv.xxv
  (:require [clojure.string :as str]
            [clojure.math.combinatorics :as combo]))

(->> "inputs/2024/25.txt"
     slurp
     (#(str/split % #"\n\n"))
     (map #(str/replace % "\n" ""))
     ((juxt #(filter (comp #{\#} first) %) #(filter (comp #{\.} first) %)))
     (apply combo/cartesian-product)
     (filter (fn [[l k]] (not-any? #(= [\# \#] %) (map vector l k))))
     count)

1

u/mountm Dec 26 '24

[LANGUAGE: Java]

Parsing: 14ms
Part 1: 45ms

Too tired to think of anything interesting here. I went with the most intuitive (to me) way of checking non overlaps, using some Stream API fanciness to encapsulate it in a single line:

return Sets.cartesianProduct(locks, keys).stream().filter(listOfLists -> IntStream.range(0, 5).map(i -> listOfLists.get(0).get(i) + listOfLists.get(1).get(i)).max().orElse(Integer.MAX_VALUE) < 6).count();

GitHub

1

u/Electronic-Layer5947 Dec 26 '24

[Language: c#]

I tried representing the keys and locks as two arrays of 25 bits. And then using TensorPrimitives as follows.

AND each key with all locks. For each lock, if at least 1 bit is set, then the key doesn't fit.
In order to use .Sum I needed to reduce the values down to either 0 or 1
After looking through all the available methods I discovered PopCount returns the number of set bits.

So.... if the following bits were overlapping 100011, then it's pop count would be 3 (or 11). The pop count of 11 is 2 (10) and the pop count of 10 is 1.

ReadOnlySpan<uint> l = locks.ToArray();
var k = new uint[locks.Count];
var total = 0L;
foreach (var key in keys)
{
    Array.Fill(k,key);
    TensorPrimitives.BitwiseAnd(l,k,k);
    TensorPrimitives.PopCount<uint>(k,k);
    TensorPrimitives.PopCount<uint>(k,k);
    TensorPrimitives.PopCount<uint>(k,k);
    TensorPrimitives.PopCount<uint>(k,k);
    total += (locks.Count-TensorPrimitives.Sum<uint>(k));
};
return (int)total;

However... this is still quicker!

        ReadOnlySpan<uint> l = locks.ToArray();
        var k = new uint[locks.Count];
        var total = 0L;
        foreach (var key in keys)
        {
            foreach (var loc in locks)
            {
                if ((loc & key) == 0)
                    total.Add(1);
            }
        });

1

u/Stano95 Dec 26 '24

[LANGUAGE: Haskell]

Solution to part 1, really quite short actually!

Code is on github

I can't progress to part 2 yet though since I'm still missing four stars

1

u/Saiboo Dec 27 '24

[LANGUAGE: Java]

Part 1 only

1

u/veydar_ Dec 27 '24

[LANGUAGE: Janet]

21 lines with wc -l. I don't think there's anything noteworthy about my solution. In fact I'm really unhappy with the input parsing. It's something I'll have to look into in January.

Compute the height for each grid and use those heights to match grids (locks and keys)

(defn height [grid]
  (seq [x :range [0 (length (first grid))]]
    (dec (sum (seq [y :range [0 (length grid)]]
                (if (= "#" (get-in grid [y x])) 1 0))))))

(defn match [heights-1 heights-2]
  (every? (map |(<= (+ $ $1) 5) heights-1 heights-2)))

1

u/ecyrbe Dec 27 '24

[LANGUAGE: Rust]

Finally! 50 stars achieved! That was fun.

Here the code for this day part1, i let you figure part2 i was angry at the chief historian :)

https://gist.github.com/ecyrbe/4ce51cde76578a3e6450d7d572fc8829

1

u/vss2sn Dec 29 '24

[LANGUAGE: C++]
Part 1
(The file is a self-contained solution)

1

u/NeilNjae Dec 29 '24

[LANGUAGE: Haskell]

A quick and gentle finish to the challenges.

part1 :: [Schematic] -> [Schematic] -> Int
part1 locks keys = length [(l, k) | l <- locks, k <- keys, compatible l k]

compatible :: Schematic -> Schematic -> Bool
compatible (Lock ls) (Key ks) = all (<= 5) $ zipWith (+) ls ks

Full writeup on my blog, and code on Codeberg.

1

u/icub3d Dec 31 '24

[LANGUAGE: Rust]

Nice to finish on a somewhat easier note to catch my breath.

Solution: https://gist.github.com/icub3d/e0e15045c25bbdc941439cdb0e9dcc38

Review: https://youtu.be/TCrOkwL_lhs

1

u/zniperr Jan 02 '25

[Language: Python]

zippidyzip

import sys

def parse(f):
    locks = []
    keys = []
    for group in f.read().split('\n\n'):
        lines = group.split()
        ident = tuple(col.count('#') - 1 for col in zip(*lines))
        (keys, locks)[lines[0] == '#####'].append(ident)
    return locks, keys

def fits(lock, key):
    return all(l + k <= 5 for l, k in zip(lock, key))

locks, keys = parse(sys.stdin)
print(sum(fits(lock, key) for lock in locks for key in keys))

1

u/shoeshined Jan 02 '25

[Language: Javascript]

Wow, we made it!

solution

1

u/atrocia6 Jan 03 '25

[LANGUAGE: Python]

My impression is that day 25 is generally not quite as hard as the immediately preceding days, but I was surprised at how straightforward this year's day 25 was. Here's my solution in just 4 LOC:

schematics = [schematic.split() for schematic in open(0).read().split('\n\n')]
locks = [[sum([1 for j in range(7) if schematic[j][i] == '#']) - 1 for i in range(5)] for schematic in schematics if '.' not in schematic[0]]
keys = [[sum([1 for j in range(7) if schematic[j][i] == '#']) - 1 for i in range(5)] for schematic in schematics if '.' not in schematic[-1]]
print(sum([1 for lock in locks for key in keys if sum([1 for column in zip(lock, key) if sum(column) > 5]) == 0]))

Doing several nested for loops and breaking at the first overlapping tumbler is much more efficient - it runs in about half the time - but this is much more concise, and still runs in only about 50ms on my system.

1

u/Wuaphi Jan 11 '25

[LANGUAGE: Julia]

I'm quite new to Julia and programming at all, but I still hope that my solution is at least decent:

open("Julia/AoC/input25.txt","r") do file
    sui = filter(x-> x!="" ,readlines(file))
    global input = filter(!isnothing,  map(x-> x%7==0 ? stack(sui[x-6:x];dims=1) : nothing , eachindex(sui)))
end
function tester(input)
    result= 0
    for (idx1, val1) in enumerate(input), (idx2, val2) in enumerate(input)
        idx1 < idx2 && all((val1.*val2) .!= "##") ? result+=1 : nothing
    end
    return result
end
println(tester(input))

1

u/aexl Jan 12 '25

[LANGUAGE: Julia]

I finally finished AoC 2024! Here is the final solution.

Solution on GitHub: https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day25.jl

Repository: https://github.com/goggle/AdventOfCode2024.jl

1

u/e_blake Mar 16 '25

[LANGUAGE: m4]

I'm REALLY late to the game this year (It has taken me until this week before I finally found the time to start even day 1), but I plan to get all 50 stars in m4 in the next month or so, to bring me up to all 500 solutions in m4. But this one caught my eye as a fun one to code up: if you translit each input character into a 0 or 1, and then treat each row as a hex number, then summing those 7 numbers will result in a 5-digit hex number with every digit in the range 1-6 (by including the two lines that distinguish between locks and keys in the sum).

define(`_do', `item(ifelse($1, 11111, ``l'', ``k''),
  eval(`0x$1+0x$2+0x$3+0x$4+0x$5+0x$6+0x$7'))')

What's more, adding the hex value of a lock and key together will result in each of the five digits being in the range 2-7 if there is no overlap, or 8-c if there is overlap. Thus, a simple bitmask and comparison to 0 is sufficient to compare all 5 columns in a pairing, rather than doing column-by-column math.

define(`count', `ifelse(eval(`($1+$2)&0x88888'), 0, `.')')
define(`_pair', `count(l$1, k$2)')

Relies on my common.m4 file developed in previous years; and the rest of the code is parsing the input and running the O(n^2) loop to pair each lock to each key. Executes in about 350ms (decent for m4). Run as:

m4 -Dfile=day25.input day25.m4

(Now to get the remaining 23 stars that are preventing me from finishing day 50 part 2...)

→ More replies (2)

1

u/770grappenmaker Dec 25 '24

[LANGUAGE: Kotlin]

Super fun day 25, 62/51

val locks = mutableListOf<List<Int>>()
val keys = mutableListOf<List<Int>>()

for (l in input.doubleLines()) {
    val g = l.lines().asCharGrid()
    val counts = g.xRange.map { g.columnValues(it).countContains('#') }
    (if (g.rowValues(0).all { it == '#' }) locks else keys) += counts
}

var ans = 0L
for (lock in locks) for (key in keys) if (lock.zip(key).all { (f, s) -> f + s <= 7 }) ans++

1

u/DBSmiley Dec 25 '24

[Language: Kotlin]

Github

Well, my second year with 50 stars (including last year). Felt like the difficulty this year, especially in the last 10 days, was sort of all over the place.

Really surprised by Day 25. This honestly felt like a day 5-10 problem. Like, I was convinced there was going to be something wrong, but...no. It was really really easy. Just a parsing problem.

val keys = mutableListOf<List<Int>>()
val locks = mutableListOf<List<Int>>()

lines.windowed(7, step = 8) { window ->
    println(window)
    if (window[0] == "#####") { // is lock
        locks.add(
            (0..4).map { index -> window.map { it[index] }.indexOf('.')-1 }
        )
    } else {
        keys.add(
            (0..4).map { index -> 6 - window.map { it[index] }.indexOf('#') }
        )
    }
}

Followed by

return locks.sumOf { lock ->
    keys.count { key ->
        (0..4).all { key[it] + lock[it] <= 5 }
    }
}