Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static int part2(char[][] grid) {
- int[][] dp = new int[grid.length][grid[0].length];
- for (int i = 1; i < grid.length; i++) {
- for (int j = 0; j < grid[i].length; j++) {
- char current = grid[i][j];
- char above = grid[i-1][j];
- // If we have the starting point above us, then emit the beam.
- if (above == 'S') {
- grid[i][j] = '|';
- dp[i][j] = 1;
- } else if (current == '^' && above == '|') {
- // If we see a splitter and have a beam above us,
- // then split the beam to the left and right of the splitter.
- if (j-1 >= 0) {
- grid[i][j-1] = '|';
- dp[i][j-1] += dp[i-1][j];
- }
- if (j+1 < grid[i].length) {
- grid[i][j+1] = '|';
- dp[i][j+1] += dp[i-1][j];
- }
- } else if (above == '|') {
- // If we're on any other spot and have a beam above us, then continue the beam downward.
- grid[i][j] = '|';
- dp[i][j] += dp[i-1][j];
- }
- }
- }
- // Sum up all the unique paths at the bottom of the grid.
- int total = 0;
- for (int i = 0; i < dp[0].length; i++) {
- total += dp[dp.length-1][i];
- }
- return total;
- }
Advertisement
Add Comment
Please, Sign In to add comment