From bcdd418b93980f317db3cd1353f42f96e04cad6f Mon Sep 17 00:00:00 2001 From: Gheiserton Date: Sat, 3 Dec 2022 14:24:27 -0500 Subject: [PATCH] Adds working files and binaries for day 3 solutions --- 2022/day03/BagCheck.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 2022/day03/BagCheck.java diff --git a/2022/day03/BagCheck.java b/2022/day03/BagCheck.java new file mode 100644 index 0000000..9f8bacf --- /dev/null +++ b/2022/day03/BagCheck.java @@ -0,0 +1,29 @@ +import java.util.Scanner; +import java.nio.file.Paths; +import java.io.IOException; + +public class BagCheck { + public static void main (String[] args) { + String bagContents = new String(); + int prioritySum = 0; + + try (Scanner inputLine = new Scanner(Paths.get("input.txt"))) { + while (inputLine.hasNext()) { + bagContents = inputLine.nextLine(); + boolean breakFlag = false; + for (int i = 0; i <= (bagContents.length()/2) && breakFlag == false; i++) { //iterates first half of string + for (int j = (bagContents.length()/2); j <= bagContents.length()-1 && breakFlag == false; j++) { //iterates second half of string + if (bagContents.charAt(i) == bagContents.charAt(j)) { + prioritySum += PriorityScore.score(bagContents.charAt(i)); + //System.out.printf("Bag Contents: %s :: Matching char %s%n", bagContents, bagContents.charAt(i)); //print for testing purposes + breakFlag = true; + } + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } + System.out.printf("Total score for matching chars is: %d", prioritySum); + } +}