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
15c715e4
Commit
15c715e4
authored
1 year ago
by
LUDMANN Pierre
Browse files
Options
Downloads
Patches
Plain Diff
[day2] part1 use parse
parent
be6bb389
Branches
day2-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/Day2.java
+18
-24
18 additions, 24 deletions
src/main/java/CieDuVaseDesNoces/Day2.java
with
18 additions
and
24 deletions
src/main/java/CieDuVaseDesNoces/Day2.java
+
18
−
24
View file @
15c715e4
...
...
@@ -8,35 +8,29 @@ import static java.lang.Integer.parseInt;
public
class
Day2
extends
Day
{
Hash
Map
<
String
,
Integer
>
limit
=
new
HashMap
<>(
3
);
Map
<
Integer
,
Map
<
String
,
Integer
>>
maxQty
=
new
HashMap
<>(
input
.
size
());
Map
<
Integer
,
Map
<
String
,
Integer
>
>
maxQtyByGame
=
new
HashMap
<>(
input
.
size
());
Day2
(
String
inputName
)
{
super
(
inputName
);
parse
();
limit
.
put
(
"red"
,
12
);
limit
.
put
(
"green"
,
13
);
limit
.
put
(
"blue"
,
14
);
}
String
part1
()
{
int
ans
=
0
;
for
(
String
game
:
input
)
{
String
[]
tmp
=
game
.
split
(
": "
,
2
);
int
gameId
=
parseInt
(
tmp
[
0
].
substring
(
"Game "
.
length
()));
boolean
possible
=
true
;
for
(
String
set
:
tmp
[
1
].
split
(
"; "
))
{
for
(
String
cubes
:
set
.
split
(
", "
))
{
String
[]
colorQty
=
cubes
.
split
(
" "
,
2
);
if
(
parseInt
(
colorQty
[
0
])
>
limit
.
get
(
colorQty
[
1
]))
{
possible
=
false
;
break
;
}
}
if
(!
possible
)
break
;
}
if
(
possible
)
ans
+=
gameId
;
}
HashMap
<
String
,
Integer
>
limits
=
new
HashMap
<>(
3
);
limits
.
put
(
"red"
,
12
);
limits
.
put
(
"green"
,
13
);
limits
.
put
(
"blue"
,
14
);
return
part1
(
limits
);
}
String
part1
(
Map
<
String
,
Integer
>
limits
)
{
int
ans
=
maxQtyByGame
.
keySet
()
.
stream
()
.
filter
(
gameId
->
maxQtyByGame
.
get
(
gameId
).
entrySet
().
stream
()
.
allMatch
(
colorMax
->
colorMax
.
getValue
()
<=
limits
.
get
(
colorMax
.
getKey
())))
.
mapToInt
(
x
->
x
).
sum
();
return
String
.
valueOf
(
ans
);
}
...
...
@@ -53,12 +47,12 @@ public class Day2 extends Day {
currentMaxQty
.
get
(
colorQty
[
1
])));
}
Integer
gameId
=
Integer
.
valueOf
(
tmp
[
0
].
substring
(
5
));
maxQty
.
put
(
gameId
,
currentMaxQty
);
maxQty
ByGame
.
put
(
gameId
,
currentMaxQty
);
}
}
String
part2
()
{
int
ans
=
maxQty
.
values
().
stream
()
int
ans
=
maxQty
ByGame
.
values
().
stream
()
.
map
(
game
->
game
.
values
().
stream
().
mapToInt
(
x
->
x
)
.
reduce
(
1
,
(
x
,
y
)
->
x
*
y
)).
reduce
(
0
,
Integer:
:
sum
);
return
String
.
valueOf
(
ans
);
...
...
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