diff --git a/10/.gitignore b/10/.gitignore new file mode 100644 index 0000000..44ba78e --- /dev/null +++ b/10/.gitignore @@ -0,0 +1 @@ +Main.class diff --git a/10/Main.java b/10/Main.java new file mode 100644 index 0000000..2a57500 --- /dev/null +++ b/10/Main.java @@ -0,0 +1,48 @@ +import java.io.*; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.lang.Math; + +public class Main { + static int instructionCounter = 1; + static int registerX = 1; + static int signalStrenghts = 0; + + public static String getCurrentPixel() { + String s = ""; + int crtPos = (instructionCounter - 1) % 40; + // System.out.printf("crtpos: %d\n", crtPos); + if (Math.abs(registerX - 1 - crtPos) <= 1) { s = "█"; } + else { s = " "; } + if (instructionCounter % 40 == 0) { s += "\r\n"; } + return s; + } + public static void instructionFinished() { + System.out.print(getCurrentPixel()); + instructionCounter++; + if (instructionCounter >= 20 && (instructionCounter - 20) % 40 == 0) { + signalStrenghts += registerX * instructionCounter; + } + } + + public static void main(String[] args) throws IOException { + String input = Files.readString(Path.of(args[0])); + String separator = System.getProperty("line.separator"); + int index = 0; + while (true) { + int nextIndex = input.indexOf(separator, index + separator.length()); + if (nextIndex < 0) { break; }; + String line = input.substring(index, nextIndex); + + instructionFinished(); + if (!line.equals("noop")) { + int val = Integer.parseInt(line.split(" ")[1]); + registerX += val; + instructionFinished(); + } + index = nextIndex + 1; + } + System.out.printf("The sum of the signal strengths is: %d\n", signalStrenghts); + } +} diff --git a/10/README.md b/10/README.md new file mode 100644 index 0000000..14002a3 --- /dev/null +++ b/10/README.md @@ -0,0 +1,8 @@ +# [Day 10](https://adventofcode.com/2022/day/10) +:gift::gift::gift::gift::gift::gift::gift::gift::gift::gift: + +Today's language: **Java** + +```shell + +``` diff --git a/10/asm.txt b/10/asm.txt new file mode 100644 index 0000000..57f49a0 --- /dev/null +++ b/10/asm.txt @@ -0,0 +1,139 @@ +noop +noop +noop +addx 6 +addx -1 +noop +addx 5 +noop +noop +addx -12 +addx 19 +addx -1 +noop +addx 4 +addx -11 +addx 16 +noop +noop +addx 5 +addx 3 +addx -2 +addx 4 +noop +noop +noop +addx -37 +noop +addx 3 +addx 2 +addx 5 +addx 2 +addx 10 +addx -9 +noop +addx 1 +addx 4 +addx 2 +noop +addx 3 +addx 2 +addx 5 +addx 2 +addx 3 +addx -2 +addx 2 +addx 5 +addx -40 +addx 25 +addx -22 +addx 2 +addx 5 +addx 2 +addx 3 +addx -2 +noop +addx 23 +addx -18 +addx 2 +noop +noop +addx 7 +noop +noop +addx 5 +noop +noop +noop +addx 1 +addx 2 +addx 5 +addx -40 +addx 3 +addx 8 +addx -4 +addx 1 +addx 4 +noop +noop +noop +addx -8 +noop +addx 16 +addx 2 +addx 4 +addx 1 +noop +addx -17 +addx 18 +addx 2 +addx 5 +addx 2 +addx 1 +addx -11 +addx -27 +addx 17 +addx -10 +addx 3 +addx -2 +addx 2 +addx 7 +noop +addx -2 +noop +addx 3 +addx 2 +noop +addx 3 +addx 2 +noop +addx 3 +addx 2 +addx 5 +addx 2 +addx -5 +addx -2 +addx -30 +addx 14 +addx -7 +addx 22 +addx -21 +addx 2 +addx 6 +addx 2 +addx -1 +noop +addx 8 +addx -3 +noop +addx 5 +addx 1 +addx 4 +noop +addx 3 +addx -2 +addx 2 +addx -11 +noop +noop +noop