Advent of Code 2019...save Santa and Xmas with programming!

ElFly

Member
Oct 27, 2017
981
edit: you can still join in! You can catch up to the puzzles being released at any time. it's not a race, but a marathon

edit 2: We have a leaderboard



Hidden content
You need to reply to this thread in order to see this content.


Hey guys, this year Advent of Code started.


Every day of december up to the 25, a small coding puzzle will be released. For example, today's (the first one) is pretty simple:


The Elves quickly load you into a spacecraft and prepare to launch.


At the first Go / No Go poll, every Elf is Go until the Fuel Counter-Upper. They haven't determined the amount of fuel required yet.


Fuel required to launch a given module is based on its mass. Specifically, to find the fuel required for a module, take its mass, divide by three, round down, and subtract 2.


For example:


  • For a mass of 12, divide by 3 and round down to get 4, then subtract 2 to get 2.
  • For a mass of 14, dividing by 3 and rounding down still yields 4, so the fuel required is also 2.
  • For a mass of 1969, the fuel required is 654.
  • For a mass of 100756, the fuel required is 33583.

The Fuel Counter-Upper needs to know the total fuel requirement. To find it, individually calculate the fuel needed for the mass of each module (your puzzle input), then add together all the fuel values.


What is the sum of the fuel requirements for all of the modules on your spacecraft?
The puzzle gives you as input a long list of numbers, the weights of each module.

So basically you just read all the numbers from a file, line by line, then calculate the fuel and add the total.

Once you get that right, you get access to a second puzzle, a little more complicated

During the second Go / No Go poll, the Elf in charge of the Rocket Equation Double-Checker stops the launch sequence. Apparently, you forgot to include additional fuel for the fuel you just added.


Fuel itself requires fuel just like a module - take its mass, divide by three, round down, and subtract 2. However, that fuel also requires fuel, and that fuel requires fuel, and so on. Any mass that would require negative fuel should instead be treated as if it requires zero fuel; the remaining mass, if any, is instead handled by wishing really hard, which has no mass and is outside the scope of this calculation.
Which is largely the same solution as for the first part, just with one little recursive function added (or, whatever method you use).

The puzzles start easy and ramp up to way crazier; but never anything with graphics, or using any weird library. The inputs are always ascii text at most.

You can sign up with a github account, google account or twitter account.

Sorry I forgot to make this thread earlier! But you can still join in.

Come on, let's save Santa.
 
Last edited:

Serif

The Fallen
Oct 27, 2017
1,513
Sweet - I've got an interview coming up, this'll be a fun way to practice - thanks :)
 

Anustart

9 Million Scovilles
Banned
Nov 12, 2017
2,967
Hey guys, this year Advent of Code started.


Every day of december up to the 25, a small coding puzzle will be released. For example, today's (the first one) is pretty simple:




The puzzle gives you as input a long list of numbers, the weights of each module.

So basically you just read all the numbers from a file, line by line, then calculate the fuel and add the total.

Once you get that right, you get access to a second puzzle, a little more complicated



Which is largely the same solution as for the first part, just with one little recursive function added (or, whatever method you use).

The puzzles start easy and ramp up to way crazier; but never anything with graphics, or using any weird library. The inputs are always ascii text at most.

You can sign up with a github account, google account or twitter account.

Sorry I forgot to make this thread earlier! But you can still join in.

Come on, let's save Santa.
Thanks for this I'm gonna give it a go!
 

Post Reply

Member
Aug 1, 2018
2,161
Thanks for posting! I was looking for something to get into to mess around in Python and this seems perfect
 

Anustart

9 Million Scovilles
Banned
Nov 12, 2017
2,967
Anyone wanna spoiler the answer for the second part so I can work backwards? Even manually crunching the numbers I get the same wrong answer my recursive function is getting :/
 
Last edited:
OP
OP
ElFly

ElFly

Member
Oct 27, 2017
981
inputs are not all the same, tho i don't think they are unique per person

you can look at my code

 

Anustart

9 Million Scovilles
Banned
Nov 12, 2017
2,967
inputs are not all the same, tho i don't think they are unique per person

you can look at my code

Thanks! I might have misunderstood one small portion in b. Will make that adjustment!

Edit: Nope, no clue what I'm doing wrong. This should be so easy and part b is just part 1.
 
Last edited:
OP
OP
ElFly

ElFly

Member
Oct 27, 2017
981
The best way to get help is normally to get someone else input and run it with your (non functioning) code, and ask the other person if their answer matches

but I forgot about this protocol from last year

the only problem is that sometimes there are many rules to code so not all inputs match all the rules and thus most solutions don't actually solve all the possible inputs

edit: the thing is that after a few wrong tries, the page will give you a cool down before accepting your answer again, so it's useful to be able to check without waiting it out
 
Last edited:

Jezze

The Fallen
Oct 25, 2017
463
F# today

Code:
let moduleMasses =
  [
    // insert numbers here
  ] |> Seq.map float

let getFuelRequired mass =
  (floor(mass / 3.) - 2.)

let rec getFuelRequiredRecursive mass =
  match (getFuelRequired mass) with
  | fuelMass when fuelMass <= 0. -> 0.
  | fuelMass -> fuelMass + (getFuelRequiredRecursive fuelMass)

let getTotalFuelRequired fuelRequiredForMassFunc masses =
  Seq.fold (fun total mass -> total + (fuelRequiredForMassFunc mass)) 0. masses

let partOneAnswer =
  getTotalFuelRequired getFuelRequired moduleMasses

let partTwoAnswer =
  getTotalFuelRequired getFuelRequiredRecursive moduleMasses
 
Last edited:
OP
OP
ElFly

ElFly

Member
Oct 27, 2017
981
I like the idea of f# but don't wanna to get trapped in the ms ecosystem... says someone who's food on the table is paid for by java
 

Jezze

The Fallen
Oct 25, 2017
463
I like the idea of f# but don't wanna to get trapped in the ms ecosystem... says someone who's food on the table is paid for by java
I hear ya. I just recently jumped out of ms shop land; been wonderful so far :P. F# is pretty easy to get into from C# and it’s not a big step to get to Haskell and a lisp after if you fancy more dynamic stuff. I haven’t used any of them besides F# professionally (yet), but I do write most of my code in an FP manner if possible .
 

NumberThree

Avenger
Oct 27, 2017
2,160
Thanks for making the thread, I'll try to keep up with this! So far I've only made dumb bruteforce solutions in C++ for the first two days, but I already want to improve the day 2 solution to make it more robust & generic
 
OP
OP
ElFly

ElFly

Member
Oct 27, 2017
981
Second day done!

Last year there were a few computer simulation puzzles. The later ones are some of the hardest, but early on they were easy

My day 2 solution: https://github.com/Randdalf/aoc19/blob/master/d02.py

I've been writing a virtual machine of sorts in my spare time, so these ones are always fun.
goddamn, proper classes and all

my code is just as long but messy


Luckily the solution was found early on. Was ready to do binary search.

Until you have to decipher the meaning of the programs and fix them.
Last year I deduced partially what the program was doing and wrote that part in java; brute forced the rest so it took a few hours.

No shame.
 
Last edited:

Randdalf

Member
Oct 28, 2017
1,690
Second day done!

Last year there were a few computer simulation puzzles. The later ones are some of the hardest, but early on they were easy



goddamn, proper classes and all

my code is just as long but messy


Luckily the solution was found early on. Was ready to do binary search.



Last year I deduced partially what the program was doing and wrote that part in java; brute forced the rest so it took a few hours.

No shame.
It's just my way of making an enum in python 🙂. I figured it would be a good idea to be neat with this one because we're clearly going to be re-using it.

I believe I did the same with the brute forcing.
 

Morzak

Member
Oct 27, 2017
114
Hm nice, Startet doing in js, lets see maybe i change to python too, would be good to get my rust of ther, or maybe try out kotlin or rust.
 

Dervius

Member
Oct 28, 2017
1,341
MK | UK
Cheers for this OP.

I've hit a snag on the challenge for the 2nd day, the one with the opcodes. It's asking for the value of the number at position 0, but mine doesn't seem to be right.

I can;t work out from the intro whether or not I should be reading each value of the list in turn, and performing the associated operation if it's a 1, 2 or 99, or whether, after performing for instance Opcode 1, then jump forward four places and continue from there.

I've set it up using For loop for the former and a while loop for the latter, neither seems to garner the right result.
 
OP
OP
ElFly

ElFly

Member
Oct 27, 2017
981
read them four by four

it does not matter that the input does not have a multiple of four amount of lines

once you hit the 99 you exit anyway

