{"id":14277,"url":"\/distributions\/14277\/click?bit=1&hash=17ce698c744183890278e5e72fb5473eaa8dd0a28fac1d357bd91d8537b18c22","title":"\u041e\u0446\u0438\u0444\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0438\u0442\u0440\u044b \u0431\u0435\u043d\u0437\u0438\u043d\u0430 \u0438\u043b\u0438 \u0437\u043e\u043b\u043e\u0442\u044b\u0435 \u0443\u043a\u0440\u0430\u0448\u0435\u043d\u0438\u044f","buttonText":"\u041a\u0430\u043a?","imageUuid":"771ad34a-9f50-5b0b-bc84-204d36a20025"}

Advent of Code 2022: Day 17

Тетрис, больше тетриса! Этот тетрис оказался поголоволомней прошлого. Общая идея была понятна сразу, но «на местности» постоянно вылезали какие-то подводные камешки

Сначала думал пойти через byte[][], выставляя нужные биты в единицу (на что намекала ширина поля), но закопался в сдвигах (жаль, что ввод не оказался набором сдвиговых операций, или я не разгадал его).

Ну, хотя бы с идеей движения «окном» не промахнулся — во второй части она пригодилась.

Подготовительная работа

enum Move { LEFT, RIGHT; private static final Map<Character, Move> cache = Map.of('<', LEFT, '>', RIGHT); static Move of(char code) { return cache.get(code); } } class Triple { public int left; public int middle; public List<Integer> right; public Triple(int left, int middle, List<Integer> right) { this.left = left; this.middle = middle; this.right = right; } public boolean equals(Object obj) { if (obj == this) { return true; } else if (!(obj instanceof Triple)) { return false; } else { Triple other = (Triple) obj; return Objects.equals(left, other.left) && Objects.equals(middle, other.middle) && Objects.equals(right, other.right); } } public int hashCode() { return Objects.hashCode(left) ^ Objects.hashCode(middle) ^ Objects.hashCode(right); } } static boolean hasMove(boolean[][] shape, int x, int y, Map<Integer, boolean[]> rows) { for (int yIdx = 0; yIdx < shape.length; yIdx++) { int currentY = y + (shape.length - yIdx); if (rows.containsKey(currentY)) { boolean[] shapeRow = shape[yIdx]; boolean[] fieldRow = rows.get(currentY); for (int xIdx = 0; xIdx < shapeRow.length; xIdx++) { if (fieldRow[x + xIdx] && shapeRow[xIdx]) return false; } } } return true; }

Triple позаимствовал в Apache Commons, чтобы не изгаляться с импортом в консольный скрипт.

Время собирать камни

static void day17(String puzzleInputUri) throws IOException, InterruptedException { boolean[][][] shapes = new boolean[][][] { { {true, true, true, true} }, // ___ { {false, true, false}, {true, true, true}, {false, true, false} }, // + { {false, false, true}, {false, false, true}, {true, true, true} }, // _| { {true}, {true}, {true}, {true} }, // | { {true, true}, {true, true} } // # }; List<Move> moves = client.send(request.uri((URI.create(puzzleInputUri))).build(), HttpResponse.BodyHandlers.ofLines()) .body() .map(String::toCharArray) .flatMap(chars -> CharBuffer.wrap(chars).chars().mapToObj(i -> (char) i)) .map(Move::of) .collect(Collectors.toList()); int level = 0; int ins = 0; Map<Integer, boolean[]> rows = new HashMap<>(); Map<Triple, Map.Entry<Long, Integer>> visited = new HashMap<>(); long additionalLevel = 0; // long totalRocks = 2022L; long totalRocks = 1_000_000_000_000L; for (long rock = 0; rock < totalRocks; rock++) { int currentShapeIdx = (int) (rock % shapes.length); boolean[][] shape = shapes[currentShapeIdx]; int x = 2; int y = level + 3; while (true) { Move direction = moves.get(ins++ % moves.size()); if ((Move.LEFT == direction && x > 0) && hasMove(shape, x - 1, y, rows)) { x--; } else if ((Move.RIGHT == direction && x < 7 - shape[0].length) && hasMove(shape, x + 1, y, rows)) { x++; } if (hasMove(shape, x, y - 1, rows) && y > 0) { y--; } else { for (int yIdx = 0; yIdx < shape.length; yIdx++) { int currentY = y + (shape.length - yIdx); boolean[] shapeRow = shape[yIdx]; boolean[] fieldRow = rows.computeIfAbsent(currentY, it -> new boolean[7]); for (int xIdx = 0; xIdx < shapeRow.length; xIdx++) { fieldRow[x + xIdx] |= shapeRow[xIdx]; } } break; } } var levels = rows.keySet().stream().mapToInt(Integer::intValue).sorted().toArray(); level = levels[levels.length - 1]; List<Integer> window = new ArrayList<>(Collections.nCopies(7, -1)); for (int i = 0; i < 7; i++) { for (int j = levels.length - 1; j >= 0; j--) { if (rows.get(levels[j])[i]) { window.add(i, level - levels[j]); break; } } } var visitedRow = new Triple((ins - 1) % moves.size(), currentShapeIdx, window); if (visited.containsKey(visitedRow)) { var previous = visited.get(visitedRow); long repeat = (totalRocks - rock) / (rock - previous.getKey()); rock += (rock - previous.getKey()) * repeat; additionalLevel += repeat * (level - previous.getValue()); } visited.put(visitedRow, Map.entry(rock, level)); } System.out.println(level + additionalLevel); }

Ну и прочитанное когда-то интервью с создателем оригинального «Тетриса» тоже помогло — там он как раз описывал, почему в игре «сгорают» ряды.

А впереди — день восемнадцатый!

0
Комментарии
-3 комментариев
Раскрывать всегда