From 3794dcd38c18708e03ee86e3296c60b1a8814494 Mon Sep 17 00:00:00 2001 From: jabrena Date: Mon, 23 Dec 2024 20:51:27 +0000 Subject: [PATCH 1/4] Initial refactoring --- .../src/main/java/info/jab/aoc/day1/Day1.java | 61 ++-------------- .../info/jab/aoc/day1/HistorianHysteria.java | 69 +++++++++++++++++++ .../test/java/info/jab/aoc/day1/Day1Test.java | 67 ++++++++++-------- 3 files changed, 112 insertions(+), 85 deletions(-) create mode 100644 2024/src/main/java/info/jab/aoc/day1/HistorianHysteria.java diff --git a/2024/src/main/java/info/jab/aoc/day1/Day1.java b/2024/src/main/java/info/jab/aoc/day1/Day1.java index 75d41c4..e808a60 100644 --- a/2024/src/main/java/info/jab/aoc/day1/Day1.java +++ b/2024/src/main/java/info/jab/aoc/day1/Day1.java @@ -1,13 +1,6 @@ package info.jab.aoc.day1; import info.jab.aoc.Day; -import com.putoet.resources.ResourceLines; -import java.util.List; -import java.util.function.BiFunction; -import java.util.function.Function; -import java.util.stream.IntStream; -import java.util.stream.Collectors; -import java.util.regex.Pattern; /** * https://adventofcode.com/2024/day/1 @@ -16,61 +9,15 @@ */ public class Day1 implements Day { - Function> loadFle = fileName -> ResourceLines.list(fileName); - - private static final String SEPARATOR = "\\s+"; - private static final Pattern SEPARATOR_PATTERN = Pattern.compile(SEPARATOR); - - private record ListSplitted(List listLeft, List listRight) {} - - Function, ListSplitted> splitInto2Lists = param -> { - var list1 = param.stream().map(line -> Integer.parseInt(SEPARATOR_PATTERN.split(line)[0])).sorted().toList(); - var list2 = param.stream().map(line -> Integer.parseInt(SEPARATOR_PATTERN.split(line)[1])).sorted().toList(); - return new ListSplitted(list1, list2); - }; - - //@juan-medina idea - Function, ListSplitted> splitInto2Lists2 = param -> { - return param.stream() - .map(line -> SEPARATOR_PATTERN.split(line)) - .collect(Collectors.teeing( - Collectors.mapping(parts -> Integer.parseInt(parts[0]), Collectors.toList()), - Collectors.mapping(parts -> Integer.parseInt(parts[1]), Collectors.toList()), - (l1, l2) -> new ListSplitted(l1.stream().sorted().toList(), l2.stream().sorted().toList()) - )); - }; - - Function calculateDistance = parameter -> { - return IntStream.range(0, parameter.listLeft().size()) - .map(i -> { - int param1 = parameter.listLeft().get(i); - int param2 = parameter.listRight().get(i); - return Math.abs(param1 - param2); - }) - .sum(); - }; - @Override public Integer getPart1Result(String fileName) { - return loadFle.andThen(splitInto2Lists).andThen(calculateDistance).apply(fileName); + HistorianHysteria historianHysteria = new HistorianHysteria(); + return historianHysteria.partOne(fileName); } - BiFunction, Integer> countOccurrences = (param1, param2) -> { - return (int) param2.stream().filter(number -> number.equals(param1)).count(); - }; - - Function calculateOcurrences = parameter -> { - return IntStream.range(0, parameter.listLeft().size()) - .map(i -> { - int param1 = parameter.listLeft().get(i); - int param2 = countOccurrences.apply(param1, parameter.listRight()); - return param1 * param2; - }) - .sum(); - }; - @Override public Integer getPart2Result(String fileName) { - return loadFle.andThen(splitInto2Lists2).andThen(calculateOcurrences).apply(fileName); + HistorianHysteria historianHysteria = new HistorianHysteria(); + return historianHysteria.partTwo(fileName); } } diff --git a/2024/src/main/java/info/jab/aoc/day1/HistorianHysteria.java b/2024/src/main/java/info/jab/aoc/day1/HistorianHysteria.java new file mode 100644 index 0000000..8dd2a66 --- /dev/null +++ b/2024/src/main/java/info/jab/aoc/day1/HistorianHysteria.java @@ -0,0 +1,69 @@ +package info.jab.aoc.day1; + +import com.putoet.resources.ResourceLines; +import java.util.List; +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.stream.IntStream; +import java.util.stream.Collectors; +import java.util.regex.Pattern; + +class HistorianHysteria { + + private static final String SEPARATOR = "\\s+"; + private static final Pattern SEPARATOR_PATTERN = Pattern.compile(SEPARATOR); + + Function> loadFle = fileName -> ResourceLines.list(fileName); + + private record ListSplitted(List listLeft, List listRight) {} + + Function, ListSplitted> splitInto2Lists = param -> { + var list1 = param.stream().map(line -> Integer.parseInt(SEPARATOR_PATTERN.split(line)[0])).sorted().toList(); + var list2 = param.stream().map(line -> Integer.parseInt(SEPARATOR_PATTERN.split(line)[1])).sorted().toList(); + return new ListSplitted(list1, list2); + }; + + //@juan-medina idea + Function, ListSplitted> splitInto2Lists2 = param -> { + return param.stream() + .map(line -> SEPARATOR_PATTERN.split(line)) + .collect(Collectors.teeing( + Collectors.mapping(parts -> Integer.parseInt(parts[0]), Collectors.toList()), + Collectors.mapping(parts -> Integer.parseInt(parts[1]), Collectors.toList()), + (l1, l2) -> new ListSplitted(l1.stream().sorted().toList(), l2.stream().sorted().toList()) + )); + }; + + Function calculateDistance = parameter -> { + return IntStream.range(0, parameter.listLeft().size()) + .map(i -> { + int param1 = parameter.listLeft().get(i); + int param2 = parameter.listRight().get(i); + return Math.abs(param1 - param2); + }) + .sum(); + }; + + BiFunction, Integer> countOccurrences = (param1, param2) -> { + return (int) param2.stream().filter(number -> number.equals(param1)).count(); + }; + + Function calculateOcurrences = parameter -> { + return IntStream.range(0, parameter.listLeft().size()) + .map(i -> { + int param1 = parameter.listLeft().get(i); + int param2 = countOccurrences.apply(param1, parameter.listRight()); + return param1 * param2; + }) + .sum(); + }; + + public Integer partOne(String fileName) { + return loadFle.andThen(splitInto2Lists).andThen(calculateDistance).apply(fileName); + } + + public Integer partTwo(String fileName) { + return loadFle.andThen(splitInto2Lists2).andThen(calculateOcurrences).apply(fileName); + } + +} diff --git a/2024/src/test/java/info/jab/aoc/day1/Day1Test.java b/2024/src/test/java/info/jab/aoc/day1/Day1Test.java index ef903b1..a22c4b0 100644 --- a/2024/src/test/java/info/jab/aoc/day1/Day1Test.java +++ b/2024/src/test/java/info/jab/aoc/day1/Day1Test.java @@ -3,60 +3,71 @@ import static org.assertj.core.api.BDDAssertions.then; import org.junit.jupiter.api.Test; + +import com.putoet.utils.Timer; + import org.junit.jupiter.api.Disabled; class Day1Test { @Test void should_solve_day1_part1_with_sample() { - //Given - String fileName = "/day1/day1-input-sample.txt"; + Timer.run(() -> { + //Given + String fileName = "/day1/day1-input-sample.txt"; - //When - var day1 = new Day1(); - var result = day1.getPart1Result(fileName); + //When + var day1 = new Day1(); + var result = day1.getPart1Result(fileName); - //Then - then(result).isEqualTo(11); + //Then + then(result).isEqualTo(11); + }); } @Test void should_solve_day1_part1() { - //Given - String fileName = "/day1/day1-input.txt"; + Timer.run(() -> { + //Given + String fileName = "/day1/day1-input.txt"; - //When - var day1 = new Day1(); - var result = day1.getPart1Result(fileName); + //When + var day1 = new Day1(); + var result = day1.getPart1Result(fileName); - //Then - then(result).isEqualTo(3574690); + //Then + then(result).isEqualTo(3574690); + }); } @Test void should_solve_day1_part2_with_sample() { - //Given - String fileName = "/day1/day1-input-sample.txt"; + Timer.run(() -> { + //Given + String fileName = "/day1/day1-input-sample.txt"; - //When - var day1 = new Day1(); - var result = day1.getPart2Result(fileName); + //When + var day1 = new Day1(); + var result = day1.getPart2Result(fileName); - //Then - then(result).isEqualTo(31); + //Then + then(result).isEqualTo(31); + }); } @Test void should_solve_day1_part2() { - //Given - String fileName = "/day1/day1-input.txt"; + Timer.run(() -> { + //Given + String fileName = "/day1/day1-input.txt"; - //When - var day1 = new Day1(); - var result = day1.getPart2Result(fileName); + //When + var day1 = new Day1(); + var result = day1.getPart2Result(fileName); - //Then - then(result).isEqualTo(22565391); + //Then + then(result).isEqualTo(22565391); + }); } } From 8684db0e44d38f1082e3e738b431b29417cd3642 Mon Sep 17 00:00:00 2001 From: jabrena Date: Mon, 23 Dec 2024 21:34:03 +0000 Subject: [PATCH 2/4] Improve markdown --- 2024/README.md | 62 ++++++++++++++++++++------------------------------ 1 file changed, 25 insertions(+), 37 deletions(-) diff --git a/2024/README.md b/2024/README.md index aacf5f0..5eb20c9 100644 --- a/2024/README.md +++ b/2024/README.md @@ -1,39 +1,27 @@ # AOC 2024 -## Data processing - -- Day 1 -- Day 2 -- Day 3 -- Day 5 -- Day 9 -- Day 11 -- Day 12 -- Day 13 -- Day 17 -- Day 18 -- Day 19 -- Day 22 -- Day 23 - -## Recursion - -- Day 7 - -## Special - -- Day 14 - -## Direction - -- Day 21 * - -## Grid - -- Day 4 -- Day 6 -- Day 8 -- Day 10 -- Day 15 -- Day 16 * -- Day 20 * +| Day | Category | Complexity | +|-------|-----------------|------------| +| Day 1 | Data processing | * | +| Day 2 | Data processing | * | +| Day 3 | Data processing | * | +| Day 5 | Data processing | ** | +| Day 9 | Data processing | | +| Day 11| Data processing | | +| Day 12| Data processing | | +| Day 13| Data processing | | +| Day 17| Data processing | | +| Day 18| Data processing | | +| Day 19| Data processing | | +| Day 22| Data processing | ** | +| Day 23| Data processing | ** | +| Day 7 | Recursion | | +| Day 14| Special | | +| Day 21| Direction | ***** | +| Day 4 | Grid | | +| Day 6 | Grid | | +| Day 8 | Grid | | +| Day 10| Grid | | +| Day 15| Grid | | +| Day 16| Grid | ***** | +| Day 20| Grid | ***** | From a1eb59dfe644f8b1dda5e3ac7628b00a161d81f6 Mon Sep 17 00:00:00 2001 From: jabrena Date: Mon, 23 Dec 2024 21:36:28 +0000 Subject: [PATCH 3/4] Improve markdown --- 2024/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2024/README.md b/2024/README.md index 5eb20c9..574024a 100644 --- a/2024/README.md +++ b/2024/README.md @@ -17,11 +17,11 @@ | Day 23| Data processing | ** | | Day 7 | Recursion | | | Day 14| Special | | +| Day 16| Direction | ***** | | Day 21| Direction | ***** | | Day 4 | Grid | | | Day 6 | Grid | | | Day 8 | Grid | | | Day 10| Grid | | | Day 15| Grid | | -| Day 16| Grid | ***** | | Day 20| Grid | ***** | From 47515725b634e9fae00ef8af83326dfa1c5935e6 Mon Sep 17 00:00:00 2001 From: jabrena Date: Thu, 26 Dec 2024 14:54:12 +0000 Subject: [PATCH 4/4] Minor change --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index faa8a5d..06a46e5 100644 --- a/pom.xml +++ b/pom.xml @@ -242,7 +242,7 @@ org.openjdk.jmh jmh-generator-annprocess - 1.37 + ${jmh.version}