diff options
Diffstat (limited to '2022/day3/part1')
| -rw-r--r-- | 2022/day3/part1/Cargo.toml | 9 | ||||
| -rw-r--r-- | 2022/day3/part1/src/main.rs | 24 |
2 files changed, 33 insertions, 0 deletions
diff --git a/2022/day3/part1/Cargo.toml b/2022/day3/part1/Cargo.toml new file mode 100644 index 0000000..f14fe90 --- /dev/null +++ b/2022/day3/part1/Cargo.toml | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | [package] | ||
| 2 | name = "part1" | ||
| 3 | version = "0.1.0" | ||
| 4 | edition = "2021" | ||
| 5 | |||
| 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
| 7 | |||
| 8 | [dependencies] | ||
| 9 | itertools = "0.10.5" | ||
diff --git a/2022/day3/part1/src/main.rs b/2022/day3/part1/src/main.rs new file mode 100644 index 0000000..2de2ce8 --- /dev/null +++ b/2022/day3/part1/src/main.rs | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | use std::collections::HashSet; | ||
| 2 | |||
| 3 | fn main() { | ||
| 4 | let rucksacks: Vec<(&str, &str)> = include_str!("../../input") | ||
| 5 | .lines() | ||
| 6 | .map(|line| line.split_at(line.len() / 2)) | ||
| 7 | .collect(); | ||
| 8 | |||
| 9 | let mut sum = 0; | ||
| 10 | for (f, s) in rucksacks { | ||
| 11 | let first: HashSet<char> = HashSet::from_iter(f.chars()); | ||
| 12 | let second: HashSet<char> = HashSet::from_iter(s.chars()); | ||
| 13 | |||
| 14 | for x in first.intersection(&second) { | ||
| 15 | if x.is_lowercase() { | ||
| 16 | sum += *x as i16 - 96; | ||
| 17 | } else { | ||
| 18 | sum += *x as i16 - 38; | ||
| 19 | } | ||
| 20 | } | ||
| 21 | } | ||
| 22 | |||
| 23 | println!("{}", sum); | ||
| 24 | } | ||
