Damn, so I learned today that Julia has both the / operator AND the ÷ operator! The / operator will produce a float, regardless of the types of the inputs (so 2/5 returns 2.5) but ÷ does integer division! So 2÷5 returns 2.
Anyways, Part 1 was brute-forceable, on my machine it ran in <0.5 seconds.
But when I attempted to brute-force Part 2...well, it ran in 2 minutes 45 seconds. It works, I guess? I can't help thinking there's a smarter solution to this.
Anyways, my part 2 code:
max_power = nothing
max_x = 0
max_y = 0
max_block_size = 0
for block_size in 1:300
upper_bound = 300 - block_size + 1
for x in 1:upper_bound
for y in 1:upper_bound
p = power_level_block(x,y, grid_num, block_size)
if max_power == nothing || p > max_power
max_power = p
max_x = x
max_y = y
max_block_size = block_size
return (max_x, max_y, max_block_size)
function power_level_block(topx,lefty, grid_num, block_size)
power = 0
for x in topx:topx+(block_size-1)
for y in lefty:lefty+(block_size-1)
p = power_level(x,y, grid_num)
power += p
function power_level(x,y, grid_num)
rack_id = x + 10
power = rack_id * y
power += grid_num
power *= rack_id
if power >= 100
power = (power ÷ 100) % 10 # WTF: the / does float division but ÷ does integer division?!
power = 0
power -= 5
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.