-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.rb
72 lines (59 loc) · 1.87 KB
/
day13.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
# frozen_string_literal: true
# https://adventofcode.com/2021/day/13
class Paper
attr_accessor :dots, :folds, :max_x, :max_y
def initialize(input_data_filename)
lines = File.readlines(input_data_filename).map(&:chomp)
initialize_dots(lines)
initialize_folds(lines)
initialize_maxes
end
def initialize_dots(lines)
@dots = lines.select { |l| l =~ /^\d/ }.map { |l| l.split('../inputs/2021/,').map(&:to_i) }
end
def initialize_folds(lines)
@folds = lines.select { |l| l.start_with?('../inputs/2021/f') }.map { |l| l.sub('../inputs/2021/fold along ', '').split('../inputs/2021/=') }.map do |f|
f[0] == 'x' ? [f[1].to_i, 0] : [0, f[1].to_i]
end
end
def initialize_maxes
@max_x = @dots.map { |d| d[0] }.max
@max_y = @dots.map { |d| d[1] }.max
end
def print_grid
rows = []
(0..@max_y).each do
rows << (0..@max_x).map { |_| '.' }
end
@dots.each do |d|
rows[d[1]][d[0]] = '#'
end
rows.each do |row|
puts row.join
end
end
def fold_along(fold)
return horizontal_fold(fold[1]) if fold[0].zero?
return vertical_fold(fold[0]) if fold[1].zero?
end
def horizontal_fold(at_y)
@dots = @dots.map { |dot| [dot[0], dot[1] < at_y ? dot[1] : at_y - (dot[1] - at_y)] }.uniq
initialize_maxes
end
def vertical_fold(at_x)
@dots = @dots.map { |dot| [dot[0] < at_x ? dot[0] : at_x - (dot[0] - at_x), dot[1]] }.uniq
initialize_maxes
end
end
def part_one(filename)
data = Paper.new(filename)
data.fold_along(data.folds[0])
puts "After the first fold there are #{data.dots.length} dots (from #{filename})"
end
['day13-input-test.txt', 'day13-input-01.txt'].each { |f| part_one(f) }
def part_two(filename)
data = Paper.new(filename)
data.folds.each { |fold| data.fold_along(fold) }
data.print_grid
end
['day13-input-test.txt', 'day13-input-01.txt'].each { |f| part_two(f) }