FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
adventofcode/src/main/java/AoC2015_15.java at main · pareronia/adventofcode · GitHub
pareronia
/
adventofcode
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Issues
0
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
adventofcode
/
src
/
main
/
java
/
AoC2015_15.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
112 lines (93 loc) · 3.74 KB
Breadcrumbs
adventofcode
/
src
/
main
/
java
/
AoC2015_15.java
Copy path
File metadata and controls
112 lines (93 loc) · 3.74 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import
java
.
util
.
List
;
import
java
.
util
.
stream
.
IntStream
;
import
java
.
util
.
stream
.
Stream
;
import
com
.
github
.
pareronia
.
aoc
.
Utils
;
import
com
.
github
.
pareronia
.
aoc
.
solution
.
Sample
;
import
com
.
github
.
pareronia
.
aoc
.
solution
.
Samples
;
import
com
.
github
.
pareronia
.
aoc
.
solution
.
SolutionBase
;
public
class
AoC2015_15
extends
SolutionBase
<
AoC2015_15
.
Ingredients
,
Integer
,
Integer
> {
private
AoC2015_15
(
final
boolean
debug
) {
super
(
debug
);
}
public
static
final
AoC2015_15
create
() {
return
new
AoC2015_15
(
false
);
}
public
static
final
AoC2015_15
createDebug
() {
return
new
AoC2015_15
(
true
);
}
@
Override
public
Ingredients
parseInput
(
final
List
<
String
>
inputs
) {
return
Ingredients
.
fromInput
(
inputs
);
}
@
Override
public
Integer
solvePart1
(
final
Ingredients
ingredients
) {
return
ingredients
.
getHighestScore
();
}
@
Override
public
Integer
solvePart2
(
final
Ingredients
ingredients
) {
return
ingredients
.
getHighestScoreWithCalorieLimit
(
500
);
}
@
Override
@
Samples
({
@
Sample
(
method
=
"part1"
,
input
=
TEST
,
expected
=
"62842880"
),
@
Sample
(
method
=
"part2"
,
input
=
TEST
,
expected
=
"57600000"
),
})
public
void
samples
() {
}
public
static
void
main
(
final
String
[]
args
)
throws
Exception
{
AoC2015_15
.
create
().
run
();
}
private
static
final
String
TEST
=
"Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8
\r
\n
"
+
"Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3"
;
record
Ingredients
(
int
[][]
ingredients
) {
private
enum
Property
{
CAPACITY
,
DURABILITY
,
FLAVOR
,
TEXTURE
,
CALORIES
}
public
static
Ingredients
fromInput
(
final
List
<
String
>
inputs
) {
return
new
Ingredients
(
inputs
.
stream
()
.
map
(
Utils
::
integerNumbers
)
.
toArray
(
int
[][]::
new
));
}
public
int
getHighestScore
() {
return
getMaximumScore
(
null
);
}
public
int
getHighestScoreWithCalorieLimit
(
final
int
limit
) {
return
getMaximumScore
(
limit
);
}
private
int
getMaximumScore
(
final
Integer
limit
) {
return
generateMeasures
()
.
mapToInt
(
m
->
calculateScore
(
m
,
limit
))
.
max
().
orElseThrow
();
}
private
Stream
<
int
[]>
generateMeasures
() {
final
Stream
.
Builder
<
int
[]>
builder
=
Stream
.
builder
();
for
(
int
i
=
0
;
i
<=
100
;
i
++) {
if
(
this
.
ingredients
.
length
==
2
) {
builder
.
add
(
new
int
[] {
i
,
100
-
i
});
continue
;
}
for
(
int
j
=
0
;
j
<=
100
-
i
;
j
++) {
for
(
int
k
=
0
;
k
<=
100
-
i
-
j
;
k
++) {
final
int
m
=
100
-
i
-
j
-
k
;
builder
.
add
(
new
int
[] {
i
,
j
,
k
,
m
});
}
}
}
return
builder
.
build
();
}
private
int
getPropertyScore
(
final
int
[]
measures
,
final
Property
p
) {
return
IntStream
.
range
(
0
,
this
.
ingredients
.
length
)
.
map
(
i
->
this
.
ingredients
[
i
][
p
.
ordinal
()] *
measures
[
i
])
.
sum
();
}
private
int
calculateScore
(
final
int
[]
measures
,
final
Integer
caloriesTarget
) {
if
(
caloriesTarget
!=
null
&&
getPropertyScore
(
measures
,
Property
.
CALORIES
) !=
caloriesTarget
) {
return
0
;
}
return
Stream
.
of
(
Property
.
values
())
.
filter
(
p
->
p
!=
Property
.
CALORIES
)
.
mapToInt
(
p
->
Math
.
max
(
0
,
getPropertyScore
(
measures
,
p
)))
.
reduce
(
1
, (
a
,
b
) ->
a
*
b
);
}
}
}
Back
|
FazBrowse Home
|
New Git URL