m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/09/a.rb
diff options
context:
space:
mode:
Diffstat (limited to '09/a.rb')
-rw-r--r--09/a.rb32
1 files changed, 32 insertions, 0 deletions
diff --git a/09/a.rb b/09/a.rb
new file mode 100644
index 0000000..ea90532
--- /dev/null
+++ b/09/a.rb
@@ -0,0 +1,32 @@
+preamble = 25
+
+def has_sum hash, sum
+ hash.keys.any? do |n|
+ hash[sum - n] > 0
+ end
+end
+
+def update array, hash, n
+ hash[n] += 1
+ hash[array.first] -= 1
+ array.shift
+ array.push n
+end
+
+lines = File.readlines('input.txt').map {|l| l.to_i}
+current25 = lines.slice(0, preamble)
+current25hash = Hash.new 0
+current25.each do |n|
+ current25hash[n] += 1
+end
+
+invalid = 0
+
+lines.slice(preamble, lines.size).each do |n|
+ if invalid == 0 && (!has_sum current25hash, n)
+ invalid = n
+ end
+ update current25, current25hash, n
+end
+
+puts invalid