We're on to Day 3 now! I'm seeing a bunch of cool languages getting submitted, which is really fun. Let's get to the puzzle!
The Puzzle...
For further actions, you may consider blocking this person and/or reporting abuse
Rust again
Rustaceans gonna stick together 🦀:
As always, also available on Github.
The
enum
implementation for your input is cool; I wouldn't have thought of that.First time I've done Advent of Code, but here is my Haskell soloution for Day 3:
Rust using lots of iterators and generators. I'm trying to learn the standard library properly now.
Nice! Didn't know about
product()
; will have to keep that in a pocket.I like that your solution allows for an arbitrary starting point, rather than always just (0,0).
One thing I've learned about AoC - try not to bake in any assumptions in part 1!
Maybe this'll be our repeating problem (like the opcodes last year)... Different starting points, different types of obstacles in the snow, terrain to avoid, ...
A bit of Go
typical December in Europe
There is my solution, Elixir as always. I'll switch to Golang/PHP when I get stuck.
Day 3, part 1:
Day 3 part 2:
What's your experience guys? Better to keep both parts in one class/module / whatever?
I typically keep them in the same file in different functions, since work from part 1 is often shared to part 2, (like today), but every so often, he throws a curveball and part 2 ends up being a total rethink, and then it may be nicer to have more separation. It probably doesn’t matter a ton :)
Still going strong with COBOL
Here is my Day 3 in Ruby
OK! Here's my solution. I wasn't sure how managing the 2D grid was going to go in C since I've historically struggled with them in Rust. But it went pretty easy and ran super fast, so I'm very happy with it :)
I had trouble for a minute because my code was skipping slots in my array because of the newline characters in the input. Once I stopped incrementing my index when I saw a newline, it shaped right up!
Day3.h:
Day3.c:
Additional code to parsing.h:
Aaaand the implementation:
Completed my day 3 with a little help from comments here
Short Python solution
I put my solution in a gist. It's only for the second part, but I think it's easy enough to adjust it back for the first.
That was quick! I'm just going to put that this one is in JavaScript for my future language-tallying self 😊 Thanks for sharing!
I'm always amazed by the main leader board. Some people seem to have solved the tasks in under five minutes. I can't even read it in that time 😁
Mine!
Hi,
In PHP again, for fun, I try to have the minimum of temporary variables.
Full size here : Advent Of Code - Day 3
Haskell
Python effort, short and simple and gives the right answer.
Hi,
Can you explain why "line = line + line" 7 times?
(Maybe i missed something in the question...)
10x :)
Hi Rabbi, you have a short string that repeats indefinitely. As you move right you quickly hit the end of the string. It was a very simple way to repeat the pattern otherwise you get an index out of range error when you exceed the original string length.
Its not very efficient as it repeats every line but it works in this instance. If you had to create longer strings you might want to refactor to check if you've hit end of string and only repeat then.
Dan
Great! That helped me understand my mistake (Thous that end of line leads to the next line) :)
Python implementation using numpy.
Spent ages trying to get this figured using array striding but eventually my head exploded and I resorted to using indices.
Vanilla JS solution in 12 lines of code:
//create arr with input as template literal
let newArr= arr.split(/\r?\n/);//split by endlines
let xCord = 0;//X coordinates
let counter = 0;//trees hit counter
for(let i = 0 ; i < newArr.length ; i++){
let temp = newArr[i].split('');//split into array of characters
if(temp[xCord]=='#'){//checking for trees
counter++;//if there is a tree increment the counter
}
xCord = xCord +3;//add +3 to the right
if(xCord>=31){//check if there is more coords than elements in array
xCord = xCord-31;//reset coords
}}
console.log(counter);//result
SQL works for me ;)
0 lines of code. No loops, no boolean flag, only SQL.
Get this table naming 'day3':
3.1 Solution
3.2 Solution
Here's my beginner's Haskell solution:
My solution in Python below for part 1. The hardest part for my was to figure out that the starting point is one further to the right then I expected...
My ugly python version
Hi! Thanks for sharing!
FYI, if you want to get some syntax highlighting in your comments, you can use three backtics followed by the language name to open and three backticks by themselves to close. Here's the Markdown cheat sheet for reference.
My solution in C#:
My JavaScript walkthrough:
My python solution here
Unrelated to the challenges, how did you do the list of post links in your post?
You can add a “series” to front matter on the post. As long as they all match, they’ll get linked up by initial release date.
Thanks!
I wrote my solution in a notebook in Javascript:
starboard.gg/nb/nLjayUM