Advent of Code: Day 2

priyanshusrivastava

Priyanshu srivastava

Posted on December 4, 2022

Advent of Code: Day 2

The Basic thing is to note that each

  • win means 6 points
  • draw means 3 points
  • lose means 0 point

Part 1

Now in part 1,

  • A and X means rock for 1 point
  • B and Y means paper for 2 points
  • C and Z means Scissor for 3 points
import java.io.File;
import java.util.Scanner;

public class Day2 {
    public static void main(String[] args) throws Exception {
        String path = "D:\\projects\\AdventOfCode\\src\\input.txt";
        File input = new File(path);
        Scanner sc = new Scanner(input);

        int points = 0;
        while (sc.hasNextLine()) {
            var line = sc.nextLine();
            char a = line.charAt(0);
            char b = line.charAt(2);

            if (b == 'X') { // rock
                if (a == 'A') { // rock
                    points += 4;
                } else if (a == 'B') { // paper
                    points += 1;
                } else { // scissor
                    points += 7;
                }
            } else if (b == 'Y') { // paper
                if (a == 'A') { // rock
                    points += 8;
                } else if (a == 'B') { // paper
                    points += 5;
                } else { // scissor
                    points += 2;
                }
            } else { // scissor
                if (a == 'A') { // rock
                    points += 3;
                } else if (a == 'B') { // paper
                    points += 9;
                } else { // scissor
                    points += 6;
                }
            }
        }

        System.out.println(points);
        sc.close();
    }
}

Enter fullscreen mode Exit fullscreen mode

Part 2

For this part,

  • X means lose = 0 point
  • Y means draw = 3 points
  • Z means win = 6 points

therefore we have to find the counter move for ourself according to the play by opponent i.e for eg.

if the condition is to lose and opponent plays a rock, we have to play a scissor.

import java.io.File;
import java.util.Scanner;

public class Day2 {
    public static void main(String[] args) throws Exception {
        String path = "D:\\projects\\AdventOfCode\\src\\input.txt";
        File input = new File(path);
        Scanner sc = new Scanner(input);

        int points = 0;
        while (sc.hasNextLine()) {
            var line = sc.nextLine();
            char a = line.charAt(0);
            char b = line.charAt(2);

            if (b == 'X') { // lose
                if (a == 'A') { // need scissor
                    points += 3;
                } else if (a == 'B') { // need rock
                    points += 1;
                } else { // need paper
                    points += 2;
                }
            } else if (b == 'Y') { // draw
                if (a == 'A') { // need rock
                    points += 4;
                } else if (a == 'B') { // need paper
                    points += 5;
                } else { // need scissor
                    points += 6;
                }
            } else { // win
                if (a == 'A') { // need paper
                    points += 8;
                } else if (a == 'B') { // need scissor
                    points += 9;
                } else { // need rock
                    points += 7;
                }
            }
        }

        System.out.println(points);
        sc.close();
    }
}

Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
priyanshusrivastava
Priyanshu srivastava

Posted on December 4, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Advent of Code: Day 2
adventofcode Advent of Code: Day 2

December 4, 2022

Advent of Code: Day 1
adventofcode Advent of Code: Day 1

December 4, 2022