Skip to content

Instantly share code, notes, and snippets.

@carlwiedemann
Created December 15, 2024 04:54
Show Gist options
  • Save carlwiedemann/07c266f463b44155bacb7cf16563dabe to your computer and use it in GitHub Desktop.
Save carlwiedemann/07c266f463b44155bacb7cf16563dabe to your computer and use it in GitHub Desktop.
Advent of Code 2024 day014.rb
require_relative "main"
module Day014
INPUT = File.read("INPUT.txt")
WIDTH = 101
HEIGHT = 103
rs = INPUT.split("\n").map do |line|
line.split(" ").map do |chunk|
V[*chunk.split("=").last.split(",").map(&:to_i)]
end
end
mean_density = 1
j = 0
loop do
j += 1
grid = Grid.new(WIDTH, HEIGHT, 0)
rs.each_with_index do |(pos, v), i|
pos += v
pos.x %= WIDTH
pos.y %= HEIGHT
rs[i][0] = pos
grid.set_value(pos, 1)
end
density = rs.sum { |(pos, _v)| grid.get_values(grid.nine(pos)).sum }
mean_density = (j == 1) ? density : ((j - 1) * mean_density + density) / j
break if density > 3 * mean_density
if j == 100
##########
# Part 1 #
##########
x_left, x_right = 0...WIDTH / 2, WIDTH / 2 + 1..WIDTH - 1
y_top, y_bottom = 0...HEIGHT / 2, HEIGHT / 2 + 1..HEIGHT - 1
qs = [
[x_right, y_top],
[x_left, y_top],
[x_left, y_bottom],
[x_right, y_bottom]
]
answer1 = qs.reduce(1) do |m, q|
m * rs.count { |(pos, _v)| q.first === pos.x && q.last === pos.y }
end
pp answer1
end
end
##########
# Part 2 #
##########
answer2 = j
pp answer2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment