12
votes
Day 3: Toboggan Trajectory
Today's problem description: https://adventofcode.com/2020/day/3
Join the Tildes private leaderboard! You can do that on this page, by entering join code 730956-de85ce0c
.
Please post your solutions in your own top-level comment. Here's a template you can copy-paste into your comment to format it nicely, with the code collapsed by default inside an expandable section with syntax highlighting (you can replace python
with any of the "short names" listed in this page of supported languages):
<details>
<summary>Part 1</summary>
```python
Your code here.
```
</details>
Parts 1 and 2
I got a little tripped on the the classic problem of reversing my x and y indexes into my 2d array. That cost me at least a few hundred ranks on the global leaderboard.
I love the stories behind the puzzles.
I actually forgot that POSIX AWK supported modulo, but I didn't have trouble going without it;
substr
starts its indexes at 1, anyway. While part 1 was very short and simple, I did feel some other POSIX limitations restrictive on structuring part 2. But it's nothing that slightly messier code can't fix.Part 1
Part 2
Code and ramen make a great midnight treat! My food just finished right as the challenge started. This one as pretty fun. I choose to interpret is as if you're colliding into the trees, rather than swerving to avoid them, so that definitely amused me while I was writing my code.
And shout out to % for being the MVP in this one!
Day 3 Part A - Python
For part A I just split up the rows based on the newline character and then used the fact that I can index on a string to get a specific character. The fact that it repeats meant modulus is a great solution for solving how x wraps around. From there it was just a matter of counting how many trees you hit on the way down.I had a feeling that part B would have multiple slopes, so I tried to make it so it would be easy to convert
Day 3 Part B - Python
For Part B I converted the main portion of part A into a method and used the different slopes as arguments. Then I stepped through each of the different slopes and stored their output in an array. Finally I multiplied the contents of that array together. Actually storing the data like I did wasn't really necessary. I could have easily just have run the different versions manually and plugged the results into a calculator, but this felt cleaner and more robust.Tips for Beginners
If you treat the forest like a grid, you can use a coordinate system to walk through it
The % symbol I pointed to in my description means modulus. If you've ever done division by hand, it's the remainder so for example 7 % 3 = 1. Using modulus lets you allow the forest to wrap around infinitely without you needing to worry about it
In part B, the easy way is to just manually modify your program for each of the different slopes and record the answers on paper. But you might find it more satisfying to see if you can create a program that can do the whole process without manual intervention
With Google Sheets I used a variation of this for everything. The only major difference between the first four and the last one was pulling odd rows. Nothing crazy.
Part 1 and 2
For the 1 - 2 one...
Solution
176 and 5,872,458,240
You're using Google Sheets, that's pretty crazy (crazy impressive, that is).
Interesting, I didn't know Advent of Code had multiple problem answers / puzzle inputs. My answers were
242
and2265549792
.it's been so much fun so far. I can't believe some of the insane languages some folks are using.
I was doing 2 pt2, kept having problems with my code, so I saw a lot of different messages: Too high, too low, here's some tips, but one stood out: "You've entered somebody else's answer! ..."
I guess otherwise it would be like Project Euler where you'll find solutions all over.
I got a little worried when I started reading and saw the diagram, figuring it would be another spacial-relation puzzle (and ho boy, I'm horrible at those!) so I was glad to see it was more mathematical than that. Repo is here
I totally re-did part 1 after reading the spec for part 2. Should have split that function out to begin with.
Part 1
Part 2
I had no idea how to approach this for a while, but the hints given by @PapaNachos were invaluable, and I managed to solve the problem fairly quickly. I'm not normally one for defining functions in my code, so planning ahead and getting the answer for part 2 just by adding a few extra lines was a really nice bonus.
Part 1 runs in 0.00059 seconds
Part 2 runs in 0.00081 seconds
I'm glad they helped! Sometimes these problems have a couple obstacles that make them difficult to wrap your head around in the first place.
All of your previous days have been so functional programming oriented, I'm surprised to see an explicit
for
loop in your second part! You could get the same thing with something likelet result_two = increments.iter().map(|x| calculate(&data, x)).product()
Did mine earlier today. I generalized the initial solution to a generic one for the second part. I've decided to go heavy into solving it mostly with numpy just to try and keep some of that knowledge fresh (was a while since I worked with it). I struggled first with since I missed the part about the pattern repeating itself, but once I realized this mistake it was fairly straight forward.
Part 2 (core code solves 1 too)
Realized I could... (see details below)
Part 2 (core code solves 1 too) v2
...ditch the hstack part to make it cleaner by just using modulo on the indexes! One further improvement (?) to the code below would be to use a flat array to represent the input, then just add `np.arange(len(v)) * len(v[0])[::vStep]` to the index `i`. Then use `sum(v.reshape(v.size)[i] == '#')` to get the tree count for a slope...Rewrote my initial Perl solutions with both now working.
Part 1 (part 2 nested inside somehow??)
Part 2
Python. Thanks again to @blitz for the help with the filename.
Part 1+2
Scheme solution
Part 1 & 2
I'm using an identity function as my parser because I don't want to mess with my infrastructure.Nothing too special here, very similar to solution to most people I imagine. 858/576.
Part 1 and 2 in JS
Solution in Rust. Cleaned up a bit.
Rust
I joined this a bit late (only today). I just finished all of them, now I am in the mood for more
Part 1 and 2 in Python
Day 3 (parts combined)
map
, and vectorization okayPart 2 was trivial once I had part 1 the way I wanted it, so I'll just do one box.
Parts 1 and 2
I keep overwriting part 1 as I work on part 2
I'm failing to understand how can we find the pattern in the input data.
Oh, the complete input? I thought that we had to figure out a row where we would cut the input file, and then paste it to the right, iteratively. Silly me.
it took me a lot more ahah