Day 1
This commit is contained in:
183
src/main/java/de/advent_of_code_2025/one/Main.java
Normal file
183
src/main/java/de/advent_of_code_2025/one/Main.java
Normal file
@@ -0,0 +1,183 @@
|
||||
package de.advent_of_code_2025.one;
|
||||
|
||||
import de.advent_of_code_2025.util.InputReader;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class Main {
|
||||
|
||||
private static enum Direction {
|
||||
LEFT, RIGHT;
|
||||
}
|
||||
|
||||
private static final class DialPosition {
|
||||
private final int value;
|
||||
private DialPosition next;
|
||||
private DialPosition previous;
|
||||
|
||||
public DialPosition(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public void setNext(DialPosition next) {
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
public void setPrevious(DialPosition previous) {
|
||||
this.previous = previous;
|
||||
}
|
||||
|
||||
public DialPosition next() {
|
||||
return this.next;
|
||||
}
|
||||
|
||||
public DialPosition previous() {
|
||||
return this.previous;
|
||||
}
|
||||
|
||||
public boolean isZero() {
|
||||
return this.value == 0;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class Dial {
|
||||
private DialPosition current;
|
||||
private DialPosition reset;
|
||||
|
||||
public Dial(DialPosition current) {
|
||||
this.current = current;
|
||||
this.reset = current;
|
||||
}
|
||||
|
||||
public int dialEnd(Direction direction, int moves) {
|
||||
if(direction == Direction.LEFT) {
|
||||
for(int i = 0; i < moves; i++) {
|
||||
this.current = this.current.previous();
|
||||
}
|
||||
}
|
||||
else {
|
||||
for(int i = 0; i < moves; i++) {
|
||||
this.current = this.current.next();
|
||||
}
|
||||
}
|
||||
|
||||
return this.current.isZero() ? 1 : 0;
|
||||
}
|
||||
|
||||
public int dialAll(Direction direction, int moves) {
|
||||
int zeroCounter = 0;
|
||||
|
||||
if(direction == Direction.LEFT) {
|
||||
for(int i = 0; i < moves; i++) {
|
||||
this.current = this.current.previous();
|
||||
|
||||
if(this.current.isZero()) {
|
||||
zeroCounter += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
for(int i = 0; i < moves; i++) {
|
||||
this.current = this.current.next();
|
||||
|
||||
if(this.current.isZero()) {
|
||||
zeroCounter += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return zeroCounter;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
this.current = this.reset;
|
||||
}
|
||||
}
|
||||
|
||||
private static final record Command(Direction direction, int moves) {
|
||||
public int apply(Dial dial) {
|
||||
return dial.dialEnd(this.direction, this.moves);
|
||||
}
|
||||
|
||||
public int applyAll(Dial dial) {
|
||||
return dial.dialAll(this.direction, this.moves);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
final List<String> lines = InputReader.read(args);
|
||||
final DialPosition start = setupDialPositions();
|
||||
final Dial dial = new Dial(start);
|
||||
final List<Command> commands = setupCommands(lines);
|
||||
int zeroCounter = 0;
|
||||
int zeroCounterAll = 0;
|
||||
|
||||
// Part 1
|
||||
|
||||
for (Command c : commands) {
|
||||
zeroCounter += c.apply(dial);
|
||||
}
|
||||
|
||||
System.out.println(zeroCounter);
|
||||
|
||||
// Part 2
|
||||
|
||||
dial.reset();
|
||||
|
||||
for (Command c : commands) {
|
||||
zeroCounterAll += c.applyAll(dial);
|
||||
}
|
||||
|
||||
System.out.println(zeroCounterAll);
|
||||
}
|
||||
|
||||
private static final List<Command> setupCommands(List<String> lines) {
|
||||
return lines.stream().map(line -> {
|
||||
Direction direction = Direction.RIGHT;
|
||||
int moves = 0;
|
||||
|
||||
if (line.startsWith("L")) {
|
||||
direction = Direction.LEFT;
|
||||
}
|
||||
|
||||
moves = Integer.parseInt(line.substring(1));
|
||||
|
||||
return new Command(direction, moves);
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static final DialPosition setupDialPositions() {
|
||||
final List<DialPosition> positions = new ArrayList<>(100);
|
||||
|
||||
IntStream.range(0, 100).mapToObj(DialPosition::new).forEach(positions::add);
|
||||
|
||||
DialPosition current = null;
|
||||
DialPosition next = null;
|
||||
DialPosition previous = null;
|
||||
|
||||
for(int i = 0; i < 100; i++) {
|
||||
current = positions.get(i);
|
||||
|
||||
if(i == 0) {
|
||||
next = positions.get(i + 1);
|
||||
previous = positions.getLast();
|
||||
}
|
||||
else if(i == positions.size() - 1) {
|
||||
next = positions.getFirst();
|
||||
previous = positions.get(i - 1);
|
||||
}
|
||||
else {
|
||||
next = positions.get(i + 1);
|
||||
previous = positions.get(i - 1);
|
||||
}
|
||||
|
||||
current.setNext(next);
|
||||
current.setPrevious(previous);
|
||||
}
|
||||
|
||||
return positions.get(50);
|
||||
}
|
||||
}
|
||||
6
src/main/resources/five/input_example.txt
Normal file
6
src/main/resources/five/input_example.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
3-5
|
||||
10-14
|
||||
12-18
|
||||
16-20
|
||||
|
||||
1
|
||||
4493
src/main/resources/one/input.txt
Normal file
4493
src/main/resources/one/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
10
src/main/resources/one/input_example.txt
Normal file
10
src/main/resources/one/input_example.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
L68
|
||||
L30
|
||||
R48
|
||||
L5
|
||||
R60
|
||||
L55
|
||||
L1
|
||||
L99
|
||||
R14
|
||||
L82
|
||||
Reference in New Issue
Block a user