Jump to content

I'm a software engineer, get me out of here: Difference between revisions

New post.
m (→‎{{header|Wren}}: Minor tidy)
(New post.)
Line 765:
20 14
22 12</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
 
public final class ImASoftwareEngineerGetMeOutOfHere {
 
public static void main(String[] args) {
searchFromCell(11, 11);
showShortestRoutesToSafety();
System.out.println();
searchFromCell(21, 11);
System.out.println("The shortest route from (21, 11) to (1, 11):");
System.out.println(createRouteToCell(1, 11));
System.out.println();
searchFromCell(1, 11);
System.out.println("The shortest route from (1, 11) to (21, 11):");
System.out.println(createRouteToCell(21, 11));
System.out.println();
searchFromCell(11, 11);
showUnreachableCells();
System.out.println();
showCellsWithLongestRouteFromHQ();
}
private static void searchFromCell(int row, int col) {
routes = IntStream.range(0, HEIGHT).boxed()
.map( i -> new ArrayList<CellWithCost>(Collections.nCopies(WIDTH, CellWithCost.ZERO)) )
.collect(Collectors.toList());
routes.get(row).set(col, new CellWithCost(row, col, 0));
List<CellWithCost> route = new ArrayList<CellWithCost>();
int cost = 0;
while ( true ) {
final int startDigit = Character.digit(GMOOH[row].charAt(col), 10);
for ( Cell direction : directions ) {
final int nextRow = row + startDigit * direction.row;
final int nextCol = col + startDigit * direction.col;
if ( nextCol >= 0 && nextCol < WIDTH && nextRow >= 0 && nextRow < HEIGHT
&& Character.digit(GMOOH[nextRow].charAt(nextCol), 10) >= 0 ) {
CellWithCost currentCell = routes.get(nextRow).get(nextCol);
if ( currentCell.equals(CellWithCost.ZERO) || currentCell.cost > cost + 1 ) {
routes.get(nextRow).set(nextCol, new CellWithCost(row, col, cost + 1));
if ( Character.digit(GMOOH[nextRow].charAt(nextCol), 10) > 0 ) {
route.addLast( new CellWithCost(nextRow, nextCol, cost + 1) );
}
}
}
}
if ( route.isEmpty() ) { // All routes have been searched
break;
}
CellWithCost nextCell = route.removeFirst();
row = nextCell.fromRow;
col = nextCell.fromCol;
cost = nextCell.cost;
}
}
private static void showShortestRoutesToSafety() {
int minimumCost = Integer.MAX_VALUE;
List<Cell> cells = new ArrayList<Cell>();
for ( int col = 0; col < WIDTH; col++ ) {
for ( int row = 0; row < HEIGHT; row++ ) {
if ( Character.digit(GMOOH[row].charAt(col), 10) == 0 ) {
CellWithCost currentCell = routes.get(row).get(col);
if ( ! currentCell.equals(CellWithCost.ZERO) ) {
final int cost = currentCell.cost;
if ( cost <= minimumCost ) {
if ( cost < minimumCost ) {
cells.clear();
minimumCost = cost;
}
cells.addLast( new Cell(row, col) );
}
}
}
}
}
String areIs = ( cells.size() > 1 ) ? "are " : "is ";
String plural = ( cells.size() > 1 ) ? "s" : "";
System.out.println("There " + areIs + cells.size() + " shortest route" + plural
+ " of " + minimumCost + " days to safety:");
for ( Cell cell : cells ) {
System.out.println(createRouteToCell(cell.row, cell.col));
}
}
private static List<Cell> createRouteToCell(int row, int col) {
List<Cell> route = new ArrayList<Cell>();
route.addLast( new Cell(row, col) );
while ( true ) {
CellWithCost currentCell = routes.get(row).get(col);
if ( currentCell.cost == 0 ) {
break;
}
row = currentCell.fromRow;
col = currentCell.fromCol;
route.addFirst( new Cell(row, col) );
}
return route;
}
private static void showUnreachableCells() {
List<Cell> unreachableCells = new ArrayList<Cell>();
for ( int col = 0; col < WIDTH; col++ ) {
for ( int row = 0; row < HEIGHT; row++ ) {
if ( Character.digit(GMOOH[row].charAt(col), 10) >= 0
&& routes.get(row).get(col).equals(CellWithCost.ZERO) ) {
unreachableCells.addLast( new Cell(row, col) );
}
}
}
System.out.println("The following cells are unreachable:");
System.out.println(unreachableCells);
}
private static void showCellsWithLongestRouteFromHQ() {
int maximumCost = Integer.MIN_VALUE;
List<Cell> cells = new ArrayList<Cell>();
for ( int col = 0; col < WIDTH; col++ ) {
for ( int row = 0; row < HEIGHT; row++ ) {
if ( Character.digit(GMOOH[row].charAt(col), 10) >= 0 ) {
CellWithCost currentCell = routes.get(row).get(col);
if ( ! currentCell.equals(CellWithCost.ZERO) ) {
final int cost = currentCell.cost;
if ( cost >= maximumCost) {
if ( cost > maximumCost ) {
cells.clear();
maximumCost = cost;
}
cells.addLast( new Cell(row, col) );
}
}
}
}
}
System.out.println("There are " + cells.size() + " cells that require " + maximumCost
+ " days to receive reinforcements from HQ:");
for ( Cell cell : cells ) {
System.out.println(createRouteToCell(cell.row, cell.col));
}
}
private static List<List<CellWithCost>> routes;
private static final List<Cell> directions = List.of( new Cell(1, -1), new Cell(1, 0), new Cell(1, 1),
new Cell(0, -1), new Cell(0, 1),
new Cell(-1, -1), new Cell(-1, 0), new Cell(-1, 1) );
private static record CellWithCost(int fromRow, int fromCol, int cost) {
public boolean equals(CellWithCost other) {
return cost == other.cost && fromRow == other.fromRow && fromCol == other.fromCol;
}
public static CellWithCost ZERO = new CellWithCost(0, 0, 0);
}
private static record Cell(int row, int col) {
public String toString() {
return "(" + row + ", " + col + ")";
}
}
private static final String[] GMOOH = """
.........00000.........
......00003130000......
....000321322221000....
...00231222432132200...
..0041433223233211100..
..0232231612142618530..
.003152122326114121200.
.031252235216111132210.
.022211246332311115210.
00113232262121317213200
03152118212313211411110
03231234121132221411410
03513213411311414112320
00427534125412213211400
.013322444412122123210.
.015132331312411123120.
.003333612214233913300.
..0219126511415312570..
..0021321524341325100..
...00211415413523200...
....000122111322000....
......00001120000......
.........00000.........
""".split("\n");
 
private static final int WIDTH = GMOOH[0].length();
private static final int HEIGHT = GMOOH.length;
 
}
</syntaxhighlight>
{{ out }}
<pre>
There are 40 shortest routes of 4 days to safety:
[(11, 11), (11, 12), (8, 9), (14, 3), (11, 0)]
[(11, 11), (10, 11), (7, 8), (7, 5), (12, 0)]
[(11, 11), (12, 10), (13, 10), (13, 5), (13, 0)]
[(11, 11), (11, 12), (8, 9), (8, 3), (6, 1)]
[(11, 11), (11, 12), (8, 9), (8, 3), (8, 1)]
[(11, 11), (12, 11), (12, 8), (12, 4), (9, 1)]
[(11, 11), (12, 11), (12, 8), (16, 4), (13, 1)]
[(11, 11), (12, 11), (12, 8), (12, 4), (15, 1)]
[(11, 11), (12, 11), (12, 8), (16, 4), (16, 1)]
[(11, 11), (12, 11), (12, 8), (8, 4), (6, 2)]
[(11, 11), (12, 11), (15, 8), (15, 5), (18, 2)]
[(11, 11), (12, 10), (11, 9), (9, 9), (3, 3)]
[(11, 11), (10, 11), (13, 8), (14, 7), (18, 3)]
[(11, 11), (10, 10), (8, 10), (5, 7), (2, 4)]
[(11, 11), (10, 11), (7, 8), (4, 5), (3, 4)]
[(11, 11), (12, 10), (13, 10), (18, 5), (19, 4)]
[(11, 11), (10, 11), (7, 8), (7, 5), (2, 5)]
[(11, 11), (10, 11), (7, 11), (7, 12), (1, 6)]
[(11, 11), (11, 12), (8, 9), (2, 9), (1, 8)]
[(11, 11), (11, 12), (8, 9), (2, 9), (1, 9)]
[(11, 11), (11, 12), (14, 9), (18, 13), (22, 9)]
[(11, 11), (12, 11), (15, 8), (18, 11), (22, 11)]
[(11, 11), (11, 12), (8, 12), (6, 12), (0, 12)]
[(11, 11), (10, 10), (8, 10), (5, 13), (1, 13)]
[(11, 11), (11, 12), (14, 9), (18, 13), (22, 13)]
[(11, 11), (11, 12), (8, 9), (2, 15), (1, 14)]
[(11, 11), (11, 12), (8, 9), (2, 15), (1, 15)]
[(11, 11), (12, 10), (13, 10), (18, 15), (21, 15)]
[(11, 11), (11, 12), (8, 9), (2, 15), (1, 16)]
[(11, 11), (11, 12), (8, 9), (2, 15), (2, 16)]
[(11, 11), (12, 11), (15, 11), (16, 12), (20, 16)]
[(11, 11), (12, 11), (12, 14), (8, 18), (3, 18)]
[(11, 11), (12, 11), (15, 14), (16, 15), (19, 18)]
[(11, 11), (12, 10), (13, 11), (17, 15), (20, 18)]
[(11, 11), (12, 11), (9, 14), (6, 17), (4, 19)]
[(11, 11), (10, 11), (10, 14), (12, 16), (16, 20)]
[(11, 11), (11, 12), (11, 15), (11, 17), (7, 21)]
[(11, 11), (12, 11), (12, 14), (16, 18), (13, 21)]
[(11, 11), (11, 12), (11, 15), (11, 17), (15, 21)]
[(11, 11), (12, 11), (12, 14), (16, 18), (16, 21)]
 
The shortest route from (21, 11) to (1, 11):
[(21, 11), (21, 10), (20, 9), (18, 9), (13, 4), (6, 11), (4, 11), (1, 11)]
 
The shortest route from (1, 11) to (21, 11):
[(1, 11), (2, 10), (5, 13), (9, 9), (15, 3), (20, 8), (20, 10), (21, 11)]
 
The following cells are unreachable:
[(4, 3), (2, 18), (18, 20)]
 
There are 5 cells that require 6 days to receive reinforcements from HQ:
[(11, 11), (12, 10), (13, 10), (18, 10), (20, 10), (21, 11), (22, 12)]
[(11, 11), (11, 12), (14, 15), (16, 17), (17, 16), (18, 16), (20, 14)]
[(11, 11), (12, 11), (9, 14), (6, 17), (4, 17), (4, 18), (3, 19)]
[(11, 11), (12, 11), (9, 14), (9, 17), (7, 17), (7, 20), (6, 20)]
[(11, 11), (12, 11), (12, 14), (12, 18), (13, 19), (13, 20), (17, 20)]
</pre>
 
=={{header|Julia}}==
915

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.