initial commit
This commit is contained in:
commit
658f3bfca5
31 changed files with 7570 additions and 0 deletions
1
01-sonar-sweep/.gitignore
vendored
Normal file
1
01-sonar-sweep/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target
|
7
01-sonar-sweep/Cargo.lock
generated
Normal file
7
01-sonar-sweep/Cargo.lock
generated
Normal file
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "sonar-sweep"
|
||||
version = "0.1.0"
|
8
01-sonar-sweep/Cargo.toml
Normal file
8
01-sonar-sweep/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "sonar-sweep"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
19
01-sonar-sweep/LICENSE
Normal file
19
01-sonar-sweep/LICENSE
Normal file
|
@ -0,0 +1,19 @@
|
|||
The MIT License (MIT)
|
||||
Copyright (c) 2021 danielyrovas
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2000
01-sonar-sweep/depths.txt
Normal file
2000
01-sonar-sweep/depths.txt
Normal file
File diff suppressed because it is too large
Load diff
3
01-sonar-sweep/puzzle.md
Normal file
3
01-sonar-sweep/puzzle.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
### puzzle
|
||||
find rate of increase of depth
|
||||
count number of times a depth measurement increases from the previous measurement
|
52
01-sonar-sweep/src/main.rs
Normal file
52
01-sonar-sweep/src/main.rs
Normal file
|
@ -0,0 +1,52 @@
|
|||
#![allow(dead_code, unused_imports)]
|
||||
|
||||
use std::{io::{stdin, Read}, str::FromStr};
|
||||
|
||||
fn main() {
|
||||
let depths = read_depths();
|
||||
let mut n = 0;
|
||||
let mut prev = 0;
|
||||
let mut f: Option<u32> = None;
|
||||
let mut p: Option<u32> = None;
|
||||
let mut window;
|
||||
for &depth in depths.iter() {
|
||||
if f == None { f = Some(depth); continue; }
|
||||
if p == None { p = f; continue; }
|
||||
window = f.unwrap() + p.unwrap() + &depth;
|
||||
// update variables that store prior 2 iterations state
|
||||
f = p;
|
||||
p = Some(depth);
|
||||
if prev == 0 {
|
||||
println!("{}, (no prior data)", window)
|
||||
} else if window > prev {
|
||||
n += 1;
|
||||
println!("{}, (increased)", window);
|
||||
} else if prev > window {
|
||||
println!("{}, (decreased)", window)
|
||||
} else {
|
||||
println!("{}, (no change)", window)
|
||||
}
|
||||
prev = window;
|
||||
}
|
||||
println!("number of increases: {}", n);
|
||||
}
|
||||
|
||||
fn read_depths() -> Vec<u32> {
|
||||
let mut s = String::new();
|
||||
stdin().read_to_string(&mut s).unwrap();
|
||||
let result = s
|
||||
.lines()
|
||||
.map(u32::from_str)
|
||||
.collect::<Result<Vec<_>, _>>();
|
||||
result.unwrap()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn print_input() {
|
||||
read_depths();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue