1
0
Fork 0
This commit is contained in:
Andrew Tomaka 2024-12-06 15:16:07 -05:00
parent 9a3c7d078c
commit 8b74765fe1
Signed by: atomaka
GPG key ID: 61209BF70A5B18BE
3 changed files with 1459 additions and 0 deletions

47
5-1.rb Executable file
View file

@ -0,0 +1,47 @@
#!/usr/bin/env ruby
problem = 5
input = File.readlines("#{problem}.input").map(&:strip)
#input = DATA.read.split("\n")
split = input.index("")
rules = input[0...split].map { _1.split("|").map(&:to_i) }
page_sets = input[split+1..].map { _1.split(",").map(&:to_i) }
result = page_sets.select do |page_set|
rules
.select { |a| (page_set & a).sort == a.sort }
.all? { |(a, b)| page_set.index(a) < page_set.index(b) }
end.map { _1[_1.size / 2] }.sum
p result
__END__
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

61
5-2.rb Executable file
View file

@ -0,0 +1,61 @@
#!/usr/bin/env ruby
problem = 5
input = File.readlines("#{problem}.input").map(&:strip)
#input = DATA.read.split("\n")
split = input.index("")
rules = input[0...split].map { _1.split("|").map(&:to_i) }
page_sets = input[split+1..].map { _1.split(",").map(&:to_i) }
result = page_sets.select do |page_set|
rules
.select { |a| (page_set & a).sort == a.sort }
.any? { |(a, b)| page_set.index(a) > page_set.index(b) }
end.map do |page_set|
matched_rules = rules
.select { |a| (page_set & a).sort == a.sort }
.reduce(Hash.new { |h, k| h[k] = Array.new }) { |a, (k, v)| a[k] << v; a }
page_set.sort do |a, b|
if matched_rules[a].include?(b)
-1
elsif matched_rules[b].include?(a)
1
else
0
end
end
end.map { _1[_1.size / 2] }.sum
p result
__END__
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

1351
5.input Normal file

File diff suppressed because it is too large Load diff