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
a20bb720
Commit
a20bb720
authored
1 year ago
by
LUDMANN Pierre
Browse files
Options
Downloads
Patches
Plain Diff
fix build and usage
parent
9cc429a6
Branches
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#16094
failed
1 year ago
Stage: build
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/CieDuVaseDesNoces/Day5.java
+110
-0
110 additions, 0 deletions
src/main/java/CieDuVaseDesNoces/Day5.java
with
110 additions
and
0 deletions
src/main/java/CieDuVaseDesNoces/Day5.java
0 → 100644
+
110
−
0
View file @
a20bb720
package
CieDuVaseDesNoces
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.*
;
import
java.util.stream.IntStream
;
import
java.util.stream.Stream
;
import
static
java
.
lang
.
Thread
.
currentThread
;
public
class
Day5
extends
Day
{
List
<
Long
>
seeds
;
List
<
String
>
categories
=
new
LinkedList
<>();
Map
<
String
,
RangeMap
>
mapsByDest
=
new
HashMap
<>(
7
);
public
Day5
(
String
inputName
)
{
try
(
InputStream
inputStream
=
currentThread
().
getContextClassLoader
()
.
getResourceAsStream
(
inputName
))
{
assert
inputStream
!=
null
;
String
[]
seedsAndMaps
=
new
String
(
inputStream
.
readAllBytes
())
.
split
(
"\n\n"
,
2
);
seeds
=
Arrays
.
stream
(
seedsAndMaps
[
0
].
substring
(
"seeds: "
.
length
()).
split
(
" +"
))
.
map
(
Long:
:
parseUnsignedLong
).
toList
();
Map
<
String
,
String
>
nextCategory
=
new
HashMap
<>(
7
);
for
(
String
mapString
:
seedsAndMaps
[
1
].
split
(
"\n\n"
))
{
String
[]
categoriesAndRanges
=
mapString
.
split
(
" +map:\n"
,
2
);
String
[]
sourceAndDestination
=
categoriesAndRanges
[
0
].
split
(
"-to-"
,
2
);
nextCategory
.
put
(
sourceAndDestination
[
0
],
sourceAndDestination
[
1
]);
parseRanges
(
sourceAndDestination
[
1
],
categoriesAndRanges
[
1
]);
}
String
category
=
nextCategory
.
get
(
"seed"
);
while
(
category
!=
null
)
{
categories
.
add
(
category
);
category
=
nextCategory
.
get
(
category
);
}
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
void
parseRanges
(
String
dest
,
String
ranges
)
{
RangeMap
ans
=
new
RangeMap
();
ans
.
put
(
0L
,
0L
);
for
(
String
rangeLine
:
ranges
.
split
(
"\n"
))
{
ans
.
put
(
rangeLine
);
}
mapsByDest
.
put
(
dest
,
ans
);
}
String
part1
()
{
Long
ans
=
categories
.
stream
().
map
(
mapsByDest:
:
get
)
.
reduce
(
seeds
.
stream
().
mapToLong
(
x
->
x
),
(
numbers
,
rangeMap
)
->
numbers
.
map
(
rangeMap:
:
apply
)
,
(
x
,
y
)
->
y
).
min
().
orElseThrow
();
return
String
.
valueOf
(
ans
);
}
String
part2
()
{
OptionalLong
ans
=
categories
.
stream
().
map
(
mapsByDest:
:
get
)
.
reduce
(
IntStream
.
range
(
0
,
seeds
.
size
()
/
2
).
mapToObj
(
i
->
new
Range
(
seeds
.
get
(
2
*
i
),
seeds
.
get
(
2
*
i
+
1
))),
(
numberRanges
,
mapRanges
)
->
numberRanges
.
flatMap
(
mapRanges:
:
apply
),
(
x
,
y
)
->
y
)
.
mapToLong
(
Range:
:
start
).
min
();
return
String
.
valueOf
(
ans
.
orElseThrow
());
}
}
record
Range
(
Long
start
,
Long
length
)
{
}
class
RangeMap
extends
TreeMap
<
Long
,
Long
>
{
void
put
(
String
rangeLine
)
{
long
[]
rangeInfo
=
Arrays
.
stream
(
rangeLine
.
split
(
" +"
))
.
mapToLong
(
Long:
:
parseUnsignedLong
).
toArray
();
Long
destinationRangeStart
=
rangeInfo
[
0
];
Long
sourceRangeStart
=
rangeInfo
[
1
];
Long
rangeLength
=
rangeInfo
[
2
];
put
(
sourceRangeStart
,
destinationRangeStart
-
sourceRangeStart
);
Long
sourceRangeEnd
=
sourceRangeStart
+
rangeLength
;
if
(!
containsKey
(
sourceRangeEnd
))
put
(
sourceRangeEnd
,
0L
);
}
Long
apply
(
Long
n
)
{
return
n
+
floorEntry
(
n
).
getValue
();
}
Stream
<
Range
>
apply
(
Range
sourceRange
)
{
Long
fromKey
=
sourceRange
.
start
();
Long
toKey
=
sourceRange
.
start
()
+
sourceRange
.
length
();
ArrayList
<
Long
>
sourceStarts
=
new
ArrayList
<>(
subMap
(
fromKey
,
toKey
).
keySet
().
stream
().
sorted
().
toList
());
sourceStarts
.
addFirst
(
sourceRange
.
start
());
List
<
Long
>
sourceEnds
=
new
ArrayList
<>(
sourceStarts
);
Collections
.
rotate
(
sourceEnds
,
-
1
);
sourceEnds
.
set
(
sourceEnds
.
size
()
-
1
,
sourceRange
.
start
()
+
sourceRange
.
length
());
return
IntStream
.
range
(
0
,
sourceStarts
.
size
()).
mapToObj
(
i
->
new
Range
(
apply
(
sourceStarts
.
get
(
i
)),
sourceEnds
.
get
(
i
)
-
sourceStarts
.
get
(
i
)));
}
}
\ No newline at end of file
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