day 13 clean
This commit is contained in:
parent
0c745248f0
commit
b6c3e7d97d
|
@ -1,6 +1,7 @@
|
|||
use std::fs::File;
|
||||
use std::io::{self, BufRead};
|
||||
use std::path::Path;
|
||||
use std::cmp::Ordering;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Elem {
|
||||
|
@ -13,40 +14,27 @@ use crate::Elem::{Number, List};
|
|||
// I modified my input because the only 2 digits number was 10
|
||||
// I replaced all 10s with ':' so that I can parse more easely
|
||||
|
||||
fn parse_line(line: &str, mut ind: usize) -> (Elem, usize) {
|
||||
let mut elems: Vec<Elem> = vec![];
|
||||
fn parse_line(line: &str, index: usize) -> (Elem, usize) {
|
||||
let mut list: Vec<Elem> = vec![];
|
||||
let mut len = 0;
|
||||
while ind < line.len() {
|
||||
let a = line[ind..ind+1].chars().next().unwrap();
|
||||
match a {
|
||||
'[' => { // Creer une List, la push sur le vecteur en cours
|
||||
|
||||
println!("Entering sublist from index {} to {}", ind, ind+1);
|
||||
let (a, b) = parse_line(line, ind+1);
|
||||
println!("Sublist len was {}, Left sublist, now at index {}", b, ind+b);
|
||||
ind += b;
|
||||
len += b;
|
||||
elems.push(a);
|
||||
},
|
||||
']' => {
|
||||
println!("Returning list with index {}", ind);
|
||||
return (List(elems), len + 1);
|
||||
} // Remonter le vecteur en cours de 1,
|
||||
'0'..=':' => {
|
||||
println!("Pushing {}", a as u8 - b'0');
|
||||
elems.push(Number(a as u8 - b'0')); // Creer un nombre, le push sur le vecteur en cours
|
||||
while index + len < line.len() {
|
||||
let c = line.as_bytes()[index + len];
|
||||
len += 1;
|
||||
match c {
|
||||
b'[' => {
|
||||
let (sublist, sublist_len) = parse_line(line, index + len);
|
||||
len += sublist_len;
|
||||
list.push(sublist);
|
||||
},
|
||||
b']' => return (List(list), len),
|
||||
b'0'..=b':' => list.push(Number(c - b'0')),
|
||||
_ => (),
|
||||
};
|
||||
ind += 1;
|
||||
len += 1;
|
||||
}
|
||||
unreachable!("parse_line end of function");
|
||||
//println!("unreachable !!!! UNREACHABLE");
|
||||
//(List(elems), len)
|
||||
}
|
||||
|
||||
fn compare(lhs: &Elem, rhs: &Elem) -> u8 {
|
||||
fn compare(lhs: &Elem, rhs: &Elem) -> Ordering {
|
||||
let mut lhs_v = &vec![];
|
||||
let mut rhs_v = &vec![];
|
||||
|
||||
|
@ -57,52 +45,25 @@ fn compare(lhs: &Elem, rhs: &Elem) -> u8 {
|
|||
rhs_v = a;
|
||||
}
|
||||
|
||||
println!("[ start list");
|
||||
for i in 0..lhs_v.len() {
|
||||
if i >= rhs_v.len() {
|
||||
println!("Right side ran out of items, so inputs are NOT in the right order");
|
||||
return 2;
|
||||
}
|
||||
match (&lhs_v[i], &rhs_v[i]) {
|
||||
(List(_), List(_)) => {
|
||||
let c = compare(&lhs_v[i], &rhs_v[i]);
|
||||
if c != 0 {
|
||||
return c;
|
||||
}
|
||||
},
|
||||
(List(_), Number(b)) => {
|
||||
println!("Mixed types; convert right to [{}] and retry comparison", b);
|
||||
let c = compare(&lhs_v[i], &List(vec![Number(*b)]));
|
||||
if c != 0 {
|
||||
return c;
|
||||
}
|
||||
},
|
||||
(Number(a), List(_)) => {
|
||||
println!("Mixed types; convert left to [{}] and retry comparison", a);
|
||||
let c = compare(&List(vec![Number(*a)]), &rhs_v[i]);
|
||||
if c != 0 {
|
||||
return c;
|
||||
}
|
||||
},
|
||||
(Number(a), Number(b)) => {
|
||||
println!("Compare {} vs {}", a, b);
|
||||
if a < b {
|
||||
println!("Left side is smaller, so input are in the right order");
|
||||
return 1;
|
||||
}
|
||||
if a > b {
|
||||
println!("Right side is smaller, so input are NOT in the right order");
|
||||
return 2;
|
||||
}
|
||||
return Ordering::Greater;
|
||||
}
|
||||
let compare_result = match (&lhs_v[i], &rhs_v[i]) {
|
||||
(List(_), List(_)) => compare(&lhs_v[i], &rhs_v[i]),
|
||||
(List(_), Number(b)) => compare(&lhs_v[i], &List(vec![Number(*b)])),
|
||||
(Number(a), List(_)) => compare(&List(vec![Number(*a)]), &rhs_v[i]),
|
||||
(Number(a), Number(b)) => a.cmp(b),
|
||||
};
|
||||
if compare_result != Ordering::Equal {
|
||||
return compare_result;
|
||||
}
|
||||
}
|
||||
|
||||
if rhs_v.len() > lhs_v.len() {
|
||||
println!("Left side ran out of items, so inputs are in the right order");
|
||||
return 1;
|
||||
return Ordering::Less;
|
||||
}
|
||||
println!("end list ]");
|
||||
0
|
||||
Ordering::Equal
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
@ -124,16 +85,12 @@ fn main() {
|
|||
|
||||
let mut ret = 0;
|
||||
|
||||
for i in (0..data.len() - 1).filter(|x| x % 2 == 0) {
|
||||
println!("\n{}", i / 2 + 1);
|
||||
if compare(&data[i], &data[i+1]) == 1 {
|
||||
println!("salut {}", i / 2 + 1);
|
||||
for i in (0..data.len()).filter(|x| x % 2 == 0) {
|
||||
if compare(&data[i], &data[i+1]) == Ordering::Less {
|
||||
ret += i / 2 + 1;
|
||||
}
|
||||
}
|
||||
//println!("{:#?}", data);
|
||||
println!("\n{}", ret);
|
||||
//println!("{:?}", data);
|
||||
println!("{}", ret);
|
||||
}
|
||||
|
||||
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||
|
@ -144,27 +101,18 @@ where P: AsRef<Path>, {
|
|||
|
||||
#[cfg(test)]
|
||||
fn compare_all_test_tool(lhs: &Elem, rhs: &Elem) {
|
||||
assert_eq!(compare(&lhs, &rhs), 1);
|
||||
assert_eq!(compare(&rhs, &lhs), 2);
|
||||
assert_eq!(compare(&rhs, &rhs), 0);
|
||||
assert_eq!(compare(&lhs, &lhs), 0);
|
||||
assert_eq!(compare(&lhs, &rhs), Ordering::Less);
|
||||
assert_eq!(compare(&rhs, &lhs), Ordering::Greater);
|
||||
assert_eq!(compare(&rhs, &rhs), Ordering::Equal);
|
||||
assert_eq!(compare(&lhs, &lhs), Ordering::Equal);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn parse_all_test_tool(lhs: &Elem, rhs: &Elem, lhs_s: &str, rhs_s: &str) {
|
||||
println!("LHS {:#?}", lhs);
|
||||
println!("LHS STRING {:#?}", lhs_s);
|
||||
let lhs_s = parse_line(lhs_s, 1).0;
|
||||
println!("LHS STRING PARSED {:#?}", lhs_s);
|
||||
assert_eq!(compare(&lhs, &lhs_s), 0);
|
||||
println!("LHS PARSING OK");
|
||||
|
||||
println!("RHS {:#?}", rhs);
|
||||
println!("RHS STRING {:#?}", rhs_s);
|
||||
assert_eq!(compare(&lhs, &lhs_s), Ordering::Equal);
|
||||
let rhs_s = parse_line(rhs_s, 1).0;
|
||||
println!("RHS STRING PARSED {:#?}", rhs_s);
|
||||
assert_eq!(compare(&rhs, &rhs_s), 0);
|
||||
println!("RHS PARSING OK");
|
||||
assert_eq!(compare(&rhs, &rhs_s), Ordering::Equal);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -14,37 +14,24 @@ use crate::Elem::{Number, List};
|
|||
// I modified my input because the only 2 digits number was 10
|
||||
// I replaced all 10s with ':' so that I can parse more easely
|
||||
|
||||
fn parse_line(line: &str, mut ind: usize) -> (Elem, usize) {
|
||||
let mut elems: Vec<Elem> = vec![];
|
||||
fn parse_line(line: &str, index: usize) -> (Elem, usize) {
|
||||
let mut list: Vec<Elem> = vec![];
|
||||
let mut len = 0;
|
||||
while ind < line.len() {
|
||||
let a = line[ind..ind+1].chars().next().unwrap();
|
||||
match a {
|
||||
'[' => { // Creer une List, la push sur le vecteur en cours
|
||||
|
||||
println!("Entering sublist from index {} to {}", ind, ind+1);
|
||||
let (a, b) = parse_line(line, ind+1);
|
||||
println!("Sublist len was {}, Left sublist, now at index {}", b, ind+b);
|
||||
ind += b;
|
||||
len += b;
|
||||
elems.push(a);
|
||||
},
|
||||
']' => {
|
||||
println!("Returning list with index {}", ind);
|
||||
return (List(elems), len + 1);
|
||||
} // Remonter le vecteur en cours de 1,
|
||||
'0'..=':' => {
|
||||
println!("Pushing {}", a as u8 - b'0');
|
||||
elems.push(Number(a as u8 - b'0')); // Creer un nombre, le push sur le vecteur en cours
|
||||
while index + len < line.len() {
|
||||
let c = line.as_bytes()[index + len];
|
||||
len += 1;
|
||||
match c {
|
||||
b'[' => {
|
||||
let (sublist, sublist_len) = parse_line(line, index + len);
|
||||
len += sublist_len;
|
||||
list.push(sublist);
|
||||
},
|
||||
b']' => return (List(list), len),
|
||||
b'0'..=b':' => list.push(Number(c - b'0')),
|
||||
_ => (),
|
||||
};
|
||||
ind += 1;
|
||||
len += 1;
|
||||
}
|
||||
unreachable!("parse_line end of function");
|
||||
//println!("unreachable !!!! UNREACHABLE");
|
||||
//(List(elems), len)
|
||||
}
|
||||
|
||||
fn compare(lhs: &Elem, rhs: &Elem) -> Ordering {
|
||||
|
@ -58,51 +45,24 @@ fn compare(lhs: &Elem, rhs: &Elem) -> Ordering {
|
|||
rhs_v = a;
|
||||
}
|
||||
|
||||
println!("[ start list");
|
||||
for i in 0..lhs_v.len() {
|
||||
if i >= rhs_v.len() {
|
||||
println!("Right side ran out of items, so inputs are NOT in the right order");
|
||||
return Ordering::Greater;
|
||||
}
|
||||
match (&lhs_v[i], &rhs_v[i]) {
|
||||
(List(_), List(_)) => {
|
||||
let c = compare(&lhs_v[i], &rhs_v[i]);
|
||||
if c != Ordering::Equal {
|
||||
return c;
|
||||
}
|
||||
},
|
||||
(List(_), Number(b)) => {
|
||||
println!("Mixed types; convert right to [{}] and retry comparison", b);
|
||||
let c = compare(&lhs_v[i], &List(vec![Number(*b)]));
|
||||
if c != Ordering::Equal {
|
||||
return c;
|
||||
}
|
||||
},
|
||||
(Number(a), List(_)) => {
|
||||
println!("Mixed types; convert left to [{}] and retry comparison", a);
|
||||
let c = compare(&List(vec![Number(*a)]), &rhs_v[i]);
|
||||
if c != Ordering::Equal {
|
||||
return c;
|
||||
}
|
||||
},
|
||||
(Number(a), Number(b)) => {
|
||||
println!("Compare {} vs {}", a, b);
|
||||
if a < b {
|
||||
println!("Left side is smaller, so input are in the right order");
|
||||
return Ordering::Less;
|
||||
}
|
||||
if a > b {
|
||||
println!("Right side is smaller, so input are NOT in the right order");
|
||||
return Ordering::Greater;
|
||||
}
|
||||
}
|
||||
let compare_result = match (&lhs_v[i], &rhs_v[i]) {
|
||||
(List(_), List(_)) => compare(&lhs_v[i], &rhs_v[i]),
|
||||
(List(_), Number(b)) => compare(&lhs_v[i], &List(vec![Number(*b)])),
|
||||
(Number(a), List(_)) => compare(&List(vec![Number(*a)]), &rhs_v[i]),
|
||||
(Number(a), Number(b)) => a.cmp(b),
|
||||
};
|
||||
if compare_result != Ordering::Equal {
|
||||
return compare_result;
|
||||
}
|
||||
}
|
||||
|
||||
if rhs_v.len() > lhs_v.len() {
|
||||
println!("Left side ran out of items, so inputs are in the right order");
|
||||
return Ordering::Less;
|
||||
}
|
||||
println!("end list ]");
|
||||
Ordering::Equal
|
||||
}
|
||||
|
||||
|
@ -140,7 +100,6 @@ fn main() {
|
|||
let a = a.unwrap();
|
||||
let b = b.unwrap();
|
||||
println!("{} {} {}", a, b, a * b);
|
||||
//println!("{:?}", data);
|
||||
}
|
||||
|
||||
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||
|
|
Loading…
Reference in New Issue