Skip to content

Commit bbf6e6e

Browse files
authored
Merge pull request #7 from nerdatmath/push-trnwkzruuspv
Push trnwkzruuspv
2 parents cd290da + 5ce63c1 commit bbf6e6e

File tree

12 files changed

+181
-6
lines changed

12 files changed

+181
-6
lines changed

2023/11/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

2023/11/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "y2023d11"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
anyhow = "1.0.100"
8+
itertools.workspace = true

2023/11/data/example1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0 1 10 99 999

2023/11/data/example2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
125 17

2023/11/data/input

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3935565 31753 437818 7697 5 38 0 123

2023/11/src/data.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#[cfg(test)]
2+
pub const EXAMPLE1: &'static str = include_str!("../data/example1");
3+
4+
#[cfg(test)]
5+
pub const EXAMPLE2: &'static str = include_str!("../data/example2");
6+
7+
#[allow(unused)]
8+
pub const INPUT: &'static str = include_str!("../data/input");

2023/11/src/main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
mod data;
2+
mod part1;
3+
mod part2;
4+
mod puzzle;
5+
mod stone;
6+
7+
fn main() {
8+
use data::INPUT;
9+
println!("Part 1: {}", part1::run(INPUT));
10+
println!("Part 2: {}", part2::run(INPUT));
11+
}

2023/11/src/part1/mod.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use crate::*;
2+
use puzzle::Puzzle;
3+
use stone::Stone;
4+
5+
type Stones = Vec<Stone>;
6+
7+
pub fn blink(stones: &Stones) -> Stones {
8+
stones.into_iter().flat_map(Stone::blink).collect()
9+
}
10+
11+
pub fn run(input: &str) -> usize {
12+
let puzzle: Puzzle = input.parse().expect("parse failed");
13+
let mut stones = puzzle.0;
14+
for _ in 0..25 {
15+
stones = blink(&stones);
16+
}
17+
stones.len()
18+
}
19+
20+
#[cfg(test)]
21+
mod test {
22+
use super::*;
23+
use anyhow::Result;
24+
use data::EXAMPLE1;
25+
use data::EXAMPLE2;
26+
27+
#[test]
28+
fn test_blink() -> Result<()> {
29+
let puzzle: Puzzle = EXAMPLE1.parse()?;
30+
assert_eq!(
31+
blink(&puzzle.0),
32+
[1, 2024, 1, 0, 9, 9, 2021976]
33+
.into_iter()
34+
.map(Stone)
35+
.collect::<Stones>()
36+
);
37+
Ok(())
38+
}
39+
40+
#[test]
41+
fn test1() {
42+
assert_eq!(run(EXAMPLE2), 55312);
43+
}
44+
}

2023/11/src/part2/mod.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use crate::*;
2+
use itertools::Itertools;
3+
use puzzle::Puzzle;
4+
use std::collections::HashMap;
5+
use stone::Stone;
6+
7+
type Stones = HashMap<Stone, usize>;
8+
9+
fn blink(stones: &Stones) -> Stones {
10+
stones
11+
.iter()
12+
.flat_map(|(&stone, &count)| stone.blink().into_iter().map(move |stone| (stone, count)))
13+
.into_grouping_map()
14+
.sum()
15+
}
16+
17+
pub fn run(input: &str) -> usize {
18+
let puzzle: Puzzle = input.parse().expect("parse failed");
19+
let mut stones: Stones = puzzle.0.iter().map(|&stone| (stone, 1usize)).collect();
20+
for _ in 0..75 {
21+
stones = blink(&stones);
22+
}
23+
stones.into_iter().map(|(_, count)| count).sum()
24+
}
25+
26+
#[cfg(test)]
27+
mod test {
28+
use super::*;
29+
use anyhow::Result;
30+
use data::EXAMPLE1;
31+
32+
#[test]
33+
fn test_blink() -> Result<()> {
34+
let puzzle: Puzzle = EXAMPLE1.parse()?;
35+
let stones: Stones = puzzle.0.iter().copied().counts();
36+
assert_eq!(
37+
blink(&stones),
38+
Stones::from([
39+
(Stone(1), 2),
40+
(Stone(2024), 1),
41+
(Stone(0), 1),
42+
(Stone(9), 2),
43+
(Stone(2021976), 1)
44+
])
45+
);
46+
Ok(())
47+
}
48+
}

2023/11/src/puzzle.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use crate::*;
2+
use anyhow::Result;
3+
use std::str::FromStr;
4+
use stone::Stone;
5+
6+
#[derive(Debug)]
7+
pub struct Puzzle(pub Vec<Stone>);
8+
9+
impl FromStr for Puzzle {
10+
type Err = anyhow::Error;
11+
12+
fn from_str(s: &str) -> Result<Self> {
13+
Ok(Puzzle(
14+
s.split(' ')
15+
.map(|s| Ok(Stone(s.parse()?)))
16+
.collect::<Result<_>>()?,
17+
))
18+
}
19+
}

0 commit comments

Comments
 (0)