-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday10.rb
More file actions
58 lines (48 loc) · 1.35 KB
/
day10.rb
File metadata and controls
58 lines (48 loc) · 1.35 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
class Part1
attr_reader :lines
attr_accessor :previous_line, :one, :two, :three
def initialize(input)
@lines = [input.each_line.map(&:strip).map(&:to_i) + [0]].flatten.sort
@previous_line = nil
@one = 0
@two = 0
@three = 1
end
def solution
memo_lines = lines.count
while has_lines?
previous_line = lines[0] if previous_line.nil?
case lines[0] - previous_line
when 1 then self.one += 1
when 2 then self.two += 1
when 3 then self.three += 1
end
previous_line = lines[0]
lines.delete(lines[0])
end
{one: one, two: two, three: three, lines: memo_lines, result: one * three}
end
private
def has_lines?
lines.count > 0
end
end
p "DAY10 #Part1: #{Part1.new(File.open('day10.txt')).solution}"
class Part2
attr_reader :lines
attr_accessor :combinations
def initialize(input)
@lines = input.each_line.map(&:strip).map(&:to_i).flatten.sort
@combinations = {0 => 1}
end
def solution
lines.each do |line|
allowed_values = [line, line - 1, line -2, line - 3].filter { |i| i >= 0}
combinations[line] = 0 if combinations[line] == nil
combinations[line] = allowed_values.map { |v| combinations[v] }.compact.sum
end
p combinations
combinations[lines.max]
end
end
p "DAY10 #Part2: #{Part2.new(File.open('day10.txt')).solution}"