Ruby

Ruby で超簡単な CGI

エラーが出て全然わからない。 $ ruby test_webrick.rb [2020-04-01 22:31:56] INFO WEBrick 1.6.0 [2020-04-01 22:31:56] INFO ruby 2.7.0 (2019-12-25) [x86_64-linux] [2020-04-01 22:31:56] INFO WEBrick::HTTPServer#start: pid=15497 port=19681 [2020…

ABC159

https://atcoder.jp/contests/abc159 過去問。 A: The Number of Even Pairs 偶数から2個か奇数から2個取ればよい。 #x個の中から2個取る組み合わせの数 def c(x) return 0 if x == 0 || x == 1 x * (x - 1) / 2 end n, m = gets.split.map(&:to_i) puts c(n…

ABC158

https://atcoder.jp/contests/abc158 過去問。 A:Station and Bus s = gets.chomp puts (s == "AAA" || s == "BBB") ? "No" : "Yes" B:Count Balls n, a, b = gets.split.map(&:to_i) blue_ball = (n / (a + b)) * a remainder = n % (a + b) blue_ball += (…

パナソニックプログラミングコンテスト2020

https://atcoder.jp/contests/panasonic2020 過去問。 A:Kth Term ary = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51] puts ary[gets.to_i - 1] B:Bishop HかWが 1 の場合を忘れていた。 h, w = g…

RubyGem "gobject-introspection" が Linux Mint にインストールできない

Linux Mint 19.3 に gem "gtk2" をインストールしようとしてハマりました。 Fetching gobject-introspection 3.4.1 Installing gobject-introspection 3.4.1 with native extensions Gem::Ext::BuildError: ERROR: Failed to build gem native extension. cu…

キーエンス プログラミング コンテスト 2020

https://atcoder.jp/contests/keyence2020 過去問。 A h, w, n = readlines.map(&:to_i) puts (n / [h, w].max.to_f).ceil B imos法かと思ったが、ちがっていた。 n = gets.to_i data = n.times.map {gets.split.map(&:to_i)}.map {|x, l| [x - l, x + l]} r…

ABC151

https://atcoder.jp/contests/abc151 過去問。 A puts gets.chomp.succ B n, k, m = gets.split.map(&:to_i) sum = gets.split.map(&:to_i).inject(&:+) target = m * n - sum result = case when target < 0 then 0 when target > k then -1 else target en…

ABC152

https://atcoder.jp/contests/abc152 過去問。 A n, m = gets.split.map(&:to_i) puts (n == m) ? "Yes" : "No" B a, b = gets.split.map(&:to_i) result = a.to_s * b tmp = b.to_s * a result = tmp if result > tmp puts result C これはすぐに思いついた…

AOJ(問題集)23

0220 Binary Digit A Doctor Loved 小数の2進表現かあ。勉強になるなあ。なお、10進表現で循環小数でなくとも、2進表現で循環小数になる場合がある。 def calc(r, str = "") return str if str.length > 4 a = r * 2 b = a.to_i str += b.to_s c = a - b if …

Ruby でタイマーを作った

Gem 'kaki-utils' に入れた。 timer.rb module Utils #簡易タイマー def timer(minutes) end_time = Time.now + (minutes * 60).to_i while Time.now < end_time print "\e[2K\e[1G" + "left: #{(end_time - Time.now).to_i} sec." sleep(1) end Utils.bell …

AOJ(問題集)22

0210 The Squares むずかしかった。何とか自力で出来た。 Table = %W(E N W S) Man = Struct.new(:x, :y, :dir, :next_x, :next_y) do def next dx, dy = [[1, 0], [0, -1], [-1, 0], [0, 1]][self.dir] self.next_x, self.next_y = self.x + dx, self.y + d…

AtCoder/ABC138

https://atcoder.jp/contests/abc138 A - Red or Not puts (gets.to_i >= 3200) ? gets.chomp : "red" B - Resistors in Parallel gets as = gets.split.map(&:to_i) puts Rational(1, as.map {|i| Rational(1, i)}.inject(:+)).to_f C - Alchemist gets as …

ある双曲線の整数解

