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
def reflection_points(pattern)
(0.5...pattern.length-0.5).step(1).filter_map {|fold|
a = pattern[0..fold.floor]
b = pattern[fold.ceil..]
if a.reverse.zip(b).reject { _1.any?(&:nil?) }.all? { _1 == _2 }
fold
else
nil
end
}.map(&:ceil)
end
Pattern = Data.define(:pattern) do
def reflection_axes
ary = to_a
reflection_points(ary).map { [:y, _1] } + reflection_points(ary.transpose).map { [:x, _1] }
end
def smudges
return enum_for(__method__) unless block_given?
max_y, max_x = extents
(0..max_y).each do |y|
(0..max_x).each do |x|
smudged = case pattern[[y,x]]
when ?# then ?.
when ?. then ?#
else fail
end
yield Pattern.new(pattern.merge({[y,x] => smudged}))
end
end
end
def to_a
max_y, max_x = extents
(0..max_y).map {|y|
(0..max_x).map {|x|
pattern.fetch([y,x])
}
}
end
def to_s
to_a.map(&:join).join("\n")
end
def extents
max_y = pattern.keys.map(&:first).max
max_x = pattern.keys.map(&:last).max
[max_y, max_x]
end
end
patterns = ARGF.read.strip.split("\n\n")
.map {|pattern|
pattern.split("\n").map(&:chars)
.each.with_index.inject({}) {|h, (row, y)|
h.merge(row.each.with_index.to_h {|elem,x| [[y,x], elem]})
}
}.map { Pattern.new(_1) }
# part one
x, y = patterns.map { _1.reflection_axes.fetch(0) }
.partition {|axis,_| axis == :x }
.map { _1.map(&:last) } # keep index (discard axis)
p x.sum + y.sum { _1 * 100 }
# part two
x, y = patterns.map {|pattern|
original_reflection_axis = pattern.reflection_axes.fetch(0)
pattern.smudges.lazy
.flat_map { _1.reflection_axes}
.reject { _1 == original_reflection_axis }
.first
}.partition {|axis,_| axis == :x }
.map { _1.map(&:last) } # keep index (discard axis)
p x.sum + y.sum { _1 * 100 }