-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_manager.rb
61 lines (54 loc) · 1.78 KB
/
update_manager.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# frozen_string_literal: true
module AdventOfCode
module Puzzles2024
module Day05
##
# Class for managing updates based on ordering rules
class UpdateManager
##
# @return [Hash<Integer, Array<Integer>>] The ordering rules
attr_reader :rules
##
# @param ordering_rules [Array<String>] The ordering rules
# @param separator [String] The separator for the rules
def initialize(ordering_rules, separator: "|")
@rules = {}
ordering_rules.each do |rule|
first, second = rule.split(separator).map(&:to_i)
@rules[first] ||= []
@rules[first] << second
end
end
##
# Check if the update is valid
# @param update [Array<Integer>] The update
# @return [Boolean] True if the update is valid, false otherwise
def valid?(update:)
update.each_with_index do |value, index|
break if index == update.length - 1
update[index + 1..].each do |sub_value|
prior_numbers = rules[sub_value] || []
return false if prior_numbers.include?(value)
end
end
true
end
##
# Fix the ordering of the update
# @param update [Array<Integer>] The update
# @return [Array<Integer>] The updated fixed
def fix_ordering(update:)
sorted = []
update.each do |value|
post_values = rules[value] || []
pre_values = rules.keys.select { |key| rules[key].include?(value) }
post_values = sorted & post_values
pre_values = sorted & pre_values
sorted = pre_values + [value] + post_values
end
sorted
end
end
end
end
end