mirror of
https://github.com/anthonyoteri/advent-of-code-2023.git
synced 2026-06-05 20:06:52 -04:00
@@ -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 = []
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
File diff suppressed because one or more lines are too long
@@ -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(())
|
||||||
|
}
|
||||||
@@ -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(())
|
||||||
|
}
|
||||||
@@ -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),
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
pub mod error;
|
||||||
|
|
||||||
|
pub mod part1;
|
||||||
|
pub mod part2;
|
||||||
@@ -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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7
|
||||||
+14525
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user