diff --git a/.gitignore b/.gitignore index 96ef6c0..d61a5fd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target Cargo.lock +**/input.txt diff --git a/Cargo.toml b/Cargo.toml index b37ec41..6f94b3f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [workspace] members = [ + "compress", "day1", ] diff --git a/compress/Cargo.toml b/compress/Cargo.toml new file mode 100644 index 0000000..2ee6874 --- /dev/null +++ b/compress/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "compress" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +brotli = "3" +clap = { version = "3", features = ["derive"] } diff --git a/compress/src/lib.rs b/compress/src/lib.rs new file mode 100644 index 0000000..a318226 --- /dev/null +++ b/compress/src/lib.rs @@ -0,0 +1,44 @@ +use std::io::{Read, Write}; + +/// Compresses the passed string into the output byte vector. +pub fn compress_to(mut input: R, mut output: W) +where + R: Read, + W: Write, +{ + brotli::BrotliCompress(&mut input, &mut output, &Default::default()).unwrap(); +} + +#[macro_export] +macro_rules! aoc_input { + ($path:literal) => {{ + let file = include_bytes!($path).to_vec(); + compress::decompress(file.as_slice()) + }}; +} + +/// Decompresses the passed reader into a string. +/// +/// Panics if the result of decompression is invalid UTF-8. +pub fn decompress(mut input: R) -> String +where + R: Read, +{ + let mut output = Vec::new(); + brotli::BrotliDecompress(&mut input, &mut output).unwrap(); + String::from_utf8(output).unwrap() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn decompress_inverts_compress() { + let s = "some string"; + let mut output = Vec::new(); + compress_to(s.as_bytes(), &mut output); + + assert_eq!(decompress(output.as_slice()), s); + } +} diff --git a/compress/src/main.rs b/compress/src/main.rs new file mode 100644 index 0000000..bc3c036 --- /dev/null +++ b/compress/src/main.rs @@ -0,0 +1,28 @@ +//! Takes an input file path and writes the compressed contents +//! to `{file_name}.compressed`. +//! +//! Used for compressing Advent of Code inputs because why not. + +use clap::Parser; +use std::fs::File; + +#[derive(Parser)] +#[clap(about = "Compresses input file to .compressed file")] +struct Args { + /// Path of file to compress. + #[clap(long, short)] + input: std::path::PathBuf, +} +fn main() { + let args = Args::parse(); + + let input_file = File::open(&args.input).unwrap(); + + let mut output_path = args.input; + output_path.set_extension("compressed"); + let output_file = File::create(&output_path).unwrap(); + + compress::compress_to(input_file, output_file); + + println!("Wrote to {output_path:?}"); +} diff --git a/day1/Cargo.toml b/day1/Cargo.toml index ecff584..262e969 100644 --- a/day1/Cargo.toml +++ b/day1/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +compress = { path = "../compress" } [dev-dependencies] criterion = { version = "0.3", features = ["html_reports"] } diff --git a/day1/benches/bench.rs b/day1/benches/bench.rs index d1e9bd0..34c73ca 100644 --- a/day1/benches/bench.rs +++ b/day1/benches/bench.rs @@ -1,9 +1,9 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion}; fn criterion_benchmark(c: &mut Criterion) { - let input = include_str!("../input.txt"); - c.bench_function("part1", |b| b.iter(|| day1::part1(black_box(input)))); - c.bench_function("part2", |b| b.iter(|| day1::part2(black_box(input)))); + let input = compress::aoc_input!("../input.compressed"); + c.bench_function("part1", |b| b.iter(|| day1::part1(black_box(&input)))); + c.bench_function("part2", |b| b.iter(|| day1::part2(black_box(&input)))); } criterion_group!(benches, criterion_benchmark); diff --git a/day1/input.compressed b/day1/input.compressed new file mode 100644 index 0000000..28329ce Binary files /dev/null and b/day1/input.compressed differ diff --git a/day1/input.txt b/day1/input.txt deleted file mode 100644 index 1855a96..0000000 --- a/day1/input.txt +++ /dev/null @@ -1 +0,0 @@ -()()(()()()(()()((()((()))((()((((()()((((()))()((((())(((((((()(((((((((()(((())(()()(()((()()(()(())(()((((()((()()()((((())((((((()(()(((()())(()((((()))())(())(()(()()))))))))((((((((((((()())()())())(())))(((()()()((((()(((()(()(()()(()(()()(()(((((((())(())(())())))((()())()((((()()((()))(((()()()())))(())))((((())(((()())(())(()))(()((((()())))())((()(())(((()((((()((()(())())))((()))()()(()(()))))((((((((()())((((()()((((()(()())(((((()(()())()))())(((()))()(()(()(()((((()(())(()))(((((()()(()()()(()(((())())(((()()(()()))(((()()(((())())(()(())())()()(())()()()((()(((()(())((()()((())()))((()()))((()()())((((()(()()(()(((()))()(()))))((()(((()()()))(()(((())()(()((()())(()(()()(()())(())()(((()(()())()((((()((()))))())()))((()()()()(())()())()()()((((()))))(()(((()()(((((((())()))()((((()((())()(()())(())()))(()(()())(((((((())))(((()))())))))()))())((())(()()((())()())()))))()((()()())(())((())((((()())())()()()(((()))())))()()))())(()()()(()((((((()()))())()))()(((()(((())((((()()()(()))())()()))))())()))())((())()())(((((())())((())())))(((())(((())(((((()(((((())(()(()())())(()(())(()))(()((((()))())()))))())))((()(()))))())))(((((())()))())()))))()))))(((()))()))))((()))((()((()(()(())()())))(()()()(())()))()((((())))))))(())(()((()()))(()))(()))(()((()))))))()()((((()()))()())()))))))()()()))(()((())(()))((()()()())()(((()((((())())))()((((()(()))))))())))()()())()))(()))))(()())()))))))((())))))))())()))()((())())))(()((()))()))(())))))(()))()())()()))((()(()))()()()()))))())()()))())(())()()))()))((()))))()()(()())))))()()()))((((()))()))))(()(())))(()())))((())())(()))()))))()())))()())()())))))))))()()))))())))((())((()))))())))(((()())))))))(()))()()))(()))()))))()())))))())((((()())))))))())))()()))))))))()))()))))()))))))(())))))))))())))))))))))))))())())((())))))))))()))((())))()))))))))())()(()))))))())))))()()()())()(()()()(()())(()))()()()(()())))())())))()))))())))))))()()()()())(())())()())()))))(()()()()()))))()))())())))((()())()())))()))()))))(()())))()))))))))(((()))()()))))))))))))))))))))(()))(()((()))())))())(()))(()(()(())))))()(()))()))()()))))))))))))()((()())(())())()(())))))())()())((()()))))(()()))))())()(())()))))))))))))))))))))()))(()(()())))))))()()((()))()))))))((())))()))))))))((()))())()()))())()()))((()))())))))))))))(()())()))(())((()(()()))(()())(())))()())(()(())()()))))()))()(()))))))(()))))))))))(()))())))))))))())))))())))(())))))()))))(())())))))))))()(()))))()())))())(()))()())))))))))))))())()()))))()))))))())))))()))))(())(()()()()((())()))())(()))((())()))())())(())(()()))))()))(())()()((())(())))(())))()))())))))))))()(((((())())))(())()))))(())))((()))()(((((((()))))()()))(())))))()(()))))(()()))()))())))))))(()())()))))))))())))(()))())()))(())()((())())()())())(()(()))))()))))))((()())(())()()(()())))()()))(())(())(()))())))()))(()))()()))((((()))))()))((()()()))))()))()))())))(()))()))))(())))()))())()(()))()())))())))))))())))())))()()))))))(()))())())))()))()()())())))))))))))))())))()))(()()))))())))())()(())))())))))))))))))))))()()())())))))()()()((()(()))()()(())()())()))()))))()()()))))))((()))))))))()(()(()((((((()()((()())))))))))))()))())))))((())())(()))())))())))))())()()())(())))())))()())())(())))))))()()(())))()))())))())())())()))))))))()))(()()()())())())))(())())))))))()()())()))))())))())()(())())))))))()())()))(()()(())())))()(()((()()((()()(((((())(()())()))(())()))(())))(())))))))()))()))((()))()))()))))))))()))))))))((()()())(()))(((()))(())))()))((())(((())))()())))())))))((())))))(())())((((((())())()(()))()(()((()())))((())()(()(()))))(())(()()())(())))())((()(((())())))(((()())())))())()(())())((((()()))))())((()))()()()()(())(((((((()()()((()))())(()())))(())())((((()()(()))))()((())))((())()))()(((()))())))()))((()(()))(())(()((((())((((()()(()()))(((())(()))))((((()(()))(())))))((()))(()))((()(((()(()))(()(()((()(())(()(()(()(()()((()))())(((())(()(()))))(()))()()))(())))(())()(((())(()))()((((()()))))())(()))))((())()((((()(((()))())())(((()))()())((())(())())(())()(())()(()()((((((()()))))()()(((()()))))()())()(((()(()))(()(()())(()(()))))(((((()(((())())))))(((((()((()()((())())((((((()(())(()()((()()()()()()()(()()))()(((()))()))(((((((())(((()((()())()((((())(((()(())))()((()(()()()((())((()())()))()))())))())((((((()))(()(()()()))(()((()(()(()))()((()(((()()()((())(((((())()(()))())())((()(())))(()(()())(())((())())())(((()()()(())))))())(()))))))()))))))())((()()()))((()((((((()))(((()((((()()()(((()))())()(()()(((()((()()()()())()()))()()()(()(())((()))))(()))())))))))()(()()(((((())()(()(((((()((()(()()())(()((((((((()((((((())()((((()()()((()((()((((((()))((())))))))())()))((()(()))()(()()(()((())((()()((((((((((((()())(()()()))((((()((((((())(()))())(()()((()()))()(((((((()((()()((((((()(((())))((())))((((((((()()(((((((())(((((()())(((())((())()((((()(((((((()(()(((()((((((()(((()(((((((((((()()((()()(()))((()()(((()(((())))((((())()(()(((())()(()(((())(((((((((((()))())))((((((())((()()((((()())())((((()()))((())(((((()(()()(()()()((())(()((()()((((()(((((()((()(()((((()())((((((()(((((()()(()(()((((())))(())(())(())((((()(()()((((()((((()()((()((((((())))(((((()))))()))(()((((((((()(((())())(((())))(()(()((())(((()((()()(((((()((()()(((())()(()))(((((((())(()(((((()))((()((()((()))(())())((((()((((())()(()))(((()(((((((((((((((())(((((((((()))(((()(()()()()((((((()((())()((((((((()(())(((((((((((()(()((())()((()()(()(()()((((()()((())(()((()()(()()((((()(((((((())))((((())(())()(((()()((()()((((()((()(((()((())(((()()()((((()((((()()(()(()((((((((())(()(((((())(()())(((((((()())()(()((((()((())(()()())((((()()(((()((((())(())(()()(((((((((()()))()(((())(()(()((((((())(()()())(()))()()(((()(((()((())(()(((((((()(()(()((()(((((()(()((()(()((((((()((((()()((((()(((()((())(()(()((()()((((()()(())()(())(((())(()((((((((()())(((((((((()(())()((((())))()))()()(((((()()((((((())(()()(((()(()(((((((()(()(((((((())(())((((()((()(())))((((()()())(()))((()())((((()(((((()(()(())(()(()()())(((((()(((((()((((()()((((((((()()))(()((((((())((((())()(()(((()()()(((()(()(())(())(((((()(())())((((())(())(()(((()(((((())((((())())((()(((((((()(((())(()(()))(((((((((()((()((()()(()((((())(((()((())((((())(()(((()(((()(()((((()(((())(()(((()(()()(()(()((()()(()())(())())((()(()(((()(((()(((()()(((((((((()(((((((((()()(((()(((()())((((()(()(((()()()((())((((((((((())(()(((()((((()())((((()((()))(((()()()(((((()(((((((())((()())(()((((())((((((((())(()((()((((((((((()()((()((()()))(((()())()())()(((()())()()(()(()(((((((())()))(())()))())()()((())()((()((((()((()((())(((((()((((((()(())))(()))())(((()))((()()(()(((()))((((())()(((()))))()(()(())()(((((())(()(()(())(())()((()()()((((()(())((()())(()(()))(()(()(()()(())()()(()((())()((()))))()))((()(()()()()((()())(()))())()(()(((((((((())())((()((()((((((())()((((())(((())((()(()()()((())(()((())(((()((((()()((()(()(((((())()))()((((((()))((())(((()()))(((())(())()))(((((((())(())())()(())(((((()))()((()))()(()()((()()()()()())((((((( \ No newline at end of file diff --git a/day1/src/lib.rs b/day1/src/lib.rs index 880426a..88950bf 100644 --- a/day1/src/lib.rs +++ b/day1/src/lib.rs @@ -57,13 +57,13 @@ mod tests { #[test] fn test_part_1_chris_input() { - let input = include_str!("../input.txt"); - assert_eq!(part1(input), 280); + let input = compress::aoc_input!("../input.compressed"); + assert_eq!(part1(&input), 280); } #[test] fn test_part_2_chris_input() { - let input = include_str!("../input.txt"); - assert_eq!(part2(input), 1797); + let input = compress::aoc_input!("../input.compressed"); + assert_eq!(part2(&input), 1797); } } diff --git a/day1/src/main.rs b/day1/src/main.rs index bc5b7cf..dd72796 100644 --- a/day1/src/main.rs +++ b/day1/src/main.rs @@ -1,7 +1,5 @@ -use std::fs::read_to_string; - fn main() { - let input = read_to_string("input.txt").unwrap(); + let input = compress::aoc_input!("../input.compressed"); println!("part1: {}", day1::part1(&input)); println!("part2: {}", day1::part2(&input)); }