Created
March 27, 2023 07:09
-
-
Save Lyuu17/ca08c9c1850d1dd96c2c07ee9eca935a to your computer and use it in GitHub Desktop.
advent of code day 3 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.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