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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christopher Bohn
Advent of Coding
Commits
5e5d59b5
Commit
5e5d59b5
authored
2 years ago
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Completed Year 2022 Day 5
parent
64c5ea1e
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
+7
-0
7 additions, 0 deletions
2022/README.md
2022/src/main/java/edu/unl/cse/bohn/year2022/Day5.java
+23
-7
23 additions, 7 deletions
2022/src/main/java/edu/unl/cse/bohn/year2022/Day5.java
with
30 additions
and
7 deletions
2022/README.md
+
7
−
0
View file @
5e5d59b5
...
...
@@ -150,8 +150,15 @@ Why do I have the feeling that there'll be some version of
### Part 2
The difference here is how we move multiple crates from one stack to another.
### Refactoring opportunity
With the difference between the two problems occurring inside a nested loop, extracting the commonality into a helper
method (more than I've already done) isn't going to work. Passing in a pointer function would do nicely, but doing the
Java equivalent is a PITA for such a small payoff. I think I'll use a boolean to indicate which of two crate movement
techniques should be used.
## Day 6
(coming soon)
...
...
This diff is collapsed.
Click to expand it.
2022/src/main/java/edu/unl/cse/bohn/year2022/Day5.java
+
23
−
7
View file @
5e5d59b5
...
...
@@ -27,7 +27,7 @@ public class Day5 extends Puzzle {
@Override
public
long
computePart1
(
List
<
String
>
data
)
{
List
<
Stack
<
Character
>>
stacks
=
createStacks
(
data
);
rearrangeCrates
(
stacks
,
data
);
rearrangeCrates
(
stacks
,
data
,
true
);
String
topsOfStacks
=
getTopsOfStacks
(
stacks
);
System
.
out
.
println
(
"Crates at the top of the stacks: "
+
topsOfStacks
);
return
0
;
...
...
@@ -35,6 +35,10 @@ public class Day5 extends Puzzle {
@Override
public
long
computePart2
(
List
<
String
>
data
)
{
List
<
Stack
<
Character
>>
stacks
=
createStacks
(
data
);
rearrangeCrates
(
stacks
,
data
,
false
);
String
topsOfStacks
=
getTopsOfStacks
(
stacks
);
System
.
out
.
println
(
"Crates at the top of the stacks: "
+
topsOfStacks
);
return
0
;
}
...
...
@@ -64,20 +68,32 @@ public class Day5 extends Puzzle {
return
Collections
.
unmodifiableList
(
stacks
);
}
private
void
rearrangeCrates
(
List
<
Stack
<
Character
>>
stacks
,
List
<
String
>
data
)
{
private
void
rearrangeCrates
(
List
<
Stack
<
Character
>>
stacks
,
List
<
String
>
data
,
boolean
moveOneCrateAtATime
)
{
Stack
<
Character
>
crane
=
new
Stack
<>();
for
(
String
instruction
:
data
)
{
if
(
instruction
.
startsWith
(
"move"
))
{
// then it really is an instruction
String
[]
tokens
=
instruction
.
strip
().
split
(
" "
);
int
numberOfCratesToMove
=
Integer
.
parseInt
(
tokens
[
1
]);
for
(
int
i
=
0
;
i
<
numberOfCratesToMove
;
i
++)
{
int
source
=
Integer
.
parseInt
(
tokens
[
3
])
-
1
;
// -1 because the original stacks were 1-indexed
int
destination
=
Integer
.
parseInt
(
tokens
[
5
])
-
1
;
if
(
moveOneCrateAtATime
)
{
for
(
int
i
=
0
;
i
<
numberOfCratesToMove
;
i
++)
{
char
crate
=
stacks
.
get
(
source
).
pop
();
stacks
.
get
(
destination
).
push
(
crate
);
}
}
else
{
for
(
int
i
=
0
;
i
<
numberOfCratesToMove
;
i
++)
{
char
crate
=
stacks
.
get
(
source
).
pop
();
crane
.
push
(
crate
);
}
while
(!
crane
.
empty
())
{
char
crate
=
crane
.
pop
();
stacks
.
get
(
destination
).
push
(
crate
);
}
}
}
}
}
private
String
getTopsOfStacks
(
List
<
Stack
<
Character
>>
stacks
)
{
StringBuilder
stringBuilder
=
new
StringBuilder
();
...
...
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