-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathJava.java
94 lines (76 loc) · 2.55 KB
/
Java.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/****************************************/
/* */
/* CodinGame.com Solutions by pathosDev */
/* */
/* Puzzle: Gravity Tumbler */
/* Difficulty: Easy */
/* Date solved: 09.11.2018 */
/* */
/****************************************/
import java.util.Arrays;
import java.util.Scanner;
public class Solution
{
public static void main(String[] args)
{
//Read inputs.
Scanner scanner = new Scanner(System.in);
String[] inputs = scanner.nextLine().split(" ");
int width = Integer.parseInt(inputs[0]);
int height = Integer.parseInt(inputs[1]);
int count = Integer.parseInt(scanner.nextLine());
//Create empty grid.
int[][] grid = new int[width][];
for (int x = 0; x < width; x++)
{
grid[x] = new int[height];
}
//Fill the grid. Replace blocks with 0 and 1.
for (int y = 0; y < height; y++)
{
String raster = scanner.nextLine();
for (int x = 0; x < width; x++)
{
grid[x][y] = (raster.charAt(x) == '.') ? 0 : 1;
}
}
//Rotate grid <count> times and move blocks down after each rotation.
for (int i = 0; i < count; i++)
{
grid = RotateGrid(grid);
grid = ApplyPhysics(grid);
}
//Print grid.
for (int y = 0; y < grid[0].length; y++)
{
for (int x = 0; x < grid.length; x++)
{
System.out.print(grid[x][y] == 0 ? '.' : '#');
}
System.out.println();
}
}
//Rotates the grid counterclockwise by 90°.
private static int[][] RotateGrid(int[][] grid)
{
int[][] gridRotated = new int[grid[0].length][];
for (int x = 0; x < gridRotated.length; x++)
{
gridRotated[x] = new int[grid.length];
for (int y = 0; y < gridRotated[0].length; y++)
{
gridRotated[x][y] = grid[y][x];
}
}
return gridRotated;
}
//Applies physics to let the blocks fall to the ground.
private static int[][] ApplyPhysics(int[][] grid)
{
for (int x = 0; x < grid.length; x++)
{
Arrays.sort(grid[x]);
}
return grid;
}
}