Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
adventofcode-2017
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Jira
Jira
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kai Brandes
adventofcode-2017
Commits
b3a70ef8
Commit
b3a70ef8
authored
Oct 26, 2018
by
Kai Brandes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add solution for 6b
parent
0062edd2
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
14 additions
and
6 deletions
+14
-6
src/day6/b.txt
src/day6/b.txt
+1
-0
src/day6/mod.rs
src/day6/mod.rs
+12
-5
tests/day6_test.rs
tests/day6_test.rs
+1
-1
No files found.
src/day6/b.txt
View file @
b3a70ef8
4 1 15 12 0 9 9 5 5 8 7 3 14 5 12 3
\ No newline at end of file
src/day6/mod.rs
View file @
b3a70ef8
...
...
@@ -24,11 +24,11 @@ fn as_string(v: &Vec<u32>) -> String {
fn
detect_loop_for
(
start
:
&
Vec
<
u32
>
)
->
(
u32
,
u32
)
{
let
mut
current_state
=
start
.clone
();
let
mut
all_seen_states
=
Hash
Set
::
new
();
let
mut
all_seen_states
=
Hash
Map
::
new
();
let
mut
iteration
=
0
;
let
mut
cs
:
String
=
as_string
(
&
current_state
);
while
!
all_seen_states
.contains
(
&
cs
)
{
all_seen_states
.insert
(
cs
.clone
());
while
all_seen_states
.get
(
&
cs
)
.is_none
(
)
{
all_seen_states
.insert
(
cs
.clone
()
,
iteration
);
let
(
i
,
val
)
=
calc_next
(
current_state
.clone
());
current_state
[
i
]
=
0
;
...
...
@@ -40,7 +40,8 @@ fn detect_loop_for(start: &Vec<u32>) -> (u32, u32) {
iteration
+=
1
;
cs
=
as_string
(
&
current_state
);
}
return
(
iteration
,
0
);
let
cycle_size
=
iteration
-
*
all_seen_states
.get
(
&
cs
)
.unwrap
();
return
(
iteration
,
cycle_size
);
}
fn
as_numbers
(
input
:
&
String
)
->
Vec
<
u32
>
{
...
...
@@ -57,7 +58,8 @@ pub fn calc_day_6_a(input: String) -> u32 {
#[wasm_bindgen]
pub
fn
calc_day_6_b
(
input
:
String
)
->
u32
{
0
let
input
=
as_numbers
(
&
input
);
return
detect_loop_for
(
&
input
)
.1
;
}
#[cfg(test)]
...
...
@@ -81,4 +83,9 @@ mod tests {
fn
it_should_calc_a
()
{
assert_eq!
(
5
,
calc_day_6_a
(
String
::
from
(
"0 2 7 0"
)))
}
#[test]
fn
it_should_calc_b
()
{
assert_eq!
(
4
,
calc_day_6_b
(
String
::
from
(
"0 2 7 0"
)))
}
}
\ No newline at end of file
tests/day6_test.rs
View file @
b3a70ef8
...
...
@@ -8,6 +8,6 @@ fn it_shoul_assert_result_of_day6_a() {
#[test]
fn
it_shoul_assert_result_of_day6_b
()
{
assert_eq!
(
0
,
adventofcode_2017
::
results
::
day_6_b
());
assert_eq!
(
2392
,
adventofcode_2017
::
results
::
day_6_b
());
}
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