Skip to content

Commit

Permalink
refactor: clean day 17 part 1 code
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 17, 2024
1 parent 19a623e commit 87a0bd5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class ChronospatialComputer {
private int[] program;
private int instructionPointer;
private StringJoiner outJoiner;
private boolean allowReservedOperand;

public ChronospatialComputer(List<String> inputs) {
a = Integer.parseInt(inputs.get(0).substring(12));
Expand All @@ -32,11 +31,10 @@ public ChronospatialComputer(List<String> inputs) {

}

public String solveA(boolean allowReservedOperand) {
public String solveA() {

instructionPointer = 0;
outJoiner = new StringJoiner(",");
this.allowReservedOperand = allowReservedOperand;

int opcode;
int operator;
Expand Down Expand Up @@ -83,9 +81,6 @@ private int getComboOperand(int operator) {
case 6:
return c;
default:
if (allowReservedOperand) {
return operator;
}
throw new IllegalArgumentException("Invalid operator: "+operator);
}
}
Expand All @@ -94,8 +89,6 @@ private void adv(int operand) {
int comboOperand = getComboOperand(operand);
int power = (int) Math.pow(2, comboOperand);
a = a / power;

// TODO es posible que haya que truncar.
}

private void bxl(int operand) {
Expand Down
18 changes: 8 additions & 10 deletions src/test/java/com/adventofcode/flashk/day17/Day17Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.List;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
Expand Down Expand Up @@ -36,7 +35,7 @@ void part1Debug1Test() {
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SINGLE_SAMPLE);

ChronospatialComputer chronospatialComputer = new ChronospatialComputer(inputs);
chronospatialComputer.solveA(false);
chronospatialComputer.solveA();

assertEquals(1, chronospatialComputer.getB());
}
Expand All @@ -53,7 +52,7 @@ void part1Debug2Test() {

ChronospatialComputer chronospatialComputer = new ChronospatialComputer(inputs);

assertEquals("0,1,2", chronospatialComputer.solveA(false));
assertEquals("0,1,2", chronospatialComputer.solveA());
}

@Test
Expand All @@ -76,7 +75,7 @@ void part1Debug3Test() {

// Program: 0,1,5,4,3,0

assertEquals("4,2,5,6,7,7,7,7,3,1,0", chronospatialComputer.solveA(false));
assertEquals("4,2,5,6,7,7,7,7,3,1,0", chronospatialComputer.solveA());
assertEquals(0, chronospatialComputer.getA());
}

Expand All @@ -91,7 +90,7 @@ void part1Debug4Test() {
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SINGLE_SAMPLE_4);

ChronospatialComputer chronospatialComputer = new ChronospatialComputer(inputs);
chronospatialComputer.solveA(true);
chronospatialComputer.solveA();

// Se permite el operador reservado porque en este caso, el input tiene un operator 7 que hay que debuggear.

Expand All @@ -109,7 +108,7 @@ void part1Debug5Test() {
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SINGLE_SAMPLE_5);

ChronospatialComputer chronospatialComputer = new ChronospatialComputer(inputs);
chronospatialComputer.solveA(false);
chronospatialComputer.solveA();

assertEquals(44354, chronospatialComputer.getB());
}
Expand All @@ -126,7 +125,7 @@ public void testSolvePart1Sample() {

ChronospatialComputer chronospatialComputer = new ChronospatialComputer(inputs);

assertEquals("4,6,3,5,6,3,5,2,1,0", chronospatialComputer.solveA(false));
assertEquals("4,6,3,5,6,3,5,2,1,0", chronospatialComputer.solveA());
}

@Test
Expand All @@ -138,11 +137,10 @@ public void testSolvePart1Input() {

// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);

ChronospatialComputer chronospatialComputer = new ChronospatialComputer(inputs);
System.out.println("Solution: "+chronospatialComputer.solveA(false));

// 3,4,5,0,5,4,1,5,0 -> Not valid
assertEquals("6,7,5,2,1,3,5,1,7",chronospatialComputer.solveA(false));
assertEquals("6,7,5,2,1,3,5,1,7",chronospatialComputer.solveA());

}

Expand Down

0 comments on commit 87a0bd5

Please sign in to comment.