Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
cs2-20wi
lab02
Commits
0ca21ce3
Commit
0ca21ce3
authored
5 years ago
by
Caleb C. Sander
Browse files
Options
Download
Email Patches
Plain Diff
Update Phase 2
parent
da81d832
master
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Bomb.java
+21
-13
src/Bomb.java
tests/BombTests.java
+7
-14
tests/BombTests.java
with
28 additions
and
27 deletions
+28
-27
src/Bomb.java
View file @
0ca21ce3
import
java.util.*
;
import
java.util.stream.Collectors
;
public
class
Bomb
{
public
String
shufflePassword
(
String
s
)
{
String
code
=
""
+
s
.
hashCode
();
List
<
String
>
l
=
code
.
codePoints
().
boxed
().
map
((
x
)
->
""
+
(
char
)(
int
)
x
).
collect
(
Collectors
.
toList
());
List
<
String
>
l
=
new
ArrayList
<>();
for
(
char
c
:
code
.
toCharArray
())
{
l
.
add
(
""
+
c
);
}
Random
r
=
new
Random
(
1337
);
Collections
.
shuffle
(
l
,
r
);
...
...
@@ -21,35 +23,41 @@ public class Bomb {
}
public
void
phase1
(
String
password
)
{
String
psave
=
password
;
Set
<
String
>
animals
=
new
HashSet
<>(
2
,
1.0f
);
animals
.
add
(
"cow"
);
animals
.
add
(
"horse"
);
animals
.
add
(
"dog"
);
int
i
=
0
;
for
(
String
animal
:
animals
)
{
if
(
password
.
charAt
(
0
)
!=
animal
.
charAt
(
0
))
{
if
(
password
.
charAt
(
i
)
!=
animal
.
charAt
(
0
))
{
System
.
err
.
println
(
"BOOM!"
);
System
.
exit
(
2
);
}
password
=
password
.
substring
(
1
)
;
i
++
;
}
System
.
err
.
println
(
"You passed phase 1 with the password \""
+
p
save
+
"\""
);
System
.
err
.
println
(
"You passed phase 1 with the password \""
+
p
assword
+
"\""
);
}
public
void
phase2
(
String
password
)
{
String
[]
passwordPieces
=
password
.
split
(
" "
);
Random
r
=
new
Random
(
1337
);
Set
<
Integer
>
numbers
=
new
HashSet
<>();
for
(
int
i
=
0
;
i
<
10000
;
i
++
)
{
while
(
numbers
.
size
()
<
10000
)
{
numbers
.
add
(
r
.
nextInt
());
}
String
[]
passwordPieces
=
password
.
split
(
" "
);
Iterator
<
Integer
>
numbersIterator
=
numbers
.
iterator
();
for
(
int
i
=
0
;
i
<
10000
;
i
++)
{
if
(
Integer
.
parseInt
(
passwordPieces
[
i
])
!=
(
int
)
numbersIterator
.
next
()
&&
i
==
500
)
{
System
.
err
.
println
(
"BOOM!"
);
System
.
exit
(
3
);
boolean
correct
=
false
;
int
i
=
0
;
for
(
int
number
:
numbers
)
{
if
(
i
==
5000
&&
Integer
.
parseInt
(
passwordPieces
[
i
])
==
number
)
{
correct
=
true
;
}
i
++;
}
if
(!
correct
)
{
System
.
err
.
println
(
"BOOM!"
);
System
.
exit
(
3
);
}
System
.
err
.
println
(
"You passed phase 2 with the password \""
+
password
+
"\""
);
}
...
...
This diff is collapsed.
Click to expand it.
tests/BombTests.java
View file @
0ca21ce3
...
...
@@ -14,24 +14,17 @@ public class BombTests {
@DisplayName
(
"Test BombMain"
)
@Test
public
void
testBombMain
()
{
PrintStream
systemErr
=
System
.
err
;
ByteArrayOutputStream
outputStream
=
new
ByteArrayOutputStream
();
System
.
setErr
(
new
PrintStream
(
outputStream
));
BombMain
.
main
(
null
);
System
.
setErr
(
new
PrintStream
(
new
FileOutputStream
(
FileDescriptor
.
err
)));
System
.
setErr
(
systemErr
);
String
output
=
outputStream
.
toString
();
System
.
out
.
println
(
output
);
String
[]
lines
=
output
.
replace
(
"\r\n"
,
"\n"
).
split
(
"\n"
);
for
(
int
i
=
0
;
i
<
3
;
i
++)
{
if
(
i
==
0
)
{
assertEquals
(
54955992
,
lines
[
i
].
split
(
"\""
)[
1
].
hashCode
());
}
else
if
(
i
==
1
)
{
assertEquals
(
103143
,
lines
[
i
].
split
(
"\""
)[
1
].
hashCode
());
}
else
{
assertEquals
(-
602269950
,
lines
[
i
].
split
(
"\""
)[
1
].
split
(
" "
)[
500
].
hashCode
());
}
}
String
[]
lines
=
output
.
split
(
"\r?\n"
);
assertEquals
(
54955992
,
lines
[
0
].
split
(
"\""
)[
1
].
hashCode
());
assertEquals
(
103143
,
lines
[
1
].
split
(
"\""
)[
1
].
hashCode
());
assertEquals
(-
1099484678
,
lines
[
2
].
split
(
"\""
)[
1
].
split
(
" "
)[
5000
].
hashCode
());
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help