Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Advent of Coding
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christopher Bohn
Advent of Coding
Commits
15c83405
Commit
15c83405
authored
Dec 2, 2022
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Refactored Year 2022 Day 2
parent
5c59b960
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
2022/README.md
+5
-0
5 additions, 0 deletions
2022/README.md
2022/src/main/java/edu/unl/cse/bohn/year2022/Day2.java
+9
-14
9 additions, 14 deletions
2022/src/main/java/edu/unl/cse/bohn/year2022/Day2.java
with
14 additions
and
14 deletions
2022/README.md
+
5
−
0
View file @
15c83405
...
@@ -39,6 +39,11 @@ The subproblems for both parts are
...
@@ -39,6 +39,11 @@ The subproblems for both parts are
Seems pretty straight-forward.
Seems pretty straight-forward.
### Refactoring opportunity
Parts 1 & 2 differ in whether we determine the outcome based off of "my" hand, or whether we determine "my" hand based off of the outcome.
Where we parameterized the commonality on Day 1, today we'll simply extract the commonality into a helper method.
## Day 3
## Day 3
(coming soon)
(coming soon)
...
...
This diff is collapsed.
Click to expand it.
2022/src/main/java/edu/unl/cse/bohn/year2022/Day2.java
+
9
−
14
View file @
15c83405
...
@@ -37,10 +37,7 @@ public class Day2 extends Puzzle {
...
@@ -37,10 +37,7 @@ public class Day2 extends Puzzle {
return
totalScore
;
return
totalScore
;
}
}
private
long
scoreOneRoundForPart1
(
String
choices
)
{
private
static
Hand
getOpponentHand
(
String
choices
)
{
long
score
;
Outcome
outcome
=
Outcome
.
UNKNOWN
;
//noinspection DuplicatedCode
Hand
opponent
=
switch
(
choices
.
charAt
(
0
))
{
Hand
opponent
=
switch
(
choices
.
charAt
(
0
))
{
case
'A'
->
Hand
.
ROCK
;
case
'A'
->
Hand
.
ROCK
;
case
'B'
->
Hand
.
PAPER
;
case
'B'
->
Hand
.
PAPER
;
...
@@ -50,6 +47,13 @@ public class Day2 extends Puzzle {
...
@@ -50,6 +47,13 @@ public class Day2 extends Puzzle {
if
(
opponent
==
Hand
.
UNKNOWN
)
{
if
(
opponent
==
Hand
.
UNKNOWN
)
{
throw
new
IllegalStateException
(
"Opponent's hand determined as \"UNKNOWN\". Choices: { "
+
choices
+
" }"
);
throw
new
IllegalStateException
(
"Opponent's hand determined as \"UNKNOWN\". Choices: { "
+
choices
+
" }"
);
}
}
return
opponent
;
}
private
long
scoreOneRoundForPart1
(
String
choices
)
{
long
score
;
Outcome
outcome
=
Outcome
.
UNKNOWN
;
Hand
opponent
=
getOpponentHand
(
choices
);
Hand
me
;
Hand
me
;
switch
(
choices
.
charAt
(
2
))
{
switch
(
choices
.
charAt
(
2
))
{
case
'X'
->
{
case
'X'
->
{
...
@@ -98,16 +102,7 @@ public class Day2 extends Puzzle {
...
@@ -98,16 +102,7 @@ public class Day2 extends Puzzle {
private
long
scoreOneRoundForPart2
(
String
choices
)
{
private
long
scoreOneRoundForPart2
(
String
choices
)
{
long
score
;
long
score
;
// Outcome outcome = Outcome.UNKNOWN;
// Outcome outcome = Outcome.UNKNOWN;
//noinspection DuplicatedCode
Hand
opponent
=
getOpponentHand
(
choices
);
Hand
opponent
=
switch
(
choices
.
charAt
(
0
))
{
case
'A'
->
Hand
.
ROCK
;
case
'B'
->
Hand
.
PAPER
;
case
'C'
->
Hand
.
SCISSORS
;
default
->
Hand
.
UNKNOWN
;
};
if
(
opponent
==
Hand
.
UNKNOWN
)
{
throw
new
IllegalStateException
(
"Opponent's hand determined as \"UNKNOWN\". Choices: { "
+
choices
+
" }"
);
}
Hand
me
;
Hand
me
;
switch
(
choices
.
charAt
(
2
))
{
switch
(
choices
.
charAt
(
2
))
{
case
'X'
->
{
case
'X'
->
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment