• # Il était vraiment sympa celui-ci :-)

    Posté par . En réponse au message Avent du Code, jour 22. Évalué à 1.

    Environ 4h-4h30 effectif dessus.
    La première partie est fastidieuse, mais çà se code bien (~2h)

    La deuxième partie est encore plus fastidieuse.
    J'ai opté pour une classe "Arrete" agrégeant 2 segments caractérisés par une direction.

    Si on est dans un de ces 2 segments en matchant la direction de ce dernier, j'applique la transformation de changement d’arête.
    J'ai donc pris le temps d'identifier manuellement les arêtes du jeu de test pour valider mon algo. Je me suis aidé d'un petit découpage papier
    pour faciliter l'opération.

    Puis, j'ai fait pareil pour mon jeu de données. C'est le plus long.

    Par exemple pour le Jeu de test, elle donne ceci.
    9,9->9,12L 8,8->5,8D
    5,5->8,5U 9,1->9,4L
    13,9->16,9U 12,8->12,5R
    1,5->4,5U 12,1->9,1U
    4,8->1,8D 9,12->12,12D
    13,12->16,12D 1,8->1,5L
    12,4->12,1R 16,9->16,12R

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Scanner;
    public class A2022D22V2 {
     public static List<String> memory = new ArrayList<>();
     public static class Point {
     int x;
     int y;
     public String toString() {
     return x + "," + y;
     }
     }
     public static class Segment {
     Point start;
     Point end;
     Direction direction;
     Segment(String part) {
     String[] points= part.split("->");
     start = parsePoint(points[0]);
     end = parsePoint(points[1].substring(0, points[1].length()-1));
     char dir = points[1].charAt(points[1].length()-1);
     direction = Arrays.stream(Direction.values()).filter(a->a.name().charAt(0) == dir).findAny().orElse(null);
     }
     public Point parsePoint(String point) {
     String[] coords = point.split(",");
     Point p = new Point();
     p.x = Integer.valueOf(coords[0]); 
     p.x -= 1 ;
     p.y = Integer.valueOf(coords[1]);
     p.y -= 1;
     return p;
     }
     public String toString() {
     return start + " -> " + end + " " + direction;
     }
     public boolean include(int s, int v, int e) {
     if(s > e) {
     int t = s;
     s = e;
     e = t;
     }
     return s <= v && v <= e;
     }
     public boolean include(A2022D22V2.Point p) {
     return include(start.x, p.x, end.x)
     && include(start.y, p.y , end.y); 
     }
     }
     public static class Arrete {
     Segment a; 
     Segment b;
     public Arrete(String row) {
     String[] part = row.split(" ");
     a = new Segment(part[0]);
     b = new Segment(part[1]); 
     } 
     public String toString() {
     return a + " <=> " + b;
     }
     public static int normalize(int dx) {
     if(dx < 0 ) {
     return -1;
     }
     if(dx == 0) {
     return 0;
     }
     return 1;
     }
     public boolean moveTo(List<String> rowMap, A2022D22V2.Segment src, A2022D22V2.Segment dst, Monkey p) {
     int dx = normalize(src.end.x - src.start.x);
     int dy = normalize(src.end.y - src.start.y);
     int sx = src.start.x;
     int sy = src.start.y;
     int i = 0;
     while (sx != p.x || sy != p.y) {
     sx += dx;
     sy += dy;
     i++;
     }
     dx = normalize(dst.end.x - dst.start.x);
     dy = normalize(dst.end.y - dst.start.y);
     sx = dst.start.x;
     sy = dst.start.y;
     while(i > 0) {
     sx += dx;
     sy += dy;
     i--; 
     }
     System.out.println("Move:" + p.x + "," + p.y);
     System.out.println("====>" + sx + "," + sy);
     if(getChar(rowMap, sx ,sy) == '#') {
     System.out.println("Blocked");
     return false;
     }
     System.out.println("Ok!");
     p.x = sx;
     p.y = sy;
     p.direction = dst.direction.left().left();
     displayMap(p, true);
     return true;
     }
     }
     public enum Direction {
     RIGHT(1, 0, "UP", "DOWN",0),
     DOWN(0, 1, "RIGHT", "LEFT", 1),
     LEFT(-1,0, "DOWN", "UP", 2),
     UP(0, -1, "LEFT", "RIGHT", 3),
     ;
     int dx;
     int dy;
     int score;
     String rotateLeft;
     String rotateRight;
     private Direction(int dx, int dy, String rotateLeft,String rotateRight, int score) {
     this.dx = dx;
     this.dy = dy;
     this.rotateLeft = rotateLeft;
     this.rotateRight = rotateRight;
     this.score =score;
     };
     Direction left() {
     return Direction.valueOf(rotateLeft);
     }
     Direction right() {
     return Direction.valueOf(rotateRight);
     }
     }
     public static class Monkey extends Point{
     Direction direction = Direction.RIGHT;
     }
     public static void main(String[] args) {
     step1();
     }
     private static void step1() {
     List<String> rowMap = new ArrayList<>();
     String path = "";
     int maxWidth = 0;
     try (Scanner in = new Scanner(A2022D22V2.class.getResourceAsStream("/res/i22V2.txt"))) { 
     while (in.hasNext()) {
     String row = in.nextLine();
     if(row.isEmpty()) {
     path = in.nextLine();
     } else {
     maxWidth = Math.max(maxWidth, row.length());
     rowMap.add(row);
     }
     }
     }
     List<Arrete> arretes = new ArrayList<>();
     try (Scanner in = new Scanner(A2022D22V2.class.getResourceAsStream("/res/i22V2rf.txt"))) {
     boolean parse = false;
     while (in.hasNext()) {
     String row = in.nextLine().trim();
     if("Parse".equals(row)) {
     parse = true;
     } else if(parse) {
     System.out.println(row);
     Arrete arrete =new Arrete(row);
     arretes.add(arrete);
     System.out.println(arrete);
     }
     }
     }
     normalizeMap(rowMap, maxWidth);
     Monkey monkey = new Monkey();
     monkey.x = rowMap.get(0).indexOf("."); 
     monkey.y = 0;
     System.out.println(path);
     int index = 0;
     while(index < path.length() && index >= 0) {
     if(path.charAt(index) == 'L') {
     System.out.println("Left");
     monkey.direction = monkey.direction.left();
     index+=1;
     } else if(path.charAt(index) == 'R') {
     System.out.println("Right");
     monkey.direction = monkey.direction.right();
     index+=1;
     } else {
     int i = Integer.valueOf(path.substring(index).replaceAll("^([0-9]*).*", "1ドル"));
     System.out.println("Move " + i);
     index += (""+i).length();
     do { 
     if(!translateToValid(rowMap, arretes, monkey)) {
     break;
     } 
     i--;
     displayMap(monkey, false);
     } while(i > 0);
     }
     displayMap(monkey, false);
     }
     displayMap(monkey, true);
     System.out.println(monkey.y+1);
     System.out.println(monkey.x+1);
     System.out.println(monkey.direction);
     int result = (monkey.y+1)*1000+(monkey.x+1)*4+monkey.direction.score;
     System.out.println(result);
     }
     private static void normalizeMap(List<String> rowMap, int maxWidth) {
     for(int y=0;y < rowMap.size();y++) {
     if(rowMap.get(y).length() < maxWidth) {
     String row = rowMap.get(y);
     while(row.length() < maxWidth) {
     row += " ";
     }
     rowMap.set(y, row);
     }
     memory.add(new String(rowMap.get(y)));
     }
     }
     public static char getChar(List<String> rowMap, int sx, int sy) {
     String row = rowMap.get(sy);
     return row.charAt(sx);
     }
     private static boolean translateToValid(List<String> rowMap, List<Arrete> arretes, Monkey monkey) { 
     for(Arrete arrete: arretes) { 
     //System.out.println(arrete.a);
     if(arrete.a.include(monkey) && arrete.a.direction == monkey.direction) {
     System.out.println(arrete.a + " " + monkey.direction);
     return arrete.moveTo(rowMap, arrete.a, arrete.b, monkey);
     }
     if(arrete.b.include(monkey) && arrete.b.direction == monkey.direction) {
     System.out.println(arrete.b + " " + monkey.direction);
     return arrete.moveTo(rowMap, arrete.b, arrete.a, monkey);
     }
     }
     if(getChar(rowMap, monkey.x + monkey.direction.dx , monkey.y + monkey.direction.dy) == '#') {
     return false;
     }
     monkey.x += monkey.direction.dx;
     monkey.y += monkey.direction.dy;
     return true;
     }
     private static void displayMap(Monkey monkey, boolean print) {
     System.out.println();
     for(int y=0;y < memory.size();y++) {
     String row = memory.get(y);
     for(int x=0;x < row.length(); x++) {
     if(monkey.x == x && monkey.y== y) {
     char c;
     switch (monkey.direction) {
     case UP: 
     c = '^';
     break; 
     case DOWN: 
     c = 'u';
     break; 
     case RIGHT: 
     c = '>';
     break; 
     case LEFT: 
     c = '<';
     break; 
     default:
     throw new IllegalArgumentException("Unexpected value: " + monkey.direction);
     }
     if(print) System.out.print(c);
     StringBuilder sb = new StringBuilder(row);
     sb.setCharAt(x, c);
     row = sb.toString();
     memory.set(y, row);
     } else {
     if(print) System.out.print(row.charAt(x));
     }
     }
     if(print) System.out.println();
     }
     }
    }