Skip to content

Instantly share code, notes, and snippets.

@rsk0315

rsk0315/e-gen.rs Secret

Created Sep 19, 2022
Embed
What would you like to do?
リアクティブ問題のデバッグ
use rand::SeedableRng;
use rand_chacha::ChaCha20Rng;
use nekolib::rand_gen;
use nekolib::utils::rand_gen_macro::*;
use nekolib::utils::SpaceSep;
fn main() {
rand_gen! {
rng: ChaCha20Rng;
n in 10_usize..=20;
mut p1 in Permutation(n);
mut p2 in Permutation(n);
}
p1.pop();
p2.pop();
let mut res = vec![vec![0; n]; n];
for (&i, &j) in p1.iter().zip(&p2) {
res[i][j] = 1;
}
println!("{}", n);
for line in &res {
println!("{}", SpaceSep(line));
}
}
use std::env;
use std::fs::File;
use std::io::{stdin, stdout, BufReader, Write};
use proconio::{
input,
marker::Usize1,
source::{auto::AutoSource, line::LineSource},
};
macro_rules! println {
( $($t:tt)* ) => {
std::println!($($t)*);
stdout().flush().unwrap();
}
}
fn main() {
// --- テストケース用ファイルの読み込み ---
let infile_source = {
let name = env::args().nth(1).unwrap();
let file = File::open(&name).unwrap();
AutoSource::new(BufReader::new(file))
};
input! {
from infile_source,
n: usize,
a: [[i32; n]; n],
}
// --- ジャッジ側の前処理 ---
let expected = {
let i = (0..n).find(|&i| (0..n).all(|j| a[i][j] == 0)).unwrap();
let j = (0..n).find(|&j| (0..n).all(|i| a[i][j] == 0)).unwrap();
(i, j)
};
// --- 提出プログラムとのやり取り ---
let stdin = stdin();
let mut source = LineSource::new(BufReader::new(stdin.lock()));
println!("{}", n);
let ql = 20;
for i in 0..=ql {
input! {
from &mut source,
ty: char,
}
assert!(['!', '?'].contains(&ty));
if ty == '!' {
input! {
from &mut source,
x: Usize1,
y: Usize1,
}
assert_eq!((x, y), expected);
return;
}
if ty == '?' {
assert!(i < ql);
input! {
from &mut source,
il: Usize1,
ir: usize,
jl: Usize1,
jr: usize,
}
let res = (il..ir)
.flat_map(|i| (jl..jr).map(move |j| (i, j)))
.filter(|&(i, j)| a[i][j] == 1)
.count();
println!("{}", res);
}
}
}
3
0 1 0
1 0 0
0 0 0
import asyncio
import os
import sys
[CONTESTANT, JUDGE] = range(2)
COLUMNS = os.get_terminal_size().columns
def output(left, right, *, preleft='', preright=''):
wl = COLUMNS // 2 - 4
wr = (COLUMNS - 1) // 2 - 4
if not left and not right:
print(f' +-{"-" * wl}-+-{"-" * wr}-+')
return
if left and right:
preleft = preright = '\x1b[1;37m'
postleft = '\x1b[m' if preleft else ''
postright = '\x1b[m' if preright else ''
print(f' |{preleft} {left:{wl}} {postleft}|{preright} {right:{wr}} {postright}|')
async def listen(read, write, who, proc):
while proc.returncode is None:
content = await asyncio.to_thread(read.readline)
if content:
contents = ['', '']
contents[who] = content.rstrip('\n')
output(*contents)
print(content, end='', flush=True, file=write)
async def sniff(c_read, j_read, c_write, j_write, c_proc, j_proc):
with open(c_read, 'w') as w_c_read:
with open(j_read, 'w') as w_j_read:
with open(c_write, 'r') as r_c_write:
with open(j_write, 'r') as r_j_write:
await asyncio.gather(
listen(r_c_write, w_j_read, CONTESTANT, c_proc),
listen(r_j_write, w_c_read, JUDGE, j_proc),
)
async def main():
paths = ['cr.pipe', 'jr.pipe', 'cw.pipe', 'jw.pipe']
[c_read, j_read, c_write, j_write] = paths
for path in paths:
if os.path.exists(path):
os.remove(path)
os.mkfifo(path)
sep = sys.argv.index('--')
contestant_command = ' '.join(sys.argv[1:sep]) + f' < {c_read} > {c_write}'
judge_command = ' '.join(sys.argv[sep+1:]) + f' < {j_read} > {j_write}'
output('', '')
output('contestant', 'judge')
output('', '')
devnull = asyncio.subprocess.DEVNULL
c_proc = await asyncio.create_subprocess_shell(contestant_command, shell=True, stderr=devnull)
j_proc = await asyncio.create_subprocess_shell(judge_command, shell=True, stderr=devnull)
await asyncio.gather(
sniff(c_read, j_read, c_write, j_write, c_proc, j_proc),
c_proc.communicate(),
j_proc.communicate(),
)
if c_proc.returncode != 0:
output('', 'RE', preright='\x1b[1;91m')
elif j_proc.returncode != 0:
output('', 'WA', preright='\x1b[1;91m')
else:
output('', 'AC', preright='\x1b[1;92m')
output('', '')
for path in paths:
os.remove(path)
if __name__ == '__main__':
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment