I thought to spent my free time by solving a puzzle: Best Before Spotify Puzzle.
I coded in Java, and yeah I did not clean up my code (just a rough work) and I have yet to optimize... so I did check for possible test cases (including leap year) and could not find any of these cases failing, but I got a reply from the bot as "wrong answer". Here is my code, could anyone please find where my test case fails?
Algorithm:
- Generate 6 different inputs (for 3 inputs, permutation goes as 3*2).
- Choose the best earliest date of all 6 inputs.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
public class bestbefore {
int array[]=new int[3];
ArrayList<Integer> temp=new ArrayList<Integer>();
int prevy=10000;
int prevd=1000;
int prevm=1000;
int flag=0;
String tem;
public static void main(String[] args)
{
bestbefore b4=new bestbefore();
b4.input();
}
void input() throws IllegalArgumentException
{
InputStreamReader input=new InputStreamReader(System.in);
BufferedReader inputs=new BufferedReader(input);
try {
tem=inputs.readLine();
String [] temps=tem.split("/");
try
{
array[0]=Integer.valueOf(temps[0]).intValue();
temp.add(array[0]);
array[1]=Integer.valueOf(temps[1]).intValue();
temp.add(array[1]);
array[2]=Integer.valueOf(temps[2]).intValue();
temp.add(array[2]);
}
catch (ArrayIndexOutOfBoundsException e) {
// TODO: handle exception
error();
}
catch (NumberFormatException e) {
// TODO: handle exception
error();
}
for(int i=0;i<3;i++)
{
for(int j=1;j<=2;j++)
{
if(j==1)
{
int year=temp.get(0);
int month=temp.get(1);
int date=temp.get(2);
//System.out.println("Passing"+year+"/"+month+"/"+date);
calculate(year,month,date);
}
else
{
int year=temp.get(0);
int month=temp.get(2);
int date=temp.get(1);
//System.out.println("Passing"+year+"/"+month+"/"+date);
calculate(year,month,date);
}
}
Collections.rotate(temp, -1);
}
if((array[0] <=0)|| (array [1]<=0)|| (array[2] <=0) ||(String.valueOf(array[0]).length()==3))
{
flag=0;
}
if(flag==0)
{
System.out.println(array[0]+"/"+array[1]+"/"+array[2]+" is illegal");
}
else
{
if(flag!=0)
{
String prem=String.valueOf(prevm);
String pred=String.valueOf(prevd);
if(String.valueOf(prevm).length()<=1)
{
prem=0+prem;
}
if(String.valueOf(prevd).length()<=1)
{
pred=0+pred;
}
System.out.println(prevy+"-"+prem+"-"+pred);
}
}
}
catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Invalid input");
}
}
private void error() {
// TODO Auto-generated method stub
System.out.println(tem+" is illegal");
System.exit(0);
}
void calculate(int year,int month,int date)
{
if(String.valueOf(year).length()<3)
{
year=(year+2000);
}
boolean leap=false;
if((year%4==0)&&((year%100==0)))
{
if(year%400==0)
{
leap=true;
//System.out.println("Leap year"+year);
}
}
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(date>=1 && date<=31)
{
if(year<prevy)
{
// System.out.println("generated"+year+"-"+month+"-"+date);
flag=1;
prevy=year;
prevm=month;
prevd=date;
}
else if((year==prevy)&& (month<prevm))
{
//System.out.println("generated"+year+"-"+month+"-"+date);
flag=1;
prevy=year;
prevm=month;
prevd=date;
}
else if ((year==prevy) &&(month==prevm))
{
if(date<prevd)
{
// System.out.println("generated"+year+"-"+month+"-"+date);
flag=1;
prevy=year;
prevm=month;
prevd=date;
}
}
else
{
}
}
break;
case 4:
case 6:
case 9:
case 11:
if(date>=1 &&date<=30)
{
if(year<prevy)
{
// System.out.println("generated"+year+"-"+month+"-"+date);
flag=1;
prevy=year;
prevm=month;
prevd=date;
}
else if((year==prevy)&& (month<prevm))
{
//System.out.println("generated"+year+"-"+month+"-"+date);
flag=1;
prevy=year;
prevm=month;
prevd=date;
}
else if ((year==prevy) &&(month==prevm))
{
if(date<prevd)
{
//System.out.println("generated"+year+"-"+month+"-"+date);
flag=1;
prevy=year;
prevm=month;
prevd=date;
}
}
else
{
}
}
break;
case 2:
if(leap==true)
{
if(date>=1 &&date<=29)
{
if(year<prevy)
{
// System.out.println("generated"+year+"-"+month+"-"+date);
flag=1;
prevy=year;
prevm=month;
prevd=date;
}
else if((year==prevy)&& (month<prevm))
{
// System.out.println("generated"+year+"-"+month+"-"+date);
flag=1;
prevy=year;
prevm=month;
prevd=date;
}
else if ((year==prevy) &&(month==prevm))
{
if(date<prevd)
{
// System.out.println("generated"+year+"-"+month+"-"+date);
flag=1;
prevy=year;
prevm=month;
prevd=date;
}
}
else
{
}
}
}
if(leap==false)
{
if(date>=1 && date<=28)
{
if(year<prevy)
{
// System.out.println("generated"+year+"-"+month+"-"+date);
flag=1;
prevy=year;
prevm=month;
prevd=date;
}
else if((year==prevy)&& (month<prevm))
{
// System.out.println("generated"+year+"-"+month+"-"+date);
flag=1;
prevy=year;
prevm=month;
prevd=date;
}
else if ((year==prevy) &&(month==prevm))
{
if(date<prevd)
{
// System.out.println("generated"+year+"-"+month+"-"+date);
flag=1;
prevy=year;
prevm=month;
prevd=date;
}
}
else
{
}
}
}
break;
}
}
}