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
64c5ea1e
Commit
64c5ea1e
authored
Dec 5, 2022
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Completed Year 2022 Day 5 Part 1
parent
853e33cc
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
+36
-1
36 additions, 1 deletion
2022/README.md
2022/src/main/java/edu/unl/cse/bohn/year2022/Day5.java
+95
-0
95 additions, 0 deletions
2022/src/main/java/edu/unl/cse/bohn/year2022/Day5.java
with
131 additions
and
1 deletion
2022/README.md
+
36
−
1
View file @
64c5ea1e
...
@@ -119,5 +119,40 @@ No, the thing to do is to extract the common part out into a helper method.
...
@@ -119,5 +119,40 @@ No, the thing to do is to extract the common part out into a helper method.
## Day 5
## Day 5
-
[
The problem
](
https://adventofcode.com/2022/day/5
)
-
[
The solution
](
src/main/java/edu/unl/cse/bohn/year2022/Day5.java
)
We've hit the first puzzle whose input will require a little creativity when parsing.
### Part 1
The subproblems are
-
Separate the initial conditions from the subsequent instructions
-
Assume there is exactly one blank line between them
-
Represent the stacks
-
At the risk of diving into the solution space,
*obviously*
we'll do this with a list of stacks
-
Determine how many stacks there are
-
Assume that the last line of the initial conditions is the stack numbers, beginning with ' ' followed by a '1'
-
Assume the stack numbers are in-order (
*i.e.*
, the last index tells us how many there are)
-
Can we assume there are only a single-digit number of stacks?
-
Assume all crates are of the form "[?]"
-
Part of that is an assumption that a crate can be represented with a single character -- and I'm going to further
assume it's not ' '
-
Assume an empty space atop a stack is of the form " "
-
Assume exactly one space between stacks
-
Follow the instructions
-
The regular format of the instructions will make them easy to parse
-
(Solution space) pop/push will be easy
-
Output the top of each stack, in order
Why do I have the feeling that there'll be some version of
[
The Towers of Hanoi
](
https://en.wikipedia.org/wiki/Tower_of_Hanoi
)
before we're done?
### Part 2
### Refactoring opportunity
## Day 6
(coming soon)
(coming soon)
This diff is collapsed.
Click to expand it.
2022/src/main/java/edu/unl/cse/bohn/year2022/Day5.java
0 → 100644
+
95
−
0
View file @
64c5ea1e
package
edu.unl.cse.bohn.year2022
;
import
edu.unl.cse.bohn.Puzzle
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.Stack
;
@SuppressWarnings
(
"unused"
)
public
class
Day5
extends
Puzzle
{
public
Day5
(
boolean
isProductionReady
)
{
super
(
isProductionReady
);
//noinspection EscapedSpace
sampleData
=
"""
\s [D] \s
[N] [C] \s
[Z] [M] [P]
1 2 3\s
move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2"""
;
}
@Override
public
long
computePart1
(
List
<
String
>
data
)
{
List
<
Stack
<
Character
>>
stacks
=
createStacks
(
data
);
rearrangeCrates
(
stacks
,
data
);
String
topsOfStacks
=
getTopsOfStacks
(
stacks
);
System
.
out
.
println
(
"Crates at the top of the stacks: "
+
topsOfStacks
);
return
0
;
}
@Override
public
long
computePart2
(
List
<
String
>
data
)
{
return
0
;
}
private
List
<
Stack
<
Character
>>
createStacks
(
List
<
String
>
data
)
{
Integer
rowWithIndices
=
null
;
int
i
=
0
;
while
(
rowWithIndices
==
null
)
{
if
(
data
.
get
(
i
).
startsWith
(
" 1"
))
{
rowWithIndices
=
i
;
}
i
++;
}
String
[]
indexStrings
=
data
.
get
(
rowWithIndices
).
strip
().
split
(
" "
);
int
numberOfStacks
=
Integer
.
parseInt
(
indexStrings
[
indexStrings
.
length
-
1
]);
List
<
Stack
<
Character
>>
stacks
=
new
ArrayList
<>(
numberOfStacks
);
for
(
i
=
0
;
i
<
numberOfStacks
;
i
++)
{
stacks
.
add
(
new
Stack
<>());
}
for
(
i
=
rowWithIndices
-
1
;
i
>=
0
;
i
--)
{
for
(
int
stack
=
0
;
stack
<
numberOfStacks
;
stack
++)
{
char
crate
=
data
.
get
(
i
).
charAt
(
4
*
stack
+
1
);
if
(
crate
!=
' '
)
{
stacks
.
get
(
stack
).
push
(
crate
);
}
}
}
return
Collections
.
unmodifiableList
(
stacks
);
}
private
void
rearrangeCrates
(
List
<
Stack
<
Character
>>
stacks
,
List
<
String
>
data
)
{
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
;
char
crate
=
stacks
.
get
(
source
).
pop
();
stacks
.
get
(
destination
).
push
(
crate
);
}
}
}
}
private
String
getTopsOfStacks
(
List
<
Stack
<
Character
>>
stacks
)
{
StringBuilder
stringBuilder
=
new
StringBuilder
();
for
(
Stack
<
Character
>
stack
:
stacks
)
{
if
(
stack
.
empty
())
{
stringBuilder
.
append
(
' '
);
// this isn't in the spec, but we'll go with it for now
}
else
{
Character
crate
=
stack
.
pop
();
stack
.
push
(
crate
);
// leave it the way we found it
stringBuilder
.
append
(
crate
);
}
}
return
stringBuilder
.
toString
();
}
}
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