remember that at the start you need to put certain values on positions 1 and 2 (don't touch position 0; it's both the first instruction and you answer at the end. I assume at some point you multiply by 0 or there's a 0 on the result of the next to 99 operation)
 

Dervius

Member
Oct 28, 2017
1,341
MK | UK
read them four by four

it does not matter that the input does not have a multiple of four amount of lines

once you hit the 99 you exit anyway

remember that at the start you need to put certain values on positions 1 and 2 (don't touch position 0; it's both the first instruction and you answer at the end. I assume at some point you multiply by 0 or there's a 0 on the result of the next to 99 operation)
Thanks for confirming four by four, reading back there is a line that clears that up. I was still getting 1 as my final position 0 value, which is incorrect (and is the same as the input).

I had misread the problem, I was writing my result in to the list at position i+3, not at the position list[list[i+3]].

All works fine now.
 
Oct 27, 2017
2,833
Thanks for this OP, caught up on the first two days this morning and looking forward to the next one. Using Python, good way to learn.
 

Antagon

Member
Nov 4, 2017
165
Thanks for the headsup! Worked my way through the first two assignments, nice way to learn awk syntax.
 
OP
OP
ElFly

ElFly

Member
Oct 27, 2017
981
awk? goddamn

always use that and sed for disposable one liners that I steal from the net
 

Anustart

9 Million Scovilles
Banned
Nov 12, 2017
2,967
Anyone care to look at this and give me a hint where my code is running afoul?

Edit: Again, reading comprehension failed me lol. Onto part b!
 
Last edited:

Post Reply

Member
Aug 1, 2018
2,161
Alright, I don't even understand what part b is wanting... :/
The combination of "noun" and "verb" values (the initial IntProgram[1] and IntProgram[2] indexes) that cause the final program output (IntProgram[0]) to equal whatever value it's asking for.

Once you get those 2 values, you can just plug them into the formula it asks for, which for me was 100 * noun + verb. The answer to that is the number you put into the submission form
 

JeTmAn

Member
Oct 25, 2017
2,934
Would be fun to do this in 25 languages.

C#
C
C++
Java
VB.NET
Javascript
Python
SQL

Would be my starters
 

Anustart

9 Million Scovilles
Banned
Nov 12, 2017
2,967
The combination of "noun" and "verb" values (the initial IntProgram[1] and IntProgram[2] indexes) that cause the final program output (IntProgram[0]) to equal whatever value it's asking for.

Once you get those 2 values, you can just plug them into the formula it asks for, which for me was 100 * noun + verb. The answer to that is the number you put into the submission form
Thanks! Why am I so dumb.

Solved it almost immediately. Seemed quite a bit easier than yesterdays part b.
 

Antagon

Member
Nov 4, 2017
165
awk? goddamn

always use that and sed for disposable one liners that I steal from the net
Masochist that I am, just did exercise one from day two in a awk one liner.

At least I'm getting a grab on the syntax. Not exactly recommended for readability though.

Code:
awk -F ',' '$0 { i = 1; $2 = 12; $3 = 2; do { print $0; a = $($(i+1)+1); b = $($(i+2)+1); c = $(i+3); if($i == 1) { $(c+1) = a + b } else { $(c+1) = a * b }; i = i + 4; } while ($i == 1 || $i == 2); print ($i == 99 ? $1 : "ERROR") }' input.txt
First print statement is for debugging purposes.

Shifting the index (awk starts with index 1, 0 is the whole line) is rather annoying though.
 
Last edited:
OP
OP
ElFly

ElFly

Member
Oct 27, 2017
981
Just read the prompt for today. Still a little groggy in the morning but it looks cool.

Luckily it's only two wires!


edit: goddamn, tried it and it works with the examples, but my solution is way too slow

the fuck

edit2: used sets and it made the damn thing reasonable. onto part 2

edit3: got part b. made a mistake cause I processed the first wire twice in part b, but the examples it gives pointed out I had an error quickly.


Day 3 done: https://github.com/Randdalf/aoc19/blob/master/d03.py

It's starting to get interesting. I think the way I solved it is pretty neat. Some people with clever answers to part one might be tripped up by part two. Keep it simple.
I am glad I didn't use a
giant matrix and just had a list of the steps visited

made part two v simple
 
Last edited:
OP
OP
ElFly

ElFly

Member
Oct 27, 2017
981
Masochist that I am, just did exercise one from day two in a awk one liner.

At least I'm getting a grab on the syntax. Not exactly recommended for readability though.

Code:
awk -F ',' '$0 { i = 1; $2 = 12; $3 = 2; do { print $0; a = $($(i+1)+1); b = $($(i+2)+1); c = $(i+3); if($i == 1) { $(c+1) = a + b } else { $(c+1) = a * b }; i = i + 4; } while ($i == 1 || $i == 2); print ($i == 99 ? $1 : "ERROR") }' input.txt
First print statement is for debugging purposes.

Shifting the index (awk starts with index 1, 0 is the whole line) is rather annoying though.
damn
 

Anustart

9 Million Scovilles
Banned
Nov 12, 2017
2,967
Anyone point me in an area to research to best split that input file in c# into two based on the carriage return?

As is now, using a streamreader, having difficulty in how to approach that.

Edit: Nevermind, got it. In fact I already had it, just had a slight bug :/
 
Last edited:

Morzak

Member
Oct 27, 2017
114
So finished Day03, not an elegant solution and the actual input took ages to run... but that is expected with that much iteration through 2 arrays...
 

NumberThree

Avenger
Oct 27, 2017
2,160
Something's fucked in my solution for the second part of day 3, I'm getting too low of a result. I'm getting the correct result for one of the examples, so it's not completely broken at least. Maybe I'll figure it out after a good night's sleep.
 
OP
OP
ElFly

ElFly

Member
Oct 27, 2017
981
So finished Day03, not an elegant solution and the actual input took ages to run... but that is expected with that much iteration through 2 arrays...
My original solution would have ended in 2 hours (python)....and then I'd have to do it again for part B if I didn't save the result.

Did some marginal optimization and it ran super fast.
 

Anustart

9 Million Scovilles
Banned
Nov 12, 2017
2,967
For Manhattan distance, if the points match we can just take one of the points and add the absolute value of x,y to get it right?

Edit: Well guess im done. Woo made it through 2 days. Man I fucking suck.

Here's my shitty ass code if anyone wants to take a look:

https://github.com/kefkamaydie/Day3

3 Files that matter are Line, Point and Program.

Shit takes 6 minutes to give me the wrong answer.

Ran my code against the test cases. Code is 13 off on test case 1, but gets the correct answer for test case 2 O.O


Edit: Sorry for being the dumbest person on era.

When comparing equivalency I was comparing absolute values, which was correct, but when computing distance I didn't use absolute values. Man I'm so stupid.
 
Last edited:

Morzak

Member
Oct 27, 2017
114
My original solution would have ended in 2 hours (python)....and then I'd have to do it again for part B if I didn't save the result.

Did some marginal optimization and it ran super fast.
Nothing that bad, but it took a few minutes, at least I only had to extend the found intersections object with the iterations it took to find it for step 2. But yeah there would be some optimizations that would make it way faster, but I don't have the energy to do that at the moment.

Did part one of Day4 on my way to work, part 2 should not be that much more work. Will do it after Work.
 
OP
OP
ElFly

ElFly

Member
Oct 27, 2017
981
Did day 4. No problems today.

For Manhattan distance, if the points match we can just take one of the points and add the absolute value of x,y to get it right?

Edit: Well guess im done. Woo made it through 2 days. Man I fucking suck.

Here's my shitty ass code if anyone wants to take a look:

https://github.com/kefkamaydie/Day3

3 Files that matter are Line, Point and Program.

Shit takes 6 minutes to give me the wrong answer.

Ran my code against the test cases. Code is 13 off on test case 1, but gets the correct answer for test case 2 O.O


Edit: Sorry for being the dumbest person on era.

When comparing equivalency I was comparing absolute values, which was correct, but when computing distance I didn't use absolute values. Man I'm so stupid.
programming is a constant exercise on finding how dumb we are

it's fine

I made the absolute values mistake too at one point yesterday

Day 4 done: https://github.com/Randdalf/aoc19/blob/master/d04.py

There's probably a quicker more elegant solution out there, but this works well enough.
Mine is decent: https://github.com/floydnunez/Advent_of_Code_2019/blob/master/day_04/day_04b.py
 
Last edited:

Randdalf

Member
Oct 28, 2017
1,690
Did day 4. No problems today.



programming is a constant exercise on finding how dumb we are

it's fine



Mine is decent:

Yeah that's the way my friend did it. I reckon it might actually be slightly slower.

Nitpick: you could directly return "2 in repeat" rather than using an if statement.
 
OP
OP
ElFly

ElFly

Member
Oct 27, 2017
981
Yeah that's the way my friend did it. I reckon it might actually be slightly slower.

Nitpick: you could directly return "2 in repeat" rather than using an if statement.
Mine does take a split second to complete, yeah

I like returning explicit trues and falses rather than conditionals most of the time