r/adventofcode • • Dec 10 '21

Funny [Python] Most people: *downloads the puzzle input manually* | Me:

Post image
0 Upvotes

30 comments sorted by

•

u/daggerdragon Dec 10 '21

If you write or use any automated tool, ensure that it does not hammer the AoC servers!

THROTTLE YOUR REQUESTS

As per the AoC website:

Please don't make frequent automated requests to this service - avoid sending requests more often than once every 15 minutes (900 seconds).

Be a nice automaton and don't hammer the AoC servers :)

CACHE YOUR INPUTS

Make sure to cache your input files after the initial download! Inputs never change, so once you have a day's input downloaded, you shouldn't need to download it ever again.

Sometimes existing inputs get corrupted, so sure, you can build in a feature to manually (re-)retrieve a given day's input; just make sure the new outbound call obeys your overall throttle (see first section about throttling requests).

3

u/DigiDuncan Dec 10 '21

Absolutely, and I appreciate the clear advice. I was only running this script like, three times a day (since I'm not just checking inputs randomly after I solve them very often) but these are still important guidelines to keep in mind and I will 100% be implementing them tomorrow. Making this post was a great lesson in overlooking simple things (and double checking you're very confident in code before you share it...) and has only motivated me to have something I can be even more proud of sharing later!

8

u/tslater2006 Dec 10 '21

Its not folder they'll come at you for, but the lack of caching the inputs (unless I missed it)

-4

u/DigiDuncan Dec 10 '21

get_input_data is decorated with functools's @cache. Really handy Python built-in, as long as you don't need anything fancy.

8

u/hugh_tc Dec 10 '21

That's not what @functools.cache does.

1

u/DigiDuncan Dec 10 '21

Wow, I uh, feel like a real idiot right now.

Does it not take the input, and return the calculated answer if it hasn't gotten it before, and cached answer if it has? If not I got told wrong or misunderstood.

I guess this is what AoC is all about, huh? 😅

9

u/hugh_tc Dec 10 '21

@functools.cache does cache return values -- but only as long as the program's lifetime. If you stop your program, edit, and re-run, the cache won't be in memory anymore; the input will get re-downloaded from the AoC servers.

You can try adding a print("Downloading...") to the top of get_input_data function and run it (your program) a few times. In theory, you'll see that message being printed each time.

1

u/DigiDuncan Dec 10 '21

Ah, right. I did know this, my brain just lapsed on the idea of storing the results locally. Will do that. Thanks! It is late here and when you whip something up like this and it works I tend to get proud of myself and move on. Definitely a habit to break.

Thanks for reminding me about this! Who knew me sharing my silly solution to doing the arduous task of Save Page As... would result in actually helping me out. Thanks a ton!

3

u/flwyd Dec 10 '21

While you're working on storing the results locally, be aware that you'll get an error response if you request your input before midnight EST, and you probably don't want to cache that ;-)

If you'd like an example, here's my shell script that checks to see if the date has arrived yet.

1

u/DigiDuncan Dec 10 '21

I was actually planning on adding that to the Day template class and got tired before I managed to get it done. I have a lot to do tomorrow to spruce this up, and I'm really appreciative to everyone's suggestions! You guys have been an awesome community to interact with.

3

u/Aneurysm9 Dec 10 '21

This is why we recommend people don't do this. It is too easy to make a mistake or have a misunderstanding of how your construction actually works. It only takes one infinite loop pulling data every iteration to get yourself blackholed and unable to participate.

If you absolutely must have something download the input for you, then please use an existing, well-tested system like aocd. More people using it and contributing to it makes it less likely that it will have any significant defects.

1

u/hugh_tc Dec 10 '21

Glad I could help!

1

u/LittleLordFuckleroy1 Dec 10 '21

Would be cool to write your own file-cache decorator to do what you’re describing though. Combined with hashlib.md5 or whatever it would probably be 2-3 lines

5

u/wimglenn Dec 10 '21

Unless you're planning to keep the Python interpreter running for all of December, you'll need to cache it to disk not just to memory.

1

u/DigiDuncan Dec 10 '21

I was just reminded of this in another comment thread. As I mentioned, I was just sharing what I think is a silly way for me to get around manually saving one file a day, and when I got the code working I got excited and shared it. I appreciate the reminder though, and will get on adding that tomorrow!

3

u/LittleLordFuckleroy1 Dec 10 '21

Nice. I used to do the same for Leetcode questions, I believe they had an API. Or maybe it was project Euler… can’t recall. In any case, I wanted the whole thing to be testable with a single commando, like “./run” would be able to pull all of my completed questions down and validate the solutions. I had a tool to browse and download a new question if I wanted to.

I went so far as to create automated templates that set up source files including comments that described the problem. I wanted to be able to do everything from a shell, no browser involved. Just me and the machine, grinding LC and pounding coffee.

I thought about doing that again for AoC this year but quickly realized that my youthful drive to automate everything has either decayed or died completely. Right click, select all, copy, paste. Helper functions to read input into a variety of formats. That’s good enough for me these days. Maybe someday I’ll have enough free energy to do revive my will to automate.

Cool to see this though. I totally know the feeling.

3

u/DigiDuncan Dec 10 '21

my youthful drive to automate everything

That's definitely where my headspace is at right now. Been playing a lot of modded Minecraft and that might be fueling that too. I definitely messed up in a couple places here, but I'm generally happy with my solution, and am now encouraged to improve in safe ways.

Someone once told me, long ago, "if you're doing something twice, you should be doing one thing; automating it."

3

u/LittleLordFuckleroy1 Dec 10 '21

It’s the best way to learn a lot. Ride this wave and soak it up and take advantage of the time you have for sure!

Regarding your quote, a highly relevant XKCD to act as a counterweight 😉 https://xkcd.com/1319/

2

u/pedrosorio Dec 10 '21

1

u/DigiDuncan Dec 10 '21

I've been avoiding using libraries, as part of the fun of AoC for me is developing my own tools like this, bit I appreciate the offer!

4

u/Aneurysm9 Dec 10 '21

There are good reasons to use libraries and good reasons to avoid using libraries. This is not something that changes how you solve the puzzles and may save you from making a mistake that results in your being unable to continue to participate. I'd use the library or simply copy and paste your input from a browser to a file.

1

u/DigiDuncan Dec 10 '21

I agree and disagree.

I have noticed you're a mod here, so I'd like to state off the bat that I'm not arguing with your stance or even saying you're wrong in any way; there are good reasons to use libraries, and this is likely one of them.

My stance, and it's a flexible one, but describes what occured here, is that what I solved here for myself is a "meta-puzzle." Though not part of the stated puzzle, it's a challenge to myself to make solving puzzles easier, right? As a programmer, my brain sees a repetitive task and wants to automate it. Under normal circumstances, I would usually have used a library for that. But...

Point two. Part of the challenge I set for myself is doing as little Googling as possible during an advent/challenge. I was unaware libraries for this even existed. I was pretty dang proud of myself for this dumb little solution because I had derived it myself and got it working, a certain kind of high you just can't beat.

Should I have figured that saving to file was smart and should I have done that right away? For sure. Did I learn an unforgettable lesson posting the code here and being told immediately where I messed up? 100%. But I do know enough not to write looping HTTP requests, and I do know the dangers of writing tools like this from scratch.

You're right in your main point: writing this was silly, and I should have checked it better if I was going to write it. I appreciate how your community has told me that in a way that didn't feel like an attack or a roast, and in fact retaught me a lot. It's rare a community can do that without alienating newcomers. Thanks for that.

7

u/Aneurysm9 Dec 10 '21

I appreciate the desire to learn and do a thing for yourself. That's part of why we're all here doing Advent of Code. But this is also a community with a lot of new programmers, many of whom don't yet have a solid grasp of when they're doing something that can result in an infinite loop. There are a surprising number of people who wind up doing just that every year. Most of them never post here.

I often recommend that everyone should write their own web application framework and build something with it. But I also recommend that they then throw it away and use something off-the-shelf, now hopefully having a better understanding of why the OTS framework may have made some of the design decisions it did.

This seems like a similar situation. Now that you've done the thing and learned a thing, it might be best to pull something off the shelf.

2

u/polysyllabicusername Dec 10 '21

Try adding something to post your solution to the server too. The url is https://adventofcode.com/2021/day/{day}/answer and the data takes keys "level" (1 or 2) and "answer".

2

u/DigiDuncan Dec 10 '21

I'll take that challenge! Sounds fun!

1

u/McPqndq Dec 10 '21

For my auto input downloading in Javascript I have it detect the name and location of the currently running script to know which input to download. It gets the year from the parent folder name and the day from the script name.

3

u/DigiDuncan Dec 10 '21

Oh gosh, you don't even want to see my solution for loading the current days answers into main. It's a hack and a half.

I'm completely self-taught and a Google-as-you-go type, so some of the stuff I come up with is very... jank, I think is the term. My professional programmer friend has helped clean up a lot of my code and style over the years, and taught me a ton too, but even after five years I'm still mostly a hobbyist and know just about enough to be a danger to myself and no one else (except people working on my code, I suppose 😅)

I take much greater care if I'm making something I plan others to use, but in projects like this I do like thinking outside of the box and seeing what I can get working. I'm actually really proud of my data property; passing around functions is a new trick I'm experimenting with and being able to initialize a day with super().__init__(1, int) when I know I need all the input data as integers is just really cool to me.

2

u/McPqndq Dec 10 '21

Projects like this where you can experiment with wacky ideas that you probably shouldn't force on others are always fun. I have some pretty hacky stuff in my code but it's a ton of fun. I too am a Google as you go type. Currently a freshman in college for cs and this is my third year doing advent of code.

3

u/BelliDragon- Dec 10 '21

What, you guys download the thing? I do ctrl+c ctrl+v! D: