-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09.rb
93 lines (89 loc) · 1.13 KB
/
day09.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
input = File.read('day9_input.txt')
# part 1
test_input =<<EOT
35
20
15
25
47
40
62
55
65
95
102
117
150
182
127
219
299
277
309
576
EOT
data = input.strip.lines.map &:to_i
preamble = 25
index = 0
invalid_num = nil
while true
window = data[index, preamble]
check = data[index + preamble]
pp window, check
if check.nil?
puts "Done"
break
end
if !window.combination(2).find { |n1, n2| n1 != n2 && n1 + n2 == check }
puts "Invalid number #{check}"
invalid_num = check
break
end
index += 1
end
# part 2
test_input =<<EOT
35
20
15
25
47
40
62
55
65
95
102
117
150
182
127
219
299
277
309
576
EOT
data = input.strip.lines.map &:to_i
#invalid_num = 127
preamble = 25
catch :found do
data.size.times do |start|
puts start
data.size.times do |size|
window = data[start, size]
sum = window.sum
if sum < invalid_num
next
elsif sum == invalid_num
puts "Found #{start}, #{size}"
puts "Min: #{ window.min }"
puts "Max: #{ window.max }"
puts "Result: #{ window.min + window.max}"
throw :found
else
break
end
end
end
end