-
Notifications
You must be signed in to change notification settings - Fork 0
/
13a_solution.rb
73 lines (60 loc) · 1.43 KB
/
13a_solution.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
require 'set'
input = File.read("./13input.txt")
dot_input, fold_input = input.split("\n\n")
dots = Set.new
max_x, max_y = [0, 0]
dot_input.split("\n").each do |line|
x, y = line.split(",").map(&:to_i)
if x > max_x
max_x = x
end
if y > max_y
max_y = y
end
dots.add [x, y]
end
folds = []
fold_input.split("\n").each do |line|
match = /fold along (.)=(\d+)/.match line
folds << [match[1].to_sym, match[2].to_i]
end
# for debugging
def draw(dots, max_x, max_y)
puts "\n--------------------\n"
(0..max_y).each do |y|
line = (0..max_x).map do |x|
dots.include?([x, y]) ? '#' : '.'
end
puts line.join("")
end
end
# returns new [max_x, max_y]
def do_fold(fold, dots, max_x, max_y)
new_max_x = fold[0] == :x ? fold[1] - 1 : max_x
new_max_y = fold[0] == :y ? fold[1] - 1 : max_y
if fold[0] == :x
((fold[1]+1)..max_x).each do |x|
(0..max_y).each do |y|
if dots.include? [x, y]
new_x = 2*fold[1] - x
dots.add [new_x, y]
end
end
end
else
(0..max_x).each do |x|
((fold[1]+1)..max_y).each do |y|
if dots.include? [x, y]
new_y = 2*fold[1] - y
dots.add [x, new_y]
end
end
end
end
[new_max_x, new_max_y]
end
def count_dots(dots, max_x, max_y)
dots.count { |dot| dot[0] <= max_x && dot[1] <= max_y }
end
max_x, max_y = do_fold(folds[0], dots, max_x, max_y)
puts count_dots(dots, max_x, max_y)