<Advent of Code 2024 day 5> (no spoilers) :thread:
# advent-of-code
a
Advent of Code 2024 day 5 (no spoilers) 🧵
c
Wondered if anyone had any ideas why my algorithm for part 2 doesn't work. It works on the sample, but not the main. I'm building the rules into a map. e.g.:
Copy code
47 -> [53, 13, 61, 29]
97 -> [13, 61, 47, 29, 53, 75]
etc
To then get the number order my algorithm is as follows: • Identify the key which doesn't appear in any of the lists. One value has to be the smallest and therefore won't appear in any list as bigger than something else. • Append to my ordered list • Remove that entry from the map • If on the last map entry add that last value to the end as its the biggest • Repeat if map not empty This works for the sample, but on the real input it falls over on the very first step. I can't see a problem with my parsing so wondering if i've misunderstood the data a bit. Any pointers welcome 🙂
j
have you had a look if a number pair appears twice? In my orderings there were at least one rule that could apply to different lines.
c
How do you mean? I've just converted my 1176 X|Y pairs to a set and it has a size of 1176 so I think each line is unique.
j
What I mean in the second part where the numbers are like 3,5,6,... there one of the rules can be needed in multiple of these lines and you delete the rule if I read your text correctly
c
Oh I see. No, at this point I'm just trying to convert the pairs A|B, C|D. etc, into an ordered list.
My thinking being when I know the full ordering of the page ordering rules it becomes straightforward to apply them to each line
So for instance with the sample data:
Copy code
Keys: [29, 47, 53, 61, 75, 97]
Vals: [13, 29, 47, 53, 61, 75]
Keys is values that appears on the left side of a pipe. Vals on the right. In the sample data 13 doesn't appear as a key which means nothing is larger than it so it's the highest by order. Conversely 97 doesn't appear on the right side so it isn't bigger than anything, so it's the smallest by order. (The ordering of the output here is just to aid in viewing the data) For my real data I get the following identical lists:
Copy code
Keys: [15, 16, 18, 19, 21, 23, 26, 27, 28, 29, 31, 34, 35, 39, 43, 45, 47, 48, 49, 51, 53, 56, 57, 59, 62, 63, 64, 65, 66, 67, 68, 69, 73, 75, 76, 78, 79, 81, 82, 83, 84, 85, 86, 87, 88, 89, 92, 94, 95]
Vals: [15, 16, 18, 19, 21, 23, 26, 27, 28, 29, 31, 34, 35, 39, 43, 45, 47, 48, 49, 51, 53, 56, 57, 59, 62, 63, 64, 65, 66, 67, 68, 69, 73, 75, 76, 78, 79, 81, 82, 83, 84, 85, 86, 87, 88, 89, 92, 94, 95]
Which either implies I'm doing something wrong or the orderings are circular somehow in a way that the sample data isn't.
Based on my algorithm above I get an ordering of
Copy code
[97, 75, 47, 61, 53, 29, 13]
Which I believe to be correct. I'm assuming this is possible for the real input but now I'm questioning whether it is or not.