Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javakarol.Welt;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- /**
- * Realisierung einer Advent of Code Karolwelt für BlueJ
- * Kann den Puzzle Input einlesen
- * Advent of Code 2024 - Day 06
- */
- public class AocWelt extends Welt {
- // Lade diese Puzzle Input Datei als default
- private final static String DEFAULT_PUZZLE_INPUT_DATEI = "day06_sample.txt";
- // Kacheln der Input Datei
- final static char LEER = '.';
- final static char HINDERNIS = '#';
- final static char WACHE = '^';
- // Strings die mit `manuellSetzen()` verwendet werden:
- // String Objekte = "zro/zge/zbl/zgr/zsc/zxx/mro/mge/mbl/mgr/msc/mxx/qua/qxx/kar/";
- final static String MANUAL_ZIEGEL_DEFAULT = "zro";
- final static String MANUAL_ZIEGEL_BLAU = "zbl";
- final static String MANUAL_ZIEGEL_ENTFERNEN = "zxx";
- // Annahmen / Konstanten für die Welt
- final static int WELT_NUM_ZIEGEL = 1;
- final static int WELT_HOEHE = 6;
- private String[] labor;
- /**
- * Erstelle die Robot Karol Welt mit der Standard-Input-Datei.
- */
- public AocWelt() {
- this(DEFAULT_PUZZLE_INPUT_DATEI);
- }
- /**
- * Erstelle die Robot Karol Welt mit einer
- * @param puzzleInputDatei Die AoC Puzzle Input Datei, aus der die Robot Karol Welt erzeugt wird.
- */
- public AocWelt(String puzzleInputDatei) {
- super(1, 1, 1); // init without .kdw file
- // read puzzle input
- this.labor = leseDatei(puzzleInputDatei);
- // resize Welt to lab
- int breite = this.labor.length;
- int laenge = this.labor[0].length();
- this.resizeWelt(breite, laenge, WELT_HOEHE, false);
- this.initWelt(breite, laenge, WELT_HOEHE, false);
- // place obstacles
- this.platziereHindernisse();
- System.out.println("Platziere Wache Karol mit `welt.platziereWacheKarol()` um ihm Befehle zu geben.");
- // done...
- this.initView();
- }
- private void platziereHindernisse() {
- // build world
- //System.out.println("initWelt(" + breite + ", " + laenge + ", " + WELT_HOEHE + ", " + false + ")");
- for (int i = 0; i < labor.length; i++) {
- for (int j = 0; j < labor[i].length(); j++) {
- // ignore height in 2d map
- var tile = labor[i].charAt(j);
- if (tile == LEER) {
- continue; // nothing to do here...
- } else if (tile == HINDERNIS) {
- int x = j + 1; // mathematically: x = columns
- int y = i + 1; // mathematically: y = rows
- this.platziereHindernis(x, y, MANUAL_ZIEGEL_DEFAULT);// Quader == Wand
- } else if (tile == WACHE) {
- //this.manuellSetzen(x, y, WORLD_KAROL);
- continue; // generate AocRoboter
- }
- }
- System.out.println();
- }
- }
- private void platziereHindernis(int x, int y, String farbe) {
- for(int z = 0; z < WELT_NUM_ZIEGEL; z++)
- this.manuellSetzen(x, y, farbe);
- }
- public void platziereNeuesHindernis(int x, int y) {
- this.platziereHindernis(x, y, MANUAL_ZIEGEL_BLAU);
- this.initView();
- }
- public void entferneNeuesHindernis(int x, int y) {
- this.manuellSetzen(x, y, MANUAL_ZIEGEL_ENTFERNEN);
- }
- /**
- * Finde die Wache Karol im AoC Puzzle Input und platziere ihn in der Welt.
- */
- public AocRoboter platziereWacheKarol() {
- // find guard
- RoboDat wachePos = null;
- for (int i = 0; i < labor.length; i++) {
- for (int j = 0; j < labor[i].length(); j++) {
- if (labor[i].charAt(j) == AocWelt.WACHE) {
- int x = j + 1;
- int y = i + 1;
- wachePos = new RoboDat(x, y, 'N');
- break;
- }
- }
- }
- return platziereWacheKarol(wachePos);
- }
- /**
- * Platziere Wache Karol manuell.
- */
- private AocRoboter platziereWacheKarol(int posX, int posY, char direct) {
- return this.platziereWacheKarol(new RoboDat(posX, posY, direct));
- }
- private AocRoboter platziereWacheKarol(RoboDat guardPos) {
- // Wache Karol in Welt platzieren
- return new AocRoboter(guardPos.posX, guardPos.posY, guardPos.direct, this);
- }
- private String[] leseDatei(String fileName) {
- // read puzzle input
- var puzzleInputPath = Paths.get(fileName);
- String[] puzzleInput = null;
- try {
- var _puzzleInput = Files.readAllLines(puzzleInputPath);
- puzzleInput = _puzzleInput.toArray(new String[0]);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- return puzzleInput;
- }
- /**
- * Meta Klasse die Position und Richtung des Roboters enthält.
- */
- private static class RoboDat {
- int posX = 1;
- int posY = 1;
- char direct = 'S';
- /**
- * @param x 1..breite
- * @param y 1..laenge
- * @param direction 'N', 'O', 'S', 'W'
- */
- RoboDat(int x, int y, char direction) {
- this.posX = x;
- this.posY = y;
- this.direct = direction;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement