Skip to content

Commit 378ea87

Browse files
committed
feat: part 1 of day 2
1 parent 3a2c7c9 commit 378ea87

File tree

7 files changed

+1112
-1
lines changed

7 files changed

+1112
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
## Progress:
55

66
- [Day 1](https://github.com/ankjevel/adventofcode/tree/2020/day_01) 🌟 🌟
7-
- [Day 2](#)
7+
- [Day 2](https://github.com/ankjevel/adventofcode/tree/2020/day_02) 🌟
88
- [Day 3](#)
99
- [Day 4](#)
1010
- [Day 5](#)

day_02/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_02"
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_02/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 Line = (u16, u16, char, String);
5+
6+
pub fn parse_input(input: &str) -> Vec<Line> {
7+
input
8+
.lines()
9+
.map(str::trim)
10+
.filter(|string| !string.is_empty())
11+
.map(|part| {
12+
let mut iter = part.split(" ").map(str::trim);
13+
let mut range = iter.next().unwrap().split("-");
14+
15+
let min = range.next().unwrap().parse::<u16>().unwrap();
16+
let max = range.next().unwrap().parse::<u16>().unwrap();
17+
18+
let character = iter.next().unwrap().to_string().chars().next().unwrap();
19+
let password = iter.next().unwrap().trim().to_owned();
20+
21+
(min, max, character, password)
22+
})
23+
.collect()
24+
}
25+
26+
#[cfg(test)]
27+
mod tests {
28+
use super::*;
29+
30+
const EXAMPLE_DATA: &'static str = "
31+
2-6 c: fcpwjqhcgtffzlbj
32+
6-9 x: xxxtwlxxx
33+
";
34+
35+
#[test]
36+
fn it_can_parse_input_as_expected() {
37+
assert_eq!(
38+
parse_input(&EXAMPLE_DATA),
39+
vec![
40+
(2, 6, 'c', "fcpwjqhcgtffzlbj".to_string()),
41+
(6, 9, 'x', "xxxtwlxxx".to_string())
42+
]
43+
);
44+
}
45+
}

day_02/src/main.rs

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

day_02/src/part_01.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use crate::Line;
2+
use std::io::Result;
3+
4+
pub fn main(input: &Vec<Line>) -> Result<u16> {
5+
let mut count = 0;
6+
for (min, max, char_to_match, password_string) in input {
7+
let matches = password_string
8+
.chars()
9+
.into_iter()
10+
.filter(|c| c == char_to_match)
11+
.count() as u16;
12+
13+
if &matches >= min && &matches <= max {
14+
count += 1;
15+
}
16+
}
17+
Ok(count)
18+
}
19+
20+
#[cfg(test)]
21+
mod tests {
22+
use super::*;
23+
use crate::parse_input;
24+
25+
const EXAMPLE_DATA: &'static str = "
26+
1-3 a: abcde
27+
1-3 b: cdefg
28+
2-9 c: ccccccccc
29+
";
30+
31+
#[test]
32+
fn it_gets_the_example_correct() {
33+
assert_eq!(main(&parse_input(EXAMPLE_DATA)).unwrap(), 2)
34+
}
35+
}

day_02/src/part_02.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use std::io::Result;
2+
3+
pub fn main() -> Result<()> {
4+
Ok(())
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {}

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