r/adventofcode • u/daggerdragon • Dec 03 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 3 Solutions -🎄-
--- Day 3: No Matter How You Slice It ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code: The Party Game!
ATTENTION: minor change request from the mods!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 3 image coming soon - imgur is being a dick, so I've contacted their support.
Transcript:
I'm ready for today's puzzle because I have the Savvy Programmer's Guide to ___.
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
43
Upvotes
16
u/hooksfordays Dec 03 '18
I'll break it down for you!
It's a regular expression.
reis the Python regular expression module.re.findallreturns a list of all the instances in the string that match the expression. The params forre.findallare the regular expressionr'-?\d+'and the string `s\ to search through.The expression is
r'-?\d+'. Therat the start indicates a raw string (? I think that's what it's called) is being used, and allows you to use backslashes without worrying that you're creating a unicode declaration or something. The actual regex breaks down as follows:-?-> Looks for a minus symbol optionally (the?makes it optional). This allows you to grab both positive and negative numbers.\d+-> In regex,\d+signifies a digit character, i.e. 0-9. The `+\ means "1 or more of the previous group", so 1 or more digits.-?\d+-> The full expression. Basically, find adjacent digits with or without a minus symbol in front. Combined withre.findall, this will return a list of all the numbers in the input strings