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
5c59b960
Commit
5c59b960
authored
Dec 2, 2022
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Finished Year 2022 Day 2
parent
92b65f0f
Branches
Branches containing commit
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
+10
-0
10 additions, 0 deletions
2022/README.md
2022/src/main/java/edu/unl/cse/bohn/year2022/Day2.java
+157
-0
157 additions, 0 deletions
2022/src/main/java/edu/unl/cse/bohn/year2022/Day2.java
with
167 additions
and
0 deletions
2022/README.md
+
10
−
0
View file @
5c59b960
...
@@ -31,5 +31,15 @@ Which means we can use the same solution for both parts, parameterized by the to
...
@@ -31,5 +31,15 @@ Which means we can use the same solution for both parts, parameterized by the to
## Day 2
## Day 2
The subproblems for both parts are
-
Score a single round
-
Determine which hand is played by each player; adjust score based on "my" hand
-
Determine the outcome of the round; adjust score based on the outcome
-
Compute the total score
Seems pretty straight-forward.
## 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
0 → 100644
+
157
−
0
View file @
5c59b960
package
edu.unl.cse.bohn.year2022
;
import
edu.unl.cse.bohn.Puzzle
;
import
java.util.List
;
@SuppressWarnings
(
"unused"
)
public
class
Day2
extends
Puzzle
{
public
enum
Hand
{
ROCK
,
PAPER
,
SCISSORS
,
UNKNOWN
}
public
enum
Outcome
{
WIN
,
LOSE
,
DRAW
,
UNKNOWN
}
public
Day2
(
boolean
isProductionReady
)
{
super
(
isProductionReady
);
sampleData
=
"""
A Y
B X
C Z"""
;
}
@Override
public
long
computePart1
(
List
<
String
>
data
)
{
long
totalScore
=
0
;
for
(
String
datum
:
data
)
{
totalScore
+=
scoreOneRoundForPart1
(
datum
);
}
return
totalScore
;
}
@Override
public
long
computePart2
(
List
<
String
>
data
)
{
long
totalScore
=
0
;
for
(
String
datum
:
data
)
{
totalScore
+=
scoreOneRoundForPart2
(
datum
);
}
return
totalScore
;
}
private
long
scoreOneRoundForPart1
(
String
choices
)
{
long
score
;
Outcome
outcome
=
Outcome
.
UNKNOWN
;
//noinspection DuplicatedCode
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
;
switch
(
choices
.
charAt
(
2
))
{
case
'X'
->
{
me
=
Hand
.
ROCK
;
score
=
1
;
outcome
=
switch
(
opponent
)
{
case
SCISSORS
->
Outcome
.
WIN
;
case
PAPER
->
Outcome
.
LOSE
;
default
->
Outcome
.
DRAW
;
};
}
case
'Y'
->
{
me
=
Hand
.
PAPER
;
score
=
2
;
outcome
=
switch
(
opponent
)
{
case
ROCK
->
Outcome
.
WIN
;
case
SCISSORS
->
Outcome
.
LOSE
;
default
->
Outcome
.
DRAW
;
};
}
case
'Z'
->
{
me
=
Hand
.
SCISSORS
;
score
=
3
;
outcome
=
switch
(
opponent
)
{
case
PAPER
->
Outcome
.
WIN
;
case
ROCK
->
Outcome
.
LOSE
;
default
->
Outcome
.
DRAW
;
};
}
default
->
{
me
=
Hand
.
UNKNOWN
;
score
=
0
;
}
}
if
(
me
==
Hand
.
UNKNOWN
)
{
throw
new
IllegalStateException
(
"My hand determined as \"UNKNOWN\". Choices: { "
+
choices
+
" }"
);
}
switch
(
outcome
)
{
case
WIN
->
score
+=
6
;
case
DRAW
->
score
+=
3
;
// no change for LOSE
}
return
score
;
}
private
long
scoreOneRoundForPart2
(
String
choices
)
{
long
score
;
// Outcome outcome = Outcome.UNKNOWN;
//noinspection DuplicatedCode
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
;
switch
(
choices
.
charAt
(
2
))
{
case
'X'
->
{
// outcome = Outcome.LOSE;
score
=
0
;
me
=
switch
(
opponent
)
{
case
SCISSORS
->
Hand
.
PAPER
;
case
PAPER
->
Hand
.
ROCK
;
case
ROCK
->
Hand
.
SCISSORS
;
default
->
Hand
.
UNKNOWN
;
};
}
case
'Y'
->
{
// outcome = Outcome.DRAW;
score
=
3
;
me
=
switch
(
opponent
)
{
case
SCISSORS
->
Hand
.
SCISSORS
;
case
PAPER
->
Hand
.
PAPER
;
case
ROCK
->
Hand
.
ROCK
;
default
->
Hand
.
UNKNOWN
;
};
}
case
'Z'
->
{
// outcome = Outcome.WIN;
score
=
6
;
me
=
switch
(
opponent
)
{
case
SCISSORS
->
Hand
.
ROCK
;
case
PAPER
->
Hand
.
SCISSORS
;
case
ROCK
->
Hand
.
PAPER
;
default
->
Hand
.
UNKNOWN
;
};
}
default
->
{
me
=
Hand
.
UNKNOWN
;
score
=
0
;
}
}
switch
(
me
)
{
case
ROCK
->
score
+=
1
;
case
PAPER
->
score
+=
2
;
case
SCISSORS
->
score
+=
3
;
default
->
throw
new
IllegalStateException
(
"My hand determined as \"UNKNOWN\". Choices: { "
+
choices
+
" }"
);
}
return
score
;
}
}
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