diff options
author | jacopograndi <jacopo.grandi@outlook.it> | 2022-08-01 22:51:56 +0200 |
---|---|---|
committer | jacopograndi <jacopo.grandi@outlook.it> | 2022-08-01 22:51:56 +0200 |
commit | fd7a3e47a5411ac81f7e9cc2e8b18e66cf2dc224 (patch) | |
tree | 08adc155ce64710b81d15a673cf7ac900f5e0ea7 /2020/day02/src | |
parent | c9cff7bc7723cb06807dee5c8e803fa8a0bba76f (diff) |
day02
Diffstat (limited to '2020/day02/src')
-rw-r--r-- | 2020/day02/src/main.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/2020/day02/src/main.rs b/2020/day02/src/main.rs new file mode 100644 index 0000000..574e1d7 --- /dev/null +++ b/2020/day02/src/main.rs @@ -0,0 +1,31 @@ +fn main() { + let mut sled_valids = 0; + let mut toboggan_valids = 0; + for line in include_str!("../input0.txt").trim().split("\n") { + let (rule, pswd) = line.split_once(": ").unwrap(); + let (range, letter) = rule.split_once(" ").unwrap(); + + let letter = letter.chars().nth(0).unwrap(); + + let (lo, hi) = range.split_once("-").unwrap(); + let lo: i32 = lo.parse().unwrap(); + let hi: i32 = hi.parse().unwrap(); + + let count: i32 = pswd.chars().map(|c| (c == letter) as i32).sum(); + if lo <= count && count <= hi { + sled_valids += 1; + } + + let tobo: i32 = pswd + .chars() + .enumerate() + .map(|(i, c)| (i as i32 + 1, c)) + .map(|(i, c)| (c == letter && (hi == i || lo == i)) as i32) + .sum(); + if tobo == 1 { + toboggan_valids += 1; + } + } + println!("sled valid passwords: {}", sled_valids); + println!("toboggan valid passwords: {}", toboggan_valids); +} |