-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.rb
79 lines (59 loc) · 1.33 KB
/
day10.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
# frozen_string_literal: true
require_relative "../task"
DELIMS = {
"(" => ")",
"[" => "]",
"{" => "}",
"<" => ">"
}.freeze
CORRUPTED_ERROR_POINTS = {
")" => 3,
"]" => 57,
"}" => 1197,
">" => 25_137
}.freeze
INCOMPLETE_ERROR_POINTS = {
")" => 1,
"]" => 2,
"}" => 3,
">" => 4
}.freeze
class Day10
include Task
def part_one
errors = []
read_input.lines.each do |line|
process_delimiters(line) { |corrupted_char| errors << corrupted_char }
end
errors.map { |item| CORRUPTED_ERROR_POINTS[item] }.reduce(&:+).to_s
end
def part_two
scores = []
read_input.lines.each do |line|
corrupted = false
delim_stack = process_delimiters(line) { corrupted = true }
next if corrupted
scores << delim_stack.reverse.inject(0) do |total, n|
(total * 5) + INCOMPLETE_ERROR_POINTS[DELIMS[n]]
end
end
scores.sort[(scores.size - 1) / 2].to_s
end
private
def process_delimiters(chunk, &corrupted_action)
delim_stack = []
chunk.chars.each do |char|
if DELIMS.keys.include?(char)
delim_stack << char
elsif DELIMS.values.include?(char)
if char == DELIMS[delim_stack.last]
delim_stack.pop
next
end
corrupted_action.call(char)
break
end
end
delim_stack
end
end