Skip to content

Commit

Permalink
feat: solve day 17 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 27, 2024
1 parent 3a57e55 commit ac7e203
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringJoiner;

import java.util.regex.Pattern;
Expand All @@ -31,9 +29,9 @@ public class ChronospatialComputer {
private List<ProgramState> programStates = new ArrayList<>();

public ChronospatialComputer(List<String> inputs) {
a = Integer.parseInt(inputs.get(0).substring(12));
b = Integer.parseInt(inputs.get(1).substring(12));
c = Integer.parseInt(inputs.get(2).substring(12));
a = Long.parseLong(inputs.get(0).substring(12));
b = Long.parseLong(inputs.get(1).substring(12));
c = Long.parseLong(inputs.get(2).substring(12));

expectedProgram = inputs.get(4).replace("Program: ", "");
program = Arrays.stream(expectedProgram.split(",")).mapToInt(Integer::valueOf).toArray();
Expand Down Expand Up @@ -83,28 +81,6 @@ public long solveB() {
// Parámetros recursivos:
// - octal string: currentOctalDigit + octalDigit

/*
String result;
StringBuilder octalBuilder = new StringBuilder();
for(int digit = 0; digit < 16; digit++) {
String currentOctalDigit = octalBuilder.toString();
for(int octalDigit = 0; octalDigit < 8; octalDigit++) {
this.a = Long.parseLong(currentOctalDigit + octalDigit, 8);
this.b = 0;
this.c = 0;
result = solveA();
if(expectedProgram.endsWith(result)) {
octalBuilder.append(octalDigit);
break;
}
}
}*/

return findRegistryA2(0, StringUtils.EMPTY, StringUtils.EMPTY);

Expand All @@ -113,10 +89,11 @@ public long solveB() {

private long findRegistryA2(int digit, String currentOctalNumber, String output) {

/*
if(digit == 11) {
System.out.printf("oct: %s -> %s", currentOctalNumber, output);
System.out.println();
}
}*/

if(!expectedProgram.endsWith(output)) {
return -1;
Expand Down
55 changes: 50 additions & 5 deletions src/main/java/com/adventofcode/flashk/day17/Program.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,47 @@
package com.adventofcode.flashk.day17;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;

public class Program {

public long a;
public long b;
public long c;
private long a;
private long b;
private long c;
private final String expectedProgram;

public Program(List<String> inputs) {
a = Integer.parseInt(inputs.get(0).substring(12));
b = Integer.parseInt(inputs.get(1).substring(12));
c = Integer.parseInt(inputs.get(2).substring(12));

expectedProgram = inputs.get(4).replace("Program: ", "");
}

public long solveB() {
return findRegistryA2(StringUtils.EMPTY, StringUtils.EMPTY);
}

public String execute() {
return run();
}

// Importante: este programa solo representa el programa de input.data
// Cualquier otro programa tendría un set de instrucciones diferente.
public String execute() {
private String execute(long a) {
this.a = a;
this.b = 0;
this.c = 0;
return run();
}

private String run() {

StringJoiner joiner = new StringJoiner(",");

while(a != 0) {
b = a % 8; // Se guarda en b 0-7, es decir, un número de 3 bits
b = b ^ 3; // bitwise xor entre lo que valga b y "011"
Expand All @@ -29,7 +52,29 @@ public String execute() {
joiner.add(String.valueOf(b % 8));
}

System.out.println(joiner.toString());
return joiner.toString();
}

private long findRegistryA2(String currentOctalNumber, String output) {

if(!expectedProgram.endsWith(output)) {
return -1;
} else if(expectedProgram.equals(output)){
return Long.parseLong(currentOctalNumber, 8);
} else if(currentOctalNumber.length() == 16) {
return -1;
}

for(int octalDigit = 0; octalDigit < 8; octalDigit++) {
long numberA = Long.parseLong(currentOctalNumber + octalDigit, 8);

String partialOutput = execute(numberA);
long result = findRegistryA2(currentOctalNumber+octalDigit, partialOutput);
if(result != -1) {
return result;
}
}

return -1;
}
}
25 changes: 25 additions & 0 deletions src/test/java/com/adventofcode/flashk/day17/Day17Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,29 @@ public void testSolvePart2Input() {

}

@Test
@Order(4)
@Tag(TestTag.PART_2)
@Tag(TestTag.INPUT)
@DisplayName(TestDisplayName.PART_2_INPUT)
@Disabled("[Disabled] Day 17 - testSolvePart2Input: Work in Progress")
public void part2InputWithProgramTest() {

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

Program program = new Program(inputs);

// Algunas consideraciones:
// La primera operación con los datos reales es 2,4
// Esto es, opcode 2, operator 4:
// - opcode 2 implica BST, esto es b = comboOperand % 8;
// - operator 4 implica que el valor de comboOperand va a ser el registro A
// Por lo tanto, valga lo que valga A, b adquirirá el valor de A % 8

// Help: https://www.reddit.com/r/adventofcode/comments/1hn01ke/2024_day_17_part_2_code_works_until_certain/
assertEquals(216549846240877L, program.solveB());

}

}

0 comments on commit ac7e203

Please sign in to comment.