Skip to content

Instantly share code, notes, and snippets.

@Lyuu17
Created March 27, 2023 07:31
Show Gist options
  • Save Lyuu17/64797fd6fc9b6431b64804abd4002643 to your computer and use it in GitHub Desktop.
Save Lyuu17/64797fd6fc9b6431b64804abd4002643 to your computer and use it in GitHub Desktop.
advent of code day 4 2022
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