Skip to content

Commit c74e87e

Browse files
committed
Add year 2022 day2
1 parent f1b4a3a commit c74e87e

File tree

4 files changed

+2580
-0
lines changed
  • year2022/src

4 files changed

+2580
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package dev.linl33.adventofcode.year2022;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.io.BufferedReader;
6+
import java.util.function.IntBinaryOperator;
7+
8+
public class Day2 extends AdventSolution2022<Integer, Integer> {
9+
public static void main(String[] args) {
10+
new Day2().runAndPrintAll();
11+
}
12+
13+
@Override
14+
public Integer part1(@NotNull BufferedReader reader) {
15+
return scoreStrategyGuide(
16+
reader,
17+
(p1, p2) -> {
18+
// add 3 to make it positive
19+
var outcome = (p2 - p1 + 1 + 3) % 3;
20+
return scoreRound(outcome, p2);
21+
}
22+
);
23+
}
24+
25+
@Override
26+
public Integer part2(@NotNull BufferedReader reader) {
27+
return scoreStrategyGuide(
28+
reader,
29+
(p1, outcome) -> {
30+
var offset = outcome + 2;
31+
var p2 = (p1 + offset) % 3;
32+
return scoreRound(outcome, p2);
33+
}
34+
);
35+
}
36+
37+
private static int scoreStrategyGuide(@NotNull BufferedReader reader, @NotNull IntBinaryOperator op) {
38+
return reader
39+
.lines()
40+
.mapToInt(line -> op.applyAsInt(line.charAt(0) - 'A', line.charAt(2) - 'X'))
41+
.sum();
42+
}
43+
44+
private static int scoreRound(int outcome, int p2) {
45+
return outcome * 3 + (p2 + 1);
46+
}
47+
}

0 commit comments

Comments
 (0)