-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13part2.rb
54 lines (53 loc) · 1.67 KB
/
day13part2.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
file = File.open "day13in.txt"
patterns = []
cur_pattern = []
file.each_line do |l|
if l.chomp.empty?
patterns << cur_pattern
cur_pattern = []
else
cur_pattern.push l.chomp
end
end
patterns << cur_pattern
sum = 0
patterns.each_with_index do |p|
columns = p.length
smudge = nil
(1..(columns - 1)).each do |i|
mismatches = []
(1..i).reverse_each do |column|
next if p[i - column].nil? || p[i + column - 1].nil?
if p[i - column] != p[i + column - 1]
mismatch = { bottom: i - column, top: i + column - 1, indexes: [], index: i }
p[i - column].split("").each_with_index do |c, char_index|
mismatch[:indexes].push(char_index) if c != p[mismatch[:top]][char_index]
end
mismatches.push mismatch
end
end
smudge = mismatches[0][:index] if mismatches.length == 1 && mismatches[0][:indexes].length == 1
end
if smudge.nil?
p = p.collect { |it| it.split("") }.transpose.map { |it| it.join("") }
columns = p.length
(1..(columns - 1)).each do |i|
mismatches = []
(1..i).reverse_each do |column|
next if p[i - column].nil? || p[i + column - 1].nil?
if p[i - column] != p[i + column - 1]
mismatch = { bottom: i - column, top: i + column - 1, indexes: [], index: i }
p[i - column].split("").each_with_index do |c, char_index|
mismatch[:indexes].push(char_index) if c != p[mismatch[:top]][char_index]
end
mismatches.push mismatch
end
end
smudge = mismatches[0][:index] if mismatches.length == 1 && mismatches[0][:indexes].length == 1
end
sum += smudge
else
sum += 100 * smudge
end
end
puts sum