From f3b616a100db1fe0524fc6dc8836a2dd80483fd4 Mon Sep 17 00:00:00 2001 From: TheCK Date: Tue, 10 Dec 2024 17:32:14 +0100 Subject: [PATCH] aoc 2016 refactor done --- adventofcode/README.md | 4 +- .../org/ck/adventofcode/year2016/Day25.java | 46 ++- .../year2016/common/AssemBunnyComputer.java | 66 +++- .../ck/adventofcode/year2016/day25/Part1.java | 320 ------------------ .../ck/adventofcode/year2016/Day25Test.java | 2 - .../year2016/day25/Part1Test.java | 13 - 6 files changed, 105 insertions(+), 346 deletions(-) delete mode 100644 adventofcode/src/main/java/org/ck/adventofcode/year2016/day25/Part1.java delete mode 100644 adventofcode/src/test/java/org/ck/adventofcode/year2016/day25/Part1Test.java diff --git a/adventofcode/README.md b/adventofcode/README.md index 9cb67fad..650d78a0 100644 --- a/adventofcode/README.md +++ b/adventofcode/README.md @@ -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 @@ -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) diff --git a/adventofcode/src/main/java/org/ck/adventofcode/year2016/Day25.java b/adventofcode/src/main/java/org/ck/adventofcode/year2016/Day25.java index 6b00e91a..8020f0e6 100644 --- a/adventofcode/src/main/java/org/ck/adventofcode/year2016/Day25.java +++ b/adventofcode/src/main/java/org/ck/adventofcode/year2016/Day25.java @@ -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); @@ -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 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 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); + } } diff --git a/adventofcode/src/main/java/org/ck/adventofcode/year2016/common/AssemBunnyComputer.java b/adventofcode/src/main/java/org/ck/adventofcode/year2016/common/AssemBunnyComputer.java index 5cbcebda..8fa8d68f 100644 --- a/adventofcode/src/main/java/org/ck/adventofcode/year2016/common/AssemBunnyComputer.java +++ b/adventofcode/src/main/java/org/ck/adventofcode/year2016/common/AssemBunnyComputer.java @@ -19,10 +19,18 @@ public AssemBunnyComputer( } public void run() { + runWithWantedOutput(Integer.MAX_VALUE, Integer.MAX_VALUE); + } + + public List runWithWantedOutput(final int wantedOutput, final int maxSteps) { + final List 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) { @@ -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; }; } @@ -52,7 +61,10 @@ public static Command of(final String line) { } public abstract int run( - final Map registers, final List commands, final int p); + final Map registers, + final List commands, + final int p, + final List output); public abstract Command toggle(); } @@ -70,7 +82,10 @@ public Increment(final String register) { @Override public int run( - final Map registers, final List commands, final int p) { + final Map registers, + final List commands, + final int p, + final List output) { registers.put(register, registers.get(register) + 1); return 1; @@ -95,7 +110,10 @@ public Decrement(final String register) { @Override public int run( - final Map registers, final List commands, final int p) { + final Map registers, + final List commands, + final int p, + final List output) { registers.put(register, registers.get(register) - 1); return 1; @@ -116,7 +134,10 @@ public Toggle(final Matcher matcher) { @Override public int run( - final Map registers, final List commands, final int p) { + final Map registers, + final List commands, + final int p, + final List output) { final int index = p + registers.get(register); if (index < commands.size()) { @@ -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 registers, + List commands, + final int p, + final List 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; @@ -166,7 +210,10 @@ public Copy( @Override public int run( - final Map registers, final List commands, final int p) { + final Map registers, + final List commands, + final int p, + final List output) { if (secondRegister != null) { registers.put( secondRegister, @@ -221,7 +268,10 @@ public Jump( @Override public int run( - final Map registers, final List commands, final int p) { + final Map registers, + final List commands, + final int p, + final List output) { if ((firstIntermediate != null && firstIntermediate != 0) || (firstIntermediate == null && registers.get(firstRegister) != 0)) { return secondIntermediate != null ? secondIntermediate : registers.get(secondRegister); diff --git a/adventofcode/src/main/java/org/ck/adventofcode/year2016/day25/Part1.java b/adventofcode/src/main/java/org/ck/adventofcode/year2016/day25/Part1.java deleted file mode 100644 index 5f540e32..00000000 --- a/adventofcode/src/main/java/org/ck/adventofcode/year2016/day25/Part1.java +++ /dev/null @@ -1,320 +0,0 @@ -package org.ck.adventofcode.year2016.day25; - -import java.util.*; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -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 Part1 { - public static void main(String[] args) { - List commands = new ArrayList<>(); - - try (Scanner in = new Scanner(System.in)) { - while (in.hasNextLine()) { - commands.add(Command.of(in.nextLine())); - } - } - - int a = 0; - while (true) { - int finalA = a; - Map registers = - new HashMap<>() { - { - put("a", finalA); - put("b", 0); - put("c", 0); - put("d", 0); - } - }; - - List output = new ArrayList<>(); - - int p = 0; - int steps = 0; - while (p < commands.size() && output.size() < 100 && steps < 1000000) { - p += commands.get(p).run(registers, commands, p, output); - ++steps; - } - - if (output.size() == 100) { - boolean matches = true; - - for (int i = 0; i < 100; ++i) { - if (output.get(i) != i % 2) { - matches = false; - break; - } - } - - if (matches) { - break; - } - } - - ++a; - } - - System.out.println(a); - } - - private abstract static class Command { - private static final Pattern pattern = - Pattern.compile("([a-z]{3}) ([a-z]|-?\\d+)( (-?\\d+|[a-z]))?"); - - public static Command of(String line) { - final Matcher matcher = pattern.matcher(line); - - if (matcher.find()) { - String command = matcher.group(1); - - switch (command) { - case "cpy": - return new Copy(matcher); - case "inc": - return new Increment(matcher); - case "dec": - return new Decrement(matcher); - case "jnz": - return new Jump(matcher); - case "tgl": - return new Toggle(matcher); - case "out": - return new Output(matcher); - } - } - - return null; - } - - public abstract int run( - Map registers, - List commands, - final int p, - final List output); - - public abstract Command toggle(); - } - - private static final class Increment extends Command { - private final String register; - - public Increment(Matcher matcher) { - register = matcher.group(2); - } - - public Increment(String register) { - this.register = register; - } - - @Override - public int run( - final Map registers, - List commands, - final int p, - final List output) { - registers.put(register, registers.get(register) + 1); - - return 1; - } - - @Override - public Command toggle() { - return new Decrement(register); - } - } - - private static final class Decrement extends Command { - private final String register; - - public Decrement(Matcher matcher) { - register = matcher.group(2); - } - - public Decrement(String register) { - this.register = register; - } - - @Override - public int run( - final Map registers, - List commands, - final int p, - final List output) { - registers.put(register, registers.get(register) - 1); - - return 1; - } - - @Override - public Command toggle() { - return new Increment(register); - } - } - - private static final class Toggle extends Command { - private final String register; - - public Toggle(Matcher matcher) { - register = matcher.group(2); - } - - @Override - public int run( - final Map registers, - List commands, - final int p, - final List output) { - int index = p + registers.get(register); - - if (index < commands.size()) { - commands.set(index, commands.get(index).toggle()); - } - - return 1; - } - - @Override - public Command toggle() { - return new Increment(register); - } - } - - private static final class Output extends Command { - private final String register; - - public Output(Matcher matcher) { - register = matcher.group(2); - } - - @Override - public int run( - final Map registers, - List commands, - final int p, - final List 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; - - private String secondRegister; - private Integer secondIntermediate; - - public Copy(Matcher matcher) { - try { - firstRegister = null; - firstIntermediate = Integer.valueOf(matcher.group(2)); - } catch (NumberFormatException e) { - firstRegister = matcher.group(2); - firstIntermediate = null; - } - - secondRegister = matcher.group(4); - secondIntermediate = null; - } - - public Copy( - String firstRegister, - Integer firstIntermediate, - String secondRegister, - Integer secondIntermediate) { - this.firstRegister = firstRegister; - this.firstIntermediate = firstIntermediate; - - this.secondRegister = secondRegister; - this.secondIntermediate = secondIntermediate; - } - - @Override - public int run( - final Map registers, - List commands, - final int p, - final List output) { - if (secondRegister != null) { - registers.put( - secondRegister, - firstIntermediate != null ? firstIntermediate : registers.get(firstRegister)); - } - - return 1; - } - - @Override - public Command toggle() { - return new Jump(firstRegister, firstIntermediate, secondRegister, secondIntermediate); - } - } - - private static final class Jump extends Command { - private String firstRegister; - private Integer firstIntermediate; - - private String secondRegister; - private Integer secondIntermediate; - - public Jump(Matcher matcher) { - try { - firstRegister = null; - firstIntermediate = Integer.valueOf(matcher.group(2)); - } catch (NumberFormatException e) { - firstRegister = matcher.group(2); - firstIntermediate = null; - } - - try { - secondRegister = null; - secondIntermediate = Integer.valueOf(matcher.group(4)); - } catch (NumberFormatException e) { - secondRegister = matcher.group(4); - secondIntermediate = null; - } - } - - public Jump( - String firstRegister, - Integer firstIntermediate, - String secondRegister, - Integer secondIntermediate) { - this.firstRegister = firstRegister; - this.firstIntermediate = firstIntermediate; - - this.secondRegister = secondRegister; - this.secondIntermediate = secondIntermediate; - } - - @Override - public int run( - final Map registers, - List commands, - final int p, - final List output) { - if ((firstIntermediate != null && firstIntermediate != 0) - || (firstIntermediate == null && registers.get(firstRegister) != 0)) { - return secondIntermediate != null ? secondIntermediate : registers.get(secondRegister); - } - - return 1; - } - - @Override - public Command toggle() { - return new Copy(firstRegister, firstIntermediate, secondRegister, secondIntermediate); - } - } -} diff --git a/adventofcode/src/test/java/org/ck/adventofcode/year2016/Day25Test.java b/adventofcode/src/test/java/org/ck/adventofcode/year2016/Day25Test.java index 7bcbf100..90c4f04d 100644 --- a/adventofcode/src/test/java/org/ck/adventofcode/year2016/Day25Test.java +++ b/adventofcode/src/test/java/org/ck/adventofcode/year2016/Day25Test.java @@ -1,14 +1,12 @@ package org.ck.adventofcode.year2016; import org.ck.adventofcode.util.BaseAOCTest; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; class Day25Test extends BaseAOCTest { @ParameterizedTest @ValueSource(strings = {"01"}) - @Disabled void testOne(String name) throws Exception { runTest(new Day25()::partOne, "day25/%s".formatted(name)); } diff --git a/adventofcode/src/test/java/org/ck/adventofcode/year2016/day25/Part1Test.java b/adventofcode/src/test/java/org/ck/adventofcode/year2016/day25/Part1Test.java deleted file mode 100644 index 62aaba06..00000000 --- a/adventofcode/src/test/java/org/ck/adventofcode/year2016/day25/Part1Test.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.ck.adventofcode.year2016.day25; - -import org.ck.codechallengelib.testhelper.BaseTest; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -public class Part1Test extends BaseTest { - @ParameterizedTest - @ValueSource(strings = {"01"}) - public void test(String name) throws Exception { - runFileAsStdIn(Part1.class, name); - } -}