User Tools

Site Tools


codesnippets
import java.io.*;
import java.lang.*;
import java.util.*;

public class Day4
{
	
    public static int[] reader(String input)throws IOException{ 
	   int rangeArray[] = new int[2];
	   BufferedReader reader = new BufferedReader(new FileReader(input));
	   while (true) {
		String line = reader.readLine();
		if (line == null){
			break;
		}
		String[] parts = line.split("-");
		int lowRange = Integer.parseInt(parts[0]);
		int highRange = Integer.parseInt(parts[1]);
		rangeArray[0] = lowRange;
		rangeArray[1] = highRange;
	   }
	  return rangeArray;
    }

    public static int checkAdjacent()throws IOException{
		int[] rangeArray = reader("input.txt");
		int[] duplicateArray = new int[rangeArray[1] - rangeArray[0]];
		int counter = 0;
		for(int i=rangeArray[0]; i < rangeArray[1]; i++){
			int num = i;
			boolean flag = false;
			int duplicateCounter = 0;
			int currentDigit = num % 10;
			num /= 10;
			while(num > 0){
				if(currentDigit < num %10){
					flag = true;
					break;
				}
				if(currentDigit == num %10){
					duplicateCounter++;
				}
				currentDigit = num % 10;
				num = num/10;
			}
			if(flag == false && duplicateCounter >= 1){
				counter++;
			}
		}
		return counter;
	}
    public static void main(String args[]){
	    try{
		System.out.println(checkAdjacent());
	    }
	    catch(IOException e){
		e.printStackTrace();
	    }
    }
}
codesnippets.txt · Last modified: 2019/12/20 02:32 by 54.240.196.184