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.

Hopefully this code is less bullyable :marseycrying:

from math import floor
input_file = 'day_10_input'
with open(f'AdventOfCode2022/{input_file}.txt', 'r') as input:
    cmds = input.read().split('\n')
cycle = 0
strength = 1

#Part 1 stuff:
cum_sum = 0
SIGNAL_CHECKS = (20, 60, 100, 140, 180, 220)
def cum_strength(cycle, strength, cum_sum):
    if cycle % 20 == 0 and cycle in SIGNAL_CHECKS:
        return cum_sum + (cycle * strength)
    return cum_sum

#Part 2 stuff:
W_WIDTH = 40
W_HEIGHT = 6
#should just use np, but not gonna setup an env just for this
window = [[0 for _ in range(W_WIDTH)] for _ in range(W_HEIGHT)]

def draw_pixel(cycle, strength, window):
    pixel_pos = (cycle-1) % W_WIDTH
    pixel_row = floor((cycle-1) / W_WIDTH)
    if abs(pixel_pos - strength) < 2:
        window[pixel_row][pixel_pos] = '#'
    else:
        window[pixel_row][pixel_pos] = '.'

for cmd in cmds:
    cycle += 1
    cum_sum = cum_strength(cycle, strength, cum_sum)
    draw_pixel(cycle, strength, window)
    if cmd.split()[0] == 'addx':
        cycle += 1
        cum_sum = cum_strength(cycle, strength, cum_sum)
        draw_pixel(cycle, strength, window)
        strength += int(cmd.split()[1])

#Part 1 Answer:
print(cum_sum)
#Part 2 Answer:
for i in range(W_HEIGHT):
    print(window[i])
Jump in the discussion.

No email address required.

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