problem | http://nabetani.sakura.ne.jp/hena/ord28spirwa/ |
---|---|
simulation(Python/Ruby/C++) | http://qiita.com/cielavenir/items/8c77a158136bd668a27b |
Regularity(Python/Ruby/C/C#/Java) | http://qiita.com/cielavenir/items/a285b0cea4a26ff886b8 |
Regularity(D/Go/Swift/PHP/ Vala ) | http://qiita.com/cielavenir/items/edb1daff9ea861a418ec |
Regularity(VB/F#/Perl) | http://qiita.com/cielavenir/items/0c84af4049ab161f82c1 |
Regularity(PowerShell) | http://qiita.com/cielavenir/items/d9ef9f12068e99e888f2 |
Regularity( AIR-lang ) | http://qiita.com/cielavenir/items/d804f61412fb4f07ba06 |
Regularity(Crystal/Perl6) | http://qiita.com/cielavenir/items/13896a662b05da8b77a2 |
I want to process the first element in a Ruby multidimensional array and return it (tap/About break etc.) |
http://qiita.com/cielavenir/items/3f209351b924e2615f5e |
An additional question was asked for me, who ended the simulation with the right method by copying and pasting the existing code. Of course, the simulation does not seem to solve the additional problem. Therefore, it is necessary to find and use regularity. However, if you don't write the code carefully, bugs can be introduced in small details. Specifically, it is +1 at the tip and -1 at the end. I noticed the latter, but not the former in time. I had the chocolate bar shoot down. Thank you very much.
hena28_fast.py
#!/usr/bin/env python
#http://qiita.com/Nabetani/items/23ebddb44f0234e7fb15
#http://nabetani.sakura.ne.jp/hena/ord28spirwa/
dir='ESWN'
if __name__=='__main__':
import sys,itertools
if sys.version_info[0]>=3: raw_input=input
try:
while True:
line=raw_input().rstrip().split(':')
n,e,s,w=[int(e) for e in line[0].split(',')]
l=[e,s,w,n]
days=int(line[1])+1
for i in itertools.count(0):
for j,e in enumerate([l[i%4]+(i%4==0),(i//4+1)*2,l[i%4]-(i%4==3)]):
if days-e<0:
print(dir[(i+j)%4])
break
days-=e
else:
continue
break
sys.stdout.flush()
except EOFError:
pass
hena28_fast.rb
#!/usr/bin/env ruby
#http://qiita.com/Nabetani/items/23ebddb44f0234e7fb15
#http://nabetani.sakura.ne.jp/hena/ord28spirwa/
dir='ESWN'
if __FILE__==$0
while gets
line=$_.chomp.split(':')
n,e,s,w=line[0].split(',').map(&:to_i)
l=[e,s,w,n]
days=line[1].to_i+1
(0..1/0.0).each{|i|
break unless [
l[i%4]+(i%4==0 ? 1 : 0),
(i/4+1)*2,
l[i%4]-(i%4==3 ? 1 : 0),
].each_with_index{|e,j|
if days-e<0
puts dir[(i+j)%4]
break
end
days-=e
}
}
STDOUT.flush
end
end
hena28_fast.c
// http://qiita.com/Nabetani/items/23ebddb44f0234e7fb15
// http://nabetani.sakura.ne.jp/hena/ord28spirwa/
#include <stdio.h>
int main(){
const char* dir="ESWN";
int n,e,s,w;
long long days;
for(;~scanf("%d,%d,%d,%d:%lld",&n,&e,&s,&w,&days);){
days+=1;
int l[]={e,s,w,n},i=0,j,f=1;
for(;f;i++){
int x[]={l[i%4]+(i%4==0),(i/4+1)*2,l[i%4]-(i%4==3)};
for(j=0;j<3;j++){
if(days-x[j]<0){
printf("%c\n",dir[(i+j)%4]);
f=0;
break;
}
days-=x[j];
}
}
fflush(stdout);
}
}
hena28_fast.cs
// http://qiita.com/Nabetani/items/23ebddb44f0234e7fb15
// http://nabetani.sakura.ne.jp/hena/ord28spirwa/
using System;
using System.Runtime.InteropServices;
class Hena28{
[DllImport("c", CallingConvention = CallingConvention.Cdecl)]
private static extern int scanf(string format, out int a, out int b, out int c, out int d, out long e);
static void Main(){
string dir="ESWN";
int n,e,s,w;
long days;
for(;scanf("%d,%d,%d,%d:%lld",out n,out e,out s,out w,out days)==5;){
days+=1;
int[] l=new int[]{e,s,w,n};
int f=1;
for(int i=0;f!=0;i++){
int[] x=new int[]{l[i%4]+(i%4==0 ? 1 : 0),(i/4+1)*2,l[i%4]-(i%4==3 ? 1 : 0)};
for(int j=0;j<3;j++){
if(days-x[j]<0){
Console.WriteLine(dir[(i+j)%4]);
f=0;
break;
}
days-=x[j];
}
}
Console.Out.Flush();
}
}
}
hena28_fast.java
// http://qiita.com/Nabetani/items/23ebddb44f0234e7fb15
// http://nabetani.sakura.ne.jp/hena/ord28spirwa/
import java.util.Scanner;
import java.util.regex.*;
class Main{
public static void main(String[]args){
Scanner cin=new Scanner(System.in);
String dir="ESWN";
for(;cin.hasNext();){
cin.findInLine("(\\d+),(\\d+),(\\d+),(\\d+):(\\d+)");
MatchResult match=cin.match();
cin.nextLine();
int[] l=new int[]{Integer.parseInt(match.group(2)),Integer.parseInt(match.group(3)),Integer.parseInt(match.group(4)),Integer.parseInt(match.group(1))};
long days=Long.parseLong(match.group(5))+1;
int i=0,j,f=1;
for(;f!=0;i++){
int[] x=new int[]{l[i%4]+(i%4==0 ? 1 : 0),(i/4+1)*2,l[i%4]-(i%4==3 ? 1 : 0)};
for(j=0;j<3;j++){
if(days-x[j]<0){
System.out.println(dir.charAt((i+j)%4));
f=0;
break;
}
days-=x[j];
}
}
System.out.flush();
}
}
}
Recommended Posts