From d0e01148ffb4558a7ed4d2dc2cd284e675f1ca76 Mon Sep 17 00:00:00 2001 From: Sugui Date: Wed, 6 Dec 2023 10:25:00 +0100 Subject: [PATCH] day 6, optimized :3 --- resources/input06.txt | 2 + src/main/java/sugui/Day06.java | 69 ++++++++++++++++++++++++++++++ src/test/java/sugui/Day06Test.java | 33 ++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 resources/input06.txt create mode 100644 src/main/java/sugui/Day06.java create mode 100644 src/test/java/sugui/Day06Test.java diff --git a/resources/input06.txt b/resources/input06.txt new file mode 100644 index 0000000..33de0fa --- /dev/null +++ b/resources/input06.txt @@ -0,0 +1,2 @@ +Time: 52 94 75 94 +Distance: 426 1374 1279 1216 diff --git a/src/main/java/sugui/Day06.java b/src/main/java/sugui/Day06.java new file mode 100644 index 0000000..7f7e531 --- /dev/null +++ b/src/main/java/sugui/Day06.java @@ -0,0 +1,69 @@ +package sugui; + +import java.io.IOException; +import java.lang.reflect.Array; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class Day06 { + + public static Path inputPath = Paths.get("resources/input06.txt"); + + private record Race(long time, long bestMark) { + } + + public static void main(String[] args) throws IOException { + var input = Files.readString(inputPath); + var firstResult = getFirstPuzzleResult(input); + var secondResult = getSecondPuzzleResult(input); + System.out.println("First result: " + firstResult); + System.out.println("Second result: " + secondResult); + } + + public static List parseFirst(String input) { + String[] lines = input.split("\n"); + String[] times = lines[0].substring(5).trim().split(" +"); + String[] distance = lines[1].substring(9).trim().split(" +"); + List races = IntStream.range(0, times.length) + .mapToObj(i -> new Race(Long.parseLong(times[i]), Long.parseLong(distance[i]))) + .collect(Collectors.toList()); + return races; + } + + public static Race parseSecond(String input) { + String[] lines = input.split("\n"); + long time = Long.parseLong( + Arrays.asList(lines[0].substring(5).trim().split(" +")).stream().collect(Collectors.joining(""))); + long distance = Long.parseLong(Arrays.asList(lines[1].substring(9).trim().split(" +")).stream() + .collect(Collectors.joining(""))); + return new Race(time, distance); + } + + public static String getFirstPuzzleResult(String input) { + List races = parseFirst(input); + long result = races.stream().map(race -> { + var firstOptimal = 1 + Math + .floor((-race.time + Math.sqrt(Math.pow(race.time, 2) - (-4) * (-race.bestMark))) / (-2)); + var lastOptimal = (-1) + Math + .ceil((-race.time - Math.sqrt(Math.pow(race.time, 2) - (-4) * (-race.bestMark))) / (-2)); + return (long) (lastOptimal - firstOptimal + 1); + }).reduce(1l, (a, b) -> a * b); + return Long.toString(result); + } + + public static String getSecondPuzzleResult(String input) { + Race race = parseSecond(input); + var firstOptimal = 1 + Math + .floor((-race.time + Math.sqrt(Math.pow(race.time, 2) - (-4) * (-race.bestMark))) / (-2)); + var lastOptimal = (-1) + Math + .ceil((-race.time - Math.sqrt(Math.pow(race.time, 2) - (-4) * (-race.bestMark))) / (-2)); + long result = (long) (lastOptimal - firstOptimal + 1); + return Long.toString(result); + } +} diff --git a/src/test/java/sugui/Day06Test.java b/src/test/java/sugui/Day06Test.java new file mode 100644 index 0000000..bf7dd96 --- /dev/null +++ b/src/test/java/sugui/Day06Test.java @@ -0,0 +1,33 @@ +package sugui; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + +import org.junit.Test; + +/** + * Unit test for simple App. + */ +public class Day06Test { + + public static String firstBasicInput = """ + Time: 7 15 30 + Distance: 9 40 200 + """; + public static String secondBasicInput = firstBasicInput; + public static String firstBasicResult = "288"; + public static String secondBasicResult = "71503"; + + @Test + public void firstBasicCase() throws IOException { + String actualResult = Day06.getFirstPuzzleResult(firstBasicInput); + assertEquals(firstBasicResult, actualResult); + } + + @Test + public void secondBasicCase() throws IOException { + String actualResult = Day06.getSecondPuzzleResult(secondBasicInput); + assertEquals(secondBasicResult, actualResult); + } +}