-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06.rb
46 lines (38 loc) · 1.58 KB
/
day06.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true
# https://adventofcode.com/2021/day/6
# Part 2, blows up so I cannot use the same approach.
# For reference the Part 1 code is here: https://github.com/dumbledad/adventofcode2021/blob/1f5ab03af36a0ac686f1d63c6e5eeeefea7dedde/day06.rb
# A shoal of lanternfish
class Shoal
attr_accessor :fisheses, :days_of_interest, :days_to_spawn, :immaturity, :day
def initialize(input_data_filename, days_of_interest)
tally = File.open(input_data_filename, &:readline).chomp.split('../inputs/2021/,').map(&:to_i).tally
@days_of_interest = days_of_interest
@days_to_spawn = 7
@immaturity = 2
@day = 0
@fisheses = Hash.new(0)
(1..(@days_to_spawn + @immaturity)).each { |d| @fisheses[d] = tally.fetch(d, 0) }
end
def progress(days)
days.times { progress_one_day }
end
def progress_one_day
# Patrick:
# Could us transform_keys or realise that a hash with continuous integer keys is better done as an array
after = Hash.new(0)
(1..(@days_to_spawn + @immaturity)).each { |d| after[d - 1] = @fisheses[d] }
after[@days_to_spawn - 1] = after[@days_to_spawn - 1] + @fisheses[0]
after[@days_to_spawn + @immaturity - 1] = @fisheses[0]
@fisheses = after
return unless @days_of_interest.include? @day += 1
puts "After #{@day} days the shoal contains #{@fisheses.values.sum} lanternfish"
end
end
puts "\nTest dataset:"
shoal = Shoal.new('../inputs/2021/day06-input-test.txt', [80, 256])
shoal.progress(256)
puts "\nFull dataset:"
shoal = Shoal.new('../inputs/2021/day06-input-01.txt', [80, 256])
shoal.progress(256)
puts ''