r/adventofcode • • Dec 02 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 2 Solutions -🎄-

NEW AND NOTEWORTHY


--- Day 2: Rock Paper Scissors ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:06:16, megathread unlocked!

103 Upvotes

1.5k comments sorted by

View all comments

3

u/NeoKadra Dec 02 '22 edited Dec 02 '22

Here is my java solution for part 2


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

public class Code{
    public static Map<String, Integer> test = Map.of(
                                            "A X", 3,
                                            "A Y", 4,
                                            "A Z", 8,
                                            "B X", 1,
                                            "B Y", 5,
                                            "B Z", 9,
                                            "C X", 2,
                                            "C Y", 6,
                                            "C Z", 7
                                            );

    public static int score = 0;
    public static void main(String[] args){
        try {
            Scanner sc = new Scanner(new File("input.txt"));
            while(sc.hasNextLine()){
                String line = sc.nextLine();            
                score += test.get(line);
            }   
            sc.close();
            System.out.println(score);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

1

u/daggerdragon Dec 05 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.

0

u/xtay2 Dec 02 '22

With Java 10 you can just write:

var score = 0;
for(var line : Files.readAllLines("input.txt))
    score += test.get(line);

1

u/Puzzled_Programmer97 Dec 02 '22

No need to create a list when we just calculate the score and then throw the line away.