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
3048a067
Commit
3048a067
authored
3 years ago
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Day 7 complete
parent
2610bc51
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
-1
13 additions, 1 deletion
2021/README.md
2021/src/main/java/edu/unl/cse/bohn/year2021/Day7.java
+48
-0
48 additions, 0 deletions
2021/src/main/java/edu/unl/cse/bohn/year2021/Day7.java
with
61 additions
and
1 deletion
2021/README.md
+
13
−
1
View file @
3048a067
...
...
@@ -43,3 +43,15 @@ For example, in the problem statement practically *screams* that the
obvious solution grows exponentially. Normally, death would keep the population
under control, but I guess Death Takes a Holiday. One little extra gotcha:
26984457539 > Integer.MAX_INT, so I need to use a long integer.
## Day 7
Naively trying each position is reasonable: O(num_crabs
*
max_distance). Or,
we could follow a similar model of Day 6, in which we track the number of crabs
in each position, which would be O(max_distance^2). These are both very
manageable, so I'll go with tracking each crab since that requires less upfront
work.
Part 2 looks a bit more interesting. We
*could*
go for an
O(num_crabs
*
max_distance^2) solution, or we could recognize what Euler
recognized: ∑_{i=1}^{n}i = n(n+1)/2.
This diff is collapsed.
Click to expand it.
2021/src/main/java/edu/unl/cse/bohn/year2021/Day7.java
0 → 100644
+
48
−
0
View file @
3048a067
package
edu.unl.cse.bohn.year2021
;
import
edu.unl.cse.bohn.Puzzle
;
import
java.util.Arrays
;
import
java.util.List
;
@SuppressWarnings
(
"unused"
)
public
class
Day7
extends
Puzzle
{
long
[]
crabs
;
long
maximum_position
;
public
Day7
(
boolean
isProductionReady
)
{
super
(
isProductionReady
);
sampleData
=
"16,1,2,0,4,2,7,1,2,14"
;
}
@Override
public
long
computePart1
(
List
<
String
>
data
)
{
crabs
=
Arrays
.
stream
(
data
.
get
(
0
).
split
(
","
)).
mapToLong
(
Long:
:
parseLong
).
toArray
();
maximum_position
=
Arrays
.
stream
(
crabs
).
max
().
orElseThrow
();
long
minimumCost
=
Long
.
MAX_VALUE
;
for
(
long
position
=
0
;
position
<=
maximum_position
;
position
++)
{
long
fuelCost
=
0
;
for
(
long
crab
:
crabs
)
{
fuelCost
+=
Math
.
abs
(
crab
-
position
);
}
minimumCost
=
Long
.
min
(
minimumCost
,
fuelCost
);
}
return
minimumCost
;
}
@Override
public
long
computePart2
(
List
<
String
>
data
)
{
crabs
=
Arrays
.
stream
(
data
.
get
(
0
).
split
(
","
)).
mapToLong
(
Long:
:
parseLong
).
toArray
();
maximum_position
=
Arrays
.
stream
(
crabs
).
max
().
orElseThrow
();
long
minimumCost
=
Long
.
MAX_VALUE
;
for
(
long
position
=
0
;
position
<=
maximum_position
;
position
++)
{
long
fuelCost
=
0
;
for
(
long
crab
:
crabs
)
{
long
displacement
=
Math
.
abs
(
crab
-
position
);
fuelCost
+=
displacement
*
(
displacement
+
1
)
/
2
;
}
minimumCost
=
Long
.
min
(
minimumCost
,
fuelCost
);
}
return
minimumCost
;
}
}
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