Skip to content

Commit 818c756

Browse files
committed
Solved day 23 part 1
1 parent a860256 commit 818c756

File tree

8 files changed

+186
-3
lines changed

8 files changed

+186
-3
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44

55
// project meta data
66
group 'de.havox_design.aoc2015'
7-
version '0.22.2'
7+
version '0.22.4'
88

99
// Switch to gradle "all" distribution.
1010
wrapper {

day23/src/main/java/de/havox_design/aoc2015/day23/MainClass.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class MainClass {
66
private static final Logger LOGGER = Logger.getLogger(MainClass.class.getName());
77

88
public static void main(String[] args) {
9-
LOGGER.info("Solution 1: 13");
10-
LOGGER.info("Solution 2: 23");
9+
LOGGER.info(() -> "Solution 1: " + Turing.solvePart1("day23.txt"));
10+
LOGGER.info(() -> "Solution 2: " + Turing.solvePart2("day23.txt"));
1111
}
1212
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package de.havox_design.aoc2015.day23;
2+
3+
public class Operation {
4+
public static final String OPERATION_HALF = "hlf";
5+
public static final String OPERATION_INCREMENT = "inc";
6+
public static final String OPERATION_JUMP = "jmp";
7+
public static final String OPERATION_JUMP_ON_EQUAL = "jie";
8+
public static final String OPERATION_JUMP_ON_ONE = "jio";
9+
public static final String OPERATION_TRIPLE = "tpl";
10+
11+
public static final String REGISTER_A = "a";
12+
public static final String REGISTER_B = "b";
13+
14+
String op;
15+
Integer value;
16+
String register;
17+
18+
@SuppressWarnings("squid:S3776")
19+
public void operate(State state) {
20+
switch (op) {
21+
case OPERATION_HALF:
22+
if (register.equals(REGISTER_A)) {
23+
state.registerA /= 2;
24+
} else {
25+
state.registerB /= 2;
26+
}
27+
state.processCount++;
28+
break;
29+
case OPERATION_TRIPLE:
30+
if (register.equals(REGISTER_A)) {
31+
state.registerA *= 3;
32+
} else {
33+
state.registerB *= 3;
34+
}
35+
state.processCount++;
36+
break;
37+
case OPERATION_INCREMENT:
38+
if (register.equals(REGISTER_A)) {
39+
state.registerA++;
40+
} else {
41+
state.registerB++;
42+
}
43+
state.processCount++;
44+
break;
45+
case OPERATION_JUMP:
46+
state.processCount += value;
47+
break;
48+
case OPERATION_JUMP_ON_EQUAL:
49+
if (register.equals(REGISTER_A) && (state.registerA % 2 == 0)) {
50+
state.processCount += value;
51+
} else if (register.equals(REGISTER_B) && (state.registerB % 2 == 0)) {
52+
state.processCount += value;
53+
} else {
54+
state.processCount++;
55+
}
56+
break;
57+
case OPERATION_JUMP_ON_ONE:
58+
if (register.equals(REGISTER_A) && state.registerA == 1) {
59+
state.processCount += value;
60+
} else if (register.equals(REGISTER_B) && state.registerB == 1) {
61+
state.processCount += value;
62+
} else {
63+
state.processCount++;
64+
}
65+
break;
66+
default:
67+
throw new IllegalStateException("unknown operation: " + op);
68+
}
69+
}
70+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package de.havox_design.aoc2015.day23;
2+
3+
public class State {
4+
int processCount = 0;
5+
int registerA = 0;
6+
int registerB = 0;
7+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package de.havox_design.aoc2015.day23;
2+
3+
import de.havox_design.aoc2015.utils.DataReader;
4+
5+
import java.util.List;
6+
import java.util.function.Function;
7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
9+
import java.util.stream.Stream;
10+
11+
public class Turing {
12+
private static final Pattern PATTERN = Pattern.compile("(\\w{3}) ([ab]|[+-]?\\d+)(, ([+-]?\\d+))?");
13+
14+
private final List<String> input;
15+
16+
public Turing(String fileName) {
17+
input = readData(fileName);
18+
}
19+
20+
public static int solvePart1(String fileName) {
21+
Turing instance = new Turing(fileName);
22+
return instance.solvePart1();
23+
}
24+
25+
public static int solvePart2(String fileName) {
26+
Turing instance = new Turing(fileName);
27+
return instance.solvePart2();
28+
}
29+
30+
public int solvePart1() {
31+
List<Operation> operations = tokenStream(input, PATTERN, this::parseOperation).toList();
32+
State state = new State();
33+
while (state.processCount < operations.size()) {
34+
Operation operation = operations.get(state.processCount);
35+
operation.operate(state);
36+
}
37+
return state.registerB;
38+
}
39+
40+
public int solvePart2() {
41+
return 0;
42+
}
43+
44+
private List<String> readData(String fileName) {
45+
return DataReader.readData(fileName, MainClass.class);
46+
}
47+
48+
private Operation parseOperation(Matcher matcher) {
49+
Operation operation = new Operation();
50+
operation.op = matcher.group(1);
51+
String first = matcher.group(2);
52+
if (first.length() == 1 && Character.isLetter(first.charAt(0))) {
53+
operation.register = first;
54+
} else {
55+
operation.value = Integer.parseInt(first);
56+
}
57+
String second = matcher.group(4);
58+
if (second != null && !second.isEmpty()) {
59+
operation.value = Integer.parseInt(second);
60+
}
61+
return operation;
62+
}
63+
64+
public <T> Stream<T> tokenStream(List<String> input, Pattern pattern, Function<Matcher, T> tokenGenerator) {
65+
return input
66+
.stream()
67+
.map(token -> matchRegex(pattern, token))
68+
.map(tokenGenerator);
69+
}
70+
71+
public Matcher matchRegex(final Pattern pattern, final CharSequence input) {
72+
final Matcher matcher = pattern.matcher(input);
73+
if (matcher.matches()) {
74+
return matcher;
75+
} else {
76+
throw new IllegalArgumentException("Input '" + input + "' does not match pattern " + pattern.pattern());
77+
}
78+
}
79+
}

day23/src/test/java/de/havox_design/aoc2015/day23/Day23Test.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package de.havox_design.aoc2015.day23;
22

3+
import org.junit.jupiter.api.Assertions;
34
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.Arguments;
7+
import org.junit.jupiter.params.provider.MethodSource;
8+
9+
import java.util.stream.Stream;
410

511
class Day23Test {
612

@@ -9,4 +15,17 @@ class Day23Test {
915
void testMainClass() {
1016
MainClass.main(new String[0]);
1117
}
18+
19+
@ParameterizedTest
20+
@MethodSource("getDataForPart1")
21+
void testPart1(String fileName, int expectedValueOfRegisterB) {
22+
Assertions.assertEquals(expectedValueOfRegisterB, Turing.solvePart1(fileName));
23+
}
24+
25+
private static Stream<Arguments> getDataForPart1() {
26+
return Stream.of(
27+
Arguments.of("part1sample1.txt", 0),
28+
Arguments.of("part1sample2.txt", 2)
29+
);
30+
}
1231
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
inc a
2+
jio a, +2
3+
tpl a
4+
inc a
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
inc b
2+
jio b, +2
3+
tpl b
4+
inc b

0 commit comments

Comments
 (0)