Skip to content

Commit 5f2686f

Browse files
committed
feat: completed day 6
1 parent 3f3ddd8 commit 5f2686f

File tree

7 files changed

+2366
-1
lines changed

7 files changed

+2366
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
- [Day 3](https://github.com/ankjevel/adventofcode/tree/2020/day_03) 🌟 🌟
99
- [Day 4](https://github.com/ankjevel/adventofcode/tree/2020/day_04) 🌟 🌟
1010
- [Day 5](https://github.com/ankjevel/adventofcode/tree/2020/day_05) 🌟 🌟
11-
- [Day 6](#)
11+
- [Day 6](https://github.com/ankjevel/adventofcode/tree/2020/day_06) 🌟 🌟
1212
- [Day 7](#)
1313
- [Day 8](#)
1414
- [Day 9](#)

day_06/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "day_06"
3+
version = "0.1.0"
4+
authors = ["Dennis Pettersson <mail@dennispettersson.se>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]

day_06/src/lib.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
pub mod part_01;
2+
pub mod part_02;
3+
4+
pub type Input = Vec<Vec<String>>;
5+
6+
pub fn parse_input(input: &str) -> Input {
7+
input
8+
.lines()
9+
.map(|string| string.trim())
10+
.into_iter()
11+
.fold(vec![vec![]], |mut list, string| {
12+
if string.is_empty() {
13+
list.push(vec![]);
14+
} else {
15+
let last = list.last_mut().unwrap();
16+
last.push(string.to_string());
17+
}
18+
list
19+
})
20+
.into_iter()
21+
.filter(|list| list.len() > 0)
22+
.collect()
23+
}
24+
25+
#[cfg(test)]
26+
mod tests {
27+
use super::*;
28+
29+
const EXAMPLE_DATA: &'static str = "
30+
abc
31+
32+
a
33+
b
34+
35+
36+
";
37+
38+
#[test]
39+
fn it_parses_example() {
40+
assert_eq!(
41+
parse_input(&EXAMPLE_DATA),
42+
vec![vec!["abc"], vec!["a", "b"]]
43+
);
44+
}
45+
}

day_06/src/main.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use std::io::Result;
2+
3+
use day_06::{parse_input, part_01::main as part_01, part_02::main as part_02};
4+
5+
fn main() -> Result<()> {
6+
let input = parse_input(include_str!("../../input/day_06"));
7+
8+
println!("part_01: {:?}", part_01(&input).unwrap());
9+
println!("part_02: {:?}", part_02(&input).unwrap());
10+
11+
Ok(())
12+
}

day_06/src/part_01.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use std::io::Result;
2+
3+
use crate::Input;
4+
5+
pub fn main(input: &Input) -> Result<usize> {
6+
Ok(input.iter().fold(0, |sum, group| {
7+
sum + ('a'..='z')
8+
.map(char::from)
9+
.filter(|c| {
10+
group
11+
.iter()
12+
.filter(|p| p.contains(&c.to_string()))
13+
.collect::<Vec<_>>()
14+
.len()
15+
> 0
16+
})
17+
.collect::<Vec<char>>()
18+
.len()
19+
}))
20+
}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
use crate::parse_input;
25+
26+
use super::*;
27+
28+
const EXAMPLE_DATA: &'static str = "
29+
abc
30+
31+
a
32+
b
33+
c
34+
35+
ab
36+
ac
37+
38+
a
39+
a
40+
a
41+
a
42+
43+
b
44+
";
45+
46+
#[test]
47+
fn it_gets_the_example_correct() {
48+
assert_eq!(main(&parse_input(&EXAMPLE_DATA)).unwrap(), 11);
49+
}
50+
}

day_06/src/part_02.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use std::io::Result;
2+
3+
use crate::Input;
4+
5+
pub fn main(input: &Input) -> Result<usize> {
6+
Ok(input.iter().fold(0, |sum, group| {
7+
sum + ('a'..='z')
8+
.map(char::from)
9+
.filter(|c| {
10+
group
11+
.iter()
12+
.filter(|p| p.contains(&c.to_string()))
13+
.collect::<Vec<_>>()
14+
.len()
15+
== group.len()
16+
})
17+
.collect::<Vec<char>>()
18+
.len()
19+
}))
20+
}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
use crate::parse_input;
25+
26+
use super::*;
27+
28+
const EXAMPLE_DATA: &'static str = "
29+
abc
30+
31+
a
32+
b
33+
c
34+
35+
ab
36+
ac
37+
38+
a
39+
a
40+
a
41+
a
42+
43+
b
44+
";
45+
46+
#[test]
47+
fn it_gets_the_example_correct() {
48+
assert_eq!(main(&parse_input(&EXAMPLE_DATA)).unwrap(), 6);
49+
}
50+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy