r/learnpython • • Dec 02 '19

Advent of Code (AoC) 2019 starts today!

https://adventofcode.com/

Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

These puzzles are a great way to practice your Python and problem solving skills.

Edit: added description from AoC site.

336 Upvotes

40 comments sorted by

2

u/jlamores Dec 02 '19

Talking about the first puzzle.

I am an absolute beginner reading right now through the list chapter of the Python Crash Course. To solve the first puzzle I think I have to make a list with all the data and use a "for loop", but I dont know how to write all this numbers to create my list. How do you do it? You copy and paste in your code? But you don't have the commas so....

2

u/CleverBunnyThief Dec 02 '19

That's what I did. Copied, pasted then added the commas after.

1

u/MiataCory Dec 02 '19

Just FYI: An "End of line" character is present, even though a comma isn't.

.readline()

1

u/CleverBunnyThief Dec 02 '19

That's good to know for the future.

Thanks!

5

u/[deleted] Dec 02 '19
empty_list = []
with open('input.txt', 'r') as input_file: 
    for number in input_file.readlines(): 
        empty_list.append(number) 
print(empty_list)

or with list comp

with open('input.txt', 'r') as input_file:
    some_list = [int(number) for number in input_file.readlines()]
    print(some_list)

3

u/MiataCory Dec 02 '19 edited Dec 02 '19

"readline" will read until an "end of line" character.
"readlines" uses readline to return the whole file as a list, which you could access by assigning it to a list: list = file.readlines() and then picking a line like: list(4)

I did the readline though, because I didn't remember readlines!

with open('input.txt', 'r') as file:
    line = file.readline()

That's how I did it anyway. Every time you call file.readline() it'll read the next line. You can do a file.seek(line number) if you want to go to a specific line, though that's usually used for going to beginning of a file, .seek(0)

2

u/jlamores Dec 02 '19

Thanks for the help. This is something I have not studied yet. I didn't know how to read a file

4

u/[deleted] Dec 02 '19

Now's the time if you plan on doing AoC. Manually editing the input file is going to get you into trouble down the road as different parts reuse the data in different ways. see day2 part 2.

2

u/Leeoku Dec 02 '19

Just curious. I'm a bit new to programming and wondered what the difficulty level is from previous participants. I did see the disclaimer on the site but just curious

3

u/CleverBunnyThief Dec 02 '19

Each day will have two new puzzles. Completing the first one will unlock the second one of the day. I've only done day 1 so far but I imagine the first one will be easier. The second one will build on the first one and add a bit more complexity.

I don't believe that there's a deadline so you can submit them later. Just sign up and have a look. I think it's a good challenge that will push you learn. Try it out and ask questions here if you get stuck.

There's a AoC subreddit so you can always go there to get help or look at other people's code. That's a good way to learn too sometimes.

https://www.reddit.com/r/adventofcode/

2

u/[deleted] Dec 02 '19

Different every question and different every year, there is no way to gauge a difficultly level without knowing your skill (on top of every question being of varying difficulty, hell, there is stuff in the middle that takes 10 minutes, and others that take me an hour). These aren't made for beginner's though. Just go through them if it interests you.

15

u/[deleted] Dec 02 '19

Very cool! I'm very new at learning Python.

I solved the first puzzle, which wasn't complicated, yet I learned a couple new tricks!

Thanks for posting!

7

u/EphemeralNight Dec 02 '19

I'm a total beginner with python but will save it for after work to do will help to improve my skills in programming. Thank you for sharing!

5

u/[deleted] Dec 02 '19

Day 1 - this is fun. Best calendar so far. This one is not making me fat.

3

u/Additional_Maximum Dec 03 '19

Right. That's what my cheese advent calendar is for.

3

u/xelf Elf Dec 02 '19

Nifty. Solved day 1 with a google spreadsheet. Something about good a programmer uses the right tool etc... =)

0

u/[deleted] Dec 02 '19

[deleted]

1

u/xelf Elf Dec 02 '19

I'm enjoying it. I did use Python for day 2. Looking forward to the next one now!

2

u/[deleted] Dec 02 '19

Thx may be the right thing for me. Gonna try it later

2

u/yodatrust Dec 02 '19

Looks cool! Will try this after work. Thanks for sharing!

2

u/Devnull1982 Dec 02 '19

Wow, nice webside, every exercise is very well explained and adorned with a game ( the santa claus etc...), I like it. I do the first exercise of Day 1 with Python very quickly with no problems. The second exercise it was easy too but something I was doing wrong that the result is not correct, after 20 min I see the error.

PD: Im still learning python ( with python crash course ) and I think this will help me alot. THX.

2

u/Bobbias Dec 02 '19

Day 2 was pretty fun. Ended up brute forcing the answer for part 2 by hand though. Once I caught on to how the 2 numbers interacted it was pretty quick to test different ones, though maybe I should automate brute forcing it later just for the hell of it.

Did learn how to send cookies with urllib requests though, because I figured it would be better practice to grab the data off the site rather than just save it to a file.

I'll be sure to cache the results when I make my brute force version of day 2 part 2 though.

1

u/Kit-ra Dec 02 '19

This looks wicked! Thanks!

1

u/CornersTheCoder Dec 02 '19

Thank you for doing this. It also looks like it goes back to 2015 which is awesome! Gives 125 projects when I learn some, even if it is not in December. Thank you again! :)

1

u/Kunal_Jain Dec 02 '19

Thank you for sharing this! will try to solve each puzzle!!

1

u/MiataCory Dec 02 '19

If anyone needs help, I just got through day 2.

I like the add-on questions. "Hey, you did the thing once, now make the computer do it many times!"

3

u/RoadieRich Python 2 diehard Dec 02 '19

I'm curious to know if anyone didn't brute-force day 2 part 2.

2

u/[deleted] Dec 02 '19

There is a handful in the solutions thread that used constraints, yes.

1

u/raglub Dec 03 '19

After brute-forcing the solution, I observed the output of each iteration and realized this is the equation that the Int code solves:

394702 + verb + noun*288000 = 394702 

reduced further:

verb + noun*288000 = 0

Since there are two unknowns and only one equation, I couldn't come up with a clever way to calculated the solution other than brute-forcing it.

1

u/shiningmatcha learn python Dec 02 '19

Is it possible to customise the font?

1

u/jmit7521 Dec 02 '19

Thanks for sharing this I will attempt some solutions after work.

1

u/[deleted] Dec 02 '19

Great, Thank you for posting!! I Just finished “Automate the Boring stuff with Python,” so this should be good to practice what I learned.

1

u/ffrkAnonymous Dec 03 '19

Looks fun. I'll give it a shot.

The sidebar list of sponsors showed TwilioQuest, that looks fun too!

-20

u/Thecrawsome Dec 02 '19

You have to chop out a lot of the question descriptions to what the question is actually asking you to do. Lots of bold abuse. Are you bolding terms? Are you bolding emphasis? Fuck it, bold anything just because you'd inflect on it in real speech!

5

u/CleverBunnyThief Dec 02 '19

I went back and looked at the first puzzle and only saw three bolded items "module", "mass" and "What is the sum of the fuel requirements". Those are the key parts of the whole puzzle. They are hints actually. I took module and mass and made them variables in my code.

I think it's a good idea since there was so much text on that page and not all of it is needed to solve the puzzle. I had to read the whole thing a few times to make sure I wasn't missing something.

Edit: Spaced out second paragraph.

-1

u/Thecrawsome Dec 02 '19

lol at least finish the first half of the first puzzle before you weigh in?

that

also

wishing really hard

1

u/CleverBunnyThief Dec 02 '19

I finished both last night.

Just some bold font. Don't see what the big deal is.

7

u/JuniorData Dec 02 '19

Are you okay?

0

u/[deleted] Dec 02 '19

[deleted]

6

u/errorseven Dec 02 '19

He's not okay. I think he needs a nap.

0

u/[deleted] Dec 02 '19

[deleted]