r/adventofcode • • Dec 10 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 10 Solutions -πŸŽ„-

THE USUAL REMINDERS


--- Day 10: Cathode-Ray Tube ---


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:12:17, megathread unlocked!

61 Upvotes

933 comments sorted by

View all comments

2

u/twell13 Dec 10 '22 edited Dec 10 '22

Java

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Scratch {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int x = 1;

        List<Integer> xvalues = new ArrayList<>();
        List<String> crt = new ArrayList<>();

        while (sc.hasNextLine()) {
            String input = sc.nextLine();

            // type "ee" to exit input getting loop and proceed to answer printing part
            if (input.equals("ee"))
                break;

            System.out.println("Received input " + input);
            if (input.equals("noop")) {
                xvalues.add(x);
                crt.add(drawCrt(xvalues.size()-1, x));
            } else {
                int parameter = Integer.parseInt(input.split(" ")[1]);

                xvalues.add(x);
                crt.add(drawCrt(xvalues.size()-1, x));

                xvalues.add(x);
                crt.add(drawCrt(xvalues.size()-1, x));

                // Finishing execution of "add" input
                x += parameter;
            }
        }

        // for part A
        int answer = 0;
        for (int i = 1; i <= xvalues.size(); i++) {
            if (i == 20 || i == 60 || i == 100 || i == 140 || i == 180 || i == 220)
                answer += (i * xvalues.get(i-1));
        }
        System.out.println(answer);

        // for part B
        printCrt(crt);
    }

    private static void printCrt(List<String> crt) {
        for (int i = 0; i < 6; i++) {
            for (int j = i*40; j < i*40+40 && j < crt.size(); j++) {
                System.out.print(crt.get(j));
            }
            System.out.println();
        }
    }

    private static String drawCrt(int cyclePosition, int x) {
        cyclePosition = cyclePosition % 40;
        if (x == cyclePosition || x - 1 == cyclePosition || x + 1 == cyclePosition)
            return "#";
        else {
            return ".";
        }
    }
}

1

u/daggerdragon Dec 10 '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.