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