-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
126c3bd
commit 857e49a
Showing
7 changed files
with
342 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 | ||
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 | ||
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 | ||
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 | ||
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 | ||
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 | ||
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 | ||
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 | ||
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 | ||
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 | ||
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'year_2015/day_04' | ||
|
||
describe Year2015::Day04 do | ||
context 'Part 1' do | ||
subject do | ||
Year2015::Day04.new(File.read('spec/year_2015/day_04_sample_one'), true) | ||
end | ||
|
||
it 'finds winning numbers' do | ||
[ | ||
[17, 48, 83, 86], | ||
[32, 61], | ||
[1, 21], | ||
[84], | ||
[], | ||
[] | ||
].each_with_index do |result, i| | ||
expect(subject.lines[i].winning_numbers).to eq(result) | ||
end | ||
end | ||
|
||
it 'calculates points' do | ||
[8, 2, 2, 1, 0, 0].each_with_index do |result, i| | ||
expect(subject.lines[i].to_i).to eq(result) | ||
end | ||
end | ||
|
||
it 'gives a final result' do | ||
expect(subject.to_i).to eq(13) | ||
end | ||
end | ||
|
||
context 'Part 2' do | ||
subject do | ||
Year2015::Day04.new(File.read('spec/year_2015/day_04_sample_one')) | ||
end | ||
|
||
it 'distributes cards' do | ||
[1, 2, 4, 8, 14, 1].each_with_index do |result, i| | ||
expect(subject.lines[i].quantity).to eq(result) | ||
end | ||
end | ||
|
||
it 'gives a final result' do | ||
expect(subject.to_i).to eq(30) | ||
end | ||
end | ||
|
||
context 'Results' do | ||
subject do | ||
File.read('spec/year_2015/day_04_input') | ||
end | ||
|
||
it 'correctly answers part 1' do | ||
expect(Year2015::Day04.new(subject, true).to_i).to eq(17803) | ||
end | ||
|
||
it 'correctly answers part 2' do | ||
expect(Year2015::Day04.new(subject).to_i).to eq(5554894) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
class Year2015 | ||
class Day04 | ||
attr_reader :version, :lines | ||
|
||
class Line | ||
attr_accessor :quantity | ||
|
||
def initialize(input_line) | ||
@line = input_line | ||
@winners = @line.split(':').last.split('|').first.scan(/\d+/).map(&:to_i) | ||
@inputs = @line.split('|').last.scan(/\d+/).map(&:to_i) | ||
@quantity = 1 | ||
end | ||
|
||
def winning_numbers | ||
(@winners & @inputs).sort | ||
end | ||
|
||
def to_i | ||
return 0 if winning_numbers.empty? | ||
|
||
2**(winning_numbers.length - 1) | ||
end | ||
end | ||
|
||
def initialize(input_file, input_part_one = false) | ||
@version = input_part_one ? 1 : 2 | ||
@lines = input_file.chomp.split("\n").map do |input_line| | ||
Line.new(input_line) | ||
end | ||
distribute_cards if version == 2 | ||
end | ||
|
||
def distribute_cards | ||
@lines.each_with_index do |line, i| | ||
cards_won = line.winning_numbers.length | ||
1.upto(cards_won) do |j| | ||
next unless @lines[i + j] | ||
|
||
@lines[i + j].quantity += line.quantity | ||
end | ||
end | ||
end | ||
|
||
def to_i | ||
return @lines.map(&:quantity).inject(&:+) if version == 2 | ||
|
||
@lines.map(&:to_i).inject(&:+) | ||
end | ||
end | ||
end |