From 5b25791bee90f48264a64b6d5930cef94c3dc255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Zaicsek?= Date: Wed, 13 Dec 2023 07:43:44 +0100 Subject: [PATCH] Day13 --- .../io/github/zebalu/aoc2023/days/Day13.java | 151 ++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 aoc2023/src/main/java/io/github/zebalu/aoc2023/days/Day13.java diff --git a/aoc2023/src/main/java/io/github/zebalu/aoc2023/days/Day13.java b/aoc2023/src/main/java/io/github/zebalu/aoc2023/days/Day13.java new file mode 100644 index 0000000..5c457d1 --- /dev/null +++ b/aoc2023/src/main/java/io/github/zebalu/aoc2023/days/Day13.java @@ -0,0 +1,151 @@ +package io.github.zebalu.aoc2023.days; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; +import java.util.NoSuchElementException; + +public class Day13 { + public static void main(String[] args) { + String input = readInput(); + String[] patterns = input.split("\n\n"); + List mazes = Arrays.stream(patterns).map(p->new Maze(p.lines().toList())).toList(); + System.out.println(mazes.stream().mapToInt(Maze::findAnyMirror).sum()); + System.out.println(mazes.stream().mapToInt(Maze::findAnyMirrorWith1Diff).sum()); + } + + private static String readInput() { + try { + return Files.readString(Path.of("day13.txt").toAbsolutePath()); + } catch (Exception e) { + throw new IllegalStateException(e); + } + } + + private record Maze(List lines) { + int findVerticalMirror() { + for(int i = 0; i0) { + return i*100; + } + i = findVerticalMirror(); + if(i>0) { + return i; + } + throw new NoSuchElementException(); + } + + int findVerticalMirrorWith1Diff() { + for(int i = 0; i0) { + return i*100; + } + i = findVerticalMirrorWith1Diff(); + if(i>0) { + return i; + } + throw new NoSuchElementException(); + } + + private boolean checkPosinLine(int pos, String line) { + int i = pos; + int j = pos+1; + boolean match = true; + while(match && i>=0 && j < line.length()) { + match = line.charAt(i) == line.charAt(j); + --i; + ++j; + } + return match; + } + + private boolean compareLines(int lineCount) { + int i = lineCount; + int j = lineCount+1; + boolean match = true; + while( match && i>=0 && j=0 && j < line.length() && diff<2) { + if(line.charAt(i) != line.charAt(j)) { + ++diff; + } + --i; + ++j; + } + return diff; + } + + private boolean compareLinesWith1Diff(int lineCount) { + int i = lineCount; + int j = lineCount+1; + int diffCount = 0; + while( i>=0 && j