LoginSignup
0
0

RubyでAtCoder ABC252(A, B, C)を解いてみた

Last updated at Posted at 2023-05-31

はじめに

Webエンジニアを目指して、RubyやRailsをいじってます。
今回は、RubyでAtCoder ABC252のA, B, Cを解きました。備忘録として解き方をまとめていきたいと思います。

A - ASCII code

a-252.rb
puts gets.to_i.chr

B - Takahashi's Failure

b-252.rb
gets.split
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)

+ taste_max = a.max
- array = a.select.with_index.map{ |taste, index| index + 1 if taste == a.max}
+ array = a.filter_map.with_index(1){ |taste, index| index if taste == taste_max }
puts b.any?{ |dislike| array.include?(dislike) } ? "Yes" : "No"

解説

最初に「おいしさ」が最大となる食品のインデックスをarray配列にもたせます。そして、bの「嫌いな食品」がarrayに含まれていればYesを、含まれていなければNoを出力します。

C - Slot Strategy

c-252.rb
array = Array.new(gets.to_i){ gets.chomp }

puts [*"0".."9"].map{ |c| array.map{ |str| str.index(c) }.tally.map{ |key, value| key + 10 * (value - 1)}.max}.min

解説

最初に、0~9の文字それぞれに対して、リールのどこにあるかを取得します。そして、リールに表示されている文字を揃えるために必要な時間(ボタンを押すときの経過時間の最大値)を求めます。求める答えは、それらの最小値となります。

おわりに

「今回は結構良い感じに書けたんじゃないか?」と1人で喜んでいます。

改善点があればお願いします!

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0