solved day5 and rubocopd

main
s00ner 2025-01-01 13:23:18 -05:00
parent 8b30be4951
commit de0f220321
4 changed files with 1458 additions and 0 deletions

1362
day05/input Normal file

File diff suppressed because it is too large Load Diff

28
day05/input.sample Normal file
View File

@ -0,0 +1,28 @@
47|53
97|13
97|61
97|47
75|29
61|13
75|53
29|13
97|29
53|29
61|53
97|53
61|29
47|13
75|47
97|75
47|61
75|61
47|29
75|13
53|13
75,47,61,53,29
97,61,53,29,13
75,29,13
75,97,47,61,53
61,13,29
97,13,75,29,47

24
day05/solution1.rb Executable file
View File

@ -0,0 +1,24 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'pry'
# ifile = 'input.sample'
ifile = 'input'
input = File.readlines(ifile).map(&:strip).reject(&:empty?)
updates, rules = input.partition { _1.match?(',') }
rules.map! { _1.split('|').map(&:to_i) }
updates.map! { _1.split(',').map(&:to_i) }
updates.select! do |update|
update.combination(2).map do |pair|
rule = rules.select { _1.include?(pair[0]) and _1.include?(pair[1]) }.flatten
next if rule.empty?
rule == pair
end.all?
end
solution = updates.map { |update| update[update.size / 2] }.sum
# binding.pry
puts solution

44
day05/solution2.rb Executable file
View File

@ -0,0 +1,44 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'pry'
def bubble_sort(array, rules)
return array if array.size <= 1
swap = true
while swap
swap = false
(array.length - 1).times do |x|
rule = rules.select { _1.include?(array[x]) and _1.include?(array[x + 1]) }.flatten
if rule != [array[x], array[x + 1]]
array[x], array[x + 1] = array[x + 1], array[x]
swap = true
end
end
end
array
end
# ifile = 'input.sample'
ifile = 'input'
input = File.readlines(ifile).map(&:strip).reject(&:empty?)
updates, rules = input.partition { _1.match?(',') }
rules.map! { _1.split('|').map(&:to_i) }
updates.map! { _1.split(',').map(&:to_i) }
updates.reject! do |update|
update.combination(2).map do |pair|
rule = rules.select { _1.include?(pair[0]) and _1.include?(pair[1]) }.flatten
next if rule.empty?
rule == pair
end.all?
end
updates.map! { |update| bubble_sort(update, rules) }
solution = updates.map { |update| update[update.size / 2] }.sum
# binding.pry
puts solution