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
000fb434
Commit
000fb434
authored
3 years ago
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Day 3 complete
parent
24e81aea
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
2021/README.md
+6
-1
6 additions, 1 deletion
2021/README.md
2021/src/main/java/edu/unl/cse/bohn/year2021/Day3.java
+109
-0
109 additions, 0 deletions
2021/src/main/java/edu/unl/cse/bohn/year2021/Day3.java
with
115 additions
and
1 deletion
2021/README.md
+
6
−
1
View file @
000fb434
...
...
@@ -10,3 +10,8 @@ get to use Java's new multiline strings.
Can we follow instructions? Yes we can. This isn't a particularly challenging
problem. Beside using Java's new Records, I'm also using Java's new style of
switch statements.
## Day 3
My CSCE 231 students should be able to do this problem in their sleep -- bit
masks are cool.
\ No newline at end of file
This diff is collapsed.
Click to expand it.
2021/src/main/java/edu/unl/cse/bohn/year2021/Day3.java
0 → 100644
+
109
−
0
View file @
000fb434
package
edu.unl.cse.bohn.year2021
;
import
edu.unl.cse.bohn.Puzzle
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.function.BiPredicate
;
import
java.util.stream.Collectors
;
@SuppressWarnings
(
"unused"
)
public
class
Day3
extends
Puzzle
{
private
List
<
Integer
>
diagnosticReports
;
private
int
reportLength
;
private
int
[]
ones
,
zeroes
;
public
Day3
()
{
day
=
3
;
sampleData
=
"""
00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010"""
;
isProductionReady
=
true
;
}
@Override
public
int
computePart1
(
List
<
String
>
data
)
{
diagnosticReports
=
data
.
stream
().
map
(
report
->
Integer
.
parseInt
(
report
,
2
)).
collect
(
Collectors
.
toList
());
reportLength
=
data
.
get
(
0
).
length
();
ones
=
new
int
[]{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
};
zeroes
=
new
int
[]{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
};
for
(
int
report
:
diagnosticReports
)
{
for
(
int
bit
=
0
;
bit
<
reportLength
;
bit
++)
{
if
((
report
&
(
1
<<
bit
))
==
0
)
{
zeroes
[
bit
]++;
}
else
{
ones
[
bit
]++;
}
}
}
int
gamma
=
0
;
int
epsilon
=
0
;
for
(
int
bit
=
0
;
bit
<
reportLength
;
bit
++)
{
if
(
ones
[
bit
]
>
zeroes
[
bit
])
{
gamma
|=
(
1
<<
bit
);
}
else
{
epsilon
|=
(
1
<<
bit
);
}
}
return
gamma
*
epsilon
;
}
private
Integer
filterReports
(
int
[]
onesArray
,
int
[]
zeroesArray
,
BiPredicate
<
Integer
,
Integer
>
predicate
)
{
List
<
Integer
>
workingSet
=
new
ArrayList
<>(
diagnosticReports
);
int
bit
=
reportLength
-
1
;
int
numberOfOnes
=
onesArray
[
bit
];
int
numberOfZeroes
=
zeroesArray
[
bit
];
while
(
workingSet
.
size
()
>
1
)
{
int
mask
=
1
<<
bit
;
if
(
predicate
.
test
(
numberOfOnes
,
numberOfZeroes
))
{
workingSet
=
workingSet
.
stream
()
.
filter
(
report
->
(
report
&
mask
)
!=
0
)
.
collect
(
Collectors
.
toList
());
}
else
{
workingSet
=
workingSet
.
stream
()
.
filter
(
report
->
(
report
&
mask
)
==
0
)
.
collect
(
Collectors
.
toList
());
}
bit
--;
// Need to re-count the ones and zeroes since the old counts are now wrong
if
(
bit
>=
0
)
{
numberOfOnes
=
numberOfZeroes
=
0
;
for
(
int
report
:
workingSet
)
{
if
((
report
&
(
1
<<
bit
))
==
0
)
{
numberOfZeroes
++;
}
else
{
numberOfOnes
++;
}
}
}
}
return
workingSet
.
get
(
0
);
}
@Override
public
int
computePart2
(
List
<
String
>
data
)
{
Integer
oxygenGeneratorRating
=
filterReports
(
ones
,
zeroes
,
(
numberOfOnes
,
numberOfZeroes
)
->
numberOfOnes
>=
numberOfZeroes
);
Integer
co2ScrubberRating
=
filterReports
(
ones
,
zeroes
,
(
numberOfOnes
,
numberOfZeroes
)
->
numberOfOnes
<
numberOfZeroes
);
return
oxygenGeneratorRating
*
co2ScrubberRating
;
}
}
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