Skip to content

Commit 64274d9

Browse files
committed
Day 2
1 parent e6d94df commit 64274d9

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed

src/test/java/com/macasaet/Day02.java

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package com.macasaet;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.stream.Stream;
6+
import java.util.stream.StreamSupport;
7+
8+
/**
9+
* --- Day 2: Rock Paper Scissors ---
10+
* https://adventofcode.com/2022/day/2
11+
*/
12+
public class Day02 {
13+
14+
protected Stream<Round> getInput() {
15+
return StreamSupport
16+
.stream(new LineSpliterator("day-02.txt"),
17+
false)
18+
.map(line -> {
19+
final var components = line.strip().split(" ");
20+
return new Round(Shape.forChar(components[0].charAt(0)),
21+
Shape.forChar(components[1].charAt(0)),
22+
ResponseStrategy.forChar(components[1].charAt(0)));
23+
});
24+
}
25+
26+
@Test
27+
public final void part1() {
28+
final var result = getInput().mapToInt(Round::naiveScore).sum();
29+
30+
System.out.println("Part 1: " + result);
31+
}
32+
33+
@Test
34+
public final void part2() {
35+
final var result = getInput().mapToInt(Round::score).sum();
36+
37+
System.out.println("Part 2: " + result);
38+
}
39+
40+
/**
41+
* A shape that a contestant can play in a round
42+
*/
43+
public enum Shape {
44+
Rock {
45+
public int score() {
46+
return 1;
47+
}
48+
49+
public Shape beatenBy() {
50+
return Paper;
51+
}
52+
53+
public Shape beats() {
54+
return Scissors;
55+
}
56+
},
57+
Paper {
58+
public int score() {
59+
return 2;
60+
}
61+
62+
public Shape beatenBy() {
63+
return Scissors;
64+
}
65+
66+
public Shape beats() {
67+
return Rock;
68+
}
69+
},
70+
Scissors {
71+
public int score() {
72+
return 3;
73+
}
74+
75+
@Override
76+
public Shape beatenBy() {
77+
return Rock;
78+
}
79+
80+
public Shape beats() {
81+
return Paper;
82+
}
83+
};
84+
85+
public static Shape forChar(final int c) {
86+
switch (c) {
87+
case 'X':
88+
case 'A':
89+
return Shape.Rock;
90+
case 'Y':
91+
case 'B':
92+
return Shape.Paper;
93+
case 'Z':
94+
case 'C':
95+
return Shape.Scissors;
96+
}
97+
throw new IllegalArgumentException();
98+
}
99+
100+
/**
101+
* @return the inherent value of this shape
102+
*/
103+
public abstract int score();
104+
105+
/**
106+
* @return the shape that beats this one
107+
*/
108+
public abstract Shape beatenBy();
109+
110+
/**
111+
* @return the shape this one beats
112+
*/
113+
public abstract Shape beats();
114+
}
115+
116+
/**
117+
* An approach to responding to the shape played by the opponent
118+
*/
119+
public enum ResponseStrategy {
120+
Lose {
121+
public Shape respond(Shape opponent) {
122+
return opponent.beats();
123+
}
124+
},
125+
Draw {
126+
public Shape respond(Shape opponent) {
127+
return opponent;
128+
}
129+
},
130+
Win {
131+
public Shape respond(Shape opponent) {
132+
return opponent.beatenBy();
133+
}
134+
};
135+
136+
public static ResponseStrategy forChar(final char c) {
137+
switch (c) {
138+
case 'X':
139+
return Lose;
140+
case 'Y':
141+
return Draw;
142+
case 'Z':
143+
return Win;
144+
}
145+
throw new IllegalArgumentException();
146+
}
147+
148+
public abstract Shape respond(final Shape opponent);
149+
}
150+
151+
/**
152+
* A single round of the game
153+
*
154+
* @param opponent The shape played by the opponent
155+
* @param player The shape chosen based on the original (incorrect) interpretation of the responseStrategy guide
156+
* @param responseStrategy The responseStrategy for responding to the opponent according to the responseStrategy guide
157+
*/
158+
public record Round(Shape opponent, Shape player, ResponseStrategy responseStrategy) {
159+
/**
160+
* @return the score based on the simple (incorrect) interpretation of the strategy guide
161+
*/
162+
public int naiveScore() {
163+
final var outcome = opponent() == player() ? 3 : opponent().beatenBy() == player() ? 6 : 0;
164+
return outcome + player().score();
165+
}
166+
167+
/**
168+
* @return the score based on following the strategy guide
169+
*/
170+
public int score() {
171+
final var response = responseStrategy().respond(opponent());
172+
final var outcome = opponent() == response ? 3 : opponent().beatenBy() == response ? 6 : 0;
173+
return outcome + response.score();
174+
}
175+
}
176+
177+
}

src/test/resources/sample/day-02.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
A Y
2+
B X
3+
C Z

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy