Skip to content

Commit

Permalink
Using some SequencedCollections as an example
Browse files Browse the repository at this point in the history
  • Loading branch information
zodac committed Sep 25, 2023
1 parent cae9be7 commit b0167b1
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 123 deletions.
16 changes: 11 additions & 5 deletions .github/workflows/java.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,26 @@ jobs:

steps:
- name: Checkout repo
uses: actions/checkout@v3
uses: actions/checkout@v4.0.0
- name: Checkout submodules
uses: actions/checkout@v3
uses: actions/checkout@v4.0.0
with:
repository: zodac/advent-of-code-inputs
token: ${{ secrets.SUBMODULE_ACCESS }}
path: advent-of-code-inputs
- name: Set up JDK
uses: actions/setup-java@v3
uses: actions/setup-java@v3.13.0
with:
distribution: 'temurin'
java-version: '21'
distribution: 'adopt'
- name: Setup Maven Action
uses: s4u/setup-maven-action@v1.10.0
with:
distribution: 'temurin'
java-version: '21'
maven-version: '3.9.3'
- name: Cache local .m2
uses: actions/cache@v3
uses: actions/cache@v3.3.2
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**.pom.xml') }}
Expand Down
7 changes: 3 additions & 4 deletions 2015/src/test/java/me/zodac/advent/Day19Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import me.zodac.advent.input.ExampleInput;
import me.zodac.advent.input.PuzzleInput;
import me.zodac.advent.pojo.Replacement;
import me.zodac.advent.util.CollectionUtils;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -43,7 +42,7 @@ void example() {
.map(Replacement::parse)
.toList();

final String molecule = CollectionUtils.getLast(values);
final String molecule = values.getLast();

final long numberOfDistinctMolecules = Day19.numberOfDistinctReplacementMolecules(molecule, replacements);
assertThat(numberOfDistinctMolecules)
Expand All @@ -60,7 +59,7 @@ void part1() {
.map(Replacement::parse)
.toList();

final String molecule = CollectionUtils.getLast(values);
final String molecule = values.getLast();

final long numberOfDistinctMolecules = Day19.numberOfDistinctReplacementMolecules(molecule, replacements);
assertThat(numberOfDistinctMolecules)
Expand All @@ -77,7 +76,7 @@ void part2() {
.map(Replacement::parse)
.toList();

final String molecule = CollectionUtils.getLast(values);
final String molecule = values.getLast();

final long numberOfDistinctMolecules = Day19.minimumStepsToCreateOutputFromInput("e", molecule, replacements);
assertThat(numberOfDistinctMolecules)
Expand Down
10 changes: 4 additions & 6 deletions 2021/src/main/java/me/zodac/advent/Day01.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

package me.zodac.advent;

import static me.zodac.advent.util.CollectionUtils.getFirst;

import java.util.Collection;
import java.util.List;
import java.util.SequencedCollection;

/**
* Solution for 2021, Day 1.
Expand All @@ -37,18 +35,18 @@ private Day01() {
* Iterates through the supplied {@code values} and compares each entry to the one before it. If the new value is greater than the previous one,
* the counter is updated.
*
* @param values the {@link List} of {@link Integer}s to be checked
* @param values the {@link SequencedCollection} of {@link Integer}s to be checked
* @return the count of the values higher than their predecessor
*/
public static int countValuesHigherThanPreviousValue(final Collection<Integer> values) {
public static int countValuesHigherThanPreviousValue(final SequencedCollection<Integer> values) {
if (values.isEmpty()) {
return 0;
}

int count = 0;

// Initialise with first value, rather than assuming the value cannot be negative
int currentValue = getFirst(values);
int currentValue = values.getFirst();

for (final int nextValue : values) {
if (nextValue > currentValue) {
Expand Down
20 changes: 9 additions & 11 deletions 2021/src/main/java/me/zodac/advent/Day03.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@

package me.zodac.advent;

import static me.zodac.advent.util.CollectionUtils.getFirst;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.SequencedCollection;
import me.zodac.advent.pojo.BitParityCount;
import me.zodac.advent.util.BinaryConversionUtils;

Expand Down Expand Up @@ -72,13 +70,13 @@ private Day03() {
* @see BinaryConversionUtils
* @see BitParityCount#createForIndexOfBinaryValues(Iterable, int)
*/
public static long calculatePowerConsumption(final Collection<String> binaryValues) {
public static long calculatePowerConsumption(final SequencedCollection<String> binaryValues) {
if (binaryValues.isEmpty()) {
return 0L;
}

// Input should have 12 digits, but no harm being a bit flexible
final int lengthOfBinaryValue = getFirst(binaryValues).length();
final int lengthOfBinaryValue = binaryValues.getFirst().length();

final StringBuilder gammaRate = new StringBuilder();
final StringBuilder epsilonRate = new StringBuilder();
Expand Down Expand Up @@ -145,7 +143,7 @@ public static long calculatePowerConsumption(final Collection<String> binaryValu
* @see BinaryConversionUtils
* @see BitParityCount#createForIndexOfBinaryValues(Iterable, int)
*/
public static long calculateLifeSupportRating(final List<String> binaryValues) {
public static long calculateLifeSupportRating(final SequencedCollection<String> binaryValues) {
if (binaryValues.isEmpty()) {
return 0L;
}
Expand All @@ -156,16 +154,16 @@ public static long calculateLifeSupportRating(final List<String> binaryValues) {
return oxygenRating * carbonDioxideRating;
}

private static long getOxygenRating(final List<String> binaryValues) {
private static long getOxygenRating(final SequencedCollection<String> binaryValues) {
return getRating(binaryValues, true);
}

private static long getCarbonDioxideRating(final List<String> binaryValues) {
private static long getCarbonDioxideRating(final SequencedCollection<String> binaryValues) {
return getRating(binaryValues, false);
}

private static long getRating(final List<String> binaryValues, final boolean mostCommon) {
final int lengthOfBinaryValue = getFirst(binaryValues).length();
private static long getRating(final SequencedCollection<String> binaryValues, final boolean mostCommon) {
final int lengthOfBinaryValue = binaryValues.getFirst().length();
List<String> validRatings = new ArrayList<>(binaryValues);

for (int i = 0; i < lengthOfBinaryValue; i++) {
Expand All @@ -190,7 +188,7 @@ private static long getRating(final List<String> binaryValues, final boolean mos
if (validRatings.size() != EXPECTED_NUMBER_OF_VALID_RATINGS) {
throw new IllegalStateException("Expected there to be only 1 valid rating, found: " + validRatings.size());
}
return BinaryConversionUtils.toDecimal(getFirst(validRatings));
return BinaryConversionUtils.toDecimal(validRatings.getFirst());
}
}

Expand Down
4 changes: 1 addition & 3 deletions 2021/src/main/java/me/zodac/advent/Day04.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package me.zodac.advent;

import static me.zodac.advent.util.CollectionUtils.getFirst;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -135,7 +133,7 @@ private static List<BingoBoard> convertBingoBoards(final List<String> bingoBoard
return Collections.emptyList();
}

final int boardSize = StringUtils.splitOnWhitespace(getFirst(bingoBoardValues)).length;
final int boardSize = StringUtils.splitOnWhitespace(bingoBoardValues.getFirst()).length;
final List<BingoBoard> bingoBoards = new ArrayList<>();
final int numberOfBoardValues = bingoBoardValues.size();

Expand Down
7 changes: 3 additions & 4 deletions 2021/src/test/java/me/zodac/advent/Day04Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package me.zodac.advent;

import static me.zodac.advent.util.CollectionUtils.getFirst;
import static org.assertj.core.api.Assertions.assertThat;

import java.util.Arrays;
Expand All @@ -40,7 +39,7 @@ void example() {
.filter(input -> !input.isBlank())
.toList();

final List<Integer> bingoNumbers = Arrays.stream(getFirst(bingoInput).split(","))
final List<Integer> bingoNumbers = Arrays.stream(bingoInput.getFirst().split(","))
.mapToInt(Integer::parseInt)
.boxed()
.toList();
Expand All @@ -64,7 +63,7 @@ void part1() {
.filter(input -> !input.isBlank())
.toList();

final List<Integer> bingoNumbers = Arrays.stream(getFirst(bingoInput).split(","))
final List<Integer> bingoNumbers = Arrays.stream(bingoInput.getFirst().split(","))
.mapToInt(Integer::parseInt)
.boxed()
.toList();
Expand All @@ -84,7 +83,7 @@ void part2() {
.filter(input -> !input.isBlank())
.toList();

final List<Integer> bingoNumbers = Arrays.stream(getFirst(bingoInput).split(","))
final List<Integer> bingoNumbers = Arrays.stream(bingoInput.getFirst().split(","))
.mapToInt(Integer::parseInt)
.boxed()
.toList();
Expand Down
9 changes: 7 additions & 2 deletions 2022/src/main/java/me/zodac/advent/Day03.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static long sumCommonCharacterValuesInStringHalves(final Iterable<String>
for (final String value : values) {
final Pair<String, String> bisectedString = StringUtils.bisect(value);
final Set<Character> commonCharacters = StringUtils.commonChars(bisectedString.first(), bisectedString.second());
final Character commonCharacter = CollectionUtils.getFirst(commonCharacters); // Assuming only one common char
final Character commonCharacter = getFirst(commonCharacters); // Assuming only one common char
total += VALUES.indexOf(commonCharacter);
}

Expand Down Expand Up @@ -111,10 +111,15 @@ public static long sumCommonCharacterValuesInGroupedStrings(final Collection<Str
for (final List<String> groupedValue : groupedValues) {
final List<String> groupAsList = new ArrayList<>(groupedValue);
final Set<Character> commonCharacters = StringUtils.commonChars(groupAsList.get(0), groupAsList.get(1), groupAsList.get(2));
final Character commonCharacter = CollectionUtils.getFirst(commonCharacters); // Assuming only one common char
final Character commonCharacter = getFirst(commonCharacters); // Assuming only one common char
total += VALUES.indexOf(commonCharacter);
}

return total;
}

private static <E> E getFirst(final Set<E> set) {
final List<E> temp = new ArrayList<>(set);
return temp.get(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package me.zodac.advent.input;

import static me.zodac.advent.util.CollectionUtils.getFirst;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -124,7 +122,7 @@ static String readSingleLine(final List<String> lines) {
throw new IllegalArgumentException("Expected a single line, found: " + lines.size());
}

return getFirst(lines);
return lines.getFirst();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

package me.zodac.advent.pojo;

import static me.zodac.advent.util.CollectionUtils.getFirst;
import static me.zodac.advent.util.CollectionUtils.getLast;

import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;
Expand All @@ -41,7 +38,7 @@ public int calculate(final List<Integer> inputs) {
if (inputs.size() != TWO_INPUTS) {
throw new IllegalArgumentException("Expected 2 input values, found: " + inputs);
}
return getFirst(inputs) & getLast(inputs);
return inputs.getFirst() & inputs.getLast();
}
},

Expand All @@ -57,7 +54,7 @@ public int calculate(final List<Integer> inputs) {
if (inputs.size() != TWO_INPUTS) {
throw new IllegalArgumentException("Expected 2 input values, found: " + inputs);
}
return getFirst(inputs) | getLast(inputs);
return inputs.getFirst() | inputs.getLast();
}
},

Expand All @@ -73,7 +70,7 @@ public int calculate(final List<Integer> inputs) {
if (inputs.size() != TWO_INPUTS) {
throw new IllegalArgumentException("Expected 2 input values, found: " + inputs);
}
return getFirst(inputs) << getLast(inputs);
return inputs.getFirst() << inputs.getLast();
}
},

Expand All @@ -89,7 +86,7 @@ public int calculate(final List<Integer> inputs) {
if (inputs.size() != TWO_INPUTS) {
throw new IllegalArgumentException("Expected 2 input values, found: " + inputs);
}
return getFirst(inputs) >> getLast(inputs);
return inputs.getFirst() >> inputs.getLast();
}
},

Expand All @@ -105,7 +102,7 @@ public int calculate(final List<Integer> inputs) {
if (inputs.size() != SINGLE_INPUT) {
throw new IllegalArgumentException("Expected 1 input values, found: " + inputs);
}
return ~getFirst(inputs);
return ~inputs.getFirst();
}
},

Expand All @@ -118,7 +115,7 @@ public int calculate(final List<Integer> inputs) {
if (inputs.size() != SINGLE_INPUT) {
throw new IllegalArgumentException("Expected 1 input values, found: " + inputs);
}
return getFirst(inputs);
return inputs.getFirst();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package me.zodac.advent.pojo.grid;

import static me.zodac.advent.util.CollectionUtils.getFirst;

import java.util.Collection;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -70,7 +68,7 @@ public static BooleanGrid parse(final List<String> gridValues, final char symbol
throw new IllegalArgumentException("Input cannot be empty");
}

final int firstElementSize = getFirst(gridValues).length();
final int firstElementSize = gridValues.getFirst().length();
if (gridValues.size() != firstElementSize) {
throw new IllegalArgumentException(
String.format("Outer size must match inner size, found outer: %s, inner: %s", gridValues.size(), firstElementSize));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package me.zodac.advent.pojo.grid;

import static me.zodac.advent.util.CollectionUtils.getFirst;

import java.util.List;
import java.util.Set;
import me.zodac.advent.pojo.Line;
Expand Down Expand Up @@ -69,7 +67,7 @@ public static IntegerGrid parse(final List<String> gridValues) {
throw new IllegalArgumentException("Input cannot be empty");
}

final int firstElementLength = getFirst(gridValues).length();
final int firstElementLength = gridValues.getFirst().length();
if (gridValues.size() != firstElementLength) {
throw new IllegalArgumentException(
String.format("Outer size must match inner size, found outer: %s, inner: %s", gridValues.size(), firstElementLength));
Expand Down
Loading

0 comments on commit b0167b1

Please sign in to comment.