FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
adventofcode/src/main/java/AoC2015_13.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_13.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
113 lines (96 loc) · 4.2 KB
Breadcrumbs
adventofcode
/
src
/
main
/
java
/
AoC2015_13.java
Copy path
File metadata and controls
113 lines (96 loc) · 4.2 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
113
import
java
.
util
.
HashMap
;
import
java
.
util
.
List
;
import
java
.
util
.
Map
;
import
java
.
util
.
stream
.
IntStream
;
import
com
.
github
.
pareronia
.
aoc
.
MutableInt
;
import
com
.
github
.
pareronia
.
aoc
.
itertools
.
IterTools
;
import
com
.
github
.
pareronia
.
aoc
.
solution
.
Sample
;
import
com
.
github
.
pareronia
.
aoc
.
solution
.
Samples
;
import
com
.
github
.
pareronia
.
aoc
.
solution
.
SolutionBase
;
public
class
AoC2015_13
extends
SolutionBase
<
AoC2015_13
.
Happiness
,
Integer
,
Integer
> {
private
AoC2015_13
(
final
boolean
debug
) {
super
(
debug
);
}
public
static
final
AoC2015_13
create
() {
return
new
AoC2015_13
(
false
);
}
public
static
final
AoC2015_13
createDebug
() {
return
new
AoC2015_13
(
true
);
}
@
Override
protected
Happiness
parseInput
(
final
List
<
String
>
inputs
) {
return
Happiness
.
fromInput
(
inputs
);
}
@
Override
public
Integer
solvePart1
(
final
Happiness
input
) {
return
input
.
getOptimalHappinessChangeWithoutMe
();
}
@
Override
public
Integer
solvePart2
(
final
Happiness
input
) {
return
input
.
getOptimalHappinessChangeWithMe
();
}
@
Override
@
Samples
({
@
Sample
(
method
=
"part1"
,
input
=
TEST
,
expected
=
"330"
),
})
public
void
samples
() {
}
public
static
void
main
(
final
String
[]
args
)
throws
Exception
{
AoC2015_13
.
create
().
run
();
}
private
static
final
String
TEST
=
"""
Alice would gain 54 happiness units by sitting next to Bob.
\r
Alice would lose 79 happiness units by sitting next to Carol.
\r
Alice would lose 2 happiness units by sitting next to David.
\r
Bob would gain 83 happiness units by sitting next to Alice.
\r
Bob would lose 7 happiness units by sitting next to Carol.
\r
Bob would lose 63 happiness units by sitting next to David.
\r
Carol would lose 62 happiness units by sitting next to Alice.
\r
Carol would gain 60 happiness units by sitting next to Bob.
\r
Carol would gain 55 happiness units by sitting next to David.
\r
David would gain 46 happiness units by sitting next to Alice.
\r
David would lose 7 happiness units by sitting next to Bob.
\r
David would gain 41 happiness units by sitting next to Carol."""
;
record
Happiness
(
int
[][]
happinessMatrix
) {
public
static
Happiness
fromInput
(
final
List
<
String
>
inputs
) {
final
Map
<
String
,
Integer
>
map
=
new
HashMap
<>();
final
Map
<
int
[],
Integer
>
values
=
new
HashMap
<>();
final
MutableInt
cnt
=
new
MutableInt
(
0
);
for
(
final
String
input
:
inputs
) {
final
String
[]
s
=
input
.
substring
(
0
,
input
.
length
() -
1
).
split
(
" "
);
final
String
d1
=
s
[
0
];
final
String
d2
=
s
[
10
];
final
int
idx1
=
map
.
computeIfAbsent
(
d1
,
x
->
cnt
.
getAndIncrement
());
final
int
idx2
=
map
.
computeIfAbsent
(
d2
,
x
->
cnt
.
getAndIncrement
());
final
int
value
=
Integer
.
parseInt
(
s
[
3
]);
values
.
put
(
new
int
[] {
idx1
,
idx2
},
"gain"
.
equals
(
s
[
2
]) ?
value
: -
value
);
}
final
int
[][]
happinessMatrix
=
new
int
[
map
.
size
() +
1
][
map
.
size
() +
1
];
values
.
entrySet
().
stream
()
.
forEach
(
e
-> {
happinessMatrix
[
e
.
getKey
()[
0
]][
e
.
getKey
()[
1
]] =
e
.
getValue
();
});
return
new
Happiness
(
happinessMatrix
);
}
private
int
solve
(
final
int
size
) {
final
int
[]
idxs
=
IntStream
.
range
(
0
,
size
).
toArray
();
return
IterTools
.
permutations
(
idxs
)
.
mapToInt
(
p
->
IntStream
.
range
(
0
,
p
.
length
)
.
map
(
i
-> {
final
int
d1
=
p
[
i
];
final
int
d2
=
p
[(
i
+
1
) %
p
.
length
];
return
this
.
happinessMatrix
[
d1
][
d2
] +
this
.
happinessMatrix
[
d2
][
d1
];
})
.
sum
())
.
max
().
orElseThrow
();
}
public
int
getOptimalHappinessChangeWithoutMe
() {
return
solve
(
this
.
happinessMatrix
.
length
-
1
);
}
public
int
getOptimalHappinessChangeWithMe
() {
return
solve
(
this
.
happinessMatrix
.
length
);
}
}
}
Back
|
FazBrowse Home
|
New Git URL