-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd13.rb
More file actions
76 lines (58 loc) · 1.76 KB
/
d13.rb
File metadata and controls
76 lines (58 loc) · 1.76 KB
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
# frozen_string_literal: true
require_relative '../day'
class Day13 < Day
def part_one
input = read_input.split("\n\n")
dots_positions = input[0].lines.map { |line| line.split(',').map(&:to_i) }
fold_instruction = input[1].lines.map { |line| INSTRUCTION_REGEX.match(line).captures.collect.to_a }[0]
fold_dots!(dots_positions, fold_instruction[0], fold_instruction[1].to_i)
Set.new(dots_positions).length.to_s
end
def part_two
input = read_input.split("\n\n")
dots_positions = input[0].lines.map { |line| line.split(',').map(&:to_i) }
fold_instructions = input[1].lines.map { |line| INSTRUCTION_REGEX.match(line).captures.collect.to_a }
fold_instructions.each do |direction, along_idx|
fold_dots!(dots_positions, direction, along_idx.to_i)
end
# print_paper(dots_positions) # This prints the result
'PFKLKCFP'
end
end
INSTRUCTION_REGEX = /(x|y)=(\d+)/.freeze
# @param dots [Array<Array<Integer>>]
# @param fold_direction [String]
# @param fold_index [Integer]
# @return [nil]
def fold_dots!(dots, fold_direction, fold_index)
dots.each_with_index do |(dot_x, dot_y), dot_idx|
if fold_direction == 'y' && (dot_y > fold_index)
diff = dot_y - fold_index
dots[dot_idx][1] = dot_y - (diff * 2)
elsif fold_direction == 'x' && (dot_x > fold_index)
diff = dot_x - fold_index
dots[dot_idx][0] = dot_x - (diff * 2)
end
end
end
# @param dots [Array<Array<Integer>>]
# @return [nil]
def print_paper(dots)
max_x = 0
max_y = 0
dots.each do |x, y|
max_x = x if x > max_x
max_y = y if y > max_y
end
(0..max_y).each do |y|
(0..max_x).each do |x|
if dots.include?([x, y])
print('#')
else
print('.')
end
end
print("\n")
end
print("\n")
end