Advertisement
Guest User

Advent of Code 2024-Day06 - Robot Karol World

a guest
Dec 16th, 2024
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.37 KB | None | 0 0
  1. import javakarol.Welt;
  2.  
  3. import java.io.IOException;
  4. import java.nio.file.Files;
  5. import java.nio.file.Paths;
  6.  
  7. /**
  8.  * Realisierung einer Advent of Code Karolwelt für BlueJ
  9.  * Kann den Puzzle Input einlesen
  10.  * Advent of Code 2024 - Day 06
  11.  */
  12. public class AocWelt extends Welt {
  13.     // Lade diese Puzzle Input Datei als default
  14.     private final static String DEFAULT_PUZZLE_INPUT_DATEI = "day06_sample.txt";
  15.  
  16.     // Kacheln der Input Datei
  17.     final static char LEER = '.';
  18.     final static char HINDERNIS = '#';
  19.     final static char WACHE = '^';
  20.  
  21.     // Strings die mit `manuellSetzen()` verwendet werden:
  22.     // String Objekte = "zro/zge/zbl/zgr/zsc/zxx/mro/mge/mbl/mgr/msc/mxx/qua/qxx/kar/";
  23.     final static String MANUAL_ZIEGEL_DEFAULT = "zro";
  24.     final static String MANUAL_ZIEGEL_BLAU = "zbl";
  25.     final static String MANUAL_ZIEGEL_ENTFERNEN = "zxx";
  26.  
  27.     // Annahmen / Konstanten für die Welt
  28.     final static int WELT_NUM_ZIEGEL = 1;
  29.     final static int WELT_HOEHE = 6;
  30.  
  31.     private String[] labor;
  32.  
  33.     /**
  34.      * Erstelle die Robot Karol Welt mit der Standard-Input-Datei.
  35.      */
  36.     public AocWelt() {
  37.         this(DEFAULT_PUZZLE_INPUT_DATEI);
  38.     }
  39.  
  40.     /**
  41.      * Erstelle die Robot Karol Welt mit einer
  42.      * @param puzzleInputDatei Die AoC Puzzle Input Datei, aus der die Robot Karol Welt erzeugt wird.
  43.      */
  44.     public AocWelt(String puzzleInputDatei) {
  45.         super(1, 1, 1);  // init without .kdw file
  46.  
  47.         // read puzzle input
  48.         this.labor = leseDatei(puzzleInputDatei);
  49.  
  50.         // resize Welt to lab
  51.         int breite = this.labor.length;
  52.         int laenge = this.labor[0].length();
  53.         this.resizeWelt(breite, laenge, WELT_HOEHE, false);
  54.         this.initWelt(breite, laenge, WELT_HOEHE, false);
  55.  
  56.         // place obstacles
  57.         this.platziereHindernisse();
  58.  
  59.         System.out.println("Platziere Wache Karol mit `welt.platziereWacheKarol()` um ihm Befehle zu geben.");
  60.  
  61.         // done...
  62.         this.initView();
  63.     }
  64.  
  65.     private void platziereHindernisse() {
  66.         // build world
  67.         //System.out.println("initWelt(" + breite + ", " + laenge + ", " + WELT_HOEHE + ", " + false + ")");
  68.  
  69.         for (int i = 0; i < labor.length; i++) {
  70.             for (int j = 0; j < labor[i].length(); j++) {
  71.                 // ignore height in 2d map
  72.                 var tile = labor[i].charAt(j);
  73.                 if (tile == LEER) {
  74.                     continue;  // nothing to do here...
  75.                 } else if (tile == HINDERNIS) {
  76.                     int x = j + 1;  // mathematically: x = columns
  77.                     int y = i + 1;  // mathematically: y = rows
  78.                     this.platziereHindernis(x, y, MANUAL_ZIEGEL_DEFAULT);// Quader == Wand
  79.                 } else if (tile == WACHE) {
  80.                     //this.manuellSetzen(x, y, WORLD_KAROL);
  81.                     continue;  // generate AocRoboter
  82.                 }
  83.             }
  84.             System.out.println();
  85.         }
  86.     }
  87.  
  88.     private void platziereHindernis(int x, int y, String farbe) {
  89.         for(int z = 0; z < WELT_NUM_ZIEGEL; z++)
  90.             this.manuellSetzen(x, y, farbe);
  91.     }
  92.  
  93.     public void platziereNeuesHindernis(int x, int y) {
  94.         this.platziereHindernis(x, y, MANUAL_ZIEGEL_BLAU);
  95.         this.initView();
  96.     }
  97.  
  98.     public void entferneNeuesHindernis(int x, int y) {
  99.         this.manuellSetzen(x, y, MANUAL_ZIEGEL_ENTFERNEN);
  100.     }
  101.  
  102.     /**
  103.      * Finde die Wache Karol im AoC Puzzle Input und platziere ihn in der Welt.
  104.      */
  105.     public AocRoboter platziereWacheKarol() {
  106.         // find guard
  107.         RoboDat wachePos = null;
  108.         for (int i = 0; i < labor.length; i++) {
  109.             for (int j = 0; j < labor[i].length(); j++) {
  110.                 if (labor[i].charAt(j) == AocWelt.WACHE) {
  111.                     int x = j + 1;
  112.                     int y = i + 1;
  113.                     wachePos = new RoboDat(x, y, 'N');
  114.                     break;
  115.                 }
  116.             }
  117.         }
  118.  
  119.         return platziereWacheKarol(wachePos);
  120.     }
  121.  
  122.     /**
  123.      * Platziere Wache Karol manuell.
  124.      */
  125.     private AocRoboter platziereWacheKarol(int posX, int posY, char direct) {
  126.         return this.platziereWacheKarol(new RoboDat(posX, posY, direct));
  127.     }
  128.  
  129.     private AocRoboter platziereWacheKarol(RoboDat guardPos) {
  130.         // Wache Karol in Welt platzieren
  131.         return new AocRoboter(guardPos.posX, guardPos.posY, guardPos.direct, this);
  132.     }
  133.  
  134.     private String[] leseDatei(String fileName) {
  135.         // read puzzle input
  136.         var puzzleInputPath = Paths.get(fileName);
  137.         String[] puzzleInput = null;
  138.         try {
  139.             var _puzzleInput = Files.readAllLines(puzzleInputPath);
  140.             puzzleInput = _puzzleInput.toArray(new String[0]);
  141.         } catch (IOException e) {
  142.             throw new RuntimeException(e);
  143.         }
  144.  
  145.         return puzzleInput;
  146.     }
  147.  
  148.     /**
  149.      * Meta Klasse die Position und Richtung des Roboters enthält.
  150.      */
  151.     private static class RoboDat {
  152.         int posX = 1;
  153.         int posY = 1;
  154.         char direct = 'S';
  155.  
  156.         /**
  157.          * @param x         1..breite
  158.          * @param y         1..laenge
  159.          * @param direction 'N', 'O', 'S', 'W'
  160.          */
  161.         RoboDat(int x, int y, char direction) {
  162.             this.posX = x;
  163.             this.posY = y;
  164.             this.direct = direction;
  165.         }
  166.     }
  167. }
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement