FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
adventofcode/src/main/java/AoC2016_04.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
/
AoC2016_04.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
104 lines (87 loc) · 3.17 KB
Breadcrumbs
adventofcode
/
src
/
main
/
java
/
AoC2016_04.java
Copy path
File metadata and controls
104 lines (87 loc) · 3.17 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
import
static
com
.
github
.
pareronia
.
aoc
.
AssertUtils
.
assertTrue
;
import
static
com
.
github
.
pareronia
.
aoc
.
Utils
.
toAString
;
import
static
java
.
util
.
Comparator
.
comparing
;
import
java
.
util
.
List
;
import
java
.
util
.
regex
.
Matcher
;
import
java
.
util
.
regex
.
Pattern
;
import
com
.
github
.
pareronia
.
aoc
.
Counter
;
import
com
.
github
.
pareronia
.
aoc
.
Counter
.
Entry
;
import
com
.
github
.
pareronia
.
aoc
.
StringOps
;
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
AoC2016_04
extends
SolutionBase
<
List
<
AoC2016_04
.
Room
>,
Integer
,
Integer
> {
private
static
final
Pattern
REGEXP
=
Pattern
.
compile
(
"([-a-z]+)-([0-9]+)
\\
[([a-z]{5})
\\
]$"
);
private
static
final
Pattern
MATCH
=
Pattern
.
compile
(
"[a-z]{9}-[a-z]{6}-[a-z]{7}$"
);
private
AoC2016_04
(
final
boolean
debug
) {
super
(
debug
);
}
public
static
final
AoC2016_04
create
() {
return
new
AoC2016_04
(
false
);
}
public
static
final
AoC2016_04
createDebug
() {
return
new
AoC2016_04
(
true
);
}
@
Override
protected
List
<
Room
>
parseInput
(
final
List
<
String
>
inputs
) {
return
inputs
.
stream
().
map
(
Room
::
fromInput
).
toList
();
}
@
Override
public
Integer
solvePart1
(
final
List
<
Room
>
rooms
) {
return
rooms
.
stream
()
.
filter
(
Room
::
isReal
)
.
mapToInt
(
Room
::
sectorId
)
.
sum
();
}
@
Override
public
Integer
solvePart2
(
final
List
<
Room
>
rooms
) {
return
rooms
.
stream
()
.
filter
(
r
->
MATCH
.
matcher
(
r
.
name
()).
matches
())
.
filter
(
r
->
r
.
decrypt
().
equals
(
"northpole object storage"
))
.
map
(
Room
::
sectorId
)
.
findFirst
().
orElseThrow
();
}
@
Samples
({
@
Sample
(
method
=
"part1"
,
input
=
TEST
,
expected
=
"1514"
)
})
public
static
void
main
(
final
String
[]
args
)
throws
Exception
{
AoC2016_04
.
create
().
run
();
}
private
static
final
String
TEST
=
"""
aaaaa-bbb-z-y-x-123[abxyz]
a-b-c-d-e-f-g-h-987[abcde]
not-a-real-room-404[oarel]
totally-real-room-200[decoy]
"""
;
record
Room
(
String
name
,
int
sectorId
,
String
checkum
) {
public
static
Room
fromInput
(
final
String
line
) {
final
Matcher
m
=
REGEXP
.
matcher
(
line
);
assertTrue
(
m
.
matches
(), () ->
"Expected match"
);
return
new
Room
(
m
.
group
(
1
),
Integer
.
parseInt
(
m
.
group
(
2
)),
m
.
group
(
3
));
}
public
boolean
isReal
() {
return
new
Counter
<>(
Utils
.
asCharacterStream
(
this
.
name
.
replace
(
"-"
,
""
)))
.
mostCommon
().
stream
()
.
sorted
(
comparing
(
e
->
e
.
count
() * -
100
+
e
.
value
().
charValue
()))
.
limit
(
5
)
.
map
(
Entry
::
value
)
.
collect
(
toAString
())
.
equals
(
this
.
checkum
);
}
private
Character
decrypt
(
final
Character
c
,
final
int
shift
) {
if
(
c
==
'-'
) {
return
' '
;
}
return
StringOps
.
nextLetter
(
c
,
shift
);
}
public
String
decrypt
() {
final
int
shift
=
this
.
sectorId
%
26
;
return
Utils
.
asCharacterStream
(
this
.
name
)
.
map
(
c
->
decrypt
(
c
,
shift
))
.
collect
(
toAString
());
}
}
}
Back
|
FazBrowse Home
|
New Git URL