Skip to content

Commit

Permalink
Cleaning solution for 2023, Day 6
Browse files Browse the repository at this point in the history
  • Loading branch information
zodac committed Dec 9, 2023
1 parent 3763032 commit 79d450d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 37 deletions.
69 changes: 36 additions & 33 deletions 2023/src/main/java/me/zodac/advent/Day06.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,49 @@ private Day06() {

}

private record Race(long time, long distance) {

}

/**
* Part 1.
* We are given a {@link List} of {@link String}s, representing a number of {@link Race}s. For each {@link Race} we can 'charge' up our velocity,
* and use the remaining time to complete the required distance. We count the number of ways to win the {@link Race}s, then multiply them up.
*
* <p>
* if {@code isMultipleRaces} is {@code true}, each column of the input is referring to a separate {@link Race}, otherwise all columns should be
* concatenated to create one large {@link Race}.
*
* @param values the input values
* @return the part 1 result
* @param values the input {@link Race}s
* @param isMultipleRaces whether the input is multiple {@link Race}s or a single large {@link Race}
* @return the number of ways to win the supplied{@link Race}s
*/
public static long part1(final List<String> values) {
final Collection<Race> races = new ArrayList<>();
public static long countNumberOfWaysToWin(final List<String> values, final boolean isMultipleRaces) {
return parseRaces(values, isMultipleRaces)
.stream()
.mapToLong(Day06::countWaysToWinRace)
.reduce(1L, (first, second) -> first * second);
}

private static Collection<Race> parseRaces(final List<String> values, final boolean isMultipleRaces) {
final List<Long> times = StringUtils.collectNumbersInOrder(values.getFirst());
final List<Long> distances = StringUtils.collectNumbersInOrder(values.getLast());
final Collection<Race> races = new ArrayList<>();

for (int i = 0; i < times.size(); i++) {
races.add(new Race(times.get(i), distances.get(i)));
}
if (isMultipleRaces) {
for (int i = 0; i < times.size(); i++) {
races.add(new Race(times.get(i), distances.get(i)));
}
} else {
final StringBuilder timeBuilder = new StringBuilder();
final StringBuilder distanceBuilder = new StringBuilder();
for (int i = 0; i < times.size(); i++) {
timeBuilder.append(times.get(i));
distanceBuilder.append(distances.get(i));
}

long total = 1;
for (final Race race : races) {
final long val = countWinningOptions(race);
total *= val;
races.add(new Race(Long.parseLong(timeBuilder.toString()), Long.parseLong(distanceBuilder.toString())));
}
return total;

return races;
}

private static long countWinningOptions(final Race race) {
private static long countWaysToWinRace(final Race race) {
int count = 0;
for (int timeToHold = 1; timeToHold < race.time - 1; timeToHold++) {
final long remainingTime = race.time - timeToHold;
Expand All @@ -75,23 +89,12 @@ private static long countWinningOptions(final Race race) {
}

/**
* Part 2.
* Simple record defining a race.
*
* @param values the input values
* @return the part 2 result
* @param time the time available
* @param distance the distance to travel
*/
public static long part2(final List<String> values) {
final List<Long> times = StringUtils.collectNumbersInOrder(values.getFirst());
final List<Long> distances = StringUtils.collectNumbersInOrder(values.getLast());

final StringBuilder timeBuilder = new StringBuilder();
final StringBuilder distanceBuilder = new StringBuilder();
for (int i = 0; i < times.size(); i++) {
timeBuilder.append(times.get(i));
distanceBuilder.append(distances.get(i));
}
private record Race(long time, long distance) {

final Race race = new Race(Long.parseLong(timeBuilder.toString()), Long.parseLong(distanceBuilder.toString()));
return countWinningOptions(race);
}
}
8 changes: 4 additions & 4 deletions 2023/src/test/java/me/zodac/advent/Day06Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public class Day06Test {
void example() {
final List<String> values = ExampleInput.readLines(INPUT_FILENAME);

final long part1Result = Day06.part1(values);
final long part1Result = Day06.countNumberOfWaysToWin(values, true);
assertThat(part1Result)
.isEqualTo(288L);

final long part2Result = Day06.part2(values);
final long part2Result = Day06.countNumberOfWaysToWin(values, false);
assertThat(part2Result)
.isEqualTo(71_503L);
}
Expand All @@ -48,7 +48,7 @@ void example() {
void part1() {
final List<String> values = PuzzleInput.readLines(INPUT_FILENAME);

final long part1Result = Day06.part1(values);
final long part1Result = Day06.countNumberOfWaysToWin(values, true);
assertThat(part1Result)
.isEqualTo(771_628L);
}
Expand All @@ -57,7 +57,7 @@ void part1() {
void part2() {
final List<String> values = PuzzleInput.readLines(INPUT_FILENAME);

final long part2Result = Day06.part2(values);
final long part2Result = Day06.countNumberOfWaysToWin(values, false);
assertThat(part2Result)
.isEqualTo(27_363_861L);
}
Expand Down

0 comments on commit 79d450d

Please sign in to comment.