mirror of
https://gitlab.com/MisterBiggs/aoc2020.git
synced 2025-08-03 04:01:29 +00:00
day 2 in rust
This commit is contained in:
9
day2/rust/Cargo.toml
Normal file
9
day2/rust/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "rust"
|
||||
version = "0.1.0"
|
||||
authors = ["Anson <anson@ansonbiggs.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
35
day2/rust/src/main.rs
Normal file
35
day2/rust/src/main.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
let input = fs::read_to_string("..\\input.txt").expect("Could not read file");
|
||||
|
||||
let lines: Vec<&str> = input.lines().collect();
|
||||
|
||||
println!("Part1:");
|
||||
let mut valid = 0;
|
||||
for line in lines.iter() {
|
||||
let l: Vec<&str> = line.split_whitespace().collect();
|
||||
|
||||
let min: i32 = l[0].split("-").collect::<Vec<&str>>()[0]
|
||||
.parse::<i32>()
|
||||
.unwrap();
|
||||
let max: i32 = l[0].split("-").collect::<Vec<&str>>()[1]
|
||||
.parse::<i32>()
|
||||
.unwrap();
|
||||
|
||||
let key: char = l[1].chars().nth(0).unwrap();
|
||||
let pass: &str = l[2];
|
||||
|
||||
let mut count = 0;
|
||||
for c in pass.chars() {
|
||||
if c == key {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
if count <= max && count >= min {
|
||||
valid += 1;
|
||||
}
|
||||
}
|
||||
println!("Valid Passwords: {}\n", valid);
|
||||
println!("\nPart2:");
|
||||
}
|
Reference in New Issue
Block a user