adventofcode

Aleks 2024-12-17T06:38:53.800539Z

Day 17 - Solutions

Aleks 2024-12-17T06:39:44.223249Z

For some reason I get a wrong answer for Part 1. Is it a correct format to submit 4,4,4,3,0,6,6,2,1?

Maravedis 2024-12-17T06:49:16.785169Z

Yes

Maravedis 2024-12-17T06:49:35.468879Z

πŸ™ŒπŸ» 1
Aleks 2024-12-17T07:02:56.773639Z

Guys, I'm really stuck. Maybe I made a stupid mistake and can't see it. I get right answers for the example and the tests, but not for my input. Can anyone look at it? https://github.com/zelark/AoC/blob/master/src/zelark/aoc_2024/day_17.clj

2024-12-17T07:11:54.657119Z

I'm in worse shape. I have an answer that produces produces the right output for part2 but aoc is rejecting it...

rjray 2024-12-17T07:12:28.237599Z

Does aoc say it's too high?

2024-12-17T07:13:34.436249Z

yes. but it's not a multiple of the thing

2024-12-17T07:13:56.385409Z

so it's not the obvious mistake I thought

rjray 2024-12-17T07:15:42.759539Z

@zelark The only think I think I see, is that it looks like you are treating every operand as a combo-operand (you're setting operand to that on line 40). But instructions 1 (bxl) and 3 (jnz) operate on literal operands.

2024-12-17T07:16:01.860219Z

oh - I got it - wheee

rjray 2024-12-17T07:16:05.000719Z

@norman Are you sure there isn't a lower number that works?

rjray 2024-12-17T07:16:09.825179Z

OK, cool πŸ™‚

2024-12-17T07:16:17.259549Z

yes. that was the thing

2024-12-17T07:16:35.297159Z

thanks - just asking the first question got me too it

rjray 2024-12-17T07:16:41.311929Z

I'm stuck on part 2 completely. I can't wrap my head around trying to reverse-engineer the "code".

2024-12-17T07:18:23.598359Z

Looking at the code gave me the general idea, but if you just iterate through the first 256 or so as you might get the idea

Aleks 2024-12-17T07:18:52.265079Z

@rjray thanks, I use the case, so it should works fine

(case operand
    (0 1 2 3) operand
    4 (state :a)
    5 (state :b)
    6 (state :c)
    ...

rjray 2024-12-17T07:20:21.308719Z

@zelark But that isn't the same as a literal operand. A literal operand value of "7" is 7. A literal op value of 5 is 5 (not the content of b).

Aleks 2024-12-17T07:21:59.703559Z

Stupid me, I got it. It seems I need more sleep

Aleks 2024-12-17T07:25:34.302259Z

Got a star, @rjray thank you a lot!

πŸ‘ 1
2024-12-17T07:26:38.914369Z

https://gitlab.com/maximoburrito/advent2024/-/blob/main/src/day17/main.clj I really shouldn't have stayed up for part 2. I have to be up early....

rjray 2024-12-17T07:28:15.454449Z

Same. I haven't seen the pattern yet, as you hinted. Might sleep on it and resume in the morning.

2024-12-17T07:29:15.700989Z

think about when it goes from 1 digit output to 2 digit output and 2 digit output to three digit output

rjray 2024-12-17T07:29:27.503239Z

Yeah, I just noticed that.

roklenarcic 2024-12-17T08:08:11.508839Z

Part 1 was so punishing because I am a clumsy typist

roklenarcic 2024-12-17T08:09:06.074489Z

Made a copy paste error and a couple of other errors when programming commands

1
roklenarcic 2024-12-17T08:10:14.388259Z

For part 2 I fully reverse engineered the calculation, but after I solved it, I see now that there is no need for that

Aleks 2024-12-17T08:41:40.051079Z

Wow, seems I know how to solve part 2

roklenarcic 2024-12-17T09:53:24.001169Z

https://gist.github.com/RokLenarcic/8cc1683fe5037e681d84535fe90ad2cf

πŸ‘ 1
πŸ‘πŸ» 1
Maravedis 2024-12-17T10:11:53.641529Z

Man, this was SUPER fun. Spoilers, because I included an explanation in my part2.

πŸ‘πŸ» 1
Aleks 2024-12-17T10:15:48.068819Z

Just solved it! I did DFS for part 2 https://github.com/zelark/AoC/blob/master/src/zelark/aoc_2024/day_17.clj

πŸ‘ 1
roklenarcic 2024-12-17T10:21:07.806009Z

@malaingreclement very nice and short implementation of the computer

roklenarcic 2024-12-17T10:22:01.536699Z

btw I see a lot of people use pow and Math/pow

roklenarcic 2024-12-17T10:22:09.380459Z

it’s just bit-shift-left

Maravedis 2024-12-17T10:22:41.494739Z

Yeah I know, but I wanted to keep the wording of the code close to the puzzle description, to help navigating between the two.

Maravedis 2024-12-17T10:23:26.687559Z

Might rewrite it now that I'm done, so that it feels more assmebly-y.

Aleks 2024-12-17T10:24:25.226509Z

@roklenarcic yeah bit-shift-left, and bit-and for mod

Maravedis 2024-12-17T10:24:41.449279Z

It's even a bit-shift-right, since we're dividing by powers of 2.

βœ”οΈ 1
Maravedis 2024-12-17T10:27:57.809479Z

@roklenarcic edited, it is cleaner. I think it would actually have helped to implemented like this from the beginning ><

roklenarcic 2024-12-17T10:30:01.726059Z

you are right

roklenarcic 2024-12-17T10:37:49.391979Z

I’ve updated my gist to clean it up a bit, now it’s shorter

Maravedis 2024-12-17T11:19:34.518339Z

With only 8 instructions in the program, and 2 "free" registers, I wonder how the puzzle maker generates unique inputs for everyone playing. There's got to be a lot of collision.

roklenarcic 2024-12-17T11:20:20.398079Z

Randomize all the instructions that mangle the number

roklenarcic 2024-12-17T11:20:30.627789Z

keep just div by 8 intact

Maravedis 2024-12-17T11:24:32.752179Z

Well, you need the first instruction to be store A mod 8 in something, the penultimate one to be outB , and the last one to be jnz 0 . That leaves you with 5 instructions to "mangle" the number. The only thing they have that is not easily reversible in modulo operations is xors. You can only xor easily with numbers from 0 to 7. Then maybe a store mod8 at some point. All I'm saying is that it seems rather few possibilities in the end, when you have more than 10k competitors.

genmeblog 2024-12-17T11:30:29.527809Z

Great hacking day. I'm not sure if my solution is suitable for every input. After "source code" analysis it came out that up to 10 bits generates the number in my case. This nicely limits the search space. https://github.com/genmeblog/advent-of-code/blob/master/src/advent_of_code_2024/day17.clj

Aleks 2024-12-17T11:34:00.582489Z

@malaingreclement just checked your program (opcodes and operands) as you left it in your solution. It's completely matched with mine. Even xors and their constants,

genmeblog 2024-12-17T12:06:27.395649Z

@malaingreclement Looking at source code of your input and comparing with mine we have xor of 3 LSB and some numbers, some higher bits based on B (0-7), and in my case also with next 3 bits from A. But generally the result relies on up to 10 bits.

tildedave 2024-12-17T12:30:53.735349Z

I enjoyed this, I always like the reverse engineering ones even though they tend to always make my head hurt. read the source code, saw the answers would be in the range 8^15 -> 8^16, briefly thought about a direct attack, realized going from reverse was easy, got to essentially the same DFS approach upthread. https://github.com/tildedave/advent-of-code/blob/main/src/advent2024/day17.clj#L204-L237

tildedave 2024-12-17T12:32:12.416609Z

I did spend "too much time" implementing the machine, lots of silly bugs.

jvuillermet 2024-12-17T14:15:10.994319Z

only happy about this part of the code that relies on merge order

state-up (case instruction
                        0 {:A (quot A (int (math/pow 2 combo-operand)))} 
                        1 {:B (bit-xor B literal-operand)}
                        2 {:B (mod combo-operand 8)}
                        3 (if (zero? A) {} {:pointer literal-operand})
                        4 {:B (bit-xor B C)}
                        5 {:output (conj output (mod combo-operand 8))}
                        6 {:B (quot A (int (math/pow 2 combo-operand)))}
                        7 {:C (quot A (int (math/pow 2 combo-operand)))})]
         (recur (merge state {:pointer (+ 2 pointer)} state-up))

3
tildedave 2024-12-17T14:17:39.970699Z

after some past AoC years, I will say that it is kind how quickly all these programs terminated.

Maravedis 2024-12-17T14:19:00.870599Z

I think they were constrained by how the number would have to be 2^70 to be just one digit longer... Which on most architectures is a nightmare.

Maravedis 2024-12-17T14:19:28.138949Z

I suppose making it based on powers of 4 wasn't easy to generate? It would have given them a lot more room.

jvuillermet 2024-12-17T14:29:25.353669Z

@norman your solution is helping me, thanks for sharing! but I dont understand how your expand function doesn't infinitely loop for a A=0 input

2024-12-17T18:08:07.353189Z

@jvuillermet each call to expand checks 8 cases. Each case can recurse, but only if out has one more digit in the output that matches.

tschady 2024-12-17T18:35:06.319399Z

OMFG I just debugged my p1 for ages, just now realized I was submitting without the commas (e.g. β€œ012” instead of β€œ0,1,2")

3
tschady 2024-12-17T20:13:15.780859Z

> With only 8 instructions in the program, and 2 β€œfree” registers, I wonder how the puzzle maker generates unique inputs for everyone playing. There’s got to be a lot of collision. @malaingreclement he’s stated before he generates β€œa lot” of i/o pairs, many people get the same ones.

bhauman 2024-12-17T20:54:42.424069Z

Here’s todays: https://github.com/bhauman/adv2024/blob/main/src/adv2024/day17/sol.clj Part 2 in 2.5s Binary search to find the window of correct length results. Then reduce the window around a maximum match val match val is

(defn matching-digits-val [n n2]
  (reduce + (map #(if (= %1 %2)
                    (long (Math/pow 10 %3))
                    0) n n2 (range))))
Could probably use optimization strategy to maximize the above function.

Maravedis 2024-12-17T21:00:02.266589Z

@bhauman living that bruteforce life. Respect.

Felipe 2024-12-18T01:48:52.002559Z

is the solution really to iterate through all the numbers in the range? I was thinking there might be some math trick to do a reverse computation on the program input. hmmm

Felipe 2024-12-18T01:51:50.968449Z

I manually tweaked the first octets until I found a decent range to search in, then let the computer do the rest πŸ™‚ not ideal, but hey, I got my star

tildedave 2024-12-18T01:52:18.575429Z

my solution builds the output in reverse, wasn't too bad (and easier than thinking about bit munging)

Maravedis 2024-12-18T04:57:14.317079Z

@felipecortezfi if you check out my code , Itried to explain the "trick" I came up with. Like tildewave, I build the output in reverse.

Felipe 2024-12-18T09:33:16.060619Z

@malaingreclement yep, that’s kind of what I did too, but I was wondering if you can simply calculate the numbers instead of doing a search. the example in part 2 makes me think you can

Aleks 2024-12-17T12:05:58.465289Z

AoC ten

Aleks 2024-12-17T12:58:24.528589Z

It reminds me 2017

Aleks 2024-12-17T16:03:36.337759Z

Today is the day

Kirill Chernyshov 2024-12-17T16:19:42.847009Z

https://aoc.xhyrom.dev/ first red πŸ™‚

2
Aleks 2024-12-17T16:29:48.738079Z

Nice resource! I didn't know about it.

Maravedis 2024-12-17T16:16:17.132719Z

Damn. I thought it was way easier than like, day 12.

2024-12-17T16:57:45.011589Z

it'll be a short stream today

πŸ‘πŸ» 1