From 55272aea40213dcfc61c7b1b20a10ad644c6851c Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sat, 3 Dec 2022 19:15:15 +0100 Subject: [PATCH] Add a parsing function to simplify the process --- src/day03.rs | 79 ++++++++++++++++++++++++++-------------------------- src/main.rs | 7 ++--- 2 files changed, 42 insertions(+), 44 deletions(-) diff --git a/src/day03.rs b/src/day03.rs index 5deba68..b38bc82 100644 --- a/src/day03.rs +++ b/src/day03.rs @@ -2,24 +2,34 @@ use std::io::BufRead; use itertools::Itertools; -pub fn priority_sum(reader: R) -> i32 -where - R: BufRead -{ +pub fn parse(s: &str) -> Vec> { + s.lines() + .map(|l| { + l.as_bytes().into_iter().map(letter_to_priority).collect_vec() + }).collect() +} + +fn letter_to_priority(v: &u8) -> u8 { + if *v >= 97 { // >= 'a' + v - 96 + } else { // < 'a' + v - 38 + } +} + +pub fn priority_sum(rucksacks: &[Vec]) -> i32 { let mut sum = 0; - let mut item_count = [0i32; 52]; - for l in reader.lines() { + let mut item_count = [0u8; 52]; + for r in rucksacks { item_count.fill(0); - let l = l.unwrap(); - let items = l.as_bytes(); - for i in 0..items.len() / 2 { - let v = letter_to_priority(items[i]); - item_count[v as usize - 1] += 1; + + for i in 0..r.len() / 2 { + item_count[r[i] as usize - 1] += 1; } - for i in items.len() / 2..items.len() { - let v = letter_to_priority(items[i]); - if item_count[v as usize - 1] > 0 { - sum += v as i32; + + for i in r.len() / 2..r.len() { + if item_count[r[i] as usize - 1] > 0 { + sum += r[i] as i32; break; } } @@ -27,35 +37,26 @@ where sum } -pub fn badge_sum(reader: R) -> i32 -where - R: BufRead -{ +pub fn badge_sum(rucksacks: &[Vec]) -> i32 { let mut sum = 0; let mut item_set = [0u8; 52]; - for group in reader.lines().chunks(3).into_iter() { + for group in rucksacks.chunks(3).into_iter() { item_set.fill(0); - for (i, rucksack) in group.enumerate() { - let items = rucksack.unwrap(); - for b in items.as_bytes().into_iter() { - let v = letter_to_priority(*b); - if i == 2 && item_set[v as usize - 1] == 3 { - sum += v as i32; - break; - } - item_set[v as usize - 1] |= 1 << i; + + for i in 0..2 { + for b in group[i].iter() { + item_set[*b as usize - 1] |= 1 << i; } } - } - sum -} -fn letter_to_priority(v: u8) -> u8 { - if v >= 97 { // >= 'a' - v - 96 - } else { // < 'a' - v - 38 + for b in group[2].iter() { + if item_set[*b as usize - 1] == 3 { + sum += *b as i32; + break; + } + } } + sum } #[cfg(test)] @@ -72,7 +73,7 @@ wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn ttgJtRGJQctTZtZT CrZsJsPPZsGzwwsLwLmpwMDw"; - assert_eq!(priority_sum(rucksacks.as_bytes()), 157); + assert_eq!(priority_sum(&parse(rucksacks)), 157); } #[test] @@ -85,6 +86,6 @@ wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn ttgJtRGJQctTZtZT CrZsJsPPZsGzwwsLwLmpwMDw"; - assert_eq!(badge_sum(rucksacks.as_bytes()), 70); + assert_eq!(badge_sum(&parse(rucksacks)), 70); } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 66d8ef0..e7b4bed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,11 +20,8 @@ fn day02() -> String { } fn day03() -> String { - let mut f = fs::File::open("data/day03.input").unwrap(); - let part1 = day03::priority_sum(BufReader::new(f.try_clone().unwrap())); - let _ = f.seek(SeekFrom::Start(0)); - let part2 = day03::badge_sum(BufReader::new(f)); - format!("part1: {}, part2: {}", part1, part2) + let rucksacks = day03::parse(&fs::read_to_string("data/day03.input").unwrap()); + format!("part1: {}, part2: {}", day03::priority_sum(&rucksacks), day03::badge_sum(&rucksacks)) } fn format_micros(t: u128) -> String { -- 2.45.2