diff options
author | Jesse Luehrs <doy@tozt.net> | 2020-12-08 02:20:12 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2020-12-08 02:20:12 -0500 |
commit | 340c586e323d3c8838c0e5fd53870a4d07350c75 (patch) | |
tree | a29c27200d8f07f37f7599b5f6dda478e5ae55b4 /src/2020 | |
parent | 935aa4ba68c865adbe36a99ae02bb6f40b73edc6 (diff) | |
download | advent-of-code-340c586e323d3c8838c0e5fd53870a4d07350c75.tar.gz advent-of-code-340c586e323d3c8838c0e5fd53870a4d07350c75.zip |
day 6
Diffstat (limited to 'src/2020')
-rw-r--r-- | src/2020/6/mod.rs | 43 | ||||
-rw-r--r-- | src/2020/mod.rs | 4 |
2 files changed, 47 insertions, 0 deletions
diff --git a/src/2020/6/mod.rs b/src/2020/6/mod.rs new file mode 100644 index 0000000..94dd4da --- /dev/null +++ b/src/2020/6/mod.rs @@ -0,0 +1,43 @@ +pub fn part1() -> anyhow::Result<i64> { + let input = crate::util::read_file_str("data/6.txt")?; + let mut yes = std::collections::HashSet::new(); + let mut total = 0; + for line in input.lines() { + if line == "" { + total += yes.len() as i64; + yes = std::collections::HashSet::new(); + } else { + for c in line.chars() { + yes.insert(c); + } + } + } + total += yes.len() as i64; + Ok(total) +} + +pub fn part2() -> anyhow::Result<i64> { + let input = crate::util::read_file_str("data/6.txt")?; + let mut yes = std::collections::HashSet::new(); + for c in 'a'..='z' { + yes.insert(c); + } + let mut total = 0; + for line in input.lines() { + if line == "" { + total += yes.len() as i64; + yes = std::collections::HashSet::new(); + for c in 'a'..='z' { + yes.insert(c); + } + } else { + for c in 'a'..='z' { + if !line.contains(c) { + yes.remove(&c); + } + } + } + } + total += yes.len() as i64; + Ok(total) +} diff --git a/src/2020/mod.rs b/src/2020/mod.rs index 8919f5f..47f5150 100644 --- a/src/2020/mod.rs +++ b/src/2020/mod.rs @@ -8,6 +8,8 @@ mod day3; mod day4; #[path = "5/mod.rs"] mod day5; +#[path = "6/mod.rs"] +mod day6; pub fn run(day: u8, puzzle: u8) -> anyhow::Result<i64> { match (day, puzzle) { @@ -21,6 +23,8 @@ pub fn run(day: u8, puzzle: u8) -> anyhow::Result<i64> { (4, 2) => day4::part2(), (5, 1) => day5::part1(), (5, 2) => day5::part2(), + (6, 1) => day6::part1(), + (6, 2) => day6::part2(), _ => Err(anyhow::anyhow!("unknown puzzle {}-{}", day, puzzle)), } } |