Skip to content

Commit e087504

Browse files
author
Robb Kidd
committed
part 1! Easy!
But oooooh, slow for part 2.
1 parent cbb2b2b commit e087504

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

2021/ruby/day06.rb

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
class Day06
2+
def self.go
3+
day = new
4+
puts "Part 1: #{day.part1}"
5+
puts "Part 2: #{day.part2}"
6+
end
7+
8+
attr_reader :input
9+
10+
def initialize(input=nil)
11+
@input = input || real_input
12+
end
13+
14+
# @example 18 days
15+
# s = LanternFishSchool.new(EXAMPLE_INPUT)
16+
# 18.times { s.a_day_passes }
17+
# s.school.count #=> 26
18+
#
19+
# @example 80 days
20+
# d = Day06.new(EXAMPLE_INPUT)
21+
# d.part1 #=> 5934
22+
def part1
23+
s = LanternFishSchool.new(input)
24+
80.times { s.a_day_passes }
25+
s.school.count
26+
end
27+
28+
def part2
29+
end
30+
31+
def real_input
32+
File.read('../inputs/day06-input.txt')
33+
end
34+
35+
EXAMPLE_INPUT = <<~INPUT
36+
3,4,3,1,2
37+
INPUT
38+
end
39+
40+
class LanternFishSchool
41+
attr_reader :size, :school
42+
43+
# @example
44+
# LanternFishSchool
45+
# .new('1,2,3')
46+
# .school #=> [1,2,3]
47+
def initialize(list='')
48+
@school = list.chomp.split(",").map(&:to_i)
49+
end
50+
51+
# @example 1 day
52+
# LanternFishSchool
53+
# .new('3,4,3,1,2')
54+
# .a_day_passes
55+
# .school #=> [2,3,2,0,1]
56+
# @example 2 days
57+
# LanternFishSchool
58+
# .new('3,4,3,1,2')
59+
# .a_day_passes
60+
# .a_day_passes
61+
# .school #=> [1,2,1,6,0,8]
62+
#
63+
def a_day_passes
64+
new_fish = @school.count(0)
65+
@school = @school.map { |fish_days|
66+
fish_days == 0 ? 6 : fish_days - 1
67+
}.append([8] * new_fish).flatten
68+
self
69+
end
70+
end

0 commit comments

Comments
 (0)