Skip to content

Commit cd290da

Browse files
authored
Merge pull request #6 from nerdatmath/push-xznymrplpkqu
Push xznymrplpkqu
2 parents 6170830 + c205f8a commit cd290da

File tree

29 files changed

+951
-5
lines changed

29 files changed

+951
-5
lines changed

2023/09/.gitignore

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

2023/09/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "y2023d09"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]

2023/09/data/example1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
0 3 6 9 12 15
2+
1 3 6 10 15 21
3+
10 13 16 21 30 45

2023/09/data/input

Lines changed: 200 additions & 0 deletions
Large diffs are not rendered by default.

2023/09/src/data.rs

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

2023/09/src/history.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use std::{num::ParseIntError, str::FromStr};
2+
3+
#[derive(Debug)]
4+
pub struct ParseError;
5+
6+
impl From<ParseIntError> for ParseError {
7+
fn from(_value: ParseIntError) -> Self {
8+
Self
9+
}
10+
}
11+
12+
#[derive(Debug)]
13+
pub struct History(pub Box<[i64]>);
14+
15+
impl FromStr for History {
16+
type Err = ParseError;
17+
18+
fn from_str(s: &str) -> Result<Self, Self::Err> {
19+
Ok(Self(
20+
s.split_ascii_whitespace()
21+
.map(|s| s.parse())
22+
.collect::<Result<_, _>>()?,
23+
))
24+
}
25+
}
26+
27+
impl History {
28+
pub fn next_value(&self) -> i64 {
29+
if self.0.len() <= 1 {
30+
return 0;
31+
}
32+
let diffs = History(self.0.windows(2).map(|s| s[1] - s[0]).collect());
33+
self.0[self.0.len() - 1] + diffs.next_value()
34+
}
35+
36+
pub fn prev_value(&self) -> i64 {
37+
if self.0.len() <= 1 {
38+
return 0;
39+
}
40+
let diffs = History(self.0.windows(2).map(|s| s[1] - s[0]).collect());
41+
self.0[0] - diffs.prev_value()
42+
}
43+
}
44+
45+
#[test]
46+
fn next_value() {
47+
let h = History(Box::new([0, 3, 6, 9, 12, 15]));
48+
assert_eq!(h.next_value(), 18);
49+
}
50+
51+
#[test]
52+
fn prev_value() {
53+
let h = History(Box::new([10, 13, 16, 21, 30, 45]));
54+
assert_eq!(h.prev_value(), 5);
55+
}

2023/09/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 history;
3+
mod part1;
4+
mod part2;
5+
mod puzzle;
6+
7+
fn main() {
8+
use data::INPUT;
9+
println!("Part 1: {}", part1::run(INPUT));
10+
println!("Part 2: {}", part2::run(INPUT));
11+
}

2023/09/src/part1/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use crate::puzzle::Puzzle;
2+
3+
pub fn run(input: &str) -> i64 {
4+
let puzzle: Puzzle = input.parse().expect("parse failed");
5+
puzzle.histories.iter().map(|h| h.next_value()).sum()
6+
}
7+
8+
#[cfg(test)]
9+
mod test {
10+
use super::*;
11+
use crate::data::EXAMPLE1;
12+
13+
#[test]
14+
fn test1() {
15+
assert_eq!(run(EXAMPLE1), 114);
16+
}
17+
}

2023/09/src/part2/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use crate::puzzle::Puzzle;
2+
3+
pub fn run(input: &str) -> i64 {
4+
let puzzle: Puzzle = input.parse().expect("parse failed");
5+
puzzle
6+
.histories
7+
.iter()
8+
.map(|h| h.prev_value())
9+
.sum()
10+
}
11+
12+
#[cfg(test)]
13+
mod test {
14+
use super::*;
15+
use crate::data::EXAMPLE1;
16+
17+
#[test]
18+
fn test1() {
19+
assert_eq!(run(EXAMPLE1), 2);
20+
}
21+
}

2023/09/src/puzzle.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::str::FromStr;
2+
3+
use crate::history::History;
4+
5+
#[derive(Debug)]
6+
pub struct ParseError;
7+
8+
impl From<<History as FromStr>::Err> for ParseError {
9+
fn from(_value: <History as FromStr>::Err) -> Self {
10+
Self
11+
}
12+
}
13+
14+
#[derive(Debug)]
15+
pub struct Puzzle {
16+
pub histories: Box<[History]>,
17+
}
18+
19+
impl FromStr for Puzzle {
20+
type Err = ParseError;
21+
22+
fn from_str(s: &str) -> Result<Self, Self::Err> {
23+
Ok(Self {
24+
histories: s.lines().map(|s| s.parse()).collect::<Result<_, _>>()?,
25+
})
26+
}
27+
}

0 commit comments

Comments
 (0)