summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock1
-rw-r--r--Cargo.toml1
-rw-r--r--src/2020/2/mod.rs4
-rw-r--r--src/2020/7/mod.rs12
-rw-r--r--src/2020/8/mod.rs4
-rw-r--r--src/2021/17/mod.rs9
-rw-r--r--src/2021/5/mod.rs3
-rw-r--r--src/main.rs3
-rw-r--r--src/regex.rs8
9 files changed, 27 insertions, 18 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 6420581..403b53b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -8,6 +8,7 @@ version = "0.1.0"
dependencies = [
"ahash",
"anyhow",
+ "lazy_static",
"paw",
"priority-queue",
"regex",
diff --git a/Cargo.toml b/Cargo.toml
index cccaac3..e863961 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -9,6 +9,7 @@ edition = "2021"
[dependencies]
ahash = "0.7.6"
anyhow = "1.0.51"
+lazy_static = "1.4.0"
paw = "1.0.0"
priority-queue = "1.2.1"
regex = "1.5.4"
diff --git a/src/2020/2/mod.rs b/src/2020/2/mod.rs
index 75cc4f1..a2ff9df 100644
--- a/src/2020/2/mod.rs
+++ b/src/2020/2/mod.rs
@@ -9,9 +9,9 @@ pub struct Line {
impl Line {
fn parse(line: &str) -> Result<Self> {
- let rx = Regex::new(r"^([0-9]+)-([0-9]+) (.): (.*)$").unwrap();
let captures =
- rx.captures(line).context("line failed to match regex")?;
+ regex_captures!(r"^([0-9]+)-([0-9]+) (.): (.*)$", line)
+ .context("line failed to match regex")?;
let c = captures
.get(3)
.unwrap()
diff --git a/src/2020/7/mod.rs b/src/2020/7/mod.rs
index 6199ad0..2c8a1a7 100644
--- a/src/2020/7/mod.rs
+++ b/src/2020/7/mod.rs
@@ -28,11 +28,7 @@ pub fn part2(graph: Graph) -> Result<i64> {
}
fn parse_line(line: &str) -> Result<(String, Vec<(i64, String)>)> {
- let main_rx = Regex::new(r"^(.*) bags contain (.*)\.$").unwrap();
- let contents_rx = Regex::new(r"^([0-9]+) (.*) bags?").unwrap();
-
- let captures = main_rx
- .captures(line)
+ let captures = regex_captures!(r"^(.*) bags contain (.*)\.$", line)
.context("line failed to match regex")?;
let color = captures.get(1).unwrap().as_str();
let contents = captures.get(2).unwrap().as_str();
@@ -44,9 +40,9 @@ fn parse_line(line: &str) -> Result<(String, Vec<(i64, String)>)> {
contents
.split(", ")
.map(|s| {
- let captures = contents_rx
- .captures(s)
- .context("line failed to match regex")?;
+ let captures =
+ regex_captures!(r"^([0-9]+) (.*) bags?", s)
+ .context("line failed to match regex")?;
Ok((
captures
.get(1)
diff --git a/src/2020/8/mod.rs b/src/2020/8/mod.rs
index d1908cd..af08f5c 100644
--- a/src/2020/8/mod.rs
+++ b/src/2020/8/mod.rs
@@ -30,8 +30,8 @@ impl std::str::FromStr for Op {
type Err = Error;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
- let rx = Regex::new(r"^([^ ]*) ((?:-|\+)[0-9]+)$").unwrap();
- let captures = rx.captures(s).context("failed to parse line")?;
+ let captures = regex_captures!(r"^([^ ]*) ((?:-|\+)[0-9]+)$", s)
+ .context("failed to parse line")?;
let ty = captures.get(1).unwrap().as_str().parse()?;
let arg = captures
.get(2)
diff --git a/src/2021/17/mod.rs b/src/2021/17/mod.rs
index 2bc54fc..b3bf5ad 100644
--- a/src/2021/17/mod.rs
+++ b/src/2021/17/mod.rs
@@ -40,11 +40,12 @@ fn fire(
pub fn parse(
fh: File,
) -> Result<(std::ops::RangeInclusive<i64>, std::ops::RangeInclusive<i64>)> {
- let rx =
- Regex::new(r"target area: x=(-?\d+)..(-?\d+), y=(-?\d+)..(-?\d+)")
- .unwrap();
let line = parse::lines(fh).next().unwrap();
- let captures = rx.captures(&line).unwrap();
+ let captures = regex_captures!(
+ r"target area: x=(-?\d+)..(-?\d+), y=(-?\d+)..(-?\d+)",
+ &line,
+ )
+ .unwrap();
let xrange: std::ops::RangeInclusive<i64> =
captures[1].parse().unwrap()..=captures[2].parse().unwrap();
let yrange: std::ops::RangeInclusive<i64> =
diff --git a/src/2021/5/mod.rs b/src/2021/5/mod.rs
index f33a9c8..6b413b0 100644
--- a/src/2021/5/mod.rs
+++ b/src/2021/5/mod.rs
@@ -73,9 +73,8 @@ impl Map {
}
pub fn parse(fh: File) -> Result<impl Iterator<Item = Vec<usize>>> {
- let rx = Regex::new("^(\\d+),(\\d+) -> (\\d+),(\\d+)$")?;
Ok(parse::lines(fh).map(move |line| {
- rx.captures(&line)
+ regex_captures!(r"^(\d+),(\d+) -> (\d+),(\d+)$", &line)
.unwrap()
.iter()
.skip(1)
diff --git a/src/main.rs b/src/main.rs
index aa6ad88..d5b4516 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -9,6 +9,9 @@
#![allow(clippy::collapsible_if)]
#![allow(clippy::comparison_chain)]
+#[macro_use]
+pub mod regex;
+
pub mod grid;
pub mod parse;
pub mod prelude;
diff --git a/src/regex.rs b/src/regex.rs
new file mode 100644
index 0000000..56ce19f
--- /dev/null
+++ b/src/regex.rs
@@ -0,0 +1,8 @@
+macro_rules! regex_captures {
+ ($rx:expr, $s:expr $(,)?) => {{
+ lazy_static::lazy_static! {
+ static ref RX: Regex = regex::Regex::new($rx).unwrap();
+ }
+ RX.captures($s)
+ }};
+}