From d0bef9e654241fb08bd1c3ef391b0d256188a0a2 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Thu, 8 Dec 2022 00:29:44 +0100 Subject: [PATCH] Improved match statement to make it more readable and slightly faster --- 2022/src/bin/day7.rs | 49 +++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/2022/src/bin/day7.rs b/2022/src/bin/day7.rs index afc3232..af8a345 100644 --- a/2022/src/bin/day7.rs +++ b/2022/src/bin/day7.rs @@ -67,42 +67,27 @@ mod implementation { input .lines() - .flat_map(|line| line.split_once(" ")) - .for_each(|(typ, rest)| { - match typ { - "$" => { - // There are two cmds: ls, cd ${dir}, we only care about dir - if let Some((_, dir)) = rest.split_once(" ") { - match dir { - "/" => current = &mut root as *mut Node, - ".." => { - unsafe { - let parent = (*current).parent; - if parent == std::ptr::null_mut() { - panic!("Node has no parent") - } - current = parent; - } - } - name => { - unsafe { - current = (*current).get_directory(name).unwrap(); - } - }, - }; + .map(|line| line.rsplit_once(" ").unwrap()) + .for_each(|split| { + match split { + ("$ cd", "/") => current = &mut root as *mut Node, + ("$ cd", "..") => unsafe { + let parent = (*current).parent; + if parent == std::ptr::null_mut() { + panic!("Node has no parent") } + current = parent; }, - "dir" => { - unsafe { - (*current).add_directory(rest.to_owned()) - } + ("$ cd", name) => unsafe { + current = (*current).get_directory(name).unwrap(); }, - size => { - unsafe { - (*current).add_file(size.parse().unwrap()) - } + ("$", "ls") => {}, + ("dir", name) => unsafe { + (*current).add_directory(name.to_owned()) + }, + (size, _name) => unsafe { + (*current).add_file(size.parse().unwrap()) }, - } });