From afe06c8955b1dd30c410ac060b2d961deab798b8 Mon Sep 17 00:00:00 2001 From: trwnh Date: Wed, 2 Dec 2020 12:24:27 -0600 Subject: [PATCH] ruby solution --- 02/02.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 02/02.rb diff --git a/02/02.rb b/02/02.rb new file mode 100644 index 0000000..0ee62a8 --- /dev/null +++ b/02/02.rb @@ -0,0 +1,18 @@ +input = File.readlines("input.txt") + +part1 = 0 +part2 = 0 +for line in input do + policy, password = line.split(': ') + counts, character = policy.split(' ') + n1, n2 = counts.split('-').map(&:to_i) + if password.count(character) >= n1 && password.count(character) <= n2 + part1 += 1 + end + if (password[n1-1] == character) ^ (password[n2-1] == character) + part2 += 1 + end +end + +puts "Part 1: #{part1}" +puts "Part 2: #{part2}" \ No newline at end of file