36*x^2-4*x-71*y^2+8=0 の整数解の導出。Ruby でできるだけ解いてみる。 solve.rb dir = [[1, 0], [0, 1], [-1, 0], [0, -1]] x = y = 0 step = 1 f = ->{p [x, y] if 36 * x ** 2 - 4 * x - 71 * y ** 2 + 8 == 0} add = ->(i) { dx, dy = dir[i] x += dx y…

AtCoder/ABC137

A - +-x a, b = gets.split.map(&:to_i) puts [a + b, a - b, a * b].max B - One Clue k, x = gets.split.map(&:to_i) puts [*x - k + 1..x + k - 1].join(" ") C - Green Bin words = gets.to_i.times.map {gets.chomp.chars.sort.join}.group_by(&:itself…

AtCorder/ABC(その1)

ABC001 A - 積雪深差 puts gets.to_i - gets.to_i B - 視程の通報 ans = case (m = gets.to_i) when 0...100 then 0 when 100..5000 then m / 100 when 6000..30000 then m / 1000 + 50 when 35000..70000 then (m / 1000 - 30) / 5 + 80 else 89 end puts s…

AOJ(問題集)21

0200 Traveling Alone: One-way Ticket of Youth until (given = gets.split.map(&:to_i)) == [0, 0] n, m = given edge = Array.new(m) {[]} h = Array.new(m) {[]} n.times do a, b, cost, time = gets.split.map(&:to_i) a -= 1 b -= 1 edge[a][b] = edge…

CPSCO2019 Session4

CPSCO2019 Session4 - AtCoder A - Swimming l, x = gets.split.map(&:to_i) n = x / l puts n.even? ? x % l : (n + 1) * l - x B - Meeting n, d = gets.split.map(&:to_i) table = d.times.map do gets.chomp.chars.map.with_index {|st, i| i if st == "…

Ruby 2.6 で Gem 'chipmunk' を使う(Ubuntu)

Ubuntu 19.04 の Ruby 2.6 で Gem 'chipmunk' がインストールできない。解決策:GitHub で修正されているものを使う。 $ git clone https://github.com/chipmunk-rb/chipmunk.git $ cd chipmunk $ rake clean compile $ gem build chipmunk.gemspec Successf…

AOJ(問題集)20

AIZU ONLINE JUDGE: Programming Challenge 0190 Eleven Puzzle 素直に幅優先探索を実行して死んだので、ここを参考に解いた。「両側探索」というらしい。 start = "fff0fff" "ff123ff" "f45678f" "ff9abff" "fff0fff" result1 = {} result1[start] = 0 stac…

AtCoder(AtCoder Beginners Selection)

AtCoder Beginners Selection - AtCoder PracticeA - はじめてのあっとこーだー(Welcome to AtCoder) a = gets.to_i b, c = gets.split.map(&:to_i) s = gets print "#{a + b + c} #{s}" ABC086A - Product a, b = gets.split.map(&:to_i) puts (a * b).od…

AOJ(問題集)19

AIZU ONLINE JUDGE: Programming Challenge 0180 Demolition of Bridges until (given = $<.gets.split.map(&:to_i)) == [0, 0] n, m = given bridges = m.times.map {$<.gets.split.map(&:to_i)}.sort_by(&:last) city = Array.new(n, false) city[0] = tru…

AOJ(ALDS)その2

AIZU ONLINE JUDGE: Programming Challenge ALDS1_9_A Complete Binary Tree #完全二分木 $<.gets given = [nil] + $<.gets.split.map(&:to_i) a = nil given.drop(1).each_with_index do |key, i| j = i + 1 str = "node #{j}: key = #{key}, " str += "par…

Ruby/Opal サンプル

require 'native' d = Native(`document`) area = d.getElementById("text") button = d.getElementById("start") interval = Native(`window.setInterval`) z, d = zd = ["ズン", "ドコ"] st = "" line = [] f = false zundoko = ->{ return if f line {int…

AOJ(問題集)18

AIZU ONLINE JUDGE: Programming Challenge 0170 Lunch until (n = $<.gets.to_i).zero? data = n.times.map {$<.gets.split}.map {|a, *b| [a] + b.map(&:to_i)} selected = [] all = [*0...n] solve = ->(left, order) { return if data[order.last][2] < …

AOJ(問題集)17

AIZU ONLINE JUDGE: Programming Challenge 0160 Delivery Fee table = [[60, 2, 600], [80, 5, 800], [100, 10, 1000], [120, 15, 1200], [140, 20, 1400], [160, 25, 1600]] until (n = $<.gets.to_i).zero? result = n.times.map {$<.gets.split.map(&:to…

AOJ(問題集)16

AIZU ONLINE JUDGE: Programming Challenge 0150 Twin Prime N = 10007 sieve = [*0..N] 2.upto(Math.sqrt(N).to_i) do |i| next if sieve[i].zero? 2.upto(N / i) {|j| sieve[i * j] = 0} end sieve = sieve[2..-1].reject {|x| x.zero?} until (n = $<.get…

AOJ(問題集)15

AIZU ONLINE JUDGE: Programming Challenge 0140 Bus Line line = [*0..9] + [5, 4, 3, 2, 1] line += line $<.gets.to_i.times do start, goal = $<.gets.split.map(&:to_i) str = if 1 <= start and start <= 5 and start > goal [*goal..start].reverse.j…

AOJ(問題集)14

AIZU ONLINE JUDGE: Programming Challenge 0130 Train $<.gets.to_i.times do given = $<.gets.chomp.split(/(->|<-)/) train = [given.shift] while (dir = given.shift) car = given.shift next if train.include?(car) (dir == "->") ? train.push(car) …

AOJ(問題集)13

AIZU ONLINE JUDGE: Programming Challenge 0120 Patisserie @memo = {} def length(circles) return @memo[circles] if @memo[circles] l = case (s = circles.size) when 0 then 0 when 1 then 0 when 2 r1, r2 = circles.first, circles.last Math.sqrt((…

AOJ(問題集)12

AIZU ONLINE JUDGE: Programming Challenge 0110 Alphametic $<.readlines.map(&:chomp).map {|l| l.split(/\+|=/)}.each do |given| catch(:jump) do 10.times do |i| d = given.map {|a| a.gsub("X", i.to_s)} next unless d.select {|s| s.length >= 2 an…