m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/15
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2020-12-25 15:32:36 +0100
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2020-12-25 15:32:36 +0100
commitcefcd292ce98af21659c6a04c3dc7a8db9197447 (patch)
tree20ffd57c5c67dd5699dd7fc46aaaefbb080fdafc /15
parent7cadfc7ce07f30f21447f3a1b748af8a59cf0703 (diff)
Add day 15
Diffstat (limited to '15')
-rw-r--r--15/a.rb25
-rw-r--r--15/b.rb25
-rw-r--r--15/input.txt1
3 files changed, 51 insertions, 0 deletions
diff --git a/15/a.rb b/15/a.rb
new file mode 100644
index 0000000..5341765
--- /dev/null
+++ b/15/a.rb
@@ -0,0 +1,25 @@
+input = File.read('input.txt').split ','
+
+@t = 1
+
+last_spoken = {}
+last_number = 0
+input.each_with_index do |n, i|
+ if i > 0
+ last_spoken[last_number] = i
+ end
+ last_number = n.to_i
+end
+
+(input.length+1..2020).each do |turn|
+ if !last_spoken[last_number]
+ last_spoken[last_number] = turn - 1
+ last_number = 0
+ else
+ new_last_number = (turn - 1) - last_spoken[last_number]
+ last_spoken[last_number] = turn - 1
+ last_number = new_last_number
+ end
+end
+
+puts last_number
diff --git a/15/b.rb b/15/b.rb
new file mode 100644
index 0000000..7c7b3f9
--- /dev/null
+++ b/15/b.rb
@@ -0,0 +1,25 @@
+input = File.read('input.txt').split ','
+
+@t = 1
+
+last_spoken = {}
+last_number = 0
+input.each_with_index do |n, i|
+ if i > 0
+ last_spoken[last_number] = i
+ end
+ last_number = n.to_i
+end
+
+(input.length+1..30000000).each do |turn|
+ if !last_spoken[last_number]
+ last_spoken[last_number] = turn - 1
+ last_number = 0
+ else
+ new_last_number = (turn - 1) - last_spoken[last_number]
+ last_spoken[last_number] = turn - 1
+ last_number = new_last_number
+ end
+end
+
+puts last_number
diff --git a/15/input.txt b/15/input.txt
new file mode 100644
index 0000000..bf1ac60
--- /dev/null
+++ b/15/input.txt
@@ -0,0 +1 @@
+0,14,6,20,1,4