aboutsummaryrefslogtreecommitdiff
path: root/2020/day02/src
diff options
context:
space:
mode:
Diffstat (limited to '2020/day02/src')
-rw-r--r--2020/day02/src/main.rs31
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);
+}