Skip to content

Instantly share code, notes, and snippets.

@Lyuu17
Created March 27, 2023 07:09
Show Gist options
  • Save Lyuu17/ca08c9c1850d1dd96c2c07ee9eca935a to your computer and use it in GitHub Desktop.
Save Lyuu17/ca08c9c1850d1dd96c2c07ee9eca935a to your computer and use it in GitHub Desktop.
advent of code day 3 2022
package org.example;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class AdventDay3 {
public static int getLowerCasePriority(char c) {
return c - 'a' + 1;
}
public static int getUpperCasePriority(char c) {
return c - 'A' + 27;
}
public static int getCommonItemValue(String first, String sec, String third) {
return first.chars()
.filter((c) -> sec.chars().anyMatch((a) -> a == c) && third.chars().anyMatch((b) -> b == c))
.map((c) -> Character.isUpperCase(c) ? getUpperCasePriority((char) c) : getLowerCasePriority((char) c))
.findFirst().getAsInt();
}
public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(new File("day3.txt"));
int sum = 0;
try {
while (true) {
sum += getCommonItemValue(scan.nextLine(), scan.nextLine(), scan.nextLine());
}
}
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