16
votes
Day 2: Dive!
Today's problem description: https://adventofcode.com/2021/day/2
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>
Quite chill, seems simpler than day 1.
Part 1
Part 2
Trying to get better at golfing, day 2. LMK if you have any golf tips, especially for python.
Part 1
Part 2
Welcome to Tildes!
For golfing, you could prune a few characters by placing the input in a file with a short name and hardcoding it:
open("i")
in place ofsys.stdin
.Edit: Even better, I just learned you can use
open(0)
to read stdin!I managed to golf your code down a bit more:
Part 1 (
chars -= 31
)Part 2 (
chars -= 26
)Using booleans as indices to replace conditionals,
exec
, and seeing if==
can be changed to>
/<
seem like they'd be pretty widely applicable tricks for one's Python golfing tool belt.That's clever... I'll try some of these going forward
C#
Part 1
Part 2
Commentary
Frustrated by leaderboard today - I was delayed by probably a minute because my inputs were still reading last year's input files. I also didn't bother getting the sign right for part 1 - I just removed the `-` myself when submitting.Racket as usual - I'll ignore the reading of the input file because it's so trivial in Racket and I expect the same for most other languages.
Part 1
Part 2
Notes
I'm going to focus my notes on introducing some of the nice features of Racket and/or interesting things about the solutions.Today is
match
The nice thing about
match
is not only de-structuring and matching against types - including literal values, but you can both bind and transform the matched input in one go withapp
.(match (split line)
match the result of splitting line (by default on space)
[(list "up" (app string->number amount)) <body>]
when you find
(list "up" <exactly one other value>)
bindamount
to the result ofstring->number
applied to that second value.For some reason my kernel was completely unresponsive when I booted up Jupyter. Weirdly restarting python didn't help, but closing out of chrome and opening it back up did
Day 2 Part A – Python
Like yesterday just a really simple text parser and some basic math, This one didn't even need a data structure.
Day 2 Part b – Python
Not much to say here, just switch depth for aim and add an equation to handle the multiplicationTips
The real trick is making sure your text parser can handle the different instructions 'forward val', 'up val' and 'down val'. I'm using python, so I was able to use 'split' to identify which command and what value was tied to it
When parsing strings don't forget to cast it to a type of variable you can do math on
whooo! Google Sheets Day 2!
Part 1
Part 2
First I brought together the ups and downs (E2)
Next I ran the horizontal stuff (F2)
Lastly, I used a VLOOKUP with TRUE to align the horizontal values with a running total and brought it all together.
Not the prettiest thing, but it worked...
Python
This is what I came up with for day 2 (omitting Part one, because of the verbosity).
Day 02 — Part 2 (Rust)
The solution is quite verbose, but defining Position/Instruction as struct/enum makes the actual solve function trivial -- a simple call to fold.Now that I posted this, I can finally look at the other solutions in the thread. :)
If you wanted to get into implementing traits for your own types, I would recommend looking at FromStr. If you implement that on your Instruction type, you could then just call
.parse()
on a string and get a Position object, rather than having to build a constructor (though you might need the turbofish.Here's how I did mine
Full solution here
Thanks, I'll keep that in mind for the remaining days -- I'm sure this won't be the last puzzle that requires parsing something into a convenient struct.
Here's my solution on Sourcehut (Rust)
My journey in to learning Rust continues. I found the logic for both parts to be easy, figuring out how to get the language to do what I wanted was a bit tougher (specifically for Part I).
Part 1
Part II
Same main as above but with a call to
part2
So far my biggest "complaint" is that the rest of you guys are too smart and clever. I was struggling with Part II yesterday so I tried to see what other people were doing and nothing I saw remotely made sense, haha.
Definitely looking at both Rust and Python solutions leads me to believe I am still using Nim like it's Python. I don't think that's wrong, exactly, since what I like about it is that it is a statically typed, compiled, fast Python-like language. Anyway, my solutions:
Part 1
Part 2
Day 2 in Go
Part 1
Part 2
Rust
My Rust skills are very rusty at the moment. I'll need to go back and see if I can make this code more idiomatically Rust.
Solution
Another warm up day, only this time I didn't lose time to a self-inflicted input handling error. I'll probably try to get straight into day 3 when it releases tonight instead of waiting for the morning.
Part 1 (Ruby)
I considered tucking the position/state into a hash or something and making it all part of a fold operation, but I was in a lazy mood.
Part 2 (Ruby)
Day 2 in Rust. I've got functions for reading the lines and doing the output but those are boring so not including the source.
Both days (Rust)
I'm very much not great at Rust, but I think it's a cool language. If anything is particularly un-idiomatic in Rust feel free to let me know! :)
Did mine in Swift!
Solution
I'm late but I wanted to give it a try anyway!
Part 1 (Rust)
Part 2 (Rust)
Both of these felt too easy.
Part 1 - Javascript
Part 2 - Javascript
I agree with everyone else, this one felt a little easy. Still, no compaints, I'm sure it will get more difficult quite quickly!
Part 1
Part 2