From bce137f249b90f4df94893b77a2fd8df8cb7a214 Mon Sep 17 00:00:00 2001 From: Oliver Akins Date: Thu, 8 Dec 2022 23:10:21 -0600 Subject: [PATCH] Add day 2 solutions --- day_02/java/Part1.java | 77 +++++++++++++++++++++++++++++++++++++++ day_02/java/Part2.java | 81 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 day_02/java/Part1.java create mode 100644 day_02/java/Part2.java diff --git a/day_02/java/Part1.java b/day_02/java/Part1.java new file mode 100644 index 0000000..481a64f --- /dev/null +++ b/day_02/java/Part1.java @@ -0,0 +1,77 @@ +package day_02.java; + +import java.io.FileNotFoundException; +import java.util.*; + +import jlib.FileSystem; + +public class Part1 { + public static void main(String[] args) { + if (args.length < 1) { + System.out.println("Not enough args"); + return; + }; + Scanner s; + try { + s = FileSystem.readFile(args[0]); + } catch (FileNotFoundException e) { + System.out.println("Can't find that file"); + return; + } + + Integer points = 0; + while (s.hasNextLine()) { + String l = s.nextLine(); + Integer round = Part1.determineScore( + l.charAt(l.length() - 1), + l.charAt(0) + ); + points = points + round; + } + System.out.println(points + " points"); + } + + /** + * Determines if we win or the opponent wins in a game of Rock Paper Scissors + * + * A = Rock + * B = Paper + * C = Scissors + * + * X = Rock + * Y = Paper + * Z = Scissors + * + * Paper + * / ^ + * / \ + * v \ + * Rock --> Scissors + * + */ + public static boolean isWin(Character me, Character opponent) { + if ( me.equals(opponent) ) { + return false; + } + else if (opponent.equals('A')) { + return me.equals('Y'); + } else if (opponent.equals('B')) { + return me.equals('Z'); + } else { + return me.equals('X'); + } + } + + public static Integer determineScore(Character me, Character opponent) { + Integer choice = (int)me - 87; + + if (choice == ((int)opponent - 64)) { + return 3 + choice; + } + + if (Part1.isWin(me, opponent)) { + return 6 + choice; + } + return choice; + } +} \ No newline at end of file diff --git a/day_02/java/Part2.java b/day_02/java/Part2.java new file mode 100644 index 0000000..167b32e --- /dev/null +++ b/day_02/java/Part2.java @@ -0,0 +1,81 @@ +package day_02.java; + +import java.io.FileNotFoundException; +import java.util.*; + +import jlib.FileSystem; + +public class Part2 { + public static void main(String[] args) { + if (args.length < 1) { + System.out.println("Not enough args"); + return; + }; + Scanner s; + try { + s = FileSystem.readFile(args[0]); + } catch (FileNotFoundException e) { + System.out.println("Can't find that file"); + return; + } + + Integer points = 0; + while (s.hasNextLine()) { + String l = s.nextLine(); + Integer round = determineScore( + l.charAt(0), + l.charAt(l.length() - 1) + ); + points = points + round; + } + System.out.println(points + " points"); + } + + /* + * + * A = Rock + * B = Paper + * C = Scissors + * + * X = Rock + * Y = Paper + * Z = Scissors + * + * Paper + * / ^ + * / \ + * v \ + * Rock --> Scissors + * + */ + + + public static int winAgainst(int choice) { + int value = choice + 1; + if (value > 3) { + value = 1; + } + return value; + }; + + public static int loseAgainst(int choice) { + int value = choice - 1; + if (value < 1) { + value = 3; + } + return value; + } + + public static Integer determineScore(Character opponent, Character desiredResult) { + int opposing = (int)opponent - 64; + switch (desiredResult) { + case 'X': + return loseAgainst(opposing); + case 'Y': + return opposing + 3; + case 'Z': + return winAgainst(opposing) + 6; + } + return 0; + } +} \ No newline at end of file