Skip to content

Commit

Permalink
More reformatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
eamonnmcmanus committed Dec 6, 2024
1 parent d3a3f57 commit 448f488
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 166 deletions.
5 changes: 4 additions & 1 deletion src/advent2022/Puzzle9.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ record Coord(int x, int y) {}
record Move(Dir dir, int amount) {}

enum Dir {
R(+1, 0), L(-1, 0), U(0, -1), D(0, +1);
R(+1, 0),
L(-1, 0),
U(0, -1),
D(0, +1);

static final ImmutableMap<String, Dir> NAME_TO_DIR =
stream(values()).collect(toImmutableMap(Dir::name, dir -> dir));
Expand Down
6 changes: 5 additions & 1 deletion src/advent2023/Puzzle14.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ public static void main(String[] args) throws Exception {
// cycleStart + (i - cycleStart - 1) % (seen.size() - cycleStart).
System.out.println("Cycle after " + seen.size() + " iterations, starting at " + cycleStart);
int billionI = cycleStart + (1_000_000_000 - cycleStart - 1) % (seen.size() - cycleStart);
System.out.println("Load for billionth same as for i=" + billionI + " = " + load(seenList.get(billionI).chars));
System.out.println(
"Load for billionth same as for i="
+ billionI
+ " = "
+ load(seenList.get(billionI).chars));
}
}

Expand Down
78 changes: 43 additions & 35 deletions src/advent2023/Puzzle16.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ private static int solve(Beam startBeam, List<String> lines) {
}
queue.addAll(advance(beam, lines));
}
Set<Coord> tiles = beams.stream().map(b -> new Coord(b.x, b.y)).collect(toCollection(TreeSet::new));
Set<Coord> tiles =
beams.stream().map(b -> new Coord(b.x, b.y)).collect(toCollection(TreeSet::new));

