mirror of
https://gitlab.com/MisterBiggs/aoc2020.git
synced 2025-08-03 20:21:29 +00:00
day 2 in rust
This commit is contained in:
15
.gitignore
vendored
15
.gitignore
vendored
@@ -1,2 +1,17 @@
|
|||||||
main.pdb
|
main.pdb
|
||||||
main.exe
|
main.exe
|
||||||
|
|
||||||
|
|
||||||
|
# Generated by Cargo
|
||||||
|
# will have compiled files and executables
|
||||||
|
debug/
|
||||||
|
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
|
||||||
|
|
||||||
|
.vscode
|
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