owned this note
owned this note
Published
Linked with GitHub
###### tags: `esolang`
# Shared Document for <font color='red'>Red Team</font> in Esolang Codegolf Contest 6
## 記号
abc def
a1b1c1 d1e1f1
:
a32b32c32 d32e32f32
l[0]...l[6]=abc def
## ロジックリスト
- l[1] = l[2] なら l[1] または l[2] そうでなければ l[4]
- `s[2<<(s[1]!=s[2])]`
- `l[4-(l[1]==l[2])*2]`
- l[4] = l[5] なら l[4] または l[5] そうでなければ l[2]
- X=abc, Y=defとして、X%100%11==0 なら X%10 そうでなければ Y/100
- Y/10%11?X%10:Y/100
- Y%110>9?X%10:Y/100 でも良い
- l[l[2]のfind_first_index * 2]
- 嘘だけど60%くらいで通るらしい
- 15パターン中 `.AB AA.` と `.AB CC.` の時に1/9の確率でWAで、これらは最低4、最大6個出現する
- `(8/9)^4*(1-(1/9)*(2/15))^2 ≒ 0.606`
- `. XX` を `X` で置換して l[2]
- `l.replace(/.+(.)(\1| ).+/,"$1")`
- `s/.*(.)(\1| ).*/\1/` は嘘だけど,`+`のコストが高いときには使える (sedとか)
- `l.match(/ .|(?<=(.))\1 /)`
- 処理系によっては `/ .|(.)(?=\1 )/` が使える (racketとか)
- `(b=c)*c + (b!=c)*d`
- `d + (b=c)*(c-d)`
- `d + (b/c)*(c/b)*(c-d)`
- substrの場合、4-(b=c)文字目から2文字とすると*2がいらない
## Sample Input
```
495 997
311 771
248 487
745 661
876 754
344 774
943 551
488 698
844 143
571 714
655 556
463 645
261 165
263 732
622 223
411 193
659 951
938 889
512 612
141 962
762 837
843 311
571 119
577 766
688 493
851 316
946 355
519 113
924 475
146 846
941 528
422 323
```
## Checker
Usage:
```
./a.out < in.txt | ./checker in.txt
```
- checker
```python
#!/usr/bin/env python3
# SPDX-License-Identifier: CC0-1.0
import traceback
import argparse
import sys
import re
from typing import List
N = 32
def get_parser():
parser = argparse.ArgumentParser(description='Checker for Esolang Codegolf Contest 6')
parser.add_argument('input_file', type=str,
help='This specifies input file. Output should be given from stdin.')
return parser
def calc_length(input_line: str, digit: int) -> int:
line = input_line[:3] + str(digit) + input_line[4:]
assert len(line) == 7
for i in range(len(line) - 2):
if line[i] == line[i+1] and line[i+1] == line[i+2]:
return len(line) - 3
return len(line)
def calc_expected_lengthes(input_lines: List[str]) -> List[int]:
res = []
for line in input_lines:
length = len(line)
for j in range(1, 10):
length = min(length, calc_length(line, j))
res += [length]
return res
def calc_answer_lengthes(input_lines: List[str], output_digits: List[int]) -> List[int]:
res = []
n = len(input_lines)
assert len(output_digits) == n
return [calc_length(input_lines[i], output_digits[i]) for i in range(n)]
def parse_output(output_data: str) -> List[int]:
for space in [' ', '\t', '\n', '\r']:
output_data = output_data.replace(space, '')
if re.search(r'[^1-9]', output_data) is not None:
invalid_characters = ','.join(re.sub(r'[1-9]', "", output_data))
print(f'[ERROR] The output contains wrong characters: "{invalid_characters}"', file=sys.stderr)
exit(1)
if len(output_data) != N:
print(f'[ERROR] The number of output digits is wrong: actual({len(output_data)}) vs expected({N})', file=sys.stderr)
exit(1)
return [int(c) for c in output_data]
def check(input_data: str, output_data: str) -> List[str]:
input_lines = input_data.split('\n')[:N]
output_digits = parse_output(output_data)
expected_lengthes = calc_expected_lengthes(input_lines)
answer_lengthes = calc_answer_lengthes(input_lines, output_digits)
assert len(expected_lengthes) == len(answer_lengthes)
errs = []
for i in range(len(expected_lengthes)):
if expected_lengthes[i] != answer_lengthes[i]:
errs += [f'[ERROR] Wrong Answer on Case {i+1}: input: {input_lines[i].strip()} -> output: {output_digits[i]}']
return errs
def read_from_file(path_to_file):
with open(path_to_file, mode='r') as f:
return f.read()
def main():
args = get_parser().parse_args()
errs = check(read_from_file(args.input_file), sys.stdin.read())
if errs != []:
print('\n'.join(errs), file=sys.stderr)
exit(1)
else:
print("OK")
main()
```
## backhand
`abcdef` を `adebcf` にする
```python3
#!/usr/bin/env python3
# SPDX-License-Identifier: CC0-1.0
import sys
import re
def gen_backhand_s(s, n, step):
new_s = [''] * n
x = 0
d = 1
for i in range(len(s)):
if new_s[x] != '':
return None
new_s[x] = s[i]
if i < n-1:
for _ in range(step):
if x == n-1 and d == 1:
d = -1
elif x == 0 and d == -1:
d = 1
x += d
for i in range(n):
if new_s[i] == '':
new_s[i] = 'X'
return ''.join(new_s)
def main():
assert len(sys.argv) >= 2
fname = sys.argv[1]
with open(fname, mode='r') as f:
s = re.sub(r'[\n \t]', '', f.read())
for k in range(3):
new_s = gen_backhand_s(s, len(s)+k, 3)
if new_s is not None:
break
sys.stdout.write(new_s)
main()
```