NERD SHIT :marseynerd: ADVENT OF CODE 10 M=E=G=A=T=H=R=E=A=D

LET'S SAVE THOSE ELVES, BROS

Okay it isn't even out yet, I'm sure it will be great, and the elves are in trouble, etc. I'm just beating you all to the bunch so I can tell you to explain your answers, we care about ALGORITHMIC COMPLEXITY now. What's the fastest and most efficient way to do it.

heymoon wtf is a big o

This is O(n)

x = REALLY_BIG_NUMBER
for i in range(x):
    process(i)

This is O(n^2)

x = REALLY_BIG_NUMBER
for i in range(x):
    for j in range(x)
        process(i,j)

There's more to it but that's the quick version. Also there are things like O(log(n)) (based), O(a^n) (cringe), and O(n!) (advanced cringe).

Okay, post ur code too but explain why its cool or no one will read it. I'll pin my favorite answers, other mods also do this

LET'S SAVE THOSE ELVES, BROS

42
Jump in the discussion.

No email address required.

Felt like today was super simple. Biggest issues was with setting cycle = 0 at the start instead of 1

There is most likely a way to do it by just adding the cycles all at once, instead of actually "waiting" for every cycle, but sounds like too much effort. and you would have to loop for every cycle anyway to draw the screen, so that shouldn't change complexity at all.


#include <fstream>
#include <string>
#include <queue>
#include <iostream>

int main()
{
    std::ifstream file("input.txt");

    int cycle{ 1 };
    int register_tube{ 1 };
    int output{ 0 };

    std::priority_queue<int, std::vector<int>, std::greater<int>> cycle_to_find{ {},  { 20, 60, 100, 140, 180, 220 } };

    int ticks_to_wait{ 0 };
    int number_to_add{ 0 };
 
    std::string cmd; 

    while (file >> cmd) {
        if (cmd == "noop") ticks_to_wait = 1;
        else {
            ticks_to_wait = 2;
            file >> number_to_add;
        }
        while (ticks_to_wait > 0) {
            --ticks_to_wait;

            int mod40 = (cycle - 1) % 40;
            if (cycle > 1 && mod40 == 0) std::cout << '\n';
            if (mod40 - 1 <= register_tube && mod40 + 1 >= register_tube) std::cout << "#";
            else std::cout << " ";

            ++cycle;
            if (number_to_add != 0 && ticks_to_wait == 0) {
                register_tube += number_to_add;
                number_to_add = 0;
            }
            if (!cycle_to_find.empty() && cycle == cycle_to_find.top()) {
                output += register_tube * cycle;
                cycle_to_find.pop();
            }

        }
    }

    std::cout << "\nPart One: " << output;
    return -1;
}

Jump in the discussion.

No email address required.

That was a mistake. You're about to find out the hard way why.

Jump in the discussion.

No email address required.

even my shortest solution so far gets bullied by the bot :marseygiveup:

Jump in the discussion.

No email address required.

that's ur fault for using c++

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.