Created
March 27, 2023 07:31
-
-
Save Lyuu17/64797fd6fc9b6431b64804abd4002643 to your computer and use it in GitHub Desktop.
advent of code day 4 2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.Scanner; | |
public class AdventDay4 { | |
public static boolean checkAssignmentPair(String str) { | |
String pair[] = str.split(","); | |
String first[] = pair[0].split("-"), second[] = pair[1].split("-"); | |
int aa = Integer.parseInt(first[0]), ab = Integer.parseInt(first[1]); | |
int ba = Integer.parseInt(second[0]), bb = Integer.parseInt(second[1]); | |
return (aa <= ba && ab >= bb) || (ba <= aa && bb >= ab); | |
} | |
public static void main(String[] args) throws FileNotFoundException { | |
Scanner scan = new Scanner(new File("day4.txt")); | |
int sum = 0; | |
try { | |
while (true) { | |
if (checkAssignmentPair(scan.nextLine())) | |
sum++; | |
} | |
} | |
catch (Exception ignored) { | |
// eof | |
} | |
System.out.println(sum); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment