-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd07.rb
More file actions
38 lines (28 loc) · 707 Bytes
/
d07.rb
File metadata and controls
38 lines (28 loc) · 707 Bytes
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
# frozen_string_literal: true
require_relative '../day'
class Day07 < Day
def part_one
run { |steps| steps }
end
def part_two
# https://en.wikipedia.org/wiki/Triangular_number
run { |steps| (steps * (steps + 1)) / 2 }
end
private
def run(&fuel_calc)
positions = parse_input(read_input)
min_cost = (1 << 64)
# TODO: There is probably a better way to avoid doing these many loops
(0..positions.max).each do |base|
cost = 0
positions.each do |pos|
cost += fuel_calc.call((pos - base).abs)
end
min_cost = cost if cost < min_cost
end
min_cost.to_s
end
def parse_input(input)
input.split(',').map(&:to_i)
end
end