Commit 3c5aa58d authored by Christopher Bohlen's avatar Christopher Bohlen

solved day 2 part 1

parent 8907871d
No preview for this file type
/target
# Created by https://www.gitignore.io/api/rust,macos,intellij
### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
### Intellij Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr
# Sonarlint plugin
.idea/sonarlint
### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### 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
# End of https://www.gitignore.io/api/rust,macos,intellij
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CargoProjects">
<cargoProject FILE="$PROJECT_DIR$/day1/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/day2/Cargo.toml" />
</component>
<component name="ProjectRootManager">
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="RustProjectSettings">
<option name="toolchainHomeDirectory" value="$USER_HOME$/.cargo/bin" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/adventofcode.iml" filepath="$PROJECT_DIR$/adventofcode.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
[[package]]
name = "day1"
version = "0.1.0"
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/day1/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/day1/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/day1/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/day1/benches" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/day2/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/day2/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/day2/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/day2/benches" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/day1/target" />
<excludeFolder url="file://$MODULE_DIR$/day2/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
File added
# Created by https://www.gitignore.io/api/rust,macos,intellij
### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
### Intellij Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr
# Sonarlint plugin
.idea/sonarlint
### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### 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
# End of https://www.gitignore.io/api/rust,macos,intellij
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CargoProjects">
<cargoProject FILE="$PROJECT_DIR$/Cargo.toml" />
</component>
<component name="ProjectRootManager">
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="RustProjectSettings">
<option name="toolchainHomeDirectory" value="$USER_HOME$/.cargo/bin" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/day1.iml" filepath="$PROJECT_DIR$/day1.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>
\ 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 ];
let mode = &args[ 2 ];
println!( "input file: {}", input_file );
println!( "mode: {}", mode );
let input = read_input_file( &input_file );
println!( "input: {}", input );
let distance = determine_distance( &input, &mode );
println!( "distance: {}", distance );
println!( "sum: {}", calculate_sum( &input, distance ) );
}
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 determine_distance( input: &str, mode: &str ) -> u32 {
if mode == "half" {
let char_count = input.chars().count() as u32;
char_count / 2
} else {
1
}
}
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( 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
}
}
#[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_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 = determine_distance( &input, "half" );
assert_eq!( expected, actual );
}
#[test]
fn determine_partner_digit_index_no_wrap() {
let expected = 3;
let actual = determine_partner_digit_index( 2, 1, 3 );
assert_eq!( expected, actual );
}
#[test]
fn determine_partner_digit_index_wrap_to_zero() {
let expected = 0;
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_match_next_1() {
let expected = 3;
let input = "1122";
let actual = calculate_sum( &input, 1 );
assert_eq!( expected, actual );
}
#[test]
fn test_calculate_sum_match_next_2() {
let expected = 4;
let input = "1111";
let actual = calculate_sum( &input, 1 );
assert_eq!( expected, actual );
}
#[test]
fn test_calculate_sum_no_match_next() {
let expected = 0;
let input = "1234";
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 );
}
}
File added
# Created by https://www.gitignore.io/api/rust,macos,intellij
### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
### Intellij Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr
# Sonarlint plugin
.idea/sonarlint
### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### Rust ###
# Generated by Cargo