-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_19.rb
84 lines (78 loc) · 1.76 KB
/
day_19.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def step1(example = false)
initial = true
@rules = {}
@parts = []
input(example).each do |line|
initial = false if line == "\n"
if initial
name, rs = line.gsub("}","").strip.split("{")
rs = rs.split(",")
@rules[name] = rs
# p [name, rules, accepted]
else
next if line == "\n"
split = line.gsub("{","").gsub("}","").strip.split(",")
o = {}
split.each do |l|
k,v = l.split("=")
o[k] = v.to_i
end
@parts << o
end
end
# p @rules
# p @parts
@valid_parts = []
find_valid_parts
p @valid_parts
sum = 0
@valid_parts.each do |part|
sum += part.values.sum
end
p sum
end
def find_valid_parts
@parts.each do |part|
current_rule = "in"
go = true
i = 0
until go == false
rule = @rules[current_rule][i]
if rule[0] == "A"
@valid_parts << part
go = false
elsif rule[0] == "R"
go = false
elsif rule[1] == "<" || rule[1] == ">"
test, right = rule.split(/[>,<]/)
amount, new_instruction = right.split(":")
sign = rule.include?('<') ? "<" : ">"
# p [test, sign, amount, new_instruction]
# p part[test].send(sign, amount.to_i)
if part[test].send(sign, amount.to_i)
if new_instruction == "A"
@valid_parts << part
go = false
elsif new_instruction == "R"
go = false
else
# p new_instruction
current_rule = new_instruction
i = 0
end
i += 1
else
i += 1
end
else
current_rule = rule
i = 0
end
end
end
end
def step2(example = false)
end
def input(example)
example == 'example' ? @input_example : @input
end