Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Advent of Code 2023 in Java
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
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LUDMANN Pierre
Advent of Code 2023 in Java
Commits
1f169a43
Commit
1f169a43
authored
1 year ago
by
LUDMANN Pierre
Browse files
Options
Downloads
Patches
Plain Diff
[day15] done
parent
81027ce7
No related branches found
No related tags found
1 merge request
!2
Day15 ludmann1
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/CieDuVaseDesNoces/Day15.java
+113
-2
113 additions, 2 deletions
src/main/java/CieDuVaseDesNoces/Day15.java
with
113 additions
and
2 deletions
src/main/java/CieDuVaseDesNoces/Day15.java
+
113
−
2
View file @
1f169a43
package
CieDuVaseDesNoces
;
import
java.util.AbstractMap.SimpleEntry
;
import
java.util.*
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
java.util.stream.Collectors
;
import
java.util.stream.IntStream
;
record
Key
(
String
key
)
{
@Override
public
int
hashCode
()
{
...
...
@@ -11,14 +18,118 @@ record Key(String key) {
}
}
class
BucketMap
<
K
,
V
>
implements
Map
<
K
,
V
>
{
public
int
num_buckets
;
ArrayList
<
LinkedList
<
SimpleEntry
<
K
,
V
>>>
boxes
;
BucketMap
()
{
num_buckets
=
256
;
boxes
=
new
ArrayList
<>(
num_buckets
);
for
(
int
i
=
0
;
i
<
num_buckets
;
i
++)
{
boxes
.
add
(
new
LinkedList
<>());
}
}
public
int
size
()
{
return
boxes
.
stream
().
mapToInt
(
LinkedList:
:
size
).
sum
();
}
public
boolean
isEmpty
()
{
return
size
()
==
0
;
}
public
boolean
containsKey
(
Object
key
)
{
return
boxes
.
get
(
key
.
hashCode
()).
stream
().
map
(
Entry:
:
getKey
)
.
anyMatch
(
key:
:
equals
);
}
public
boolean
containsValue
(
Object
value
)
{
return
values
().
contains
(
value
);
}
public
V
get
(
Object
key
)
{
return
boxes
.
get
(
key
.
hashCode
()).
stream
()
.
filter
(
entry
->
entry
.
getKey
().
equals
(
key
)).
findAny
()
.
orElse
(
null
).
getValue
();
}
public
V
put
(
K
key
,
V
value
)
{
LinkedList
<
SimpleEntry
<
K
,
V
>>
box
=
boxes
.
get
(
key
.
hashCode
());
Optional
<
SimpleEntry
<
K
,
V
>>
optionalEntry
=
box
.
stream
().
filter
(
entry
->
entry
.
getKey
().
equals
(
key
))
.
findAny
();
if
(
optionalEntry
.
isPresent
())
return
optionalEntry
.
get
().
setValue
(
value
);
else
{
SimpleEntry
<
K
,
V
>
ans
=
new
SimpleEntry
<>(
key
,
value
);
box
.
add
(
ans
);
return
null
;
}
}
public
V
remove
(
Object
key
)
{
LinkedList
<
SimpleEntry
<
K
,
V
>>
box
=
boxes
.
get
(
key
.
hashCode
());
SimpleEntry
<
K
,
V
>
entryToRemove
=
box
.
stream
().
filter
(
entry
->
entry
.
getKey
().
equals
(
key
))
.
findAny
().
orElse
(
null
);
return
box
.
remove
(
entryToRemove
)
?
entryToRemove
.
getValue
()
:
null
;
}
public
void
putAll
(
Map
<?
extends
K
,
?
extends
V
>
m
)
{
m
.
forEach
(
this
::
put
);
}
public
void
clear
()
{
boxes
.
forEach
(
List:
:
clear
);
}
public
Set
<
K
>
keySet
()
{
return
boxes
.
stream
()
.
flatMap
(
box
->
box
.
stream
().
map
(
SimpleEntry:
:
getKey
))
.
collect
(
Collectors
.
toSet
());
}
public
Collection
<
V
>
values
()
{
return
boxes
.
stream
()
.
flatMap
(
box
->
box
.
stream
().
map
(
SimpleEntry:
:
getValue
))
.
toList
();
}
public
Set
<
Entry
<
K
,
V
>>
entrySet
()
{
return
boxes
.
stream
().
flatMap
(
List:
:
stream
).
collect
(
Collectors
.
toSet
());
}
public
List
<
V
>
getBucket
(
int
i
)
{
return
boxes
.
get
(
i
).
stream
().
map
(
SimpleEntry:
:
getValue
).
toList
();
}
}
public
class
Day15
extends
Day
{
Day15
(
String
inputName
)
{
super
(
inputName
);
}
@Override
String
part1
()
{
return
null
;
return
String
.
valueOf
(
Arrays
.
stream
(
input
.
getFirst
().
split
(
","
))
.
mapToInt
(
key
->
new
Key
(
key
).
hashCode
()).
sum
());
}
@Override
String
part2
()
{
return
null
;
BucketMap
<
Key
,
Integer
>
series
=
new
BucketMap
<>();
Matcher
matcher
=
Pattern
.
compile
(
"\\G(?:^|,)\\s*(?<label>\\w+)(?:-|=(?<focal>[1-9]))\\s*"
)
.
matcher
(
input
.
getFirst
());
while
(
matcher
.
find
())
{
Key
label
=
new
Key
(
matcher
.
group
(
"label"
));
String
focal
=
matcher
.
group
(
"focal"
);
if
(
focal
==
null
)
series
.
remove
(
label
);
else
series
.
put
(
label
,
Integer
.
valueOf
(
focal
));
}
return
String
.
valueOf
(
IntStream
.
range
(
0
,
series
.
num_buckets
).
map
(
i
->
IntStream
.
range
(
0
,
series
.
getBucket
(
i
).
size
())
.
map
(
j
->
series
.
getBucket
(
i
).
get
(
j
)
*
(
j
+
1
)).
sum
()
*
(
i
+
1
)).
sum
());
}
}
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