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