"When to take medicine" problem doubt

class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		while(t-->0)
		{
		    int c=0;
		    String str[]=sc.next().split(":");
		    int y=Integer.parseInt(str[0]);
		    int m=Integer.parseInt(str[1]);
		    int d=Integer.parseInt(str[2]);
		    if(m==1 || m==3 || m==5 || m==7 || m==8 || m==10 ||m==12)
		    {
		        c=(31-d)/2 + 1;
		    }
		    else if(m==2)
		    {
		        if((y%4==0) && (y%100!=0 || y%400==0))
		        {
		            c=(29-d)/2+1;
		        }
		        else{
		            c=(59-d)/2+1;
		        }
		    }
		    else{
		        c=(61-d)/2+1;
		    }
		    System.out.println(c);
		}
	}
}

I was unable to solve this problem so I looked one solution but unable to understand.
Please can someone explain the logic above?