Skip to content

Update Core Math Lib #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ tasks.withType<JavaCompile>().configureEach {
options.compilerArgs.add("--enable-preview")
}

tasks.withType<JavaCompile> {
options.encoding = "UTF-8"
}

jmh {
warmupIterations = 2
iterations = 2
Expand Down
102 changes: 102 additions & 0 deletions src/main/java/org/togetherjava/aoc/core/Regex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package org.togetherjava.aoc.core;

import java.util.List;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;

public class Regex {

public static final Pattern WHITESPACES = Pattern.compile("\\s+");
public static final Pattern INTEGERS = Pattern.compile("-?\\d+");
public static final Pattern WORDS = Pattern.compile("\\w+");
public static final Pattern EOL = Pattern.compile("\\R");
public static final Pattern BLANK_LINES = Pattern.compile("\\R{2,}");

/**
* Find all the matches in the given input string and collect to an immutable list.
* @param input input to search
* @param regex pattern to match on
* @return immutable list of matches
*/
public static List<MatchResult> allMatches(Pattern regex, String input) {
return regex.matcher(input).results().toList();
}

/**
* Find all the matches in the given input string and collect to an immutable list.
* @param input input to search
* @param regex string pattern to match on
* @return immutable list of matches
*/
public static List<MatchResult> allMatches(String regex, String input) {
return allMatches(Pattern.compile(regex), input);
}

/**
* Extract a list of all the signed integers in the input string.
* @param s input text
* @return immutable List of 64-bit integer matches
*/
public static List<Long> parseLongs(String s) {
return INTEGERS.matcher(s)
.results()
.map(MatchResult::group)
.map(Long::parseLong)
.toList();
}

/**
* Extract a list of all the signed integers in the input string.
* @param s input text
* @return immutable List of 32-bit integer matches
*/
public static List<Integer> parseInts(String s) {
return INTEGERS.matcher(s)
.results()
.map(MatchResult::group)
.map(Integer::parseInt)
.toList();
}

/**
* Extract a list of all the contiguous words (\w+)
* @param s input text
* @return immutable List of string matches
*/
public static List<String> parseStrings(String s) {
return WORDS.matcher(s)
.results()
.map(MatchResult::group)
.toList();
}

/**
* Split the given input on the given regex delimiter.
* @param regex Pattern to split on
* @param input input to split
* @return immutable List of string results after splitting
*/
public static List<String> split(String regex, String input) {
return List.of(input.split(regex));
}

/**
* Split the given input on the given regex delimiter.
* @param regex Pattern to split on
* @param input input to split
* @return immutable List of string results after splitting
*/
public static List<String> split(Pattern regex, String input) {
return List.of(regex.split(input));
}

/**
* Split the given input on whitespaces. Multiple whitespaces are treated as one.
* @param input input string
* @return immutable List of strings
*/
public static List<String> split(String input) {
return split(WHITESPACES, input);
}

}
70 changes: 70 additions & 0 deletions src/main/java/org/togetherjava/aoc/core/math/Combinatorics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.togetherjava.aoc.core.math;


import java.util.ArrayList;
import java.util.List;

public class Combinatorics {

/**
* Arrange n from k with repetition allowed.
* <br>
* Permutations with repetitions allowed.
*
* @param n permutation resulting size (arrange n)
* @param kValues from k values
* @return all permutations, with repetitions allowed, of length n
* @param <T> type of elements to permute
*/
public static <T> List<List<T>> arrangeWithRepetition(int n, List<T> kValues) {
List<List<T>> permutations = new ArrayList<>();
arrangeWithRepetitionRecursive(kValues, new ArrayList<>(), n, permutations);
return permutations;
}

private static <T> void arrangeWithRepetitionRecursive(List<T> values, List<T> current, int remaining, List<List<T>> permutations) {
if (remaining == 0) {
permutations.add(new ArrayList<>(current));
return;
}

for (T value : values) {
current.add(value);
arrangeWithRepetitionRecursive(values, current, remaining - 1, permutations);
current.remove(current.size() - 1);
}
}

public static <T> List<List<T>> arrange(List<T> values, int length) {
List<List<T>> permutations = new ArrayList<>();
arrangeHelper(values, new ArrayList<>(), length, permutations);
return permutations;
}

private static <T> void arrangeHelper(List<T> values, List<T> current, int remaining, List<List<T>> permutations) {
if (remaining == 0) {
permutations.add(new ArrayList<>(current));
return;
}

for (T value : values) {
if (current.contains(value)) {
continue;
}
current.add(value);
arrangeHelper(values, current, remaining - 1, permutations);
current.remove(current.size() - 1);
}
}

public static List<String> getRotations(String s) {
List<String> rotations = new ArrayList<>();
int length = s.length();
for (int i = 0; i < length; i++) {
String rotated = s.substring(i) + s.substring(0, i);
rotations.add(rotated);
}
return rotations;
}

}
93 changes: 93 additions & 0 deletions src/main/java/org/togetherjava/aoc/core/math/Direction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.togetherjava.aoc.core.math;


import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public enum Direction {
NORTH(0, 1, 0),
NORTH_EAST(1, 1, 45),
EAST(1, 0, 90),
SOUTH_EAST(1, -1, 135),
SOUTH(0, -1, 180),
SOUTH_WEST(-1, -1, 225),
WEST(-1, 0, 270),
NORTH_WEST(-1, 1, 315);

private final int x;
private final int y;
private final int angleInDegrees;

Direction(int x, int y, int angleInDegrees) {
this.x = x;
this.y = y;
this.angleInDegrees = angleInDegrees;
}

private static final List<Direction> DIRECTIONS = Stream.of(values())
.sorted(Comparator.comparingInt(Direction::getAngleInDegrees)).toList();
private static final List<Direction> CARDINAL = List.of(NORTH, EAST, SOUTH, WEST);
private static final List<Direction> ORDINAL = List.of(NORTH_EAST, SOUTH_EAST, SOUTH_WEST, NORTH_WEST);
private static final Map<Integer, Direction> ANGLE_MAP = DIRECTIONS.stream()
.collect(Collectors.toMap(Direction::getAngleInDegrees, Function.identity()));

public static Direction getByAngle(int angle) {
return ANGLE_MAP.get(angle % 360);
}

public static Direction getByPosition(int x, int y) {
for (Direction direction : DIRECTIONS) {
if (direction.x == x && direction.y == y) {
return direction;
}
}
return null;
}

public static List<Direction> getAll() {
return DIRECTIONS;
}

public static List<Direction> getCardinal() {
return CARDINAL;
}

public static List<Direction> getOrdinal() {
return ORDINAL;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

public int getAngleInDegrees() {
return angleInDegrees;
}

public Direction rotate(int angleInDegrees) {
return Direction.getByAngle(this.angleInDegrees + angleInDegrees);
}

public Direction rotateRight() {
return rotate(90);
}

public Direction rotateLeft() {
return rotate(-90);
}

public Direction opposite() {
return rotate(180);
}



}
21 changes: 21 additions & 0 deletions src/main/java/org/togetherjava/aoc/core/math/Distance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.togetherjava.aoc.core.math;

public class Distance {
public static double euclidean(Point a, Point b) {
double dx = a.x() - b.x();
double dy = a.y() - b.y();
return Math.sqrt(dx * dx + dy * dy);
}

public static double taxicab(Point a, Point b) {
double dx = Math.abs(a.x() - b.x());
double dy = Math.abs(a.y() - b.y());
return dx + dy;
}

public static double chebyshev(Point a, Point b) {
double dx = Math.abs(a.x() - b.x());
double dy = Math.abs(a.y() - b.y());
return Math.max(dx, dy);
}
}
15 changes: 15 additions & 0 deletions src/main/java/org/togetherjava/aoc/core/math/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.togetherjava.aoc.core.math;

public record Point(double x, double y) {

public static final Point ORIGIN = new Point(0, 0);

public Point move(double x, double y) {
return new Point(this.x + x, this.y + y);
}

public static Point of(double x, double y) {
return new Point(x, y);
}

}
Loading