Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compress inputs #6

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
Cargo.lock
**/input.txt
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]

members = [
"compress",
"day1",
]

10 changes: 10 additions & 0 deletions compress/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"] }
44 changes: 44 additions & 0 deletions compress/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::io::{Read, Write};

/// Compresses the passed string into the output byte vector.
pub fn compress_to<R, W>(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<R>(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);
}
}
28 changes: 28 additions & 0 deletions compress/src/main.rs
Original file line number Diff line number Diff line change
@@ -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:?}");
}
1 change: 1 addition & 0 deletions day1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
6 changes: 3 additions & 3 deletions day1/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
Binary file added day1/input.compressed
Binary file not shown.
1 change: 0 additions & 1 deletion day1/input.txt

This file was deleted.

8 changes: 4 additions & 4 deletions day1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 1 addition & 3 deletions day1/src/main.rs
Original file line number Diff line number Diff line change
@@ -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));
}