-
Notifications
You must be signed in to change notification settings - Fork 0
/
day1.rb
76 lines (64 loc) · 2.05 KB
/
day1.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
def get_line_cal_val_part1(line)
first_digit = nil
last_digit = nil
line.each_char do |char|
if char.match?(/[[:digit:]]/)
if first_digit.nil?
first_digit = char
end
last_digit = char
end
end
return Integer([first_digit, last_digit].join)
end
def get_line_cal_val_part2(line)
text_to_int = {"one" => "1", "two" => "2", "three" => "3", "four" => "4", "five" => "5", "six" => "6", "seven" => "7", "eight" => "8", "nine" => "9"}
#First find digit candidates
first_digit = nil
first_dight_pos = 1000
last_digit = nil
last_digit_pos = 0
line.each_char.with_index do |char, i|
if char.match?(/[[:digit:]]/)
if first_digit.nil?
first_digit = char
first_dight_pos = i
end
last_digit = char
last_digit_pos = i
end
end
#Random large number (must be larger then longest input line)
first_word_pos = 1000
first_word_val = nil
last_word_pos = 0
last_word_val = nil
#Now find word candidates
text_to_int.keys.each do |num_word|
curr_first_word_pos = line.index(num_word)
curr_last_word_pos = line.rindex(num_word)
unless curr_first_word_pos.nil?
if curr_first_word_pos <= first_word_pos
first_word_pos = curr_first_word_pos
first_word_val = text_to_int[num_word]
end
if curr_last_word_pos >= last_word_pos
last_word_pos = curr_last_word_pos
last_word_val = text_to_int[num_word]
end
end
end
#Determine wheather word or digit is first/last and overwrite if word wins
if first_word_pos < first_dight_pos
first_digit = first_word_val
end
if last_word_pos > last_digit_pos
last_digit = last_word_val
end
return Integer([first_digit, last_digit].join)
end
cal_sum = 0
File.readlines('input.txt').each do |line|
cal_sum += get_line_cal_val_part2(line)
end
puts cal_sum