making 2023 a generic aoc repo

This commit is contained in:
Dylan Thies
2023-12-04 19:40:31 -05:00
parent 09f1da1295
commit 51cf3fa8e7
28 changed files with 2 additions and 2 deletions

16
2023/day-1/Cargo.toml Normal file
View File

@@ -0,0 +1,16 @@
[package]
name = "day-1"
version.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
derive-getters.workspace = true
error-stack.workspace = true
itertools.workspace = true
log.workspace = true
nom.workspace = true

2
2023/day-1/src/lib.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod part1;
pub mod part2;

12
2023/day-1/src/main.rs Normal file
View File

@@ -0,0 +1,12 @@
#![warn(clippy::all, clippy::pedantic)]
use day_1::part1::part1;
use day_1::part2::part2;
fn main() {
let input = include_str!("./input.txt");
let (_, part1_result) = part1(input).unwrap();
println!("part 1: {part1_result}");
let part2_result = part2(input);
println!("part 2: {part2_result}");
}

59
2023/day-1/src/part1.rs Normal file
View File

@@ -0,0 +1,59 @@
#![warn(clippy::all, clippy::pedantic)]
use nom::{
self,
character::complete::{alphanumeric1, newline},
multi::separated_list1,
};
/// Day-1 part 1 of AC2023
///
/// # Arguments
/// - input the input for day1 as a string
///
/// # Panics
/// This panics whenever a number isn't present in a line of the input
///
/// # Errors
/// errors when can't parse the input
pub fn part1(input: &str) -> nom::IResult<&str, String> {
let (_, values) = parse_input(input)?;
println!("{values:?}");
Ok((
"",
values
.iter()
.map(|v| v.first().expect("always at least one number") * 10 + v.last().expect("always atleast one number"))
.sum::<u32>()
.to_string(),
))
}
fn parse_input(input: &str) -> nom::IResult<&str, Vec<Vec<u32>>> {
let (i, j) = separated_list1(newline, alphanumeric1)(input)?;
let res = j
.iter()
.map(|v| {
v.chars()
.filter_map(|x| x.to_digit(10))
.collect::<Vec<u32>>()
})
.collect::<Vec<Vec<u32>>>();
Ok((i, res))
}
#[cfg(test)]
mod test {
use super::*;
const INPUT: &str = "1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet";
#[test]
fn part1_works() {
let (_, result) = part1(INPUT).unwrap();
assert_eq!(result, "142".to_string());
}
}

70
2023/day-1/src/part2.rs Normal file
View File

@@ -0,0 +1,70 @@
#![warn(clippy::all, clippy::pedantic)]
/// Day 1 Part 2 of AOC2023
///
/// # Arguments
/// - puzzle input
///
/// # Panics
/// this panics if there is no numbers in a line
pub fn part2(input: &str) -> String {
let values = input.lines().map(parse_line).collect::<Vec<Vec<u32>>>();
println!("{values:?}");
values
.iter()
.map(|v| v.first().expect("There is always at least one number") * 10 + v.last().expect("there is always at least one number"))
.sum::<u32>()
.to_string()
}
fn parse_line(line: &str) -> Vec<u32> {
(0..line.len())
.filter_map(|index| {
let reduced_line = &line[index..];
let result = if reduced_line.starts_with("one") {
Some(1)
} else if reduced_line.starts_with("two") {
Some(2)
} else if reduced_line.starts_with("three") {
Some(3)
} else if reduced_line.starts_with("four") {
Some(4)
} else if reduced_line.starts_with("five") {
Some(5)
} else if reduced_line.starts_with("six") {
Some(6)
} else if reduced_line.starts_with("seven") {
Some(7)
} else if reduced_line.starts_with("eight") {
Some(8)
} else if reduced_line.starts_with("nine") {
Some(9)
} else if reduced_line.starts_with("zero") {
Some(0)
} else {
reduced_line.chars().next().expect("there is alwayss a character").to_digit(10)
};
result
})
.collect()
}
#[cfg(test)]
mod test {
use super::*;
const INPUT: &str = "two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen";
#[test]
fn part2_works() {
let result = part2(INPUT);
assert_eq!(result, "281".to_string());
}
}