Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.16 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.math.*;
  4.  
  5. class Cell {
  6.     int x;
  7.     int y;
  8.     char type;
  9.  
  10.     public Cell(int x, int y, char type) {
  11.         this.x = x;
  12.         this.y = y;
  13.         this.type = type;
  14.     }
  15.  
  16.     @Override
  17.     public String toString() {
  18.         return "(" + this.x + ", " + this.y + ", " + this.type + ")";
  19.     }
  20. }
  21.  
  22. class RobotState {
  23.     int x;
  24.     int y;
  25.     char dir;
  26.  
  27.     public RobotState(int x, int y, char dir) {
  28.         this.x = x;
  29.         this.y = y;
  30.         this.dir = dir;
  31.     }
  32.  
  33.     @Override
  34.     public boolean equals(Object o) {
  35.         if (o == this) {
  36.             return true;
  37.         }
  38.  
  39.         if (!(o instanceof RobotState)) {
  40.             return false;
  41.         }
  42.  
  43.         RobotState r = (RobotState)o;
  44.  
  45.         return (r.x == this.x && r.y == this.y && r.dir == this.dir);
  46.     }
  47.  
  48.     @Override
  49.     public int hashCode() {
  50.         int result = 17;
  51.         result = 31 * result + this.x;
  52.         result = 31 * result + this.y;
  53.         result = 31 * result + this.dir;
  54.  
  55.         return result;
  56.     }
  57. }
  58.  
  59. class Arrow {
  60.     int x;
  61.     int y;
  62.     char dir;
  63.  
  64.     public Arrow(int x, int y, char dir) {
  65.         this.x = x;
  66.         this.y = y;
  67.         this.dir = dir;
  68.     }
  69.  
  70.     public Arrow(int x, int y, int dir) {
  71.         this.x = x;
  72.         this.y = y;
  73.        
  74.         switch (dir) {
  75.             case 0:
  76.                 this.dir = 'U';
  77.                 break;
  78.             case 1:
  79.                 this.dir = 'D';
  80.                 break;
  81.             case 2:
  82.                 this.dir = 'L';
  83.                 break;
  84.             case 3:
  85.                 this.dir = 'R';
  86.                 break;
  87.             case 4:
  88.                 this.dir = '.';
  89.             default:
  90.                 this.dir = '.';
  91.                 break;
  92.         }
  93.     }
  94.  
  95.     @Override
  96.     public String toString() {
  97.         return this.x + " " + this.y + " " + this.dir;
  98.     }
  99.  
  100.     @Override
  101.     public boolean equals(Object o) {
  102.         if (o == this) {
  103.             return true;
  104.         }
  105.  
  106.         if (!(o instanceof Arrow)) {
  107.             return false;
  108.         }
  109.  
  110.         Arrow r = (Arrow)o;
  111.  
  112.         return (r.x == this.x && r.y == this.y);
  113.     }
  114.  
  115.     @Override
  116.     public int hashCode() {
  117.         int result = 19;
  118.         result = 37 * result + this.x;
  119.         result = 37 * result + this.y;
  120.  
  121.         return result;
  122.     }
  123. }
  124.  
  125. class Robot {
  126.     int x;
  127.     int y;
  128.     char dir;
  129.     boolean active = true;
  130.     ArrayList<RobotState> states = new ArrayList<RobotState>();
  131.  
  132.     public Robot(int x, int y, char dir) {
  133.         this.x = x;
  134.         this.y = y;
  135.         this.dir = dir;
  136.     }
  137.  
  138.     public void addState(RobotState rs) {
  139.         states.add(rs);
  140.     }
  141.  
  142.     public boolean visitedBefore(RobotState rs) {
  143.         return states.contains(rs);
  144.     }
  145.  
  146.     public void kill() {
  147.         this.active = false;
  148.     }
  149.  
  150. }
  151.  
  152. class Simulation {
  153.     Grid grid;
  154.     int simsRun = 0;
  155.  
  156.     public Simulation(Grid grid) {
  157.         this.grid = grid;
  158.     }
  159.  
  160.     public int getNumValidCells() {
  161.         return this.grid.validCells.size();
  162.     }
  163.  
  164.     public Cell getValidCell(int index) {
  165.         return this.grid.validCells.get(index);
  166.     }
  167.  
  168.     public String getArrowString() {
  169.         return this.grid.getArrows();
  170.     }
  171.  
  172.     public void addArrowsToGrid(ArrayList<Arrow> arrows) {
  173.         for (Arrow a : arrows) {
  174.             this.grid.addArrow(a);
  175.         }
  176.     }
  177.  
  178.     public void reset() {
  179.         this.grid.reset();
  180.     }
  181.  
  182.     public int play() {
  183.         while(this.grid.takeTurn()) {}
  184.         simsRun++;
  185.         return this.grid.score;
  186.     }
  187. }
  188.  
  189. class Grid {
  190.     Cell[][] cells = new Cell[10][19];
  191.     Cell[][] uneditedCells = new Cell[10][19];
  192.     ArrayList<Robot> robots = new ArrayList<Robot>();
  193.     ArrayList<Robot> uneditedRobots = new ArrayList<Robot>();
  194.     ArrayList<Cell> validCells = new ArrayList<Cell>();
  195.     ArrayList<Cell> allCells = new ArrayList<Cell>();
  196.     ArrayList<Arrow> arrows = new ArrayList<Arrow>();
  197.     int turn = 0;
  198.     int score = 0;
  199.    
  200.     public int getValidCells() {
  201.         return this.validCells.size();
  202.     }
  203.  
  204.     public void addCell(Cell cell) {
  205.         this.cells[cell.y][cell.x] = cell;
  206.         this.uneditedCells[cell.y][cell.x] = new Cell(cell.x, cell.y, cell.type);
  207.         this.allCells.add(cell);
  208.         if (cell.type == '.') {
  209.             this.validCells.add(cell);
  210.         }
  211.     }
  212.  
  213.     public void reset() {
  214.         //System.err.println("Reset() called");
  215.         for (int y = 0; y < 10; y++) {
  216.             for (int x = 0; x < 19; x++) {
  217.                 this.cells[y][x].x = this.uneditedCells[y][x].x;
  218.                 this.cells[y][x].y = this.uneditedCells[y][x].y;
  219.                 this.cells[y][x].type = this.uneditedCells[y][x].type;
  220.             }
  221.         }
  222.         for (Robot r : this.uneditedRobots) {
  223.             this.robots.add(new Robot(r.x, r.y, r.dir));
  224.         }
  225.         this.turn = 0;
  226.         this.score = 0;
  227.         this.arrows.clear();
  228.  
  229.         //System.err.println(this.cells);
  230.     }
  231.  
  232.     public void addRobot(Robot robot) {
  233.         this.uneditedRobots.add(new Robot(robot.x, robot.y, robot.dir));
  234.         this.robots.add(robot);
  235.     }
  236.  
  237.     public void addArrow(Arrow arrow) {
  238.         this.cells[arrow.y][arrow.x].type = arrow.dir;
  239.         this.arrows.add(arrow);
  240.     }
  241.  
  242.     public String getArrows() {
  243.         String res = "";
  244.         for (int i = 0; i < this.arrows.size(); i++) {
  245.             if (i == 0) {
  246.                 res += this.arrows.get(i).toString();
  247.             } else {
  248.                 res += " " + this.arrows.get(i).toString();
  249.             }
  250.         }
  251.         return res;
  252.     }
  253.  
  254.     public boolean takeTurn() {
  255.         Iterator<Robot> robotIter = this.robots.iterator();
  256.         //Increment score based on number of robots still active
  257.         this.score += this.robots.size();
  258.        
  259.  
  260.         //Increment turn by one
  261.         this.turn++;
  262.  
  263.         // System.err.println("--------- " + this.turn + " ------------");
  264.         // System.err.println("Size:  " + this.robots.size());
  265.         // System.err.println("Score: " + this.score);
  266.         // System.err.println();
  267.         //Temp Cell
  268.         Cell c;
  269.  
  270.         //If it is the first turn, let robots change dir if they are on an arrow
  271.         if (this.turn == 1) {
  272.             while (robotIter.hasNext()) {
  273.                 Robot robot = robotIter.next();
  274.                 RobotState firstState = new RobotState(robot.x, robot.y, robot.dir);
  275.                 robot.addState(firstState);
  276.                 c = this.cells[robot.y][robot.x];
  277.                 if (c.type != '.') {
  278.                     robot.dir = c.type;
  279.                 }
  280.             }
  281.         }
  282.  
  283.         //Move robots by one cell in the dir they are facing, wrap to other side if off-grid
  284.         for (Robot robot : this.robots) {
  285.             switch (robot.dir) {
  286.                 case 'U':
  287.                     robot.y--;
  288.                     if (robot.y < 0) {
  289.                         robot.y = 9;
  290.                     }
  291.                     break;
  292.                 case 'D':
  293.                     robot.y++;
  294.                     if (robot.y > 9) {
  295.                         robot.y = 0;
  296.                     }
  297.                     break;
  298.                 case 'L':
  299.                     robot.x--;
  300.                     if (robot.x < 0) {
  301.                         robot.x = 18;
  302.                     }
  303.                     break;
  304.                 case 'R':
  305.                     robot.x++;
  306.                     if (robot.x > 18) {
  307.                         robot.x = 0;
  308.                     }
  309.                     break;
  310.                 default:
  311.                     break;
  312.             }
  313.         }
  314.         robotIter = this.robots.iterator();
  315.         //Change dir of robots if they are on an arrow
  316.         while (robotIter.hasNext()) {
  317.             Robot robot = robotIter.next();
  318.             c = this.cells[robot.y][robot.x];
  319.             if (c.type != '.') {
  320.                 robot.dir = c.type;
  321.             }
  322.         }
  323.  
  324.         RobotState currState;
  325.         robotIter = this.robots.iterator();
  326.         //Mark robots for death
  327.         while (robotIter.hasNext()) {
  328.             Robot robot = robotIter.next();
  329.             c = this.cells[robot.y][robot.x];
  330.             currState = new RobotState(robot.x, robot.y, robot.dir);
  331.             //System.err.println(c);
  332.             if (c.type == '#') {
  333.                 robot.kill();
  334.                 //System.err.println("Robot Fell");
  335.             } else if (robot.states.contains(currState)) {
  336.                 robot.kill();
  337.                 //System.err.println("Robot Repeated");
  338.             } else {
  339.                 robot.addState(currState);
  340.                 //System.err.println(robot.states.size());
  341.             }
  342.         }
  343.         ArrayList<Robot> tempRobots = new ArrayList<Robot>();
  344.  
  345.         for (Robot r : this.robots) {
  346.             if (r.active) {
  347.                 tempRobots.add(r);
  348.             }
  349.         }
  350.         this.robots = tempRobots;
  351.         //System.err.println(this.score);
  352.         // if (!(this.robots.size() != 0)) {
  353.         //     System.err.println("---New Sim---");
  354.         // }
  355.         return this.robots.size() != 0;
  356.     }
  357.  
  358. }
  359.  
  360. class Manager {
  361.     Simulation sim;
  362.     Grid grid;
  363.     long startTime;
  364.    
  365.    
  366.     public Manager(Grid grid, long startTime) {
  367.         this.grid = grid;
  368.         this.startTime = startTime;
  369.         this.sim = new Simulation(this.grid);
  370.     }
  371.  
  372.     public void putArrowsOnGrid(ArrayList<Arrow> arrows) {
  373.         this.sim.addArrowsToGrid(arrows);
  374.     }
  375.  
  376.     public String getArrowString() {
  377.         return this.sim.getArrowString();
  378.     }
  379.  
  380.     public ArrayList<State> generateNewStates(State state) {
  381.         ArrayList<State> newStates = new ArrayList<State>();
  382.         Random random = new Random();
  383.         int chance1 = 0;
  384.        
  385.         ArrayList<Arrow> tempArrows;
  386.         int randIndex;
  387.         Arrow lookArrow;
  388.  
  389.         boolean add = false;
  390.  
  391.  
  392.         int size = state.arrows.size();
  393.         if (size == 0) {
  394.             add = true;
  395.         }
  396.         Arrow tempArrow;
  397.         for (int i = 0; i < 30; i++) {
  398.             state.shuffle();
  399.             chance1 = random.nextInt(100);
  400.             tempArrows = new ArrayList<Arrow>();
  401.  
  402.             //Flip one arrow, possibly delete it.
  403.             if (chance1 < 30 && !add) {
  404.                 randIndex = random.nextInt(size);
  405.                 for (int a = 0; a < size; a++) {
  406.                     tempArrow = state.arrows.get(a);
  407.                     if (a != randIndex) {
  408.                         tempArrows.add(new Arrow(tempArrow.x, tempArrow.y, tempArrow.dir));
  409.                     } else {
  410.                         tempArrows.add(new Arrow(tempArrow.x, tempArrow.y, random.nextInt(10)));
  411.                     }
  412.                 }
  413.             }
  414.            
  415.             //Flip 2 arrows, possibly delete them.
  416.             else if (chance1 < 60 && !add) {
  417.                 randIndex = random.nextInt(size);
  418.                 for (int a = 0; a < size; a++) {
  419.                     tempArrow = state.arrows.get(a);
  420.                     if (a != randIndex && a != randIndex + 1) {
  421.                         tempArrows.add(new Arrow(tempArrow.x, tempArrow.y, tempArrow.dir));
  422.                     } else {
  423.                         tempArrows.add(new Arrow(tempArrow.x, tempArrow.y, random.nextInt(10)));
  424.                     }
  425.                 }
  426.             }
  427.  
  428.             //Add an arrow, possibly
  429.             else if (chance1 < 80 && !add) {
  430.                 randIndex = random.nextInt(state.grid.getValidCells());
  431.                 for (int a = 0; a < size; a++) {
  432.                     tempArrow = state.arrows.get(a);
  433.                     tempArrows.add(new Arrow(tempArrow.x, tempArrow.y, tempArrow.dir));
  434.                 }
  435.                
  436.                 randIndex = random.nextInt(state.grid.getValidCells());
  437.                 lookArrow = new Arrow(state.grid.validCells.get(randIndex).x, state.grid.validCells.get(randIndex).y, random.nextInt(10));
  438.                 if (!tempArrows.contains(lookArrow) && lookArrow.dir != '.') {
  439.                     tempArrows.add(lookArrow);
  440.                 }
  441.             }
  442.  
  443.             //Add up to 5 arrows
  444.             else {
  445.  
  446.                 for (int a = 0; a < size; a++) {
  447.                     tempArrow = state.arrows.get(a);
  448.                     tempArrows.add(new Arrow(tempArrow.x, tempArrow.y, tempArrow.dir));
  449.                 }
  450.                 for (int r = 0; r < 5; r++) {
  451.                     randIndex = random.nextInt(state.grid.getValidCells());
  452.                     lookArrow = new Arrow(state.grid.validCells.get(randIndex).x, state.grid.validCells.get(randIndex).y, random.nextInt(10));
  453.                     if (!tempArrows.contains(lookArrow) && lookArrow.dir != '.') {
  454.                         tempArrows.add(lookArrow);
  455.                     }
  456.                 }
  457.             }
  458.  
  459.             newStates.add(new State(this.sim.grid, tempArrows, this.scoreArrows(tempArrows)));
  460.            
  461.         }
  462.        
  463.         return newStates;
  464.     }
  465.  
  466.     public int scoreArrows(ArrayList<Arrow> arrows) {
  467.         this.sim.reset();
  468.         this.putArrowsOnGrid(arrows);
  469.         int score = this.sim.play();
  470.         //System.err.println(score);
  471.         return score;
  472.     }
  473.  
  474.     public ScoreAndString getHillClimbArrows(long startTime) {
  475.         String bestArrowString = "";
  476.         this.sim.reset();
  477.         State initialState = new State(this.sim.grid, this.generateRandArrows(), this.sim.play());
  478.         ArrayList<State> newStates = this.generateNewStates(initialState);
  479.         State bestNewState = this.getBestState(newStates);
  480.  
  481.         while(System.currentTimeMillis() - startTime < 490) {
  482.             newStates = this.generateNewStates(bestNewState);
  483.             bestNewState = this.getBestState(newStates);
  484.         }
  485.  
  486.         this.sim.reset();
  487.         this.putArrowsOnGrid(bestNewState.arrows);
  488.         bestArrowString = this.sim.grid.getArrows();
  489.  
  490.         ScoreAndString sas = new ScoreAndString(bestArrowString, this.sim.play());
  491.  
  492.         this.sim.reset();
  493.         return sas;
  494.     }
  495.  
  496.     public State getBestState(ArrayList<State> states) {
  497.         Collections.sort(states);
  498.         return states.get(states.size() - 1);
  499.     }
  500.  
  501.     public ArrayList<Arrow> generateRandArrows() {
  502.         ArrayList<Arrow> arrows = new ArrayList<Arrow>();
  503.         Random random = new Random();
  504.        
  505.         int numValidCells = this.sim.getNumValidCells();
  506.  
  507.         for (int i = 0; i < random.nextInt(numValidCells); i++) {
  508.             Cell c = this.sim.getValidCell(random.nextInt(numValidCells));
  509.             arrows.add(new Arrow(c.x, c.y, random.nextInt(4)));
  510.         }
  511.        
  512.         return arrows;
  513.        
  514.     }
  515.  
  516. }
  517.  
  518. class ScoreAndString {
  519.     String arrowString;
  520.     int score;
  521.     public ScoreAndString(String aString, int score) {
  522.         this.arrowString = aString;
  523.         this.score = score;
  524.     }
  525. }
  526.  
  527. class State  implements Comparable<State> {
  528.     ArrayList<Arrow> tempArrows;
  529.     ArrayList<Arrow> arrows = new ArrayList<Arrow>();
  530.     int score;
  531.     Grid grid;
  532.     public State(Grid grid, ArrayList<Arrow> arrows, int score) {
  533.         this.grid = grid;
  534.         this.tempArrows = arrows;
  535.         this.score = score;
  536.  
  537.         for (int i = 0; i < this.tempArrows.size(); i++) {
  538.             Arrow tempArrow = this.tempArrows.get(i);
  539.             if (tempArrow.dir != '.') {
  540.                 this.arrows.add(new Arrow(tempArrow.x, tempArrow.y, tempArrow.dir));
  541.             }
  542.         }
  543.     }
  544.  
  545.     public void shuffle() {
  546.         Collections.shuffle(this.arrows);
  547.     }
  548.  
  549.     public int compareTo(State anotherInstance) {
  550.         return this.score - anotherInstance.score;
  551.     }
  552. }
  553.  
  554.  
  555. class Player {
  556.  
  557.     public static void main(String args[]) {
  558.         Scanner in = new Scanner(System.in);
  559.         Grid grid = new Grid();
  560.  
  561.         final long startTime = System.currentTimeMillis();
  562.         for (int y = 0; y < 10; y++) {
  563.             String line = in.nextLine();
  564.             char[] charArr = line.toCharArray();
  565.  
  566.             for (int x = 0; x < charArr.length; x++) {
  567.                 Cell c = new Cell(x, y, charArr[x]);
  568.  
  569.                 grid.addCell(c);
  570.  
  571.             }
  572.         }
  573.         int robotCount = in.nextInt();
  574.         for (int i = 0; i < robotCount; i++) {
  575.             int x = in.nextInt();
  576.             int y = in.nextInt();
  577.             String direction = in.next();
  578.  
  579.             Robot r = new Robot(x, y, direction.charAt(0));
  580.  
  581.             grid.addRobot(r);
  582.         }
  583.  
  584.         Manager manager = new Manager(grid, startTime);
  585.  
  586.         String bestArrows = "";
  587.         int bestScore = 1;
  588.         ScoreAndString sas;
  589.         for (int i = 0; i < 2; i++) {
  590.             sas = manager.getHillClimbArrows(System.currentTimeMillis());
  591.             System.err.println(sas.score + ": " + sas.arrowString);
  592.             if (sas.score > bestScore) {
  593.                 bestScore = sas.score;
  594.                 bestArrows = sas.arrowString;
  595.             }
  596.         }
  597.         System.out.println(bestArrows);
  598.         final long endTime = System.currentTimeMillis();
  599.  
  600.         System.err.println("Time for program " + (endTime - startTime));
  601.         System.err.println("Sims Run: " + manager.sim.simsRun);
  602.     }
  603. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement