Day 3 complete

This commit is contained in:
2025-05-26 12:46:12 -05:00
parent 1dc412f341
commit e4a130780c
2 changed files with 187 additions and 41 deletions

45
day_03/README.md Normal file
View File

@@ -0,0 +1,45 @@
# --- Day 3: Mull It Over ---
"Our computers are having issues, so I have no idea if we have any Chief Historians in stock! You're welcome to check the warehouse, though," says the mildly flustered shopkeeper at the North Pole Toboggan Rental Shop. The Historians head out to take a look.
The shopkeeper turns to you. "Any chance you can see why our computers are having issues again?"
The computer appears to be trying to run a program, but its memory (your puzzle input) is corrupted. All of the instructions have been jumbled up!
It seems like the goal of the program is just to multiply some numbers. It does that with instructions like `mul(X,Y)`, where X and Y are each 1-3 digit numbers. For instance, `mul(44,46)` multiplies 44 by 46 to get a result of 2024. Similarly, `mul(123,4)` would multiply 123 by 4.
However, because the program's memory has been corrupted, there are also many invalid characters that should be ignored, even if they look like part of a mul instruction. Sequences like `mul(4*`, `mul(6,9!`, `?(12,34)`, or `mul ( 2 , 4 )` do nothing.
For example, consider the following section of corrupted memory:
`xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))`
Only the four highlighted sections are real mul instructions. Adding up the result of each instruction produces 161 `(2*4 + 5*5 + 11*8 + 8*5)`.
Scan the corrupted memory for uncorrupted mul instructions. What do you get if you add up all of the results of the multiplications?
Your puzzle answer was 184576302.
# --- Part Two ---
As you scan through the corrupted memory, you notice that some of the conditional statements are also still intact. If you handle some of the uncorrupted conditional statements in the program, you might be able to get an even more accurate result.
There are two new instructions you'll need to handle:
The do() instruction enables future mul instructions.
The don't() instruction disables future mul instructions.
Only the most recent do() or don't() instruction applies. At the beginning of the program, mul instructions are enabled.
For example:
`xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))`
This corrupted memory is similar to the example from before, but this time the mul(5,5) and mul(11,8) instructions are disabled because there is a don't() instruction before them. The other mul instructions function normally, including the one at the end that gets re-enabled by a do() instruction.
This time, the sum of the results is 48 `(2*4 + 8*5)`.
Handle the new instructions; what do you get if you add up all of the results of just the enabled multiplications?
Your puzzle answer was 118173507.
Both parts of this puzzle are complete! They provide two gold stars: **

View File

