From 7c962a202b50e0d844f96d48e57fefa96c9d893b Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sat, 21 Dec 2024 17:16:29 -0800 Subject: [PATCH] day3 part 1 --- Cargo.lock | 7 ++++++ Cargo.toml | 2 +- day02/src/main.rs | 38 ++++++++++++++---------------- day03/Cargo.toml | 7 ++++++ day03/src/main.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 22 deletions(-) create mode 100644 day03/Cargo.toml create mode 100644 day03/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 912c1b2..c097eec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,6 +13,13 @@ dependencies = [ name = "day02" version = "0.1.0" +[[package]] +name = "day03" +version = "0.1.0" +dependencies = [ + "winnow", +] + [[package]] name = "memchr" version = "2.7.4" diff --git a/Cargo.toml b/Cargo.toml index 4668a35..6fcfcb6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] resolver = "2" -members = ["day01", "day02"] +members = ["day01", "day02", "day03"] [workspace.dependencies] winnow = "0.6" diff --git a/day02/src/main.rs b/day02/src/main.rs index 548f279..ce07a2e 100644 --- a/day02/src/main.rs +++ b/day02/src/main.rs @@ -27,6 +27,23 @@ fn pt1(input: &str) -> usize { safe } +fn pt2(input: &str) -> usize { + let input = parse(input); + let mut safe = 0; + for line in input.iter() { + for i in 0..(line.len()) { + let left = &line[0..i]; + let right = &line[(i + 1)..(line.len())]; + let line: Vec = left.iter().cloned().chain(right.iter().cloned()).collect(); + if safe_line(&line) { + safe += 1; + break; + } + } + } + safe +} + fn safe_line(line: &[i64]) -> bool { let mut dir = None; let mut last = None; @@ -54,27 +71,6 @@ fn safe_line(line: &[i64]) -> bool { lsafe } -fn pt2(input: &str) -> usize { - let input = parse(input); - let mut safe = 0; - for line in input.iter() { - if safe_line(line) { - safe += 1; - } else { - for i in 0..(line.len()) { - let left = &line[0..i]; - let right = &line[(i + 1)..(line.len())]; - let line: Vec = left.iter().cloned().chain(right.iter().cloned()).collect(); - if safe_line(&line) { - safe += 1; - break; - } - } - } - } - safe -} - fn parse(input: &str) -> Vec> { let mut lines = Vec::new(); for line in input.split("\n").filter(|s| !s.is_empty()) { diff --git a/day03/Cargo.toml b/day03/Cargo.toml new file mode 100644 index 0000000..3fbefc6 --- /dev/null +++ b/day03/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "day03" +version = "0.1.0" +edition = "2024" + +[dependencies] +winnow.workspace = true diff --git a/day03/src/main.rs b/day03/src/main.rs new file mode 100644 index 0000000..0ecf189 --- /dev/null +++ b/day03/src/main.rs @@ -0,0 +1,60 @@ +use winnow::{ + PResult, Parser, + ascii::dec_int, + combinator::{alt, delimited, preceded, separated_pair}, + token::any, +}; + +fn main() { + let input = std::fs::read_to_string("input").unwrap(); + println!("{}", pt1(&input)); +} + +#[derive(Debug, PartialEq, Eq)] +enum Inst { + Mul(i32, i32), +} + +fn pt1(input: &str) -> i32 { + let mut input = input; + let v = parse(&mut input).unwrap(); + v.iter().map(|Inst::Mul(a, b)| (a * b)).sum() +} + +fn parse_inst(input: &mut &str) -> PResult { + let _ = "mul".parse_next(input)?; + let pair: (i32, i32) = + delimited('(', separated_pair(dec_int, ',', dec_int), ')').parse_next(input)?; + Ok(Inst::Mul(pair.0, pair.1)) +} + +fn parse(input: &mut &str) -> PResult> { + // why can't I just do `repeat(0.., alt((parse_inst, preceded(any, + // parse_inst))))` and call it a day? + let mut v = Vec::new(); + loop { + if input.is_empty() { + break; + } + match alt((parse_inst, preceded(any, parse_inst))).parse_next(input) { + Ok(i) => { + v.push(i); + } + Err(_e) => {} + } + } + Ok(v) +} + +#[cfg(test)] +mod test { + use super::*; + + static INPUT: &str = "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))"; + + #[test] + fn p1() { + let v = pt1(INPUT); + assert_eq!(v, 161) + } +}