Day 15 - Part 1

Signed-off-by: Anthony Oteri <anthony.oteri@gmail.com>
This commit is contained in:
Anthony Oteri
2023-12-15 08:52:04 -05:00
parent dcd80319df
commit 43fccba513
11 changed files with 14710 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
[package]
name = "day-15"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
itertools = { workspace = true }
nom = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
miette = { workspace = true }
thiserror = { workspace = true }
dhat = { workspace = true }
[dev-dependencies]
divan = { workspace = true }
env_logger = { workspace = true }
test-log = { workspace = true }
rstest = { workspace = true }
[[bench]]
name = "day-00"
path = "benches/benchmark.rs"
harness = false
[features]
dhat-heap = []
+15
View File
@@ -0,0 +1,15 @@
use day_15::*;
fn main() {
divan::main();
}
#[divan::bench]
fn part1() {
part1::process(divan::black_box(include_str!("../input.txt"))).unwrap();
}
#[divan::bench]
fn part2() {
part2::process(divan::black_box(include_str!("../input.txt"))).unwrap();
}
+1
View File
File diff suppressed because one or more lines are too long
+21
View File
@@ -0,0 +1,21 @@
use day_15::part1::process;
use miette::Context;
#[cfg(feature = "dhat-heap")]
#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc;
#[tracing::instrument]
fn main() -> miette::Result<()> {
#[cfg(feature = "dhat-heap")]
let _profiler = dhat::Profiler::new_heap();
#[cfg(not(feature = "dhat-heap"))]
tracing_subscriber::fmt::init();
let file = include_str!("../../input.txt");
let result = process(file).context("process part 1")?;
println!("{}", result);
Ok(())
}
+21
View File
@@ -0,0 +1,21 @@
use day_15::part2::process;
use miette::Context;
#[cfg(feature = "dhat-heap")]
#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc;
#[tracing::instrument]
fn main() -> miette::Result<()> {
#[cfg(feature = "dhat-heap")]
let _profiler = dhat::Profiler::new_heap();
#[cfg(not(feature = "dhat-heap"))]
tracing_subscriber::fmt::init();
let file = include_str!("../../input.txt");
let result = process(file).context("process part 1")?;
println!("{}", result);
Ok(())
}
+9
View File
@@ -0,0 +1,9 @@
use miette::Diagnostic;
use thiserror::Error;
#[derive(Error, Diagnostic, Debug)]
pub enum AocError {
#[error(transparent)]
#[diagnostic(code(aoc::io_error))]
IoError(#[from] std::io::Error),
}
+4
View File
@@ -0,0 +1,4 @@
pub mod error;
pub mod part1;
pub mod part2;
+66
View File
@@ -0,0 +1,66 @@
use itertools::Itertools;
use crate::error::AocError;
use nom::{
bytes::complete::{tag, take_while},
multi::separated_list1,
IResult,
};
fn parse(input: &str) -> IResult<&str, Vec<&str>> {
separated_list1(
tag(","),
take_while(|c: char| c.is_alphanumeric() || c == '=' || c == '-'),
)(input)
}
#[tracing::instrument]
fn hash_line(line: &str) -> usize {
let ascii_codes = line.chars().map(|c| c as u8).collect_vec();
let mut sum: usize = 0;
for code in ascii_codes {
sum += code as usize;
sum *= 17;
sum %= 256;
}
sum
}
#[tracing::instrument]
pub fn process(input: &str) -> miette::Result<u64, AocError> {
let (input, parsed) = parse(input).unwrap();
debug_assert!(input.is_empty(), "Failed to completely parse input");
Ok(parsed.iter().map(|&l| hash_line(l) as u64).sum::<u64>())
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;
#[test_log::test(rstest)]
#[case("rn=1", 30)]
#[case("cm-", 253)]
#[case("qp=3", 97)]
#[case("cm=2", 47)]
#[case("qp-", 14)]
#[case("pc=4", 180)]
#[case("ot=9", 9)]
#[case("ab=5", 197)]
#[case("pc-", 48)]
#[case("pc=6", 214)]
#[case("ot=7", 231)]
fn test_line(#[case] input: &str, #[case] output: usize) {
assert_eq!(hash_line(input), output);
}
#[test_log::test]
fn test_process() -> miette::Result<()> {
let input = include_str!("../test-input.txt");
assert_eq!(1320, process(input)?);
Ok(())
}
}
+18
View File
@@ -0,0 +1,18 @@
use crate::error::AocError;
#[tracing::instrument]
pub fn process(input: &str) -> miette::Result<u64, AocError> {
Ok(0)
}
#[cfg(test)]
mod tests {
use super::*;
#[test_log::test]
fn test_process() -> miette::Result<()> {
let input = include_str!("../test-input.txt");
assert_eq!(0, process(input)?);
Ok(())
}
}
+1
View File
@@ -0,0 +1 @@
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7
+14525
View File
File diff suppressed because it is too large Load Diff