From a5e78315cfd73019dccf60185a8427c38154325f Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Fri, 2 Dec 2022 23:19:18 -0600 Subject: [PATCH] Part 1 --- 2022/rust/src/day3.rs | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/2022/rust/src/day3.rs b/2022/rust/src/day3.rs index 084f9cc..02222e5 100644 --- a/2022/rust/src/day3.rs +++ b/2022/rust/src/day3.rs @@ -1,13 +1,39 @@ mod common; +use std::collections::HashSet; + fn main() { - let input = common::day_input(2); + let input = common::day_input(3); println!("Part 1: {}", part1(&input)); println!("Part 2: {}", part2(&input)); } fn part1(input: &str) -> i32 { - 0 + let mut r = 0_i32; + for l in input.lines() { + println!("{}", l); + let mut s: HashSet = HashSet::new(); + let bytes = l.as_bytes(); + let mut n = 0; + let bar = bytes.len() / 2; + println!("{}", bar); + for b in bytes { + print!("{}.", b); + if s.contains(b) && n >= bar { + if *b >= 97 && *b <= 122 { + r += (*b as i32) - 96; + } else { + r += (*b as i32) - 65 + 27; + } + println!(" == {}", r); + break; + } else if n < bar { + s.insert(*b); + } + n += 1 + } + } + return r; } fn part2(input: &str) -> i32 { @@ -18,11 +44,16 @@ fn part2(input: &str) -> i32 { mod tests { use super::*; - const TEST_INPUT: &str = ""; + const TEST_INPUT: &str = "vJrwpWtwJgWrhcsFMMfFFhFp +jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL +PmmdzqPrVvPwwTWBwg +wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn +ttgJtRGJQctTZtZT +CrZsJsPPZsGzwwsLwLmpwMDw"; #[test] fn test_part1() { - assert_eq!(part1(TEST_INPUT), 0) + assert_eq!(part1(TEST_INPUT), 157) } #[test]