From 83f4f0b6c6a400a3918a9da75ef43c451742eba7 Mon Sep 17 00:00:00 2001 From: Patrick Michl Date: Thu, 10 Dec 2020 09:22:27 +0100 Subject: [PATCH] Solution for Day 10 --- day_10/first.rb | 17 ++++++++ day_10/input | 104 +++++++++++++++++++++++++++++++++++++++++++++++ day_10/second.rb | 12 ++++++ 3 files changed, 133 insertions(+) create mode 100644 day_10/first.rb create mode 100644 day_10/input create mode 100644 day_10/second.rb diff --git a/day_10/first.rb b/day_10/first.rb new file mode 100644 index 0000000..aea5b0f --- /dev/null +++ b/day_10/first.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +input = File.readlines('./input').map(&:to_i).sort + +$d = { 1 => 0, 2 => 0, 3 => 0 } +$j = 0 +input << input.max + 3 + +input.each do |adapter| + diff = adapter - $j + if diff.between? 1, 3 + $d[diff] += 1 + $j = adapter + end +end + +puts $d[1] * $d[3] diff --git a/day_10/input b/day_10/input new file mode 100644 index 0000000..aeeece4 --- /dev/null +++ b/day_10/input @@ -0,0 +1,104 @@ +165 +78 +151 +15 +138 +97 +152 +64 +4 +111 +7 +90 +91 +156 +73 +113 +93 +135 +100 +70 +119 +54 +80 +170 +139 +33 +123 +92 +86 +57 +39 +173 +22 +106 +166 +142 +53 +96 +158 +63 +51 +81 +46 +36 +126 +59 +98 +2 +16 +141 +120 +35 +140 +99 +121 +122 +58 +1 +60 +47 +10 +87 +103 +42 +132 +17 +75 +12 +29 +112 +3 +145 +131 +18 +153 +74 +161 +174 +68 +34 +21 +24 +85 +164 +52 +69 +65 +45 +109 +148 +11 +23 +129 +84 +167 +27 +28 +116 +110 +79 +48 +32 +157 +130 diff --git a/day_10/second.rb b/day_10/second.rb new file mode 100644 index 0000000..ca09670 --- /dev/null +++ b/day_10/second.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +input = File.readlines('./input').map(&:to_i).sort + +input << input.max + 3 + +$c = { 0 => 1 } + +input.each do |e| + $c[e] = $c.values_at(e - 1, e - 2, e - 3).compact.sum +end +puts $c[input.last]