return tiles.size();
}
Expand All @@ -65,38 +66,41 @@ private static int solve(Beam startBeam, List<String> lines) {
*/
private static List<Beam> advance(Beam beam, List<String> lines) {
char c = lines.get(beam.y).charAt(beam.x);
List<Beam> beams = switch (c) {
case '.' -> List.of(beam.advance());
case '-' -> {
if (beam.dir == Dir.LEFT || beam.dir == Dir.RIGHT) {
yield List.of(beam.advance());
} else {
yield List.of(beam.moveLeft(), beam.moveRight());
}
}
case '|' -> {
if (beam.dir == Dir.UP || beam.dir == Dir.DOWN) {
yield List.of(beam.advance());
} else {
yield List.of(beam.moveUp(), beam.moveDown());
}
}
case '/' -> List.of(
switch (beam.dir) {
case LEFT -> beam.moveDown();
case RIGHT -> beam.moveUp();
case UP -> beam.moveRight();
case DOWN -> beam.moveLeft();
});
case '\\' -> List.of(
switch (beam.dir) {
case LEFT -> beam.moveUp();
case RIGHT -> beam.moveDown();
case UP -> beam.moveLeft();
case DOWN -> beam.moveRight();
});
default -> throw new AssertionError(c);
};
List<Beam> beams =
switch (c) {
case '.' -> List.of(beam.advance());
case '-' -> {
if (beam.dir == Dir.LEFT || beam.dir == Dir.RIGHT) {
yield List.of(beam.advance());
} else {
yield List.of(beam.moveLeft(), beam.moveRight());
}
}
case '|' -> {
if (beam.dir == Dir.UP || beam.dir == Dir.DOWN) {
yield List.of(beam.advance());
} else {
yield List.of(beam.moveUp(), beam.moveDown());
}
}
case '/' ->
List.of(
switch (beam.dir) {
case LEFT -> beam.moveDown();
case RIGHT -> beam.moveUp();
case UP -> beam.moveRight();
case DOWN -> beam.moveLeft();
});
case '\\' ->
List.of(
switch (beam.dir) {
case LEFT -> beam.moveUp();
case RIGHT -> beam.moveDown();
case UP -> beam.moveLeft();
case DOWN -> beam.moveRight();
});
default -> throw new AssertionError(c);
};
return beams.stream()
.filter(b -> b.x >= 0 && b.x < lines.get(0).length() && b.y >= 0 && b.y < lines.size())
.toList();
Expand Down Expand Up @@ -125,7 +129,8 @@ Beam moveRight() {
}

record Coord(int x, int y) implements Comparable<Coord> {
private static final Comparator<Coord> COMPARATOR = Comparator.comparing(Coord::y).thenComparing(Coord::x);
private static final Comparator<Coord> COMPARATOR =
Comparator.comparing(Coord::y).thenComparing(Coord::x);

@Override
public int compareTo(Coord that) {
Expand All @@ -134,7 +139,10 @@ public int compareTo(Coord that) {
}

enum Dir {
LEFT(-1, 0), RIGHT(+1, 0), UP(0, -1), DOWN(0, +1);
LEFT(-1, 0),
RIGHT(+1, 0),
UP(0, -1),
DOWN(0, +1);

final int deltaX;
final int deltaY;
Expand Down
8 changes: 6 additions & 2 deletions src/advent2023/Puzzle17.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ void solve(int minSteps, int maxSteps) {
if (state.steps < maxSteps) {
Position forward = pos.advance();
if (validPosition(forward)) {
queue.add(new State(state.cost + cells[forward.i][forward.j].cost, forward, state.steps + 1));
queue.add(
new State(state.cost + cells[forward.i][forward.j].cost, forward, state.steps + 1));
}
}
}
Expand Down Expand Up @@ -102,7 +103,10 @@ public int compareTo(State that) {
record PositionAndSteps(Position position, int steps) {}

enum Dir {
LEFT(0, -1), RIGHT(0, +1), UP(-1, 0), DOWN(+1, 0);
LEFT(0, -1),
RIGHT(0, +1),
UP(-1, 0),
DOWN(+1, 0);

private int deltaI;
private int deltaJ;
Expand Down
133 changes: 72 additions & 61 deletions src/advent2023/Puzzle18.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,21 @@ public static void main(String[] args) throws Exception {
String lineString = new String(in.readAllBytes(), UTF_8);
List<String> lines = List.of(lineString.split("\n"));
for (boolean part2 : new boolean[] {false, true}) {
List<Step> steps = lines.stream()
.map(LINE_PATTERN::matcher)
.peek(m -> {
if (!m.matches()) {
throw new AssertionError(m);
}
}).
map(m -> part2
? Step.fromHex(Integer.parseInt(m.group(3), 16))
: new Step(
NAME_TO_DIR.get(m.group(1)),
Integer.parseInt(m.group(2))))
.toList();
List<Step> steps =
lines.stream()
.map(LINE_PATTERN::matcher)
.peek(
m -> {
if (!m.matches()) {
throw new AssertionError(m);
}
})
.map(
m ->
part2
? Step.fromHex(Integer.parseInt(m.group(3), 16))
: new Step(NAME_TO_DIR.get(m.group(1)), Integer.parseInt(m.group(2))))
.toList();
solve(steps);
}
}
Expand All @@ -65,38 +67,39 @@ private static void solve(List<Step> steps) {
// literally have the coordinates of the vertices of a polygon so we can just plug those into
// the formula. Because the path has thickness, there's a little extra complexity but not much.
Point point = new Point(0, 0);
Map<Point, VLine> upLines = new TreeMap<>(); // line going up from the given point
Map<Point, VLine> downLines = new TreeMap<>(); // line going down from the given point
Map<Point, VLine> upLines = new TreeMap<>(); // line going up from the given point
Map<Point, VLine> downLines = new TreeMap<>(); // line going down from the given point
Map<Point, HLine> rightLines = new TreeMap<>(); // line going right from the given point
for (Step step : steps) {
Point newPoint = switch (step.dir) {
case UP -> {
Point p = new Point(point.x, point.y - step.n);
VLine line = new VLine(point.x, p.y, point.y);
upLines.put(point, line);
downLines.put(p, line);
yield p;
}
case DOWN -> {
Point p = new Point(point.x, point.y + step.n);
VLine line = new VLine(point.x, point.y, p.y);
upLines.put(p, line);
downLines.put(point, line);
yield p;
}
case LEFT -> {
Point p = new Point(point.x - step.n, point.y);
HLine line = new HLine(p.x, point.x);
rightLines.put(p, line);
yield p;
}
case RIGHT -> {
Point p = new Point(point.x + step.n, point.y);
HLine line = new HLine(point.x, p.x);
rightLines.put(point, line);
yield p;
}
};
Point newPoint =
switch (step.dir) {
case UP -> {
Point p = new Point(point.x, point.y - step.n);
VLine line = new VLine(point.x, p.y, point.y);
upLines.put(point, line);
downLines.put(p, line);
yield p;
}
case DOWN -> {
Point p = new Point(point.x, point.y + step.n);
VLine line = new VLine(point.x, point.y, p.y);
upLines.put(p, line);
downLines.put(point, line);
yield p;
}
case LEFT -> {
Point p = new Point(point.x - step.n, point.y);
HLine line = new HLine(p.x, point.x);
rightLines.put(p, line);
yield p;
}
case RIGHT -> {
Point p = new Point(point.x + step.n, point.y);
HLine line = new HLine(point.x, p.x);
rightLines.put(point, line);
yield p;
}
};
point = newPoint;
}
int minY = rightLines.keySet().stream().mapToInt(Point::y).min().getAsInt();
Expand All @@ -107,16 +110,16 @@ private static void solve(List<Step> steps) {
for (int y = minY; y <= maxY; y++) {
long thisCount = 0;
int yy = y;
List<VLine> vLinesAtY = downLines.values().stream()
.filter(line -> line.includes(yy))
.sorted(xFirst)
.toList();
List<VLine> vLinesAtY =
downLines.values().stream().filter(line -> line.includes(yy)).sorted(xFirst).toList();
assert !vLinesAtY.isEmpty();
Integer start = null;
for (int i = 0; i < vLinesAtY.size(); i++) {
// If start, we were in an inside area. Now if we are crossing the middle of a vertical
// line, we count the width to here and set start to null. But if this is the end of a vertical line,
// we still count its width to here, but the rest depends on whether we are exiting the inside
// line, we count the width to here and set start to null. But if this is the end of a
// vertical line,
// we still count its width to here, but the rest depends on whether we are exiting the
// inside
// (second vertical line going in opposite direction), or remaining in it (second vertical
// line going in same direction). If remaining, we should set start to just after the second
// vertical line. If leaving, we should set start to null.
Expand Down Expand Up @@ -168,13 +171,16 @@ private static void solve(List<Step> steps) {
System.out.println("Filled cells " + count);
}

enum LineState {UP, DOWN, MID};
enum LineState {
UP,
DOWN,
MID
};

static LineState lineState(Point p, Map<Point, VLine> downLines, Map<Point, VLine> upLines) {
return downLines.containsKey(p)
? LineState.DOWN
: upLines.containsKey(p)
? LineState.UP : LineState.MID;
: upLines.containsKey(p) ? LineState.UP : LineState.MID;
}

record Point(int x, int y) implements Comparable<Point> {
Expand Down Expand Up @@ -211,18 +217,20 @@ boolean includes(int y) {
}
}

private static final Pattern LINE_PATTERN = Pattern.compile("([LRUD]) ([0-9]+) \\(#([0-9a-f]{6})\\)");
private static final Pattern LINE_PATTERN =
Pattern.compile("([LRUD]) ([0-9]+) \\(#([0-9a-f]{6})\\)");

record Step(Dir dir, int n) {
static Step fromHex(int hex) {
int n = hex >> 4;
Dir dir = switch (hex & 15) {
case 0 -> Dir.RIGHT;
case 1 -> Dir.DOWN;
case 2 -> Dir.LEFT;
case 3 -> Dir.UP;
default -> throw new AssertionError(hex & 15);
};
Dir dir =
switch (hex & 15) {
case 0 -> Dir.RIGHT;
case 1 -> Dir.DOWN;
case 2 -> Dir.LEFT;
case 3 -> Dir.UP;
default -> throw new AssertionError(hex & 15);
};
return new Step(dir, n);
}
}
Expand All @@ -231,7 +239,10 @@ static Step fromHex(int hex) {
ImmutableMap.of("L", Dir.LEFT, "R", Dir.RIGHT, "U", Dir.UP, "D", Dir.DOWN);

enum Dir {
LEFT(-1, 0), RIGHT(+1, 0), UP(0, -1), DOWN(0, +1);
LEFT(-1, 0),
RIGHT(+1, 0),
UP(0, -1),
DOWN(0, +1);

private final int deltaX;
private final int deltaY;
Expand Down
Loading

0 comments on commit 448f488

Please sign in to comment.