diff options
Diffstat (limited to '2022/03/a.rs')
| -rw-r--r-- | 2022/03/a.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/2022/03/a.rs b/2022/03/a.rs new file mode 100644 index 0000000..568546c --- /dev/null +++ b/2022/03/a.rs | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | mod common; | ||
| 2 | use common::*; | ||
| 3 | |||
| 4 | fn get_rep_val(rucksack: &[u8]) -> i64 { | ||
| 5 | let mut seen = [false; 256]; | ||
| 6 | let mid = rucksack.len()/2; | ||
| 7 | for i in 0..mid { | ||
| 8 | seen[rucksack[i] as usize] = true; | ||
| 9 | } | ||
| 10 | for i in mid..rucksack.len() { | ||
| 11 | if seen[rucksack[i] as usize] { | ||
| 12 | return value(rucksack[i] as char); | ||
| 13 | } | ||
| 14 | } | ||
| 15 | panic!("Could not find repeated char"); | ||
| 16 | } | ||
| 17 | |||
| 18 | fn main() { | ||
| 19 | let mut sum = 0; | ||
| 20 | let mut line = String::new(); | ||
| 21 | while std::io::stdin().read_line(&mut line).unwrap() > 0 { | ||
| 22 | sum += get_rep_val(line.as_bytes()); | ||
| 23 | line.clear(); | ||
| 24 | } | ||
| 25 | println!("{sum}"); | ||
| 26 | } | ||
