Commit 8907871d authored by Christopher Bohlen's avatar Christopher Bohlen

initial commit

parents
File added
/target
**/*.rs.bk
[[package]]
name = "day1"
version = "0.1.0"
[package]
name = "day1"
version = "0.1.0"
authors = ["Christopher Bohlen <christopher.bohlen@codecentric.de>"]
[dependencies]
5672987533353956199629683941564528646262567117433461547747793928322958646779832484689174151918261551689221756165598898428736782194511627829355718493723961323272136452517987471351381881946883528248611611258656199812998632682668749683588515362946994415852337196718476219162124978836537348924591957188827929753417884942133844664636969742547717228255739959316351852731598292529837885992781815131876183578461135791315287135243541659853734343376618419952776165544829717676988897684141328138348382882699672957866146524759879236555935723655326743713542931693477824289283542468639522271643257212833248165391957686226311246517978319253977276663825479144321155712866946255992634876158822855382331452649953283788863248192338245943966269197421474555779135168637263279579842885347152287275679811576594376535226167894981226866222987522415785244875882556414956724976341627123557214837873872723618395529735349273241686548287549763993653379539445435319698825465289817663294436458194867278623978745981799283789237555242728291337538498616929817268211698649236646127899982839523784837752863458819965485149812959121884771849954723259365778151788719941888128618552455879369919511319735525621198185634342538848462461833332917986297445388515717463168515123732455576143447454835849565757773325367469763383757677938748319968971312267871619951657267913817242485559771582167295794259441256284168356292785568858527184122231262465193612127961685513913835274823892596923786613299747347259254823531262185328274367529265868856512185135329652635938373266759964119863494798222245536758792389789818646655287856173534479551364115976811459677123592747375296313667253413698823655218254168196162883437389718167743871216373164865426458794239496224858971694877159591215772938396827435289734165853975267521291574436567193473814247981877735223376964125359992555885137816647382139596646856417424617847981855532914872251686719394341764324395254556782277426326331441981737557262581762412544849689472281645835957667217384334435391572985228286537574388834835693416821419655967456137395465649249256572866516984318344482684936625486311718525523265165
\ No newline at end of file
test
\ No newline at end of file
use std::env;
use std::fs::File;
use std::io::prelude::*;
fn main() {
let args: Vec<String> = env::args().collect();
let input_file = &args[ 1 ];
println!( "input file: {}", input_file );
let input = read_input_file( &input_file );
println!( "input: {}", input );
println!( "sum: {}", calculate_sum( &input ) );
}
fn read_input_file( input_file: &str ) -> String {
let mut file = File::open( input_file )
.expect( "Input file not found" );
let mut input = String::new();
file.read_to_string( &mut input )
.expect( "Error reading input file" );
input
}
fn calculate_sum( input: &str ) -> u32 {
let input = append_first_char_to_end( &input );
sum_matching_digits( &input )
}
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 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
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_read_input_file() {
let expected = "test";
let input_file = "data/test_input.txt";
let actual = read_input_file( &input_file );
assert_eq!( expected, actual );
}
#[test]
fn test_append_first_char_to_end() {
let expected = "testt";
let input = "test";
let actual = append_first_char_to_end( &input );
assert_eq!( expected, actual );
}
#[test]
fn test_sum_matching_digits_match() {
let expected = 3;
let input = "1122";
let actual = sum_matching_digits( &input );
assert_eq!( expected, actual );
}
#[test]
fn test_sum_matching_digits_no_match() {
let expected = 0;
let input = "1234";
let actual = sum_matching_digits( &input );
assert_eq!( expected, actual );
}
#[test]
fn test_calculate_sum() {
let expected = 3;
let input = "1122";
let actual = calculate_sum( &input );
assert_eq!( expected, actual );
}
#[test]
fn test_calculate_sum_all_same_values() {
let expected = 4;
let input = "1111";
let actual = calculate_sum( &input );
assert_eq!( expected, actual );
}
#[test]
fn test_calculate_sum_all_different_values() {
let expected = 0;
let input = "1234";
let actual = calculate_sum( &input );
assert_eq!( expected, actual );
}
}
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