2016/16
This commit is contained in:
4
2016/16/Cargo.lock
generated
Normal file
4
2016/16/Cargo.lock
generated
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
[root]
|
||||||
|
name = "16"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
6
2016/16/Cargo.toml
Normal file
6
2016/16/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "16"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
82
2016/16/src/main.rs
Normal file
82
2016/16/src/main.rs
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
pub fn generate_data(input: &str) -> String {
|
||||||
|
let mut data = input.to_owned();
|
||||||
|
|
||||||
|
data.push('0');
|
||||||
|
|
||||||
|
for c in input.chars().rev() {
|
||||||
|
data.push(if c == '0' { '1' } else { '0' });
|
||||||
|
}
|
||||||
|
|
||||||
|
data
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn calculate_checksum(input: &str) -> String {
|
||||||
|
let mut data = String::new();
|
||||||
|
|
||||||
|
for pair in input.as_bytes().chunks(2) {
|
||||||
|
data.push(if pair[0] == pair[1] { '1' } else { '0' })
|
||||||
|
}
|
||||||
|
|
||||||
|
data
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn fill_disk(state: &str, length: usize) -> (String, String) {
|
||||||
|
let mut state = state.to_owned();
|
||||||
|
|
||||||
|
while state.len() < length {
|
||||||
|
state = generate_data(&state);
|
||||||
|
}
|
||||||
|
|
||||||
|
state.truncate(length);
|
||||||
|
|
||||||
|
let mut checksum = calculate_checksum(&state);
|
||||||
|
|
||||||
|
while checksum.len() % 2 == 0 {
|
||||||
|
checksum = calculate_checksum(&checksum);
|
||||||
|
}
|
||||||
|
|
||||||
|
(state, checksum)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let data = fill_disk("01110110101001000", 272);
|
||||||
|
|
||||||
|
println!("checksum_1={}", data.1);
|
||||||
|
|
||||||
|
let data = fill_disk("01110110101001000", 35651584);
|
||||||
|
|
||||||
|
println!("checksum_2={}", data.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example_01() {
|
||||||
|
assert_eq!("100".to_owned(), generate_data("1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example_02() {
|
||||||
|
assert_eq!("001".to_owned(), generate_data("0"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example_03() {
|
||||||
|
assert_eq!("11111000000".to_owned(), generate_data("11111"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example_04() {
|
||||||
|
assert_eq!("1111000010100101011110000".to_owned(), generate_data("111100001010"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example_05() {
|
||||||
|
assert_eq!("110101".to_owned(), calculate_checksum("110010110100"));
|
||||||
|
assert_eq!("100".to_owned(), calculate_checksum("110101"));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user