FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
adventofcode/src/main/java/AoC2015_08.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_08.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
86 lines (70 loc) · 2.36 KB
Breadcrumbs
adventofcode
/
src
/
main
/
java
/
AoC2015_08.java
Copy path
File metadata and controls
86 lines (70 loc) · 2.36 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
import
static
com
.
github
.
pareronia
.
aoc
.
StringUtils
.
countMatches
;
import
java
.
util
.
List
;
import
java
.
util
.
regex
.
Pattern
;
import
com
.
github
.
pareronia
.
aoc
.
solution
.
Sample
;
import
com
.
github
.
pareronia
.
aoc
.
solution
.
Samples
;
import
com
.
github
.
pareronia
.
aoc
.
solution
.
SolutionBase
;
public
class
AoC2015_08
extends
SolutionBase
<
List
<
String
>,
Integer
,
Integer
> {
private
static
final
Pattern
HEX
=
Pattern
.
compile
(
"
\\
\\
x[0-9a-f]{2}"
);
private
AoC2015_08
(
final
boolean
debug
) {
super
(
debug
);
}
public
static
final
AoC2015_08
create
() {
return
new
AoC2015_08
(
false
);
}
public
static
final
AoC2015_08
createDebug
() {
return
new
AoC2015_08
(
true
);
}
@
Override
protected
List
<
String
>
parseInput
(
final
List
<
String
>
inputs
) {
return
inputs
;
}
private
int
solve
(
final
List
<
String
>
inputs
,
final
Mode
mode
) {
return
inputs
.
stream
().
mapToInt
(
mode
::
overhead
).
sum
();
}
@
Override
public
Integer
solvePart1
(
final
List
<
String
>
inputs
) {
return
solve
(
inputs
,
Mode
.
DECODE
);
}
@
Override
public
Integer
solvePart2
(
final
List
<
String
>
inputs
) {
return
solve
(
inputs
,
Mode
.
ENCODE
);
}
private
enum
Mode
{
DECODE
,
ENCODE
;
public
int
overhead
(
String
str
) {
return
switch
(
this
) {
case
DECODE
-> {
assert
str
.
charAt
(
0
) ==
'"'
&&
str
.
charAt
(
str
.
length
() -
1
) ==
'"'
;
int
cnt
=
2
;
while
(
str
.
contains
(
"
\\
\\
"
)) {
str
=
str
.
replaceFirst
(
"[
\\
\\
]{2}"
,
""
);
cnt
++;
}
cnt
+=
countMatches
(
str
,
"
\\
\"
"
);
cnt
+=
3
*
HEX
.
matcher
(
str
).
results
().
count
();
yield
cnt
;
}
case
ENCODE
->
2
+
countMatches
(
str
,
"
\\
"
) +
countMatches
(
str
,
"
\"
"
);
};
}
}
@
Override
@
Samples
({
@
Sample
(
method
=
"part1"
,
input
=
TEST
,
expected
=
"12"
),
@
Sample
(
method
=
"part2"
,
input
=
TEST
,
expected
=
"19"
),
})
public
void
samples
() {
}
public
static
void
main
(
final
String
[]
args
)
throws
Exception
{
AoC2015_08
.
create
().
run
();
}
private
static
final
String
TEST
=
"""
""
"abc"
"aaa
\\
"aaa"
"
\\
x27
\"
\
"""
;
}
Back
|
FazBrowse Home
|
New Git URL