-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07.rb
43 lines (36 loc) · 1.59 KB
/
day07.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# frozen_string_literal: true
# https://adventofcode.com/2021/day/7
# All the crabs
class Crabs
attr_accessor :horizontals, :fuel_costs
def initialize(input_data_filename, increasing)
@horizontals = File.open(input_data_filename, &:readline).chomp.split('../inputs/2021/,').map(&:to_i)
@fuel_costs = {}
(@[email protected]).each do |target|
@fuel_costs[target] = fuel_to(target, increasing)
end
end
def fuel_to(horizontal, increasing)
return @horizontals.reduce(0) { |sum, h| sum + (horizontal - h).magnitude } unless increasing
@horizontals.reduce(0) { |sum, h| sum + (1..(horizontal - h).magnitude).sum }
end
def min_fuel_cost
cost = [@fuel_costs.min_by { |_, f| f }].first
{ horizontal: cost[0], fuel: cost[1] }
end
end
puts "\nPART ONE"
puts "\nTest dataset:"
crabs = Crabs.new('../inputs/2021/day07-input-test.txt', false)
puts "It would take #{crabs.min_fuel_cost[:fuel]} units of fuel to get to #{crabs.min_fuel_cost[:horizontal]}"
puts "\nFull dataset:"
crabs = Crabs.new('../inputs/2021/day07-input-01.txt', false)
puts "It would take #{crabs.min_fuel_cost[:fuel]} units of fuel to get to #{crabs.min_fuel_cost[:horizontal]}"
puts "\nPART TWO"
puts "\nTest dataset:"
crabs = Crabs.new('../inputs/2021/day07-input-test.txt', true)
puts "It would take #{crabs.min_fuel_cost[:fuel]} units of fuel to get to #{crabs.min_fuel_cost[:horizontal]}"
puts "\nFull dataset:"
crabs = Crabs.new('../inputs/2021/day07-input-01.txt', true)
puts "It would take #{crabs.min_fuel_cost[:fuel]} units of fuel to get to #{crabs.min_fuel_cost[:horizontal]}"
puts ''