Last active
December 13, 2024 16:18
-
-
Save carlwiedemann/2470b21fd518de31a617fe2713595973 to your computer and use it in GitHub Desktop.
Advent of Code 2024 day013.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative "main" | |
module Day013 | |
INPUT = File.read("INPUT.txt") | |
answer1 = 0 | |
answer2 = 0 | |
INPUT.split("\n\n").each do |group| | |
ax, ay, bx, by, px1, py1 = group.split("\n").map { |line| line.split(",").map { |s| s[/\d+/].to_i } }.flatten | |
a1 = (px1 * by - py1 * bx).to_f / (by * ax - ay * bx) | |
b1 = (py1 - a1 * ay).to_f / by | |
if a1.to_i == a1 && b1.to_i == b1 && a1 <= 100 && b1 <= 100 | |
answer1 += (3 * a1 + b1).to_i | |
end | |
px2 = px1 + 10_000_000_000_000 | |
py2 = py1 + 10_000_000_000_000 | |
a2 = (px2 * by - py2 * bx).to_f / (by * ax - ay * bx) | |
b2 = (py2 - a2 * ay).to_f / by | |
if a2.to_i == a2 && b2.to_i == b2 | |
answer2 += (3 * a2 + b2).to_i | |
end | |
end | |
pp answer1 | |
pp answer2 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment