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
187c435a
Commit
187c435a
authored
Dec 15, 2021
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Day 11 complete
parent
1a082328
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
+13
-0
13 additions, 0 deletions
2021/README.md
2021/src/main/java/edu/unl/cse/bohn/year2021/Day11.java
+131
-0
131 additions, 0 deletions
2021/src/main/java/edu/unl/cse/bohn/year2021/Day11.java
with
144 additions
and
0 deletions
2021/README.md
+
13
−
0
View file @
187c435a
...
@@ -121,3 +121,16 @@ This looks like a job for a stack!
...
@@ -121,3 +121,16 @@ This looks like a job for a stack!
Update: Yes, yes it was a job for a stack. Some maps simplified matters but
Update: Yes, yes it was a job for a stack. Some maps simplified matters but
were not essential.
were not essential.
## Day 11
Part 1 looks straight-forward. As I did with Day 9, the nested array will have
a "frame" to simplify checking neighbors. One point of clarification: if an
octopus's energy reaches 11 due to neighbors' flashing, does it still reset to
0, or does it
*actually*
flash at 10, reset to 0, and then takes on the energy
from the other neighbor (
*i.e.*
, I should subtract 10 from 11, not blindly
reset to 0). Or, upon rereading the spec, we don't reset to 0 until the very end
of the step. Well, we'll find out...
Part 2, isn't any more challenging than checking whether the number of flashes
is equal to the number of octopi.
This diff is collapsed.
Click to expand it.
2021/src/main/java/edu/unl/cse/bohn/year2021/Day11.java
0 → 100644
+
131
−
0
View file @
187c435a
package
edu.unl.cse.bohn.year2021
;
import
edu.unl.cse.bohn.Puzzle
;
import
java.util.Arrays
;
import
java.util.List
;
@SuppressWarnings
(
"unused"
)
public
class
Day11
extends
Puzzle
{
private
int
[][]
octopi
;
@SuppressWarnings
(
"CommentedOutCode"
)
public
Day11
(
boolean
isProductionReady
)
{
super
(
isProductionReady
);
// sampleData = """
// 11111
// 19991
// 19191
// 19991
// 11111""";
sampleData
=
"""
5483143223
2745854711
5264556173
6141336146
6357385478
4167524645
2176841721
6882881134
4846848554
5283751526"""
;
}
private
void
initializeOctopi
(
List
<
String
>
data
)
{
octopi
=
new
int
[
data
.
size
()
+
2
][
data
.
get
(
0
).
length
()
+
2
];
for
(
int
[]
row
:
octopi
)
{
Arrays
.
fill
(
row
,
Integer
.
MIN_VALUE
);
}
for
(
int
i
=
0
;
i
<
data
.
size
();
i
++)
{
String
line
=
data
.
get
(
i
);
for
(
int
j
=
0
;
j
<
line
.
length
();
j
++)
{
octopi
[
i
+
1
][
j
+
1
]
=
Integer
.
parseInt
(
String
.
valueOf
(
line
.
charAt
(
j
)));
}
}
}
private
void
printOctopi
()
{
for
(
int
i
=
1
;
i
<
octopi
.
length
;
i
++)
{
for
(
int
j
=
1
;
j
<
octopi
[
i
].
length
-
1
;
j
++)
{
if
(
octopi
[
i
][
j
]
<
0
)
{
System
.
out
.
print
(
"-"
);
}
else
{
System
.
out
.
print
(
octopi
[
i
][
j
]);
}
}
System
.
out
.
println
();
}
System
.
out
.
println
();
}
private
long
stepOctopi
()
{
boolean
[][]
hasFlashed
=
new
boolean
[
octopi
.
length
][
octopi
[
0
].
length
];
boolean
stillFlashing
=
true
;
for
(
boolean
[]
row
:
hasFlashed
)
{
Arrays
.
fill
(
row
,
false
);
}
for
(
int
i
=
1
;
i
<
octopi
.
length
-
1
;
i
++)
{
for
(
int
j
=
1
;
j
<
octopi
[
i
].
length
-
1
;
j
++)
{
octopi
[
i
][
j
]++;
}
}
while
(
stillFlashing
)
{
stillFlashing
=
false
;
for
(
int
i
=
1
;
i
<
octopi
.
length
-
1
;
i
++)
{
for
(
int
j
=
1
;
j
<
octopi
[
i
].
length
-
1
;
j
++)
{
if
(
octopi
[
i
][
j
]
>
9
&&
!
hasFlashed
[
i
][
j
])
{
hasFlashed
[
i
][
j
]
=
true
;
stillFlashing
=
true
;
octopi
[
i
-
1
][
j
-
1
]++;
octopi
[
i
-
1
][
j
]++;
octopi
[
i
-
1
][
j
+
1
]++;
octopi
[
i
][
j
-
1
]++;
octopi
[
i
][
j
+
1
]++;
octopi
[
i
+
1
][
j
-
1
]++;
octopi
[
i
+
1
][
j
]++;
octopi
[
i
+
1
][
j
+
1
]++;
}
}
}
}
long
numberOfFlashes
=
0
;
for
(
int
i
=
0
;
i
<
octopi
.
length
;
i
++)
{
for
(
int
j
=
0
;
j
<
octopi
[
i
].
length
;
j
++)
{
if
(
octopi
[
i
][
j
]
<
0
)
{
// the frame
octopi
[
i
][
j
]
=
Integer
.
MIN_VALUE
;
}
else
if
(
hasFlashed
[
i
][
j
])
{
octopi
[
i
][
j
]
=
0
;
numberOfFlashes
++;
}
}
}
return
numberOfFlashes
;
}
@Override
public
long
computePart1
(
List
<
String
>
data
)
{
// maximumSteps = 2;
int
maximumSteps
=
100
;
initializeOctopi
(
data
);
long
totalFlashes
=
0
;
// printOctopi();
for
(
int
i
=
0
;
i
<
maximumSteps
;
i
++)
{
totalFlashes
+=
stepOctopi
();
// printOctopi();
}
return
totalFlashes
;
}
@Override
public
long
computePart2
(
List
<
String
>
data
)
{
initializeOctopi
(
data
);
long
stepNumber
=
0
;
long
numberOfOctopi
=
(
long
)
data
.
size
()
*
(
long
)
data
.
get
(
0
).
length
();
long
numberOfFlashes
;
do
{
numberOfFlashes
=
stepOctopi
();
stepNumber
++;
}
while
(
numberOfFlashes
<
numberOfOctopi
);
return
stepNumber
;
}
}
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