-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02.rb
138 lines (127 loc) · 3.43 KB
/
day02.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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# frozen_string_literal: true
require 'pp'
require_relative '../helpers/input_reader'
execute(1) do |games|
games_out = games.sum(0) do |game|
game_id, rounds = game.split(':')
rounds = rounds.split(';')
p game_id
maxes = {
"red" => 0,
"green" => 0,
"blue" => 0,
}
rounds.map(&:strip).map do |round|
round.split(", ").each do |color_count|
count, color = color_count.split(" ")
maxes[color] = if maxes[color] < count.to_i
count.to_i else maxes[color]
end
end
end
p rounds
p maxes
if maxes["red"] <= 12 && maxes["green"] <= 13 && maxes["blue"] <= 14
game_id.split(' ')[1].to_i
else
0
end
end
p games_out
end
execute(2) do |games|
games_out = games.map do |game|
game_id, rounds = game.split(':')
rounds = rounds.split(';')
p game_id
maxes = {
"red" => 0,
"green" => 0,
"blue" => 0,
}
rounds.map(&:strip).map do |round|
round.split(", ").each do |color_count|
count, color = color_count.split(" ")
maxes[color] = if maxes[color] < count.to_i
count.to_i else maxes[color]
end
end
end
p rounds
p maxes
if maxes["red"] <= 12 && maxes["green"] <= 13 && maxes["blue"] <= 14
game_id.split(' ')[1].to_i
else
0
end
maxes.each_value.reduce(&:*)
end.sum
p games_out
end
execute(1, alternative_text: "Code Golf") do |games|
games_out = games.sum(0) do |game|
game_id, rounds = game.split(':')
rounds = rounds.split(';')
maxes = {
"red" => 0,
"green" => 0,
"blue" => 0,
}
rounds.map(&:strip).map do |round|
round.split(", ").each do |color_count|
count, color = color_count.split(" ")
maxes[color] = if maxes[color] < count.to_i
count.to_i else maxes[color]
end
end
end
if maxes["red"] <= 12 && maxes["green"] <= 13 && maxes["blue"] <= 14
game_id.split(' ')[1].to_i
else
0
end
end
p games_out
end
execute(2, alternative_text: "Code Golf Readable") do |games|
games_out = games.map do |line|
line.sub!(/.*:/,'')
maxes = Hash.new(0)
line.split(?;) do |round|
round.split(?,) do |color_count|
count, color = color_count.split
maxes[color] = maxes[color]<count.to_i ?count.to_i : maxes[color]
end
end
maxes.each_value.reduce(&:*)
end.sum
p games_out
end
execute(2, alternative_text: "Code Golf 142 chars") do |g|
p g.map{_1.sub!(/.*:/,'')
m=Hash.new(0)
_1.split(?;){|r|r.split(?,){|a|b,c=a.split
m[c]=[m[c],b.to_i].max}}
m.values.reduce(&:*)}.sum
end
# https://www.reddit.com/r/adventofcode/comments/188w447/2023_day_2_solutions/kbneo94/
execute(2, alternative_text: "Code Golf modified cwby_bbop solution") do |games|
games_out = games.map do |line|
line.sub!(/.*:/,'')
maxes = Hash.new(0)
line.split(?;) do |round|
round.scan(/(\d+) (\w+)/).map do |k, v|
maxes[v] = [maxes[v], k.to_i].max
end
end
maxes.values.reduce(&:*)
end.sum
p games_out
end
# https://www.reddit.com/r/adventofcode/comments/188w447/2023_day_2_solutions/kbneo94/
execute(2, alternative_text: "Code Golf modified cwby_bbop compressed") do |g|
p g.map{|l|l.sub!(/.*:/,'')
m=Hash.new(0)
l.split(?;){|r|r.scan(/(\d+) (\w+)/){|k,v|m[v]=[m[v],k.to_i].max}}
m.values.reduce(&:*)}.sum
end