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
89a24af4
Commit
89a24af4
authored
3 years ago
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Day 12 complete
parent
187c435a
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
2021/README.md
+14
-0
14 additions, 0 deletions
2021/README.md
2021/src/main/java/edu/unl/cse/bohn/year2021/Day12.java
+167
-0
167 additions, 0 deletions
2021/src/main/java/edu/unl/cse/bohn/year2021/Day12.java
with
181 additions
and
0 deletions
2021/README.md
+
14
−
0
View file @
89a24af4
...
@@ -134,3 +134,17 @@ of the step. Well, we'll find out...
...
@@ -134,3 +134,17 @@ of the step. Well, we'll find out...
Part 2, isn't any more challenging than checking whether the number of flashes
Part 2, isn't any more challenging than checking whether the number of flashes
is equal to the number of octopi.
is equal to the number of octopi.
# Day 12
For part 1, I hope there aren't any big caves directly connected to other big
caves, because that would introduce a cycle. If we only wanted the shortest
path then I could use a BFS, and cycles wouldn't be a problem. But we want all
paths, and I think cycle detection is probably beyond the scope of Advent of
Code, especially before the halfway point. For part 1 I'm just going to build
the path tree. I have a sneaking suspicion I really should build the cave graph
for part 2, but we shall see...
Good news! I was wrong about needing to build the cave graph for part 2. I just
need to specialize my
`Cave`
inner class to change the rules for adding
children.
This diff is collapsed.
Click to expand it.
2021/src/main/java/edu/unl/cse/bohn/year2021/Day12.java
0 → 100644
+
167
−
0
View file @
89a24af4
package
edu.unl.cse.bohn.year2021
;
import
edu.unl.cse.bohn.Puzzle
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
@SuppressWarnings
(
"unused"
)
public
class
Day12
extends
Puzzle
{
private
Map
<
String
,
Set
<
String
>>
adjacencyMap
;
@SuppressWarnings
(
"CommentedOutCode"
)
public
Day12
(
boolean
isProductionReady
)
{
super
(
isProductionReady
);
sampleData
=
"""
start-A
start-b
A-c
A-b
b-d
A-end
b-end"""
;
// sampleData = """
// dc-end
// HN-start
// start-kj
// dc-start
// dc-HN
// LN-dc
// HN-end
// kj-sa
// kj-HN
// kj-dc""";
// sampleData = """
// fs-end
// he-DX
// fs-he
// start-DX
// pj-DX
// end-zg
// zg-sl
// zg-pj
// pj-he
// RW-he
// fs-DX
// pj-RW
// zg-RW
// start-pj
// he-WI
// zg-he
// pj-fs
// start-RW""";
}
private
void
buildMap
(
List
<
String
>
data
)
{
adjacencyMap
=
new
HashMap
<>();
for
(
String
line
:
data
)
{
String
[]
termini
=
line
.
split
(
"-"
);
if
(!
adjacencyMap
.
containsKey
(
termini
[
0
]))
{
adjacencyMap
.
put
(
termini
[
0
],
new
HashSet
<>());
}
adjacencyMap
.
get
(
termini
[
0
]).
add
(
termini
[
1
]);
if
(!
adjacencyMap
.
containsKey
(
termini
[
1
]))
{
adjacencyMap
.
put
(
termini
[
1
],
new
HashSet
<>());
}
adjacencyMap
.
get
(
termini
[
1
]).
add
(
termini
[
0
]);
}
adjacencyMap
=
Map
.
copyOf
(
adjacencyMap
);
// make it unmodifiable
}
@Override
public
long
computePart1
(
List
<
String
>
data
)
{
buildMap
(
data
);
Cave
root
=
new
Cave
(
"start"
,
null
,
new
HashSet
<>(),
adjacencyMap
);
// for (Cave cave : Cave.exits) {
// System.out.println(cave);
// }
return
Cave
.
exits
.
size
();
}
@Override
public
long
computePart2
(
List
<
String
>
data
)
{
RevisitableCave
root
=
new
RevisitableCave
(
"start"
,
null
,
new
HashSet
<>(),
adjacencyMap
,
false
);
return
RevisitableCave
.
exits
.
size
();
}
private
static
class
Cave
{
public
static
final
Set
<
Cave
>
exits
=
new
HashSet
<>();
protected
String
caveName
;
protected
Cave
parent
;
protected
Set
<
String
>
visitedSmallCaves
;
protected
List
<?
extends
Cave
>
children
;
private
Cave
()
{
// this constructor exists to keep Java happy wrt the subclass whose public constructor has different
// parameters than the parent class's constructor
caveName
=
null
;
parent
=
null
;
visitedSmallCaves
=
null
;
children
=
null
;
}
public
Cave
(
String
caveName
,
Cave
parent
,
Set
<
String
>
visitedSmallCaves
,
Map
<
String
,
Set
<
String
>>
adjacencyMap
)
{
this
.
caveName
=
caveName
;
this
.
parent
=
parent
;
this
.
visitedSmallCaves
=
visitedSmallCaves
;
if
(
caveName
.
equals
(
caveName
.
toLowerCase
()))
{
this
.
visitedSmallCaves
.
add
(
caveName
);
}
if
(
caveName
.
equals
(
"end"
))
{
exits
.
add
(
this
);
children
=
null
;
}
else
{
children
=
adjacencyMap
.
get
(
caveName
).
stream
()
.
filter
(
neighbor
->
!
visitedSmallCaves
.
contains
(
neighbor
))
.
map
(
neighbor
->
new
Cave
(
neighbor
,
this
,
new
HashSet
<>(
visitedSmallCaves
),
adjacencyMap
))
.
toList
();
}
}
public
String
toString
()
{
StringBuilder
stringBuilder
=
new
StringBuilder
(
caveName
);
if
(!
caveName
.
equals
(
"start"
))
{
stringBuilder
.
insert
(
0
,
parent
.
toString
()
+
" - "
);
}
return
stringBuilder
.
toString
();
}
}
private
static
class
RevisitableCave
extends
Cave
{
public
static
final
Set
<
Cave
>
exits
=
new
HashSet
<>();
public
RevisitableCave
(
String
caveName
,
Cave
parent
,
Set
<
String
>
visitedSmallCaves
,
Map
<
String
,
Set
<
String
>>
adjacencyMap
,
boolean
haveVisitedSmallCaveTwice
)
{
this
.
caveName
=
caveName
;
this
.
parent
=
parent
;
this
.
visitedSmallCaves
=
visitedSmallCaves
;
final
boolean
updatedHaveVisitedSmallCaveTwice
;
if
(
caveName
.
equals
(
caveName
.
toLowerCase
()))
{
if
(
visitedSmallCaves
.
contains
(
caveName
))
{
updatedHaveVisitedSmallCaveTwice
=
true
;
}
else
{
updatedHaveVisitedSmallCaveTwice
=
haveVisitedSmallCaveTwice
;
}
this
.
visitedSmallCaves
.
add
(
caveName
);
}
else
{
updatedHaveVisitedSmallCaveTwice
=
haveVisitedSmallCaveTwice
;
}
if
(
caveName
.
equals
(
"end"
))
{
exits
.
add
(
this
);
children
=
null
;
}
else
{
children
=
adjacencyMap
.
get
(
caveName
).
stream
()
.
filter
(
neighbor
->
!
neighbor
.
equals
(
"start"
))
.
filter
(
neighbor
->
!(
updatedHaveVisitedSmallCaveTwice
&&
visitedSmallCaves
.
contains
(
neighbor
)))
.
map
(
neighbor
->
new
RevisitableCave
(
neighbor
,
this
,
new
HashSet
<>(
visitedSmallCaves
),
adjacencyMap
,
updatedHaveVisitedSmallCaveTwice
))
.
toList
();
}
}
}
}
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