|
| 1 | +package com.vuryss.aoc.solutions.event2018; |
| 2 | + |
| 3 | +import com.vuryss.aoc.solutions.SolutionInterface; |
| 4 | +import com.vuryss.aoc.util.Point; |
| 5 | + |
| 6 | +import java.lang.Override; |
| 7 | +import java.lang.String; |
| 8 | +import java.lang.SuppressWarnings; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +@SuppressWarnings("unused") |
| 14 | +public class Day18 implements SolutionInterface { |
| 15 | + @Override |
| 16 | + public Map<String, String> part1Tests() { |
| 17 | + return Map.of( |
| 18 | + """ |
| 19 | + .#.#...|#. |
| 20 | + .....#|##| |
| 21 | + .|..|...#. |
| 22 | + ..|#.....# |
| 23 | + #.#|||#|#| |
| 24 | + ...#.||... |
| 25 | + .|....|... |
| 26 | + ||...#|.#| |
| 27 | + |.||||..|. |
| 28 | + ...#.|..|. |
| 29 | + """, |
| 30 | + "1147" |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public Map<String, String> part2Tests() { |
| 36 | + return Map.of(); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public String part1Solution(String input, boolean isTest) { |
| 41 | + return calculateResourceValue(input, 10); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public String part2Solution(String input, boolean isTest) { |
| 46 | + return calculateResourceValue(input, 1000000000); |
| 47 | + } |
| 48 | + |
| 49 | + private int gridHash(char[][] grid) { |
| 50 | + return Arrays.deepHashCode(grid); |
| 51 | + } |
| 52 | + |
| 53 | + private String calculateResourceValue(String input, int minutes) { |
| 54 | + char[][] grid = Arrays.stream(input.trim().split("\n")).map(String::toCharArray).toArray(char[][]::new); |
| 55 | + int size = grid.length; |
| 56 | + Point point; |
| 57 | + var seen = new HashMap<Integer, Integer>(); |
| 58 | + seen.put(gridHash(grid), 0); |
| 59 | + var foundCycle = false; |
| 60 | + |
| 61 | + for (var i = 0; i < minutes; i++) { |
| 62 | + char[][] newGrid = new char[size][size]; |
| 63 | + |
| 64 | + for (var y = 0; y < size; y++) { |
| 65 | + for (var x = 0; x < size; x++) { |
| 66 | + int trees = 0, lumberyards = 0; |
| 67 | + |
| 68 | + for (var p: new Point(x, y).surroundingPoints()) { |
| 69 | + if (p.x < 0 || p.x >= size || p.y < 0 || p.y >= size) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + if (grid[p.y][p.x] == '|') { |
| 74 | + trees++; |
| 75 | + } else if (grid[p.y][p.x] == '#') { |
| 76 | + lumberyards++; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if (grid[y][x] == '.') { |
| 81 | + newGrid[y][x] = trees >= 3 ? '|' : '.'; |
| 82 | + } else if (grid[y][x] == '|') { |
| 83 | + newGrid[y][x] = lumberyards >= 3 ? '#' : '|'; |
| 84 | + } else { |
| 85 | + newGrid[y][x] = lumberyards >= 1 && trees >= 1 ? '#' : '.'; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + grid = newGrid; |
| 91 | + |
| 92 | + if (foundCycle) { |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + if (seen.containsKey(gridHash(grid))) { |
| 97 | + int cycleSize = i - seen.get(gridHash(grid)); |
| 98 | + int minutesLeft = minutes - i; |
| 99 | + int leftFromLastCycle = minutesLeft % cycleSize; |
| 100 | + i = minutes - leftFromLastCycle; |
| 101 | + foundCycle = true; |
| 102 | + } else { |
| 103 | + seen.put(gridHash(grid), i); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + int trees = 0, lumberyards = 0; |
| 108 | + |
| 109 | + for (var row: grid) { |
| 110 | + for (var c: row) { |
| 111 | + if (c == '|') { |
| 112 | + trees++; |
| 113 | + } else if (c == '#') { |
| 114 | + lumberyards++; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return String.valueOf(trees * lumberyards); |
| 120 | + } |
| 121 | +} |
0 commit comments