m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/09/a.rb
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2020-12-25 15:25:06 +0100
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2020-12-25 15:25:06 +0100
commit15c6d8d79d9ec00a7abb14f90bd65547893c76f2 (patch)
treed710511f35e0da2ade30e35c21a844adb1de8734 /09/a.rb
parent1362f76e60c32a7bf0e7240b01524d16734f8dd4 (diff)
Add day 9
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