Commit 914efc53 authored by Christopher Bohlen's avatar Christopher Bohlen

solved part two

parent 0e188e6f
No preview for this file type
/target
# Created by https://www.gitignore.io/api/rust,visualstudiocode
### Rust ###
# Generated by Cargo
# will have compiled files and executables
/target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
# End of https://www.gitignore.io/api/rust,visualstudiocode
\ No newline at end of file
No preview for this file type
......@@ -6,10 +6,14 @@ use std::io::prelude::*;
fn main() {
let args: Vec<String> = env::args().collect();
let input_file = &args[ 1 ];
let mode = &args[ 2 ];
println!( "input file: {}", input_file );
println!( "mode: {}", mode );
let input = read_input_file( &input_file );
println!( "input: {}", input );
println!( "sum: {}", calculate_sum( &input ) );
let distance = determine_distance( &input, &mode );
println!( "distance: {}", distance );
println!( "sum: {}", calculate_sum( &input, distance ) );
}
fn read_input_file( input_file: &str ) -> String {
......@@ -21,28 +25,41 @@ fn read_input_file( input_file: &str ) -> String {
input
}
fn calculate_sum( input: &str ) -> u32 {
let input = append_first_char_to_end( &input );
sum_matching_digits( &input )
fn determine_distance( input: &str, mode: &str ) -> u32 {
if mode == "half" {
let char_count = input.chars().count() as u32;
char_count / 2
} else {
1
}
}
fn append_first_char_to_end( input: &str ) -> String {
let mut result = input.to_string();
let first_char = result.chars().next().unwrap();
result.push( first_char );
result
fn calculate_sum( input: &str, distance: u32 ) -> u32 {
let digit_vector = input.chars().map( |c| c.to_digit( 10 ).unwrap() ).collect::<Vec<_>>();
sum_matching_digits( &digit_vector, distance )
}
fn sum_matching_digits( input: &str ) -> u32 {
input.to_string().chars()
.map ( |c| c.to_digit( 10 ).unwrap() )
.fold( ( 0, 0 ), |( sum, previous ), current|
( if current == previous {
sum + current
} else {
sum
}, current )
).0
fn sum_matching_digits( digit_vector: &Vec<u32>, distance: u32 ) -> u32 {
let length = digit_vector.len() as u32;
let mut sum = 0;
for current_digit_index in 0..length {
let current_digit = digit_vector[ (current_digit_index as usize) ];
let partner_digit_index = determine_partner_digit_index( current_digit_index, distance, length - 1 );
let partner_digit = digit_vector[ (partner_digit_index as usize) ];
if current_digit == partner_digit {
sum += current_digit;
}
}
sum
}
fn determine_partner_digit_index( current_digit_index: u32, distance: u32, last_digit_index: u32 ) -> u32 {
let partner_digit_index = current_digit_index + distance;
if partner_digit_index > last_digit_index {
partner_digit_index - last_digit_index - 1
} else {
partner_digit_index
}
}
......@@ -59,50 +76,103 @@ mod tests {
}
#[test]
fn test_append_first_char_to_end() {
let expected = "testt";
fn test_determine_distance() {
let expected = 1;
let input = "test";
let actual = determine_distance( &input, "next" );
assert_eq!( expected, actual );
}
#[test]
fn test_determine_distance_half() {
let expected = 2;
let input = "test";
let actual = append_first_char_to_end( &input );
let actual = determine_distance( &input, "half" );
assert_eq!( expected, actual );
}
#[test]
fn test_sum_matching_digits_match() {
fn determine_partner_digit_index_no_wrap() {
let expected = 3;
let input = "1122";
let actual = sum_matching_digits( &input );
let actual = determine_partner_digit_index( 2, 1, 3 );
assert_eq!( expected, actual );
}
#[test]
fn test_sum_matching_digits_no_match() {
fn determine_partner_digit_index_wrap_to_zero() {
let expected = 0;
let input = "1234";
let actual = sum_matching_digits( &input );
let actual = determine_partner_digit_index( 2, 2, 3 );
assert_eq!( expected, actual );
}
#[test]
fn determine_partner_digit_index_wrap_to_one() {
let expected = 1;
let actual = determine_partner_digit_index( 2, 3, 3 );
assert_eq!( expected, actual );
}
#[test]
fn test_calculate_sum() {
fn test_calculate_sum_match_next_1() {
let expected = 3;
let input = "1122";
let actual = calculate_sum( &input );
let actual = calculate_sum( &input, 1 );
assert_eq!( expected, actual );
}
#[test]
fn test_calculate_sum_all_same_values() {
fn test_calculate_sum_match_next_2() {
let expected = 4;
let input = "1111";
let actual = calculate_sum( &input );
let actual = calculate_sum( &input, 1 );
assert_eq!( expected, actual );
}
#[test]
fn test_calculate_sum_all_different_values() {
fn test_calculate_sum_no_match_next() {
let expected = 0;
let input = "1234";
let actual = calculate_sum( &input );
let actual = calculate_sum( &input, 1 );
assert_eq!( expected, actual );
}
#[test]
fn test_calculate_sum_match_half_1() {
let expected = 6;
let input = "1212";
let actual = calculate_sum( &input, 2 );
assert_eq!( expected, actual );
}
#[test]
fn test_calculate_sum_match_half_2() {
let expected = 4;
let input = "123425";
let actual = calculate_sum( &input, 3 );
assert_eq!( expected, actual );
}
#[test]
fn test_calculate_sum_match_half_3() {
let expected = 12;
let input = "123123";
let actual = calculate_sum( &input, 3 );
assert_eq!( expected, actual );
}
#[test]
fn test_calculate_sum_match_half_4() {
let expected = 4;
let input = "12131415";
let actual = calculate_sum( &input, 4 );
assert_eq!( expected, actual );
}
#[test]
fn test_calculate_sum_no_match_half() {
let expected = 0;
let input = "1221";
let actual = calculate_sum( &input, 2 );
assert_eq!( expected, actual );
}
......
{"rustc_fingerprint":2138305567319606772,"outputs":{"1617349019360157463":["___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/chbohlen/.rustup/toolchains/stable-x86_64-apple-darwin\ndebug_assertions\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_feature=\"ssse3\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\nunix\n",""],"3144802570395919623":["___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/chbohlen/.rustup/toolchains/stable-x86_64-apple-darwin\ndebug_assertions\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_feature=\"ssse3\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\nunix\n",""],"1164083562126845933":["rustc 1.29.0 (aa3ca1994 2018-09-11)\nbinary: rustc\ncommit-hash: aa3ca1994904f2e056679fce1f185db8c7ed2703\ncommit-date: 2018-09-11\nhost: x86_64-apple-darwin\nrelease: 1.29.0\nLLVM version: 7.0\n",""]}}
\ No newline at end of file
{"rustc":7055533318316241747,"features":"[]","target":5197351762734362779,"profile":9515766214563667082,"path":1036222786711178230,"deps":[],"local":[{"MtimeBased":[[1537090586,728782013],".fingerprint/day1-52c9347f5124fc13/dep-bin-day1-52c9347f5124fc13"]}],"rustflags":[],"edition":"Edition2015"}
\ No newline at end of file
{"rustc":7055533318316241747,"features":"[]","target":5197351762734362779,"profile":4409417805420424436,"path":1036222786711178230,"deps":[],"local":[{"MtimeBased":[[1537090775,829172137],".fingerprint/day1-df895c25585996ee/dep-test-bin-day1-df895c25585996ee"]}],"rustflags":[],"edition":"Edition2015"}
\ No newline at end of file
/Users/chbohlen/Documents/playground/katas/day1/target/debug/day1-df895c25585996ee.dSYM: /Users/chbohlen/Documents/playground/katas/day1/src/main.rs
deps/day1-df895c25585996ee.dSYM
\ No newline at end of file
/Users/chbohlen/Documents/playground/katas/day1/target/debug/day1.dSYM: /Users/chbohlen/Documents/playground/katas/day1/src/main.rs
deps/day1-52c9347f5124fc13.dSYM
\ No newline at end of file
/Users/chbohlen/Documents/playground/katas/day1/target/debug/deps/day1-52c9347f5124fc13: src/main.rs
/Users/chbohlen/Documents/playground/katas/day1/target/debug/deps/day1-52c9347f5124fc13.d: src/main.rs
src/main.rs:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.day1-52c9347f5124fc13</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
/Users/chbohlen/Documents/playground/katas/day1/target/debug/deps/day1-df895c25585996ee: src/main.rs
/Users/chbohlen/Documents/playground/katas/day1/target/debug/deps/day1-df895c25585996ee.d: src/main.rs
src/main.rs:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.day1-df895c25585996ee</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment