-
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
ee59d52
commit 36bf5b2
Showing
6 changed files
with
126 additions
and
1 deletion.
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
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,9 @@ | ||
Vixen can fly 8 km/s for 8 seconds, but then must rest for 53 seconds. | ||
Blitzen can fly 13 km/s for 4 seconds, but then must rest for 49 seconds. | ||
Rudolph can fly 20 km/s for 7 seconds, but then must rest for 132 seconds. | ||
Cupid can fly 12 km/s for 4 seconds, but then must rest for 43 seconds. | ||
Donner can fly 9 km/s for 5 seconds, but then must rest for 38 seconds. | ||
Dasher can fly 10 km/s for 4 seconds, but then must rest for 37 seconds. | ||
Comet can fly 3 km/s for 37 seconds, but then must rest for 76 seconds. | ||
Prancer can fly 9 km/s for 12 seconds, but then must rest for 97 seconds. | ||
Dancer can fly 37 km/s for 1 seconds, but then must rest for 36 seconds. |
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,2 @@ | ||
Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds. | ||
Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds. |
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,37 @@ | ||
require 'year_2015/day_14' | ||
|
||
describe Year2015::Day14 do | ||
context 'when Part 1' do | ||
subject(:sample_one) do | ||
described_class.new(File.read('spec/year_2015/day_14_sample_one'), true) | ||
end | ||
|
||
it 'gives a final result' do | ||
expect(sample_one.to_i(1000)).to eq(1120) | ||
end | ||
end | ||
|
||
context 'when Part 2' do | ||
subject(:sample_one) do | ||
described_class.new(File.read('spec/year_2015/day_14_sample_one')) | ||
end | ||
|
||
it 'gives a final result' do | ||
expect(sample_one.to_i(1000)).to eq(689) | ||
end | ||
end | ||
|
||
context 'when Results' do | ||
subject(:input_data) do | ||
File.read('spec/year_2015/day_14_input') | ||
end | ||
|
||
it 'correctly answers part 1' do | ||
expect(described_class.new(input_data, true).to_i(2503)).to eq(2655) | ||
end | ||
|
||
it 'correctly answers part 2' do | ||
expect(described_class.new(input_data).to_i(2503)).to eq(1059) | ||
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,62 @@ | ||
class Year2015 | ||
class Day14 | ||
class Reindeer | ||
attr_reader :name, :curpos | ||
attr_accessor :points | ||
|
||
def initialize(input_line) | ||
input_line =~ /(\w+) can fly (\d+) km\/s for (\d+) seconds, but then must rest for (\d+) seconds./ | ||
@name = $1 | ||
@speed = $2.to_i | ||
@length = $3.to_i | ||
@rest = $4.to_i | ||
@curpos = 0 | ||
@state = 'RUNNING' | ||
@progress = 0 | ||
@points = 0 | ||
end | ||
|
||
def switch_state(new_state) | ||
@state = new_state | ||
@progress = 0 | ||
end | ||
|
||
def advance(kms = 1) | ||
1.upto(kms) do | ||
@progress += 1 | ||
if @state == 'RUNNING' | ||
@curpos += @speed | ||
switch_state('RESTING') if @progress == @length | ||
elsif @progress == @rest | ||
switch_state('RUNNING') | ||
end | ||
end | ||
curpos | ||
end | ||
end | ||
|
||
def initialize(input_data, input_part_one = false) | ||
@input_file = input_data | ||
@version = input_part_one ? 1 : 2 | ||
@reindeers = input_data.chomp.split("\n").map do |input_line| | ||
Reindeer.new(input_line) | ||
end | ||
end | ||
|
||
def simple_to_i(kms = 1) | ||
@reindeers.map{|reindeer| reindeer.advance(kms) }.max | ||
end | ||
|
||
def to_i(kms = 1) | ||
return simple_to_i(kms) if @version == 1 | ||
|
||
0.upto(kms) do | ||
lead = @reindeers.map(&:advance).max | ||
@reindeers.select{|reindeer| reindeer.curpos == lead }.each do |reindeer| | ||
reindeer.points += 1 | ||
end | ||
end | ||
@reindeers.map(&:points).max | ||
end | ||
end | ||
end |