-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday1.rb
More file actions
75 lines (58 loc) · 1.4 KB
/
day1.rb
File metadata and controls
75 lines (58 loc) · 1.4 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class Elf
def initialize(name, calories)
@name = name
@calories = calories
end
attr_accessor :name, :calories
end
class Group
def initialize()
@group = Array.new
end
attr_reader :group
def input_file(input)
elf_name = 1
elf = Elf.new(elf_name, 0)
File.foreach(input) do |line|
line = line.chomp
if line.length == 0 then
self.add_elf(elf)
elf_name += 1
elf = Elf.new(elf_name, 0)
else
elf.calories = elf.calories + line.to_i
end
end
end
def add_elf(elf)
@group<<elf
end
def num_of_elves
@group.length
end
def top3
sorted = @group.sort_by {|elf| elf.calories}.reverse
sorted[0].calories + sorted[1].calories + sorted[2].calories
end
def most_calories
best_elf = Elf.new("test", 0)
group.each do |elf|
if elf.calories > best_elf.calories then
best_elf = elf
end
end
return best_elf
end
def to_s
temp = ""
@group.each do |elf|
temp = temp + "elf name #{elf.name} total calories #{elf.calories}\n"
end
return temp
end
end
my_group = Group.new()
my_group.input_file("input")
puts my_group.to_s
p my_group.most_calories
p my_group.top3