FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
adventofcode/src/main/java/AoC2016_16.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_16.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
85 lines (70 loc) · 2.41 KB
Breadcrumbs
adventofcode
/
src
/
main
/
java
/
AoC2016_16.java
Copy path
File metadata and controls
85 lines (70 loc) · 2.41 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
import
java
.
util
.
List
;
import
com
.
github
.
pareronia
.
aoc
.
StringOps
;
import
com
.
github
.
pareronia
.
aoc
.
StringUtils
;
import
com
.
github
.
pareronia
.
aoc
.
solution
.
SolutionBase
;
public
class
AoC2016_16
extends
SolutionBase
<
AoC2016_16
.
DragonCurve
,
String
,
String
> {
private
AoC2016_16
(
final
boolean
debug
) {
super
(
debug
);
}
public
static
AoC2016_16
create
() {
return
new
AoC2016_16
(
false
);
}
public
static
AoC2016_16
createDebug
() {
return
new
AoC2016_16
(
true
);
}
private
String
solve
(
final
DragonCurve
dragonCurve
,
final
int
size
) {
return
dragonCurve
.
checkSumForSize
(
size
);
}
@
Override
protected
DragonCurve
parseInput
(
final
List
<
String
>
inputs
) {
return
new
DragonCurve
(
inputs
.
get
(
0
));
}
@
Override
public
String
solvePart1
(
final
DragonCurve
dragonCurve
) {
return
solve
(
dragonCurve
,
272
);
}
@
Override
public
String
solvePart2
(
final
DragonCurve
dragonCurve
) {
return
solve
(
dragonCurve
,
35651584
);
}
@
Override
public
void
samples
() {
final
AoC2016_16
test
=
AoC2016_16
.
createDebug
();
assert
test
.
solve
(
test
.
parseInput
(
List
.
of
(
TEST
)),
20
).
equals
(
"01100"
);
}
public
static
void
main
(
final
String
[]
args
)
throws
Exception
{
AoC2016_16
.
create
().
run
();
}
private
static
final
String
TEST
=
"10000"
;
record
DragonCurve
(
String
initialState
) {
private
String
dragonCurve
(
final
String
input
) {
final
String
b
=
StringOps
.
translate
(
StringUtils
.
reverse
(
input
),
"01"
,
"10"
);
return
new
StringBuilder
().
append
(
input
)
.
append
(
"0"
).
append
(
b
).
toString
();
}
private
char
[]
checkSum
(
final
char
[]
data
) {
final
char
[]
pairs
=
new
char
[
data
.
length
/
2
];
for
(
int
i
=
0
;
i
<
data
.
length
-
1
;
i
+=
2
) {
if
(
data
[
i
] ==
data
[
i
+
1
]) {
pairs
[
i
/
2
] =
'1'
;
}
else
{
pairs
[
i
/
2
] =
'0'
;
}
}
if
(
pairs
.
length
%
2
!=
0
) {
return
pairs
;
}
else
{
return
checkSum
(
pairs
);
}
}
public
String
checkSumForSize
(
final
int
size
) {
String
data
=
this
.
initialState
;
while
(
data
.
length
() <
size
) {
data
=
dragonCurve
(
data
);
}
return
String
.
valueOf
(
checkSum
(
data
.
substring
(
0
,
size
).
toCharArray
()));
}
}
}
Back
|
FazBrowse Home
|
New Git URL