Skip to content

Instantly share code, notes, and snippets.

@cypher-nullbyte
Last active May 21, 2020 09:29
Show Gist options
  • Save cypher-nullbyte/bc15c0cefb1985eec5b948a4f0f0e50e to your computer and use it in GitHub Desktop.
Save cypher-nullbyte/bc15c0cefb1985eec5b948a4f0f0e50e to your computer and use it in GitHub Desktop.
Vpropel VIT | POD | 20/05/2020 | Crossy Roads | 14
import java.util.ArrayList;
import java.util.Scanner;
public class Main
{
public static class Points
{
public int x=0;
public int y=0;
public int getX()
{
return x;
}
public void setX(int x)
{
this.x = x;
}
public int getY()
{
return y;
}
public void setY(int y)
{
this.y = y;
}
}
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
int n= input.nextInt();
input.nextLine();
String s=input.next();
if(s.charAt(0)=='[')
{
String s_modified="";
for(int i=1;i<s.length()-1;i++){
s_modified+=s.charAt(i);
}
s=s_modified;
}
String[] str=s.split(",");
ArrayList<Integer> arr=new ArrayList<>();
for(String i:str) arr.add(Integer.parseInt(i));
ArrayList<Points> list=new ArrayList<>();
list.add(new Points());
int x_coord=0;
int y_coord=0;
for(int i=0;i<arr.size();i++)
{
int temp=arr.get(i);
switch (i%4)
{
case 0:
for(int j=0;j<temp;j++)
{
y_coord++;
for(Points pt:list)
{
if(pt.x==x_coord && pt.y==y_coord)
{
System.out.print("Yes");
return;
}
}
Points pt=new Points();
pt.setX(x_coord);
pt.setY(y_coord);
list.add(pt);
}
break;
case 1:
for(int j=0;j<temp;j++)
{
x_coord--;
for(Points pt:list)
{
if(pt.x==x_coord&&pt.y==y_coord)
{
System.out.print("Yes");
return;
}
}
Points pt=new Points();
pt.setX(x_coord);
pt.setY(y_coord);
list.add(pt);
}
break;
case 2:
for(int j=0;j<temp;j++)
{
y_coord--;
for(Points pt:list)
{
if(pt.x==x_coord&&pt.y==y_coord)
{
System.out.print("Yes");
return;
}
}
Points pt=new Points();
pt.setX(x_coord);
pt.setY(y_coord);
list.add(pt);
}
break;
case 3:
for(int j=0;j<temp;j++)
{
x_coord++;
for(Points pt:list)
{
if(pt.x==x_coord&&pt.y==y_coord)
{
System.out.print("Yes");
return;
}
}
Points pt=new Points();
pt.setX(x_coord);
pt.setY(y_coord);
list.add(pt);
}
}
}
System.out.print("No");
}
}
// ### Thank YOu :)
// @cs_jawanda
// .Chiranjeet Singh Jawanda.
// VIT VELLORE
n=int(input())
lt=input()
if '[' in lt:
lt=lt[1:len(lt)-1]
l=lt.split(',')
l=[int(x) for x in l]
visited=[[0,0]]
x,y=0,0
for i in range(n):
if i%4==0:
for j in range(l[i]):
y+=1;
for k in range(len(visited)):
if visited[k][0]==x and visited[k][1]==y:
print("Yes")
exit(None)
visited.append([x,y])
elif i%4==1:
for j in range(l[i]):
x-=1;
for k in range(len(visited)):
if visited[k][0]==x and visited[k][1]==y:
print("Yes")
exit(None)
visited.append([x,y])
elif i%4==2:
for j in range(l[i]):
y-=1;
for k in range(len(visited)):
if visited[k][0]==x and visited[k][1]==y:
print("Yes")
exit(None)
visited.append([x,y])
else:
for j in range(l[i]):
x+=1;
for k in range(len(visited)):
if visited[k][0]==x and visited[k][1]==y:
print("Yes")
exit(None)
visited.append([x,y])
print("No")
'''
// ### Thank YOu :)
// @cs_jawanda
// .Chiranjeet Singh Jawanda.
// VIT VELLORE
'''
// Reducing code redundancy :)
import java.util.ArrayList;
import java.util.Scanner;
public class Main
{
static class Point
{
int x=0;
int y=0;
Point()
{}
Point(int x,int y)
{ this.x=x;this.y=y;}
}
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
int n=input.nextInt();
String s=input.next();
if(s.charAt(0)=='[')
{
String temp="";
for(int i=1;i<s.length()-1;i++)
temp+=s.charAt(i);
s=temp;
}
String[] str=s.split(",");
ArrayList<Integer> arr=new ArrayList<>();
for(String i:str) arr.add(Integer.parseInt(i));
ArrayList<Point> list=new ArrayList<>();
list.add(new Point());
int x=0,y=0;
for(int i=0;i<arr.size();i++)
{
int temp=arr.get(i);
for(int j=0;j<temp;j++)
{
switch(i%4)
{
case 0: y++;break;
case 1: x--;break;
case 2: y--;break;
case 3: x++;break;
}
for(Point pt:list)
if(pt.x==x && pt.y==y)
{
System.out.print("Yes");
System.exit(0);
}
list.add(new Point(x,y));
}
}
System.out.print("No");
}
}
Crossy Roads
Given a array where index 0 represents the distance walked towards north index 1 represents the distance walked
towards west index 2 represents the distance walked towards south and index 3 represents the distance walled towards east
and index 4 represents the distance towards north ,index 5 represents the distance towards west and so on
Assume that you are initially at origin. Given the distances find out whether you’ll cross the path you walked before
Print Yes if they cross or else No
Example:-
┌───┐
│ │
└───┼──>
Input: [2,1,1,2]
Output: Yes
Example:-
┌──────┐
│ │
└────────────>
Input: [1,2,3,4]
Output: No
Example:-
┌───┐
│ │
└───┼>
Input: [1,1,1,1]
Output: Yes
Input format:-
n-number of walks
Distance of walks separated by comma
Output format:-
Yes/No
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment