#2024 AoC Day 4
84 messages · Page 1 of 1 (latest)
This one's TOO easy
It's day 4
Good ol' Excel, nothing beats that!
Nope, just making a mess of this
For part 1 ||I already had a solution for, in another project... but I had to find that past project, dig out the right function, generalise it a bit... ended up pretty low on the leaderboard. Meanwhile, part 2, I built anew from scratch here, and it ended up doing much better. Lesson learned: never reuse anything.||
WOW Part 2 was SO MUCH SIMPLIER than Part 1 !!
"or even overlapping other words" is what scared me.
I thought it'd be impossible for me to solve if "XMsomethingAS" was valid.
I basically ||hardcoded searching all ways for XMAS and SAMX||
It's just 8 ways
it'd be quicker if I could suppress "Out of Bounds" errors...
I worked around that just fine 😛
I like my part 2 solution notably more than I like my part 1 solution, for what that's worth
A bunch of off-by-one error and incorrect signs for checking in different directions, but generally no actual trouble
Basically just ||checked in each direction at each point||
had a funny problem in part one where it found an extra one, cause it ||was looping at the edge thanks to python letting you index negative numbers (and me screwing up the check to stop that)||
its only been the second time out of 4
“Ceres Search”… how did the AoC people get hold of my notes??
(Spent most of the last quarter upgrading and retooling a lot storage system called Ceres
||my code supports any square shape. i could probably have done any rectangle but that would require a slightly different approach||
it was also unsurprisingly jank
(Also if you like the idea of today’s puzzle and/or foxen: || https://www.amazon.co.uk/Find-Fox-Almost-Impossible-Search/dp/1788405943/ ||
this is what i get for coding at 11pm
||i didnt even bother simplifying the last array indices...||
of course.
||the hard part was figuring out how to check the diagonals without running into out-of-bounds errors||
that was not a problem in part2
This was the main logic of mine
I started out ||just doing the big list of directions, and then later decided to use a direction variable to make parts easier, and I think I could make all this logic much simpler if I used to there as well|||
imagine defining things
i have yet to bother with that. i really should do that eventually
i just. havent bothered to
im sure ill start doing things more properly when i have to
I also ||optimistically allowed it to work for any word, thinking that might change in part 2
Of course once I got to part 2, and could just hard code it in, it got a lot smaller||
ok, yeah, that's way more compact, and honestly, easier to read
I got the brilliant idea to solve part 1 using a technique I saw once for the 8 queens problem, where diagonals are stored in a separate data structure.
It worked!
It was utterly useless for part 2
Then again, what I did for part 2 is utterly useless for part 1, so... eh.
Wow, that's mean.
I didn't see any in my example, so decided to not bother with the possibility
Also, it turns out things run a lot better if you don't accidentally put the y and x output of one method as the x and y input of another...
I used my ||grid and 2d vector|| utils written for previous years
||https://github.com/StarlitGhost/Advent-of-Code/blob/main/2024/4/script.py||
and I was once again annoyed by dir being a builtin in python
When I said before that I already had a solution in "another project", that other project was ||doing stats about the Don't Find the Fox videos||
(for reference ||https://www.youtube.com/watch?v=S8hzAQJwMOQ&lc=Ugw30Lc2zaVZAPDKNvR4AaABAg||)
Oh wait are you the one who posted the full stats for that?
Im not proud of how i solved this, but it works
my first attempt failed ||when I couldnt do basic maths properly, and so all the "4"s in the checks for boundaries were set to "3"||
Should've gone to work already but I have no idea why my quick code is apparently getting the wrong answer. Will look when I get home I guess:
||```
with open("D4.input", 'r') as f:
lines = f.readlines()
#xmas +1, +n-1, +n, +n+1
#samx +1, +n-1, +n, +n+1
lines = [line.strip() for line in lines]
n = len(lines[0])
bstr = ''.join(lines)
N = len(bstr)
total = 0
exs = []
for i, c in enumerate(bstr):
if c in 'XS':
sign = 1 - 2*(c == 'S')
for gap in [1, n-1, n, n+1]:
if i + 3gap < N:
if ''.join([bstr[i + jgap] for j in range(4)]) == 'XMAS'[::sign]:
total += 1
exs += [[i, gap, sign]]
Oh duh, realized in the shower ||I didn't include a line boundary check.|| I'll fix it tonight I guess
I ask a question so similar to this one when I interview people that I could do today's in my sleep.
I've seen hundreds of attempts at this problem.
one dumb thing you can do in a spreadsheet is put your grid down a few rows and columns and then just let your offset go "out of bounds" 😛
That's how I solved it.
I love avoiding bounds checking with a more permissive data structure, yeah.
last year I wrote a whole reusable "character matrix" class that was super useful. and then today I forgot I had it and wasted like 15 minutes doing it again from scratch. that's what I get for rushing
Finally got around to solving this and being the kind of person I am, I realised that || XMAS and SMAX both fit in an int32... so my algorithm was to read the file a byte at a time, shift that byte into the appropriate row/column/diag1/diag2 integer, and see if it is equal to the two known constants. It technically makes it O(side_length) in memory usage! ||
now do it with SSE.
My brain is only expanding that as the job title (which I’m guessing is not what you mean?)
my brain is dying trying to figure out how to do the diagonals...
I cannot work out how to get what I want for the diagonals at all...
Trying to build all the possible diagonal strings by traversing a 2d character array but failing absolutely miserably to get my brain to find a way to do it
It took me a while to work it out. My brain started craking it by || starting with the longest diagonal as the "middle" diagonal -- it has the same length as the sides. each side of it, there's a diagonal one shorter, down to one of length one. Once I realised that, the relation between the (x,y) on the grid and which diagonal got easier || but that may just be how my brain works
still not really getting it 😐
i just didn't do anything like that at all in the first place
I've been staring at this for over an hour now, Day 3 was easy and Day 4 is when I go "how have i been paid for programming for 10 year" and get depressed
is this too simple an explanation, as in "yeah, that's obvious, I got that"? ||if you start with point p,q, you check (p,q), (p+1,q+1), (p+2,q+2), (p+3,q+3), for one direction, and (p,q), (p+1,q-1), (p+2,q-2), (p+3,q-3) for the other? (I checked for XMAS and SAMX). [Boundary checking as you need to]||
thanks for this hint
still not getting this one 😐
||Bad Code here: https://github.com/jamstruth/advent-of-code-2024/blob/main/day4.py||
||Trying to go through from top-left to bottom-right checking for XMAS or SAMX. Only Checking Right, Down, Down-Right and down-left to avoid any double-counting (a SAMX Down-Right is the same as an XMAS up-left) Not telling me if I'm low or high||
I can suggest running this on the example, and outputting your tests. I can see already that you will be missing things in the bottom row/right hand column. Consider a bottom row "XMAS", you don't get there because you stop at len-step_value
(and there is no down-left test in that code)
down-left was my downfall.
got it, ||bad col offset check. failed if it was exactly 0||
boundary conditions are fun!! (no, no they aren't)
got part 2 fairly quick after that struggle with part 1... Just banging my head on my first idea and then being angry