-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.rb
102 lines (82 loc) · 1.96 KB
/
day18.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
def end_paranthesis_index(str)
i = 0
nesting = 0
while i < str.length
c = str[i]
if c == "("
nesting += 1
elsif c == ")"
nesting -= 1
if nesting == 0
return i
end
end
i += 1
end
throw "No ending paranthesis for " + str
end
def parse_expr(expr, eval_func)
# puts expr.join("")
tokens = []
i = 0
tok = ""
while i <= expr.length-1
c = expr[i]
if c == "+" || c == "*"
tokens.push(tok.to_i)
tokens.push(c)
tok = ""
elsif c == "("
epi = end_paranthesis_index(expr[i..expr.length])
tok = eval_func.call(parse_expr(expr[i+1..i+epi-1], eval_func))
i = i+epi
else
tok += c
end
i += 1
end
tokens.push(tok.to_i)
return tokens
end
def eval_expr1(tokens)
tokens = Array.new(tokens)
t1 = tokens.shift()
while tokens.length > 0
op = tokens.shift()
t2 = tokens.shift()
if op == "+"
t1 = t1 + t2
elsif op == "*"
t1 = t1 * t2
end
end
return t1
end
def eval_expr2(tokens)
if (tokens.length == 3)
return eval_expr1(tokens)
end
tokens = Array.new(tokens)
t1 = tokens.shift()
op = tokens.shift()
t2 = tokens.shift()
op2 = tokens.shift()
t3 = tokens.shift()
if op == "+"
return eval_expr2([eval_expr2([t1, op, t2])] + [op2, t3] + tokens)
elsif op == "*"
return eval_expr2([t1, op] + [eval_expr2([t2, op2, t3] + tokens)])
end
end
file = File.open("input/day18.txt", "r")
lines = file.readlines
sum1 = 0
sum2 = 0
for line in lines
line = line.strip.chars.filter{|x| x != " "}.join("")
tokens1 = parse_expr(line, method(:eval_expr1))
sum1 += eval_expr1(tokens1)
tokens2 = parse_expr(line, method(:eval_expr2))
sum2 += eval_expr2(tokens2)
end
puts sum1, sum2