diff options
author | Prefetch | 2023-02-25 11:41:27 +0100 |
---|---|---|
committer | Prefetch | 2023-02-25 11:41:27 +0100 |
commit | 3b877bf4cc667eb8bcc787d145203600a4dba2d2 (patch) | |
tree | c1d247def29fcb58ae28e4ae4e4d73d1b4e1b27f /d06/src/main.rs |
Initial commit
Diffstat (limited to 'd06/src/main.rs')
-rw-r--r-- | d06/src/main.rs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/d06/src/main.rs b/d06/src/main.rs new file mode 100644 index 0000000..e482735 --- /dev/null +++ b/d06/src/main.rs @@ -0,0 +1,63 @@ +use std::collections::HashMap; +use std::fs; + +fn solve_puzzle(lines: &Vec<&str>, use_max: bool) -> String { + let mut freqs = Vec::new(); + for _i in 0..lines[0].len() { + let hm: HashMap<char, usize> = HashMap::new(); + freqs.push(hm); + } + + // Count char occurrences at each index in line + for line in lines { + for (i, c) in line.chars().enumerate() { + *freqs[i].entry(c).or_insert(0) += 1; + } + } + + let mut result = String::new(); + for hm in freqs { + let c = if use_max { + // Part 1: take most common character + hm.into_iter().max_by_key(|x| x.1).unwrap().0 + } else { + // Part 2: take least common character + hm.into_iter().min_by_key(|x| x.1).unwrap().0 + }; + result.push(c); + } + + result +} + +fn main() { + // Read messages from input text file + let input = fs::read_to_string("input.txt").unwrap(); + let lines = input.lines().collect(); + + // Part 1 gives "mlncjgdg" for me + println!("Part 1 solution: {}", solve_puzzle(&lines, true)); + + // Part 2 gives "bipjaytb" for me + println!("Part 2 solution: {}", solve_puzzle(&lines, false)); +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE1_LINES: [&str; 16] = [ + "eedadn", "drvtee", "eandsr", "raavrd", "atevrs", "tsrnev", "sdttsa", "rasrtv", + "nssdts", "ntnada", "svetve", "tesnvt", "vntsnd", "vrdear", "dvrsen", "enarar", + ]; + + #[test] + fn part1_example1() { + assert_eq!(solve_puzzle(&Vec::from(EXAMPLE1_LINES), true), "easter"); + } + + #[test] + fn part2_example1() { + assert_eq!(solve_puzzle(&Vec::from(EXAMPLE1_LINES), false), "advent"); + } +} |