From 23d31f945eaafa8caba8ead9d4e7351cbad3f1ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Zaicsek?= Date: Mon, 25 Dec 2023 07:46:09 +0100 Subject: [PATCH] Day25 -- sloow ~10 minutes --- .../io/github/zebalu/aoc2023/days/Day25.java | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 aoc2023/src/main/java/io/github/zebalu/aoc2023/days/Day25.java diff --git a/aoc2023/src/main/java/io/github/zebalu/aoc2023/days/Day25.java b/aoc2023/src/main/java/io/github/zebalu/aoc2023/days/Day25.java new file mode 100644 index 0000000..f686500 --- /dev/null +++ b/aoc2023/src/main/java/io/github/zebalu/aoc2023/days/Day25.java @@ -0,0 +1,113 @@ +package io.github.zebalu.aoc2023.days; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Queue; +import java.util.Set; + +public class Day25 { + public static void main(String[] args) { + String input = readInput(); + Map> graph = new HashMap<>(); + input.lines().forEach(l->processLine(l, graph)); + Map, Integer> edgeFrequency = new HashMap, Integer>(); + List vertexes = new ArrayList<>(graph.keySet()); + for(int i=0; i, Integer>>comparingInt(e->e.getValue()).reversed()).limit(3).forEach(e->{ + cutEdge(graph, e.getKey()); + }); + int part1Size = findConnectedSize(graph.keySet().iterator().next(), graph); + int part2Size = graph.size()-part1Size; + System.out.println(part1Size*part2Size); + System.out.println("Marry Christmas!"); + } + + private static void markEdges(String start, String target, Map> graph, + Map, Integer> edgeFrequency) { + Queue queue = new LinkedList<>(); + Set visited = new HashSet<>(); + queue.add(new Step(start, List.of())); + visited.add(start); + while (!queue.isEmpty()) { + Step curr = queue.poll(); + if(target.equals(curr.vertex)) { + curr.edges.forEach(e->{ + int v = edgeFrequency.getOrDefault(e, 0); + edgeFrequency.put(e, v+1); + }); + return; + } + graph.get(curr.vertex).stream().filter(n->!visited.contains(n)).forEach(n->{ + List> nextEdges = new ArrayList<>(curr.edges()); + nextEdges.add(new HashSet<>(Set.of(curr.vertex, n))); + Step nextStep = new Step(n, nextEdges); + queue.add(nextStep); + visited.add(n); + }); + } + } + + private static void cutEdge(Map> graph, Set edge) { + var it = edge.iterator(); + String a = it.next(); + String b = it.next(); + graph.get(a).remove(b); + graph.get(b).remove(a); + } + + private static int findConnectedSize(String start, Map> graph) { + Queue queue = new LinkedList<>(); + Set visited = new HashSet<>(); + queue.add(start); + visited.add(start); + while (!queue.isEmpty()) { + String curr = queue.poll(); + graph.get(curr).stream().filter(n->!visited.contains(n)).forEach(n->{ + queue.add(n); + visited.add(n); + }); + } + return visited.size(); + } + + record Step(String vertex, List> edges) { + + } + + private static String readInput() { + try { + return Files.readString(Path.of("day25.txt").toAbsolutePath()); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + private static void processLine(String line, Map> graph) { + var parts = line.split(": "); + var name = parts[0]; + var cons = parts[1].split(" "); + for(var con: cons) { + markConnectionn(graph, name, con); + } + } + + private static void markConnectionn(Map> graph, String from, String to) { + graph.computeIfAbsent(from, (k)->new HashSet<>()).add(to); + graph.computeIfAbsent(to, (k)->new HashSet<>()).add(from); + } +}