Solving day 3 of Advent of Code 2021 in JavaScript

preview_player
Показать описание
Advent of Code is a website that releases a new programming puzzle every day between the 1st and the 25th of December.

0:00 Part 1
10:20 Part 2

Have fun!

Рекомендации по теме
Комментарии
Автор

Thanks for the video! It's nice to see a better answer. I got there but I kind of cheated and ran my for loop manually until I saw one more value for the CO2 Rating. I did it all in one for loop for each bit, and I couldn't exit the loop without stopping short since the oxygen rating took more bits to filter to one value.

danmiller
Автор

Just dropping by again to say thanks for the super helpful video. Being new to coding, a lot of these things you're doing are way over my head but I feel like I'm learning with each day by watching your videos (after failing trying myself haha).

xShosTaK
Автор

I'm glad I found this video; I got stuck on part two and couldn'g figure out what was wrong... that last part was tricky, thanks! Also, great explanation!

felipemelendez
Автор

After writing some spaghetti code in part 2, I started over and realized that the recursive function could do two things: generate a gamma or epsilon rate for a set of candidate numbers (as we did in part 1), then filter the list of candidates. If candidates.length !=1, loop again, now generating a -new- gamma or epsilon based on the filtered set of numbers, then filtering again ...

bikefixe
Автор

A slightly more short ver of part 1 (w/o counting both 0s and 1s)

const len = lines.length,
{ gammaRate, epsilonRate } = lines.reduce((res, line) => {
[...line].forEach((n, i) => res[i] += n == 1);

return res;
}, Array(lines[0].length).fill(0)).reduce((res, oneBitCounter) => {
oneBitCounter > len - oneBitCounter
? (res.gammaRate += 1, res.epsilonRate += 0)
: (res.gammaRate += 0, res.epsilonRate += 1);

return res;
}, { gammaRate: "", epsilonRate: "" });

lordkim
Автор

Is there a more efficient way(time complexity) for the first part than doing a nested for loop? Thanks for the video

fdafdsaful