@@ -8,6 +8,7 @@ use std::env;
use std::error::Error; use std::error::Error;
use std::fs::File; use std::fs::File;
use std::io::{BufReader, prelude::*}; use std::io::{BufReader, prelude::*};
use std::iter::Peekable;
use std::str::FromStr; use std::str::FromStr;
#[derive(Eq, PartialEq, Clone, Debug)] #[derive(Eq, PartialEq, Clone, Debug)]
enum Token { enum Token {
@@ -21,14 +22,6 @@ enum Token {
NewLine, NewLine,
} }
fn is_mul_text_token(text_token: &str) -> bool {
if text_token.ends_with("mul") {
true
} else {
false
}
}
fn tokenize(input_str: String) -> Vec<Token> { fn tokenize(input_str: String) -> Vec<Token> {
let mut tokens: Vec<Token> = Vec::new(); let mut tokens: Vec<Token> = Vec::new();
let mut chars = input_str.chars().peekable(); let mut chars = input_str.chars().peekable();
@@ -37,7 +30,9 @@ fn tokenize(input_str: String) -> Vec<Token> {
// Text Numeric // Text Numeric
_ if char::is_alphabetic(c) => { _ if char::is_alphabetic(c) => {
let mut text = c.to_string(); let mut text = c.to_string();
while let Some(alphabetic_char) = chars.next_if(|p| p.is_alphabetic()) { // HACK: including ' as part of alphabetic to implent "don't" expressions for part 2
while let Some(alphabetic_char) = chars.next_if(|p| p.is_alphabetic() || *p == '\'')
{
text.push(alphabetic_char) text.push(alphabetic_char)
} }
Token::Text(text) Token::Text(text)
@@ -73,6 +68,10 @@ struct MulOp {
} }
impl MulOp { impl MulOp {
pub fn new(lhs: String, rhs: String) -> MulOp {
MulOp { lhs, rhs }
}
pub fn multiple<T>(&self) -> Result<<T as Mul>::Output, <T as FromStr>::Err> pub fn multiple<T>(&self) -> Result<<T as Mul>::Output, <T as FromStr>::Err>
where where
T: FromStr + Mul, T: FromStr + Mul,
@@ -83,42 +82,131 @@ impl MulOp {
} }
} }
fn parse_tokens(tokens: &Vec<Token>) -> Vec<MulOp> { fn is_mul_text_token(text_token: &str) -> bool {
if text_token.ends_with("mul") {
true
} else {
false
}
}
fn parse_mul<'a>(token_iter: &mut Peekable<impl Iterator<Item = &'a Token>>) -> Option<MulOp> {
// Take OpenParenthesis or return None
if !token_iter.next_if_eq(&&Token::OpenParenthesis).is_some() {
return None;
}
// Take Number or return None
let lhs =
if let Some(Token::Number(lhs)) = token_iter.next_if(|v| matches!(v, Token::Number(_))) {
lhs.to_owned()
} else {
return None;
};
// Take Comma or return None
if !token_iter.next_if_eq(&&Token::Comma).is_some() {
return None;
}
// Take Number or return None
let rhs = if let Some(Token::Number(rhs)) =
token_iter.next_if(|v: &&Token| matches!(v, Token::Number(_)))
{
rhs.to_owned()
} else {
return None;
};
// Take CloseParenthesis or return None
if !token_iter.next_if_eq(&&Token::CloseParenthesis).is_some() {
return None;
}
Some(MulOp::new(lhs, rhs))
}
#[derive(Debug)]
struct DoOp {}
impl DoOp {
pub fn new() -> DoOp {
DoOp {}
}
}
fn is_do_text_token(text_token: &str) -> bool {
if text_token.ends_with("do") {
true
} else {
false
}
}
fn parse_do<'a>(token_iter: &mut Peekable<impl Iterator<Item = &'a Token>>) -> Option<DoOp> {
// Take OpenParenthesis or return None
if !token_iter.next_if_eq(&&Token::OpenParenthesis).is_some() {
return None;
}
// Take CloseParenthesis or return None
if !token_iter.next_if_eq(&&Token::CloseParenthesis).is_some() {
return None;
}
Some(DoOp::new())
}
#[derive(Debug)]
struct DontOp {}
impl DontOp {
pub fn new() -> DontOp {
DontOp {}
}
}
fn is_dont_text_token(text_token: &str) -> bool {
if text_token.ends_with("don't") {
true
} else {
false
}
}
fn parse_dont<'a>(token_iter: &mut Peekable<impl Iterator<Item = &'a Token>>) -> Option<DontOp> {
// Take OpenParenthesis or return None
if !token_iter.next_if_eq(&&Token::OpenParenthesis).is_some() {
return None;
}
// Take CloseParenthesis or return None
if !token_iter.next_if_eq(&&Token::CloseParenthesis).is_some() {
return None;
}
Some(DontOp::new())
}
#[derive(Debug)]
enum Ops {
Mul(MulOp),
Do(DoOp),
Dont(DontOp),
}
fn parse_tokens(tokens: &Vec<Token>) -> Vec<Ops> {
let mut tokens = tokens.iter().peekable(); let mut tokens = tokens.iter().peekable();
let mut muls: Vec<MulOp> = Vec::new(); let mut muls: Vec<Ops> = Vec::new();
while let Some(token) = tokens.next() { while let Some(token) = tokens.next() {
match token { match token {
Token::Text(t) => { Token::Text(t) => {
if is_mul_text_token(t) { if is_mul_text_token(t) {
// Take OpenParenthesis or continue let op = parse_mul(tokens.by_ref());
if !tokens.next_if_eq(&&Token::OpenParenthesis).is_some() { if let Some(op) = op {
continue; muls.push(Ops::Mul(op));
} }
// Take Number or continue } else if is_do_text_token(t) {
let lhs = if let Some(Token::Number(lhs)) = let op = parse_do(tokens.by_ref());
tokens.next_if(|v| matches!(v, Token::Number(_))) if let Some(op) = op {
{ muls.push(Ops::Do(op));
lhs.to_owned()
} else {
continue;
};
// Take Comma or continue
if !tokens.next_if_eq(&&Token::Comma).is_some() {
continue;
} }
// Take Number or continue } else if is_dont_text_token(t) {
let rhs = if let Some(Token::Number(rhs)) = let op = parse_dont(tokens.by_ref());
tokens.next_if(|v: &&Token| matches!(v, Token::Number(_))) if let Some(op) = op {
{ muls.push(Ops::Dont(op));
rhs.to_owned()
} else {
continue;
};
// Take CloseParenthesis or continue
if !tokens.next_if_eq(&&Token::CloseParenthesis).is_some() {
continue;
} }
muls.push(MulOp { lhs, rhs });
} }
} }
_ => (), _ => (),
@@ -149,12 +237,25 @@ fn main() -> Result<(), Box<dyn Error>> {
let muls = parse_tokens(&tokens); let muls = parse_tokens(&tokens);
//println!("{:?}", muls); //println!("{:?}", muls);
let mut accumulator: i64 = 0; let mut accumulator_part1: i64 = 0;
for mul in muls { let mut accumulator_part2: i64 = 0;
accumulator += mul.multiple::<i64>()?; let mut enabled: bool = true;
for op in muls {
match op {
Ops::Mul(mul_op) => {
let value = mul_op.multiple::<i64>()?;
accumulator_part1 += value;
if enabled {
accumulator_part2 += value;
}
}
Ops::Do(_) => enabled = true,
Ops::Dont(_) => enabled = false,
}
} }
println!("Answer: {}", accumulator); println!("Answer part 1: {}", accumulator_part1);
println!("Answer part 2: {}", accumulator_part2);
Ok(()) Ok(())
} }