Day 6 - Some cleanup

This commit is contained in:
2025-12-06 08:49:10 +00:00
parent 9a450ba973
commit 6ea12e5a8b

View File

@@ -2,8 +2,18 @@ use anyhow::Result;
use std::fs; use std::fs;
enum Op { enum Op {
Sum, Sum = '+' as isize,
Mult, Mult = '*' as isize,
}
impl Into<Op> for char {
fn into(self) -> Op {
match self {
'*' => Op::Mult,
'+' => Op::Sum,
_ => panic!("invalid char"),
}
}
} }
struct Ans { struct Ans {
@@ -12,56 +22,61 @@ struct Ans {
op: Option<Op>, op: Option<Op>,
} }
impl Ans {
fn new(mult: u64, sum: u64) -> Ans {
Ans {
op: None,
mult,
sum,
}
}
fn extend(&mut self, num: u64) {
self.mult *= num;
self.sum += num;
}
fn res(&self) -> u64 {
match self.op {
Some(Op::Sum) => self.sum,
Some(Op::Mult) => self.mult,
_ => panic!("you are doing something wrong"),
}
}
}
fn sol2(text: &str) -> Result<()> { fn sol2(text: &str) -> Result<()> {
let lines = text.split("\n").collect::<Vec<&str>>(); let lines = text.split("\n").collect::<Vec<&str>>();
// There is an empty line at the end // There is an empty line at the end
let lines_len = lines.len() - 1; let lines_len = lines.len() - 1;
let row_size = lines[0].len();
let mut grand_total = 0; let mut grand_total = 0;
let mut cur_ans = Ans::new(1, 0);
let mut cur_ans = Ans { let mut x: i64 = lines[0].len() as i64 + 1;
op: None, while x >= 0 {
mult: 1,
sum: 0,
};
for x in (0..=row_size).rev() {
// Note if we have found a no space char // Note if we have found a no space char
let mut cur_num = 0; let mut cur_num = 0;
let mut has_found = false; let mut has_found = false;
let mut has_commited = false; let mut has_commited = false;
for y in 0..lines_len { for y in 0..lines_len {
let c = lines[y].chars().nth(x).unwrap_or(' '); let c = lines[y].chars().nth(x as usize).unwrap_or(' ');
match c { match c {
' ' => { ' ' => {
if has_found { if has_found && !has_commited {
if !has_commited { // We have reached the end of the colum
has_commited = true; has_commited = true;
// We have reached the end of the colum cur_ans.extend(cur_num);
cur_ans.mult *= cur_num;
cur_ans.sum += cur_num;
}
} }
} }
'*' => { '*' | '+' => {
if !has_found { assert!(has_found);
panic!("unrechable") if !has_commited {
} cur_ans.extend(cur_num);
if has_commited {
grand_total += cur_ans.mult;
} else {
grand_total += cur_ans.mult * cur_num;
}
}
'+' => {
if !has_found {
panic!("unrechable")
}
if has_commited {
grand_total += cur_ans.sum;
} else {
grand_total += cur_ans.sum + cur_num;
} }
cur_ans.op = Some(c.into());
grand_total += cur_ans.res();
cur_ans = Ans::new(1, 0);
x -= 1;
} }
c => { c => {
// convert ascci to int // convert ascci to int
@@ -71,14 +86,7 @@ fn sol2(text: &str) -> Result<()> {
} }
} }
} }
// An empty col we reset ans x -= 1;
if !has_found {
cur_ans = Ans {
op: None,
mult: 1,
sum: 0,
};
}
} }
println!("sol2: {}", grand_total); println!("sol2: {}", grand_total);
@@ -99,38 +107,18 @@ fn main() -> Result<()> {
.fold(probs, |mut probs, (i, op)| { .fold(probs, |mut probs, (i, op)| {
if probs.len() <= i { if probs.len() <= i {
let as_num: u64 = op.parse().unwrap(); let as_num: u64 = op.parse().unwrap();
probs.push(Ans { probs.push(Ans::new(as_num, as_num));
sum: as_num,
mult: as_num,
op: None,
});
probs
} else { } else {
match op { match op {
"+" => { "+" | "*" => probs[i].op = Some(op.chars().nth(0).unwrap().into()),
probs[i].op = Some(Op::Sum); _ => probs[i].extend(op.parse().unwrap()),
}
"*" => {
probs[i].op = Some(Op::Mult);
}
_ => {
let as_num: u64 = op.parse().unwrap();
probs[i].sum += as_num;
probs[i].mult *= as_num;
}
} }
probs
} }
probs
}) })
}); });
let sol1 = problems.iter().fold(0, |s, i| { let sol1 = problems.iter().fold(0, |s, i| s + i.res());
s + match i.op {
Some(Op::Sum) => i.sum,
Some(Op::Mult) => i.mult,
None => panic!("unreachable"),
}
});
println!("sol1: {}!", sol1); println!("sol1: {}!", sol1);