diff --git a/2015/README.md b/2015/README.md index 885cdd5..8af7ace 100644 --- a/2015/README.md +++ b/2015/README.md @@ -9,8 +9,8 @@ - [x] Day 06 - [x] Day 07 - [x] Day 08 -- [ ] Day 09 -- [ ] Day 10 +- [x] Day 09 +- [x] Day 10 - [ ] Day 11 - [ ] Day 12 - [ ] Day 13 diff --git a/2015/day-10/.ruby-version b/2015/day-10/.ruby-version new file mode 100644 index 0000000..4a36342 --- /dev/null +++ b/2015/day-10/.ruby-version @@ -0,0 +1 @@ +3.0.0 diff --git a/2015/day-10/inputs/puzzle.txt b/2015/day-10/inputs/puzzle.txt new file mode 100644 index 0000000..067b0ee --- /dev/null +++ b/2015/day-10/inputs/puzzle.txt @@ -0,0 +1 @@ +1321131112 diff --git a/2015/day-10/inputs/test.txt b/2015/day-10/inputs/test.txt new file mode 100644 index 0000000..7270077 --- /dev/null +++ b/2015/day-10/inputs/test.txt @@ -0,0 +1 @@ +111221 diff --git a/2015/day-10/main.rb b/2015/day-10/main.rb new file mode 100755 index 0000000..2ea1b2a --- /dev/null +++ b/2015/day-10/main.rb @@ -0,0 +1,42 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require "pathname" +require "stringio" + +def look_and_say(input) + counter = 1 + + output = input.each_char.map(&:to_i).chain([nil]).each_cons(2).reduce(StringIO.new) do |acc, (val1, val2)| + if val1 == val2 + counter += 1 + else + acc.write counter + acc.write val1 + counter = 1 + end + + acc + end + + output.rewind + output +end + +def solve_part1(content) + puts 40.times.reduce(StringIO.new(content)) { |acc| look_and_say(acc) }.size +end + +def solve_part2(content) + puts 50.times.reduce(StringIO.new(content)) { |acc, _| look_and_say(acc) }.size +end + +def main(files) + files.each do |file| + content = file.read.chomp + solve_part1(content) + solve_part2(content) + end +end + +main(ARGV.map { |x| Pathname(x) })