Skip to content

Instantly share code, notes, and snippets.

View yancya's full-sized avatar
💭
😂

Shinta Koyanagi yancya

💭
😂
View GitHub Profile
@yancya
yancya / main.rb
Created April 10, 2018 11:37
オフラインリアルタイムどう書くE23 への yancya の回答(BigQuery 版) http://nabetani.sakura.ne.jp/hena/orde23nokoch/
require 'google-cloud-bigquery'
def main(str)
n, inputs = str.split(',').yield_self { |n, inputs| [n, inputs.chars] }
tables = inputs.map.with_index { |symbol, i|
<<~SQL
t#{i+1} AS (
SELECT ARRAY_CONCAT(t#{i}.nums, [rules.`no`]) AS nums
, t#{i}.angle + rules.angle AS angle
@yancya
yancya / main.rb
Last active April 7, 2018 15:00
オフラインリアルタイムどう書くE23 への yancya の回答 http://nabetani.sakura.ne.jp/hena/orde23nokoch/
require 'pg'
RULES = {
'a' => [0, 1, -1, 0],
'b' => [0, 1, 0, -1, 0]
}
ANS = '0+-'
def main(str)
@yancya
yancya / main.rb
Last active April 9, 2018 05:04
オフラインリアルタイムどう書くE23 への yancya の回答 http://nabetani.sakura.ne.jp/hena/orde23nokoch/
require 'pg'
def main(str)
n, inputs = str.split(',')
inputs_sql = inputs.chars.map { |s| "('#{s}')" }.join(', ')
sql = <<~SQL
WITH RECURSIVE rules(symbol, "no", angle) AS (
VALUES('a', 1, 0), ('a', 2, 60), ('a', 3, -60), ('a', 4, 0),
('b', 1, 0), ('b', 2, 60), ('b', 3, 0), ('b', 4, -60), ('b', 5, 0))
CREATE TEMP TABLE hoge (id integer, name text);
WITH t(id, name) AS (VALUES (1, 'hoge'), (2, 'fuga'), (3, 'piyo'))
, t2 AS (INSERT INTO hoge SELECT id, name FROM t)
INSERT INTO hoge SELECT id * 10, name FROM t;
SELECT * FROM hoge;
-- id | name
-- ----+------
@yancya
yancya / EXCEPT.sql
Created December 15, 2017 11:04
INTERSECT and EXCEPT for BigQuery
#standardsql
WITH a AS (
SELECT * FROM UNNEST([1,2,3,4]) AS n
), b AS (
SELECT * FROM UNNEST([4,5,6,7]) AS n)
SELECT * FROM a
WITH T(ID, A, TIME) AS (
VALUES (1, 2, '2017-12-10')
, (2, 3, '2017-12-10')
, (3, 2, '2017-12-10')
, (4, 4, '2017-12-11')
, (5, 4, '2017-12-11')
, (6, 2, '2017-12-11'))
SELECT A
, COUNT(A) AS cnt
@yancya
yancya / 1_main.rb
Last active December 2, 2017 14:52
オフラインリアルタイムどう書くE20 への yancya の解答 https://yhpg.doorkeeper.jp/events/67049
require 'pg'
def main(str)
start, goal = str.chars
PG.connect.exec_params(<<~SQL, [start, goal]).first['cnt']
WITH RECURSIVE patterns("from", "to") AS (
VALUES ('0', '1'), ('0', '6'), ('1', '0'), ('1', '7'), ('2', '3'),
('3', '2'), ('3', '4'), ('3', '9'), ('4', '3'), ('4', '5'),
('5', '4'), ('5', 'B'), ('6', '0'), ('6', 'C'), ('7', '1'),
('7', '8'), ('8', '7'), ('8', '9'), ('9', '8'), ('9', '3'),
@yancya
yancya / card.sql
Last active November 16, 2017 05:26
WITH local_card_ids(id, "order") AS (VALUES(1, 0), (2, 1), (3, 2), (4, 3), (5, 4))
SELECT cache.cards.*
FROM cache.cards
JOIN local_card_ids USING(id)
ORDER BY local_card_ids."order";

データ

  WITH "user" AS (VALUES(1, 'taro'), (2, 'jiro'))
SELECT JSON_AGG("user")
  FROM "user";
             json_agg
@yancya
yancya / index.js
Last active November 2, 2017 03:12
const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
let mainWindow = null;
app.on('window-all-closed', function() {
if (process.platform != 'darwin') {
app.quit();
}
});