From 70f6149889511f61f91f525eba5a82e938cb41ec Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 11 Dec 2022 22:09:16 -0500 Subject: support string solutions --- Cargo.lock | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/bin/2021/day13.rs | 7 ++-- src/bin/2022/day10.rs | 16 +++++---- src/bin/2022/day5.rs | 18 +++-------- src/prelude.rs | 1 + 6 files changed, 108 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 82e006e..75e8f4b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,7 @@ version = 3 name = "advent-of-code" version = "0.1.0" dependencies = [ + "advent-of-code-ocr", "ahash", "anyhow", "once_cell", @@ -15,6 +16,16 @@ dependencies = [ "structopt", ] +[[package]] +name = "advent-of-code-ocr" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b421cf3c223f8ad6a06fe2812d28dc202a6b6849aa58c28556d75a8b28ed3c56" +dependencies = [ + "itertools", + "phf", +] + [[package]] name = "ahash" version = "0.8.2" @@ -96,6 +107,12 @@ dependencies = [ "vec_map", ] +[[package]] +name = "either" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" + [[package]] name = "getrandom" version = "0.2.8" @@ -141,6 +158,15 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -192,6 +218,48 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f0b59668fe80c5afe998f0c0bf93322bf2cd66cafeeb80581f291716f3467f2" +[[package]] +name = "phf" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "928c6535de93548188ef63bb7c4036bd415cd8f36ad25af44b9789b2ee72a48c" +dependencies = [ + "phf_macros", + "phf_shared", +] + +[[package]] +name = "phf_generator" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1181c94580fa345f50f19d738aaa39c0ed30a600d95cb2d3e23f94266f14fbf" +dependencies = [ + "phf_shared", + "rand", +] + +[[package]] +name = "phf_macros" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92aacdc5f16768709a569e913f7451034034178b05bdc8acda226659a3dccc66" +dependencies = [ + "phf_generator", + "phf_shared", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "phf_shared" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1fb5f6f826b772a8d4c0394209441e7d37cbbb967ae9c7e0e8134365c9ee676" +dependencies = [ + "siphasher", +] + [[package]] name = "priority-queue" version = "1.3.0" @@ -244,6 +312,21 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" + [[package]] name = "regex" version = "1.7.0" @@ -261,6 +344,12 @@ version = "0.6.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" +[[package]] +name = "siphasher" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de" + [[package]] name = "strsim" version = "0.8.0" diff --git a/Cargo.toml b/Cargo.toml index 2901de2..e7995c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +advent-of-code-ocr = "0.1.1" ahash = "0.8.2" anyhow = "1.0.66" once_cell = "1.16.0" diff --git a/src/bin/2021/day13.rs b/src/bin/2021/day13.rs index 75e547b..b21071f 100644 --- a/src/bin/2021/day13.rs +++ b/src/bin/2021/day13.rs @@ -122,13 +122,12 @@ pub fn part1( pub fn part2( (mut paper, folds): (Paper, Vec<(bool, usize)>), -) -> Result { +) -> Result { for fold in folds { paper.fold(fold.0, fold.1); } - println!("{}", paper); - Ok(paper.total()) + Ok(ocr(&paper.to_string())) } #[test] @@ -139,6 +138,6 @@ fn test() { ); assert_eq!( part2(parse(parse::data(2021, 13).unwrap()).unwrap()).unwrap(), - 95 + "ECFHLHZF" ); } diff --git a/src/bin/2022/day10.rs b/src/bin/2022/day10.rs index 8f7b617..ff9ab11 100644 --- a/src/bin/2022/day10.rs +++ b/src/bin/2022/day10.rs @@ -62,22 +62,26 @@ pub fn part1(ops: impl Iterator) -> Result { Ok(total) } -pub fn part2(ops: impl Iterator) -> Result { +pub fn part2(ops: impl Iterator) -> Result { let mut cpu = Cpu::new(); for op in ops { cpu.step(op); } + let mut s = String::new(); for (y, row) in cpu.history.chunks(40).enumerate() { + if row.len() < 40 { + break; + } for (x, pos) in row.iter().enumerate() { if i64::try_from(x).unwrap().abs_diff(*pos) <= 1 { - print!("#"); + s.push('#'); } else { - print!("."); + s.push('.'); } } - println!(); + s.push('\n'); } - Ok(0) + Ok(ocr(&s)) } #[test] @@ -88,6 +92,6 @@ fn test() { ); assert_eq!( part2(parse(parse::data(2022, 10).unwrap()).unwrap()).unwrap(), - 0 + "ZFBFHGUP" ); } diff --git a/src/bin/2022/day5.rs b/src/bin/2022/day5.rs index da1e5a2..b09e2e0 100644 --- a/src/bin/2022/day5.rs +++ b/src/bin/2022/day5.rs @@ -86,7 +86,7 @@ pub fn parse(fh: File) -> Result { }) } -fn part1_str(mut crates: Crates) -> Result { +pub fn part1(mut crates: Crates) -> Result { for Instruction { count, from, to } in crates.instructions { for _ in 0..count { let c = crates.piles[from - 1].pop().unwrap(); @@ -100,12 +100,7 @@ fn part1_str(mut crates: Crates) -> Result { .collect()) } -pub fn part1(crates: Crates) -> Result { - println!("{}", part1_str(crates)?); - Ok(0) -} - -fn part2_str(mut crates: Crates) -> Result { +pub fn part2(mut crates: Crates) -> Result { for Instruction { count, from, to } in crates.instructions { let mut tmp = vec![]; for _ in 0..count { @@ -123,19 +118,14 @@ fn part2_str(mut crates: Crates) -> Result { .collect()) } -pub fn part2(crates: Crates) -> Result { - println!("{}", part2_str(crates)?); - Ok(0) -} - #[test] fn test() { assert_eq!( - part1_str(parse(parse::data(2022, 5).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2022, 5).unwrap()).unwrap()).unwrap(), "PSNRGBTFT" ); assert_eq!( - part2_str(parse(parse::data(2022, 5).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2022, 5).unwrap()).unwrap()).unwrap(), "BNTZFPMMW" ); } diff --git a/src/prelude.rs b/src/prelude.rs index 46a7ee5..f02b36d 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -10,6 +10,7 @@ pub use std::collections::VecDeque; pub use std::fs::File; pub use std::io::{BufRead as _, Read as _}; +pub use advent_of_code_ocr::parse_string_to_letters as ocr; pub use ahash::{AHashMap as HashMap, AHashSet as HashSet}; pub use anyhow::{anyhow, bail, Context as _, Error, Result}; pub use regex::Regex; -- cgit v1.2.3-54-g00ecf