import java.io.IOException; import java.io.RandomAccessFile; public class main { public static void main(String[] args) { try { RandomAccessFile file = new RandomAccessFile("Input.txt", "r"); String str; int indexOfSeperator = 0, indexOfFirstDash = 0, indexOfSecondDash = 0; int sectionBeginningElf1, sectionEndingElf1, sectionBeginningElf2, sectionEndingElf2; int countPart1 = 0, countPart2 = 0; while ((str = file.readLine()) != null) { indexOfSeperator = str.indexOf(","); indexOfFirstDash = str.indexOf("-"); indexOfSecondDash = str.indexOf("-", indexOfFirstDash+1); sectionBeginningElf1 = Integer.valueOf(str.substring(0, indexOfFirstDash)); sectionEndingElf1 = Integer.valueOf(str.substring(indexOfFirstDash+1,indexOfSeperator)); sectionBeginningElf2 = Integer.valueOf(str.substring(indexOfSeperator+1, indexOfSecondDash)); sectionEndingElf2 = Integer.valueOf(str.substring(indexOfSecondDash+1)); // Part 1 if ((sectionBeginningElf1 <= sectionBeginningElf2 && sectionEndingElf1 >= sectionEndingElf2) || (sectionBeginningElf2 <= sectionBeginningElf1 && sectionEndingElf2 >= sectionEndingElf1)) { countPart1++; } // Part 2 if (between(sectionBeginningElf1, sectionBeginningElf2, sectionEndingElf2) || between(sectionEndingElf1, sectionBeginningElf2, sectionEndingElf2) || between(sectionBeginningElf2, sectionBeginningElf1, sectionEndingElf1) || between(sectionEndingElf2, sectionBeginningElf1, sectionEndingElf1)) { countPart2++; } //System.out.println(sectionBeginningElf1 + " " + sectionEndingElf1 + " - " + sectionBeginningElf2 + " " + sectionEndingElf2); } System.out.println("Part 1: " + countPart1); System.out.println("Part 2: " + countPart2); file.close(); } catch (IOException e) { e.printStackTrace(); } } public static boolean between(int variable, int minValueInclusive, int maxValueInclusive) { return variable >= minValueInclusive && variable <= maxValueInclusive; } }