#2024 AoC Day 2
99 messages ยท Page 1 of 1 (latest)
Man, they made this puzzle a real hard one today...
235/109 but I blame having to fight with the website refusing most of the requests I sent at it...
servers on fire
there are solutions on the leaderboard with times shorter than how long it took me to load the puzzle for the first time
yeah
I got the site being down at first, too
I spent a hilariously long time ||excluding the wrong rows|| on part 2, classic stupid mistake
Another easy one
I didn't ||undo enough of part 1|| which is what I get for ||using it as a starting point even though that's normally a good idea||
easy road continues, never to get hard
Part 1 let me break out two things I like but don't frequently have a use for, ||windows and labeled loops||: ||https://github.com/ElementalAlchemist/advent-of-code-2024/blob/main/d02p1/src/main.rs||
||I didn't exactly finish Part 1 with my code working properly but apparently all the passing trials were at the bottom so I just checked for the latest failure reported and subtracted the line # from 1000||
||fixed it properly for Part 2, where I got tripped up by not properly copying a list and accidentally just making a reference instead||
my first attempt said "break a rule? safe!"
Part 1 was easy enough, part 2 I feel I was very inefficient
|| I just checked every version (removing one value) for each unsafe report, until I found a safe one. If the reports had been quite long it would have taken forever. ||
I dont wanna post my code it's literally my first time and it'll be some matter of messy and silly
I mean I did that too. It seems like the most straightforward way to do it tbh
yeah, brute force baby!
The "smart" way to do it would be || to look at only the increaase/decrease that caused a failure, and try removing the numbers before and after ||
But I couldn't be bothered
that seems like it would take way longer to do anyways
for anyone who cares about speed points
You better believe writing messy code is the de facto approach. Whatever works!
Yeah, but if it's anything like last year, later problems you can't really get away with brute force, so I'm trying to not get in the habit
||bonus: I also removed all the passing tests because removing one number from a passing test might cause it to fail, and I didn't want to write more code to catch that||
generally "is there a trick? if so, the puzzle will be such that brute forcing will make the runtime impractical". Fortunately, we will have only easy puzzles this year in which brute force is linear time!
mhm sure
You know, I'm happy to be learning how to use map and filter, but wow does it make things less readable (imo)
Also debugging using print is harder cause printing them out just give <map object> or whatever, instead of the contents
I was VERY inefficient with my solve. Might as well have done it manually.
But hey, a star's a star.
everyone's code is some mix of messy and silly
that's the way it is
here's my mess for today:
||```py
from aocd import data
lines = data.split('\n')
o = 0
for l in lines:
r = list(map(int, l.split()))
for j in range(len(r)+1):
if j == 0:
u = r
else:
u = r[:j-1]+r[j:]
if u != sorted(u) and u != sorted(u)[::-1]:
continue
b = 0
for i in range(len(u)-1):
k = abs(u[i]-u[i+1])
if k == 0 or k > 3:
b = 1
break
if not b:
o += 1
break
print(o)
my code for this one suuuuucks and I want to assure any prospective employers who may be watching that I don't code like this outside of a race context ||https://github.com/mrphlip/aoc/blob/master/2024/02.py||
||Me, trying to remember how you efficiently check if a list is monotonic: so, you can zip it with itself, and check the ordering, and... then check all those orderings are the same? like, collect the orders, and check for distinct values? hmm, how to do this cleanly... wait, how long are these input lists again? very short, you say? eff it, s == sorted(s)||
||This also makes me two-for-two on puzzles where there's some aspect of the Python language/library that I didn't remember off the top of my head, and checking would have taken more time than just writing the code to cover for all possibilities. In this case, I couldn't remember whether sorted returned a list or an iterator, so my code has list(sorted(s)) which, it turns out, does an unnecessary copy. Yesterday, I couldn't remember if collections.Counter has a default value or throws a KeyError, so I wrote b.get(i, 0) instead, which was also unnecessary. But in either case, actually checking if it was necessary would have taken more time than I saved.||
I did have an issue in the first version of my code of ||comparing a map object to a list for equality||
what the hell is a collections.Counter?
oh, I did a simliar thing ||I did s == list(sorted(s)) but my s was actually a tuple||
>>> from collections import Counter
>>> a = Counter([1,1,1,2,1,2])
>>> a[1]
4
>>> a[2]
2
>>> a[3]
0
comes in very handy in coding puzzles in particular
ah, neat
more efficient than myList.count(2), I guess
if you need repeated access
yeah, 'cause it only has to scan the input sequence once, and uses hashmap magic to collect duplicates
when I have to do that sort of thing efficiently I usually just use a Defaultdict
the ultimate implementation is exactly what you'd expect... https://github.com/python/cpython/blob/main/Lib/collections/__init__.py#L541-L542
for elem in iterable:
mapping[elem] = mapping.get(elem, 0) + 1
I used ||a bunch of all conditions on the list of diffs between elements|| for p1,
then just ||added an any over slices with one element removed|| for p2
||https://github.com/StarlitGhost/Advent-of-Code/blob/main/2024/2/script.py||
I feel like it turned out clean enough for this early in the event
Oh, lol, that's exactly what I wrote for day 1
Oh, I didn't think of ||sorting to check for if it was increasing/decreasing||, that's smart
Im struggling to work out why my solution for part 1 is failing,|| i've got to the point where im printing all the lines where it is deeming the line to be "safe", and going through each one by one
but all seem to match the criteria, they all have the same sign, and the largest difference is 3||
What tripped me up [just a clarification on the rules] || are you including difference of 0 as acceptable? ||
nope, ||i have a test for checking if the difference is >3 or equal to 0||
the annoying thing is, the test data works perfectly, so there must be another hidden edge case
You missed a detail: ||The differences must all have the same sign within a given line||
i use a different check for that
wait, are those hard-coded numbers of levels?
I'll just share the whole code
yeah, you've hardcoded the number of levels
ah, i see
i need to change some stuff ||to be based on length(work)||
thats what i get for not fully checking what the real data looked like
Had to go do my job for a bit, but part two wasnt too bad, ||figured i'd move my check into a function, and just run a temporary report through it for part 2||
The switch to Python from Excel this year was early. I just didn't want to deal with today's problem in Excel, specifically the ||variable length of inputs|| would just be annoying to deal with. I'm sure Excel has a nice way of doing that but I don't really want to try it.
I did learn the syntax for filtering inside of a Python list comprehension today, though.
switched to python from c++ this year and idk if maybe I'm just bad at c++ (or probably both) but python seems to require less writing to do the same thing so that's nice
oh hey I went with this approach for part 2. ||I think it's faster than removing every single one. Cos that should be basically O(n) - cos only two removals are allowed per range, rather than O(n^2), right?||
That's correct. It's a lot faster in the cases where the input scales in that direction. But today wasn't about stressing your scaling in that direction.
yeah, totally agree it's not necessary! ๐
Is there an O(n*m)? Cause it the number of reports and length scale independently?
Or do you group all those sorts of things into n for that kind of analysis?
That analysis was for a single report.
Multiply each by the number of reports for a description of the scaling of the whole solution
Big O notation is about the general case of the algorithm, though
Sure, but define "the algorithm" for a single report.
There's no way to do anything more interesting than process each report independently anyway.
alright, well that's a very dumb spreadsheet
there are maybe cleverer ways but whatever
definitely spent too long trying to find the cleverer ways
very very dumb
feeling smug watching my sister and her boyfriend try to do things the clever way and take way longer than i did
sorry, reading up a bit. this is a very handy construction. not 100% sure if it differs in sheets vs excel.
=ARRAYFORMULA(IF(ISBLANK(array),,do_thing))
and not sure if there's a better construction that does similar
I did not know about this. Thank you!
Part 1 tripped me a couple of times and then refactoring for 2 I managed to flip my logic without noticing and was counting the bad reports
and p2 continues to trip me up, I'm low but I don't know why
||I'm kept my logic but now I output all the bad indexes. If the list has bad levels I copy it, remove one and check again for bad indexes then repeat for the other bad levels. My totally safe reports is still good but my reports with dampener is too low||
do you want to post your code for suggestions?
<placeholder for removed code>
a question ||is it reporting the right bad indexes? for safe reports, there are no bad indexes, but where there are problems, if it isn't returning the right list, then the dampening might not remove it||
||oh yeah... it could be that by removing the previous value it resolves it... thanks||
||didn't help... brute forcing it is... realised that whether its asc or desc could be wrong and and that wouldn't trigger with my logic||
yeah the difficulty ||locating "which" one is the error or which one to remove is why i ended up just trying to remove all of them||
||Remove all reports. No bad reports. Problem solved.||
||ah what fun... my bruteforcer isn't brute forcing. Its missing the last index for some reason?||
||Dammit, range isn't inclusive||
Got it in the end with brute force... now to see if I could have done it better from these comments ๐
Brute force! Brute Force!! BRUTE FORCE!!!
that's enough for tonight I think... its 11pm. I'll take on the other tomorrow ๐ฆ
I've said it many times before but premature optimisation is bad. Get it right then get it fast... Do I listen to myself? No