From e8da4502bfc2f113c4add831454c759e12afbf29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jegors=20=C4=8Cemisovs?= Date: Sun, 26 Dec 2021 23:27:18 +0200 Subject: [PATCH] Add App.groovy --- build.gradle | 2 ++ src/main/groovy/lv/id/jc/App.groovy | 44 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/main/groovy/lv/id/jc/App.groovy diff --git a/build.gradle b/build.gradle index a8bd76b..8c58790 100644 --- a/build.gradle +++ b/build.gradle @@ -11,6 +11,8 @@ repositories { } dependencies { + implementation 'org.codehaus.groovy:groovy-all:3.0.9' + testImplementation 'org.spockframework:spock-core:2.0-groovy-3.0' testImplementation 'org.codehaus.groovy:groovy-all:3.0.8' } diff --git a/src/main/groovy/lv/id/jc/App.groovy b/src/main/groovy/lv/id/jc/App.groovy new file mode 100644 index 0000000..e11fe55 --- /dev/null +++ b/src/main/groovy/lv/id/jc/App.groovy @@ -0,0 +1,44 @@ +package lv.id.jc + +import algorithm.BreadthFirstSearch +import algorithm.DijkstrasAlgorithm +import algorithm.Graph +import algorithm.SearchAlgorithm + +class App { + private static final Graph complex = new Graph<>(Map.of( + "A", Map.of("B", 5, "H", 2), + "B", Map.of("A", 5, "C", 7), + "C", Map.of("B", 7, "D", 3, "G", 4), + "D", Map.of("C", 20, "E", 4), + "E", Map.of("F", 5), + "F", Map.of("G", 6), + "G", Map.of("C", 4), + "H", Map.of("G", 3) + )) + private static final SearchAlgorithm fastest = new DijkstrasAlgorithm<>() + private static final SearchAlgorithm shortest = new BreadthFirstSearch<>() + + static void main(String[] args) { + System.out.println(complex) + + printRoute(complex, "D", "C") + printRoute(complex, "A", "G") + printRoute(complex, "D", "H") + } + + private static void printRoute(final Graph graph, final String source, final String target) { + final var routeOne = shortest.findPath(graph, source, target) + final var routeTwo = fastest.findPath(graph, source, target) + final var message = """ + Find the path from %s to %s + - the shortest take %.0f min and the path is %s + - the fastest take %.0f min and the path is %s""" + .formatted( + source, target, + graph.getDistance(routeOne), routeOne, + graph.getDistance(routeTwo), routeTwo) + + System.out.println(message) + } +}