Skip to content

Commit

Permalink
aoc 2016 refactor done
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCK committed Dec 10, 2024
1 parent 4eee944 commit f3b616a
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 346 deletions.
4 changes: 2 additions & 2 deletions adventofcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@
[20162302solution]: src/main/java/org/ck/adventofcode/year2016/Day23.java
[20162401solution]: src/main/java/org/ck/adventofcode/year2016/Day24.java
[20162402solution]: src/main/java/org/ck/adventofcode/year2016/Day24.java
[20162501solution]: src/main/java/org/ck/adventofcode/year2016/day25/Part1.java
[20162501solution]: src/main/java/org/ck/adventofcode/year2016/Day25.java

[20160101tests]: src/test/java/org/ck/adventofcode/year2016/Day01Test.java
[20160102tests]: src/test/java/org/ck/adventofcode/year2016/Day01Test.java
Expand Down Expand Up @@ -404,7 +404,7 @@
[20162302tests]: src/test/java/org/ck/adventofcode/year2016/Day23Test.java
[20162401tests]: src/test/java/org/ck/adventofcode/year2016/Day24Test.java
[20162402tests]: src/test/java/org/ck/adventofcode/year2016/Day24Test.java
[20162501tests]: src/test/java/org/ck/adventofcode/year2016/day25/Part1Test.java
[20162501tests]: src/test/java/org/ck/adventofcode/year2016/Day25Test.java

# 2017 (15/16)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
package org.ck.adventofcode.year2016;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import org.apache.commons.lang3.NotImplementedException;
import org.ck.adventofcode.util.AOCSolution;
import org.ck.adventofcode.year2016.common.AssemBunnyComputer;
import org.ck.codechallengelib.annotation.Solution;

@Solution(
id = 20162501,
name = "Day 25: Clock Signal",
url = "https://adventofcode.com/2016/day/25",
category = "2016")
public class Day25 extends AOCSolution {
private static final int FOREVER_COUNT = 100;

@Override
protected void runPartOne(final Scanner in) {
run(in);
Expand All @@ -15,5 +27,37 @@ protected void runPartTwo(final Scanner in) {
throw new NotImplementedException();
}

private void run(final Scanner in) {}
private void run(final Scanner in) {
final List<String> commands = new ArrayList<>();

while (in.hasNextLine()) {
commands.add(in.nextLine());
}

int a = 0;
while (true) {
final AssemBunnyComputer computer =
new AssemBunnyComputer(commands, Map.of("a", a, "b", 0, "c", 0, "d", 0));
final List<Integer> output = computer.runWithWantedOutput(FOREVER_COUNT, 1000000);

if (output.size() == FOREVER_COUNT) {
boolean matches = true;

for (int i = 0; i < FOREVER_COUNT; ++i) {
if (output.get(i) != i % 2) {
matches = false;
break;
}
}

if (matches) {
break;
}
}

++a;
}

print(a);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@ public AssemBunnyComputer(
}

public void run() {
runWithWantedOutput(Integer.MAX_VALUE, Integer.MAX_VALUE);
}

public List<Integer> runWithWantedOutput(final int wantedOutput, final int maxSteps) {
final List<Integer> output = new ArrayList<>();

int p = 0;
while (p < commands.size()) {
p += commands.get(p).run(registers, commands, p);
while (p < commands.size() && p < maxSteps && output.size() < wantedOutput) {
p += commands.get(p).run(registers, commands, p, output);
}

return output;
}

public int getRegisterValue(final String register) {
Expand All @@ -44,6 +52,7 @@ public static Command of(final String line) {
case "dec" -> new Decrement(matcher);
case "jnz" -> new Jump(matcher);
case "tgl" -> new Toggle(matcher);
case "out" -> new Output(matcher);
default -> null;
};
}
Expand All @@ -52,7 +61,10 @@ public static Command of(final String line) {
}

public abstract int run(
final Map<String, Integer> registers, final List<Command> commands, final int p);
final Map<String, Integer> registers,
final List<Command> commands,
final int p,
final List<Integer> output);

public abstract Command toggle();
}
Expand All @@ -70,7 +82,10 @@ public Increment(final String register) {

@Override
public int run(
final Map<String, Integer> registers, final List<Command> commands, final int p) {
final Map<String, Integer> registers,
final List<Command> commands,
final int p,
final List<Integer> output) {
registers.put(register, registers.get(register) + 1);

return 1;
Expand All @@ -95,7 +110,10 @@ public Decrement(final String register) {

@Override
public int run(
final Map<String, Integer> registers, final List<Command> commands, final int p) {
final Map<String, Integer> registers,
final List<Command> commands,
final int p,
final List<Integer> output) {
registers.put(register, registers.get(register) - 1);

return 1;
Expand All @@ -116,7 +134,10 @@ public Toggle(final Matcher matcher) {

@Override
public int run(
final Map<String, Integer> registers, final List<Command> commands, final int p) {
final Map<String, Integer> registers,
final List<Command> commands,
final int p,
final List<Integer> output) {
final int index = p + registers.get(register);

if (index < commands.size()) {
Expand All @@ -132,6 +153,29 @@ public Command toggle() {
}
}

private static final class Output extends Command {
private final String register;

public Output(Matcher matcher) {
register = matcher.group("firstOperand");
}

@Override
public int run(
final Map<String, Integer> registers,
List<Command> commands,
final int p,
final List<Integer> output) {
output.add(registers.get(register));
return 1;
}

@Override
public Command toggle() {
return new Increment(register);
}
}

private static final class Copy extends Command {
private String firstRegister;
private Integer firstIntermediate;
Expand Down Expand Up @@ -166,7 +210,10 @@ public Copy(

@Override
public int run(
final Map<String, Integer> registers, final List<Command> commands, final int p) {
final Map<String, Integer> registers,
final List<Command> commands,
final int p,
final List<Integer> output) {
if (secondRegister != null) {
registers.put(
secondRegister,
Expand Down Expand Up @@ -221,7 +268,10 @@ public Jump(

@Override
public int run(
final Map<String, Integer> registers, final List<Command> commands, final int p) {
final Map<String, Integer> registers,
final List<Command> commands,
final int p,
final List<Integer> output) {
if ((firstIntermediate != null && firstIntermediate != 0)
|| (firstIntermediate == null && registers.get(firstRegister) != 0)) {
return secondIntermediate != null ? secondIntermediate : registers.get(secondRegister);
Expand Down
Loading

0 comments on commit f3b616a

Please sign in to comment.