File tree Expand file tree Collapse file tree 4 files changed +2580
-0
lines changed
java/dev/linl33/adventofcode/year2022
resources/dev/linl33/adventofcode/year2022
test/java/dev/linl33/adventofcode/year2022/test Expand file tree Collapse file tree 4 files changed +2580
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments