Compare commits

...

3 Commits

Author SHA1 Message Date
Jeff Yates dc84f2c151 solved part 2 2020-12-09 12:20:47 -05:00
Jeff Yates 1cd2b62ba0 solved part 1 2020-12-09 11:29:07 -05:00
Jeff Yates 21e651af44 added input files 2020-12-09 10:55:42 -05:00
4 changed files with 1079 additions and 0 deletions

1000
day09/input Normal file

File diff suppressed because it is too large Load Diff

20
day09/input.sample Normal file
View File

@ -0,0 +1,20 @@
35
20
15
25
47
40
62
55
65
95
102
117
150
182
127
219
299
277
309
576

18
day09/solution1.rb Executable file
View File

@ -0,0 +1,18 @@
#!/bin/env ruby
def valid_nums (nums)
nums.map { |num| nums
.map { |sub| sub + num if sub != num }}.flatten.uniq.compact
end
numbers = File.readlines('./input').to_ary.map(&:strip!).map(&:to_i)
preamble = 25
numbers.each.with_index do |number, i|
next if i < preamble
if !valid_nums(numbers[(i-preamble)..(i-1)]).include?(number)
puts number
break
end
end

41
day09/solution2.rb Executable file
View File

@ -0,0 +1,41 @@
#!/bin/env ruby
def valid_nums (nums)
nums.map { |num| nums
.map { |sub| sub + num if sub != num }}.flatten.uniq.compact
end
def find_invalid (numbers, preamble)
numbers.each.with_index do |number, i|
next if i < preamble
if !valid_nums(numbers[(i-preamble)..(i-1)]).include?(number)
return i, number
break
end
end
end
numbers = File.readlines('./input').to_ary.map(&:strip!).map(&:to_i)
preamble = 25
index, wrong_number = find_invalid(numbers, preamble)
pp index, wrong_number
numbers.slice!(index..)
numbers.delete_if { |number| number > wrong_number }
max = numbers.length
step = 1
solution = nil
while step < max do
range_sums = numbers.map.with_index { |num, i| {"#{i..i+step}" => numbers[i..i+step].sum}}
range_sums.each do |set|
if set.values.include?(wrong_number)
solution = set
break
end
end
step += 1
end
first, last = solution.keys[0].split('..').map(&:to_i)
pp solution
p numbers[first..last].min + numbers[first..last].max