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
751e4d08
Commit
751e4d08
authored
1 year ago
by
LUDMANN Pierre
Browse files
Options
Downloads
Patches
Plain Diff
[day8] there is a lie but i have been spoiled
parent
c77e4d58
Branches
day8-ludmann1
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/CieDuVaseDesNoces/Day8.java
+22
-9
22 additions, 9 deletions
src/main/java/CieDuVaseDesNoces/Day8.java
with
22 additions
and
9 deletions
src/main/java/CieDuVaseDesNoces/Day8.java
+
22
−
9
View file @
751e4d08
package
CieDuVaseDesNoces
;
import
org.apache.commons.math3.util.ArithmeticUtils
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
...
...
@@ -8,7 +10,6 @@ import java.util.Map;
import
java.util.Objects
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
java.util.stream.Stream
;
import
static
java
.
lang
.
Thread
.
currentThread
;
...
...
@@ -26,8 +27,9 @@ class Day8 extends Day {
reader
.
lines
().
forEach
(
line
->
{
Matcher
nodeMatcher
=
nodePattern
.
matcher
(
line
);
if
(!
nodeMatcher
.
matches
())
return
;
network
.
put
(
nodeMatcher
.
group
(
"from"
),
new
ElementMap
(
nodeMatcher
.
group
(
"left"
),
nodeMatcher
.
group
(
"right"
)));
network
.
put
(
nodeMatcher
.
group
(
"from"
),
new
ElementMap
(
nodeMatcher
.
group
(
"left"
),
nodeMatcher
.
group
(
"right"
)));
});
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
...
...
@@ -35,21 +37,32 @@ class Day8 extends Day {
}
String
part1
()
{
return
part1
(
"AAA"
,
"Z
ZZ
"
);
return
part1
(
"AAA"
,
"Z
$
"
);
}
String
part1
(
String
startLabel
,
String
target
Label
)
{
String
part1
(
String
startLabel
,
String
target
Regex
)
{
String
label
=
startLabel
;
Pattern
targetPattern
=
Pattern
.
compile
(
targetRegex
);
int
step
=
0
;
int
length
=
instructions
.
length
;
while
(!
label
.
equals
(
targetLabel
))
{
label
=
network
.
get
(
label
).
get
(
instructions
[
step
++
%
length
]);
}
do
{
label
=
network
.
get
(
label
).
get
(
instructions
[
step
++
%
length
]);
}
while
(!
targetPattern
.
matcher
(
label
).
find
());
return
String
.
valueOf
(
step
);
}
String
part2
()
{
return
null
;
return
part2
(
"A$"
,
"Z$"
);
}
String
part2
(
String
startRegex
,
String
targetRegex
)
{
Pattern
targetPattern
=
Pattern
.
compile
(
startRegex
);
long
ans
=
network
.
keySet
().
stream
()
.
filter
(
label
->
targetPattern
.
matcher
(
label
).
find
())
.
map
(
label
->
part1
(
label
,
targetRegex
))
.
mapToLong
(
Long:
:
parseUnsignedLong
)
.
reduce
(
1
,
ArithmeticUtils:
:
lcm
);
return
String
.
valueOf
(
ans
);
}
static
final
class
ElementMap
extends
HashMap
<
Character
,
String
>
{
...
...
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