...
 
Commits (12)
File deleted
# Created by https://www.gitignore.io/api/rust,visualstudiocode
# Created by https://www.gitignore.io/api/rust,macos,intellij,visualstudiocode
### 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
......@@ -21,4 +128,4 @@ Cargo.lock
!.vscode/extensions.json
# End of https://www.gitignore.io/api/rust,visualstudiocode
\ No newline at end of file
# End of https://www.gitignore.io/api/rust,macos,intellij,visualstudiocode
# Created by https://www.gitignore.io/api/rust,macos,intellij,visualstudiocode
### 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
### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
# End of https://www.gitignore.io/api/rust,macos,intellij,visualstudiocode
\ No newline at end of file
[[package]]
name = "day1"
version = "0.1.0"
# Created by https://www.gitignore.io/api/rust,macos,intellij,visualstudiocode
### 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
### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
# End of https://www.gitignore.io/api/rust,macos,intellij,visualstudiocode
\ No newline at end of file
[package]
name = "day2"
version = "0.1.0"
authors = ["Christopher Bohlen <christopher.bohlen@codecentric.de>"]
[dependencies]
737 1866 1565 1452 1908 1874 232 1928 201 241 922 281 1651 1740 1012 1001
339 581 41 127 331 133 51 131 129 95 499 527 518 435 508 494
1014 575 1166 259 152 631 1152 1010 182 943 163 158 1037 1108 1092 887
56 491 409 1263 1535 41 1431 1207 1393 700 1133 53 131 466 202 62
632 403 118 352 253 672 711 135 116 665 724 780 159 133 90 100
1580 85 1786 1613 1479 100 94 1856 546 76 1687 1769 1284 1422 1909 1548
479 356 122 372 786 1853 979 116 530 123 1751 887 109 1997 160 1960
446 771 72 728 109 369 300 746 86 910 566 792 616 84 338 57
6599 2182 200 2097 4146 7155 7018 1815 1173 4695 201 7808 242 3627 222 7266
1729 600 651 165 1780 2160 626 1215 149 179 1937 1423 156 129 634 458
1378 121 146 437 1925 2692 130 557 2374 2538 2920 2791 156 317 139 541
1631 176 1947 259 2014 153 268 752 2255 347 227 2270 2278 544 2379 349
184 314 178 242 145 410 257 342 183 106 302 320 288 151 449 127
175 5396 1852 4565 4775 665 4227 171 4887 181 2098 4408 2211 3884 2482 158
1717 3629 244 258 281 3635 235 4148 3723 4272 3589 4557 4334 4145 3117 4510
55 258 363 116 319 49 212 44 303 349 327 330 316 297 313 67
use std::env;
use std::fs::File;
use std::io::BufReader;
use std::io::prelude::*;
fn main() {
let args: Vec<String> = env::args().collect();
let input_file = &args[ 1 ];
println!( "input file: {}", input_file );
let lines = read_input_file( &input_file );
println!( "lines:" );
for line in lines.clone() {
println!( "{}" , line );
}
println!( "check sum 'by difference': {}", calculate_check_sum_by_difference( &lines ) );
println!( "check sum 'by even division': {}", calculate_check_sum_by_even_division( &lines ) );
}
fn read_input_file( input_file: &str ) -> Vec<String> {
let file = File::open( input_file )
.expect( "Input file not found" );
let br = BufReader::new( file );
br.lines()
.map( |l| l.expect( "Could not parse line" ) )
.collect()
}
fn calculate_check_sum_by_difference( lines: &Vec<String> ) -> u32 {
lines.iter().map( |l| l.split_whitespace() )
.map( |v| v.map( |s| s.parse::<u32>().unwrap() )
.fold( ( <u32>::max_value(), 0 ), |( smallest, largest ), current|
( if current < smallest {
current
} else {
smallest
},
if current > largest {
current
} else {
largest
} )
)
).fold ( 0, |sum, ( smallest, largest )| sum + largest - smallest )
}
fn calculate_check_sum_by_even_division( lines: &Vec<String> ) -> u32 {
let mut number_vectors = lines.iter().map( |l| l.split_whitespace() )
.map( |v| v.map( |s| s.parse::<u32>().unwrap() )
.collect::<Vec<u32>>()
).collect::<Vec<Vec<u32>>>();
number_vectors.iter_mut().for_each( |v| v.sort_by( |a, b| b.cmp( a ) ) );
number_vectors.iter().map::<Vec<f64>, _>( |v| {
let mut result = Vec::new();
for i in 0..v.len() {
for j in i + 1..v.len() {
result.push( v[i] as f64 / v[j] as f64 );
}
}
result
} ).flat_map( |v| v )
.filter( |r| r.fract() == 0.0 )
.map( |r| r as u32 )
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_read_input_file() {
let expected = vec![ "test1", "test2", "test3" ];
let input_file = "data/test_input.txt";
let actual = read_input_file( &input_file );
assert_eq!( expected, actual );
}
#[test]
fn test_calculate_check_sum_by_difference() {
let expected = 18;
let lines = vec![ "5 1 9 5".to_string(), "7 5 3".to_string(), "2 4 6 8".to_string() ];
let actual = calculate_check_sum_by_difference( &lines );
assert_eq!( expected, actual );
}
#[test]
fn test_calculate_check_sum_by_even_division() {
let expected = 9;
let lines = vec![ "5 9 2 8".to_string(), "9 4 7 3".to_string(), "3 8 6 5".to_string() ];
let actual = calculate_check_sum_by_even_division( &lines );
assert_eq!( expected, actual );
}
}