Skip to content

Commit bada40d

Browse files
committed
feat: completed part 1
1 parent 232a698 commit bada40d

File tree

6 files changed

+330
-1
lines changed

6 files changed

+330
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- [Day 1](https://github.com/ankjevel/adventofcode/tree/2023/day_01) ⭐️ ⭐️
66
- [Day 2](https://github.com/ankjevel/adventofcode/tree/2023/day_02) ⭐️ ⭐️
77
- [Day 3](https://github.com/ankjevel/adventofcode/tree/2023/day_03) ⭐️ ⭐️
8-
- [Day 4](#)
8+
- [Day 4](https://github.com/ankjevel/adventofcode/tree/2023/day_04) ⭐️
99
- [Day 5](#)
1010
- [Day 6](#)
1111
- [Day 7](#)

day_04/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "day_04"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
doctest = false
8+
9+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
10+
11+
[dependencies]

day_04/src/lib.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
pub mod part_01;
2+
3+
pub type Input = Vec<(Vec<u16>, Vec<u16>)>;
4+
5+
fn numbers(input: &str) -> Vec<u16> {
6+
input
7+
.split(' ')
8+
.filter(|string| !string.trim().is_empty())
9+
.map(|string| string.trim().parse::<_>().unwrap())
10+
.collect()
11+
}
12+
13+
pub fn parse_input(input: &str) -> Input {
14+
input
15+
.trim_start()
16+
.trim_end()
17+
.lines()
18+
.map(str::trim)
19+
.map(|string| {
20+
let card = string.split(": ").collect::<Vec<_>>()[1];
21+
let split = card.split(" | ").collect::<Vec<_>>();
22+
(numbers(split[0]), numbers(split[1]))
23+
})
24+
.collect()
25+
}
26+
27+
#[cfg(test)]
28+
mod tests {
29+
use super::*;
30+
31+
const EXAMPLE_DATA: &'static str = "
32+
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
33+
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
34+
";
35+
36+
#[test]
37+
fn it_parses_example() {
38+
assert_eq!(
39+
parse_input(&EXAMPLE_DATA),
40+
vec![
41+
(
42+
vec![41, 48, 83, 86, 17], //
43+
vec![83, 86, 6, 31, 17, 9, 48, 53]
44+
),
45+
(
46+
vec![13, 32, 20, 16, 61], //
47+
vec![61, 30, 68, 82, 17, 32, 24, 19]
48+
)
49+
]
50+
);
51+
}
52+
}

day_04/src/main.rs

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

day_04/src/part_01.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use std::io::Result;
2+
3+
use crate::Input;
4+
5+
pub fn main(input: &Input) -> Result<u32> {
6+
Ok(input
7+
.iter()
8+
.map(|(winning_numbers, numbers)| {
9+
winning_numbers
10+
.iter()
11+
.filter(|n| numbers.contains(n))
12+
.fold(0, |sum, _| if sum == 0 { 1 } else { sum * 2 })
13+
})
14+
.sum())
15+
}
16+
17+
#[cfg(test)]
18+
mod tests {
19+
use crate::parse_input;
20+
21+
use super::*;
22+
23+
const EXAMPLE_DATA: &'static str = "
24+
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
25+
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
26+
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
27+
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
28+
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
29+
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
30+
";
31+
32+
#[test]
33+
fn it_gets_the_example_correct() -> Result<()> {
34+
assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 13);
35+
Ok(())
36+
}
37+
}

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