Skip to content

Instantly share code, notes, and snippets.

@andrewzah
Created December 6, 2023 00:53
Show Gist options
  • Select an option

  • Save andrewzah/c9a6b082920586ee55608510a3b7f3f2 to your computer and use it in GitHub Desktop.

Select an option

Save andrewzah/c9a6b082920586ee55608510a3b7f3f2 to your computer and use it in GitHub Desktop.
advent of code 2023 - day 5 - cpu go brrr
lines = File.read("input.txt").split("\n\n")
seeds = lines[0].split(": ").last.split(" ").map(&:to_i)
mappings = lines[1..].map{|line|
elems = line.split("\n")
name = elems[0].split(" ").first
entries = []
elems = elems[1..].each do |str|
dest, src, range = str.split(" ").map(&:to_i)
if src > dest
op = :negative
offset = src - dest
else
op = :positive
offset = dest - src
end
entries << {
:src_lower => src,
:src_upper => src+range-1,
:dest_lower => dest,
:dest_upper => dest+range-1,
:offset => offset,
:op => op,
}
end
[name, entries]
}.to_h
def get_mapping(num, arr)
arr.each do |hash|
lower = hash[:src_lower]
upper = hash[:src_upper]
if (lower..upper).include?(num)
newnum = hash[:op] == :positive ? num + hash[:offset] : num - hash[:offset]
return newnum
end
end
num
end
def get_mapping_reverse(num, arr)
arr.each do |hash|
lower = hash[:dest_lower]
upper = hash[:dest_upper]
if (lower..upper).include?(num)
newnum = hash[:op] == :positive ? num + hash[:offset] : num - hash[:offset]
return newnum
end
end
num
end
seeds.map! do |seed|
soil = get_mapping(seed, mappings["seed-to-soil"])
fertilizer = get_mapping(soil, mappings["soil-to-fertilizer"])
water = get_mapping(fertilizer, mappings["fertilizer-to-water"])
light = get_mapping(water, mappings["water-to-light"])
temperature = get_mapping(light, mappings["light-to-temperature"])
humidity = get_mapping(temperature, mappings["temperature-to-humidity"])
location = get_mapping(humidity, mappings["humidity-to-location"])
{ :soil => soil, :fertilizer => fertilizer, :water => water,
:light => light, :temperature => temperature,
:humidity => humidity, :location => location}
end
pp seeds.sort_by{|h| h[:location]}.first
lines = File.read("input.txt").split("\n\n")
seeds = lines[0].split(": ").last.split(" ").map(&:to_i)
seed_ranges = seeds.each_slice(2).map do |start, range|
{
:lower_bound => start,
:upper_bound => start+range-1,
}
end
mappings = lines[1..].map{|line|
elems = line.split("\n")
name = elems[0].split(" ").first
entries = []
elems = elems[1..].each do |str|
dest, src, range = str.split(" ").map(&:to_i)
if dest > src
op = :negative
offset = dest - src
else
op = :positive
offset = src - dest
end
entries << {
:src_lower => src,
:src_upper => src+range-1,
:dest_lower => dest,
:dest_upper => dest+range-1,
:offset => offset,
:op => op,
}
end
[name, entries]
}.to_h
def get_mapping(num, arr)
arr.each do |hash|
lower = hash[:src_lower]
upper = hash[:src_upper]
if (lower..upper).include?(num)
newnum = hash[:op] == :positive ? num + hash[:offset] : num - hash[:offset]
return newnum
end
end
num
end
def get_mapping_reverse(num, arr)
arr.each do |hash|
lower = hash[:dest_lower]
upper = hash[:dest_upper]
if (lower..upper).include?(num)
newnum = hash[:op] == :positive ? num + hash[:offset] : num - hash[:offset]
return newnum
end
end
num
end
#[0..4294967295].each do |location|
(0..4294967295).each do |location|
humidity = get_mapping_reverse(location, mappings["humidity-to-location"])
temperature = get_mapping_reverse(humidity, mappings["temperature-to-humidity"])
light = get_mapping_reverse(temperature, mappings["light-to-temperature"])
water = get_mapping_reverse(light, mappings["water-to-light"])
fertilizer = get_mapping_reverse(water, mappings["fertilizer-to-water"])
soil = get_mapping_reverse(fertilizer, mappings["soil-to-fertilizer"])
seed = get_mapping_reverse(soil, mappings["seed-to-soil"])
seed_ranges.each do |hash|
lower = hash[:lower_bound]
upper = hash[:upper_bound]
if (lower..upper).include?(seed)
puts location
exit
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment