A nice way to solve both parts in one go is to modify the DFS so it returns a list of (r,c) coordinates instead of the count, so then p1 is len(dfs()) and p2 just becomes len(set(dfs())). It wouldn't have occurred to me to do it this way for Part 1 because of exponential blowup, but it does solve both parts nicely in hindsight :)
As I was struggling to solve part 1 I accidentally solved part 2 first. You can unify part 1 and part 2 by just not checking the seen set in part 2. def dfs(r, c, board, num, seen, part1): if not (0
Mostly by not reading stuff! Advent of Code problems have three parts: the story, the actual question, and a worked example. The story is always 100% skippable. The example can be skipped although its probably a good idea to look at the input so you know what kind of data you're dealing with. The "main question" is at the bottom: "What is the sum of the scores of all trailheads on your topographic map?" This uses some unclear jargon ("what is a 'trailhead'? what is its 'score'?) but we can scroll back up to read "A trailhead is any position that starts one or more hiking trails - here, these positions will always have height 0. Assembling more fragments of pages, you establish that a trailhead's score is the number of 9-height positions reachable from that trailhead via a hiking trail." These couple sentences are all that is needed to understand what part 1 is asking for. The tricky part is just finding them among all the other stuff. Then in part two, there are two key sentences: "A trailhead's rating is the number of distinct hiking trails which begin at that trailhead." "What is the sum of the ratings of all trailheads?" So we only need to read ~5 sentences to understand both parts, which is pretty quick to read.
mathematically good, but overengineered?!?
@4:07, after P1
removing if (r,c) in SEEN: continue
should do it, shouldn't it?
A nice way to solve both parts in one go is to modify the DFS so it returns a list of (r,c) coordinates instead of the count, so then p1 is len(dfs()) and p2 just becomes len(set(dfs())). It wouldn't have occurred to me to do it this way for Part 1 because of exponential blowup, but it does solve both parts nicely in hindsight :)
I think you mean p1 is set() and p2 is just len() because p2's answer is bigger-it overcounts based on the paths, not the endpoints
As I was struggling to solve part 1 I accidentally solved part 2 first. You can unify part 1 and part 2 by just not checking the seen set in part 2.
def dfs(r, c, board, num, seen, part1):
if not (0
how do you read so quick
He uses python to read😂😂😂
Mostly by not reading stuff!
Advent of Code problems have three parts: the story, the actual question, and a worked example. The story is always 100% skippable. The example can be skipped although its probably a good idea to look at the input so you know what kind of data you're dealing with.
The "main question" is at the bottom: "What is the sum of the scores of all trailheads on your topographic map?" This uses some unclear jargon ("what is a 'trailhead'? what is its 'score'?) but we can scroll back up to read "A trailhead is any position that starts one or more hiking trails - here, these positions will always have height 0. Assembling more fragments of pages, you establish that a trailhead's score is the number of 9-height positions reachable from that trailhead via a hiking trail."
These couple sentences are all that is needed to understand what part 1 is asking for. The tricky part is just finding them among all the other stuff.
Then in part two, there are two key sentences:
"A trailhead's rating is the number of distinct hiking trails which begin at that trailhead."
"What is the sum of the ratings of all trailheads?"
So we only need to read ~5 sentences to understand both parts, which is pretty quick to read.
1:54 if you just add a continue there, none of the SEEN stuff is necessary, but that's an obvious in hindsight / as a viewer thing