diff --git a/src/day7.ml b/src/day7.ml index c2709ba..0e819cc 100644 --- a/src/day7.ml +++ b/src/day7.ml @@ -1,5 +1,6 @@ open Containers +(* could have simplified this a lot by getting rid of File *) type node = | File of { name : string @@ -23,17 +24,13 @@ let get_dir_tree () = match node with | File _ -> failwith "current directory cannot be a file" | Dir dir -> - (match%pcre line with - | {|\$ ls|} -> node - | {|dir (?.*)|} -> + (match String.split ~by:" " line with + | [ "$"; "ls" ] -> node + | [ "dir"; name ] -> let new_dir = Dir { name; parent = Some (Dir dir); children = [] } in dir.children <- new_dir :: dir.children; node - | {|(?\d+) (?.*)|} -> - let new_file = File { name; size = int_of_string size } in - dir.children <- new_file :: dir.children; - node - | {|cd \.\.|} -> + | [ "$"; "cd"; ".." ] -> (match dir.parent with | Some parent -> parent | _ -> @@ -42,13 +39,17 @@ let get_dir_tree () = "ERROR:%d: cant cd .. because node '%s' has no parent" !line_num dir.name) - | {|cd (?.*)|} -> + | [ "$"; "cd"; name ] -> dir.children |> List.find (fun child -> match child with | Dir child_dir -> String.Infix.(name = child_dir.name) | _ -> false) - | line -> failwith @@ Printf.sprintf "failed to parse line '%s'" line)) + | [ size; name ] -> + let new_file = File { name; size = int_of_string size } in + dir.children <- new_file :: dir.children; + node + | _ -> failwith @@ Printf.sprintf "failed to parse line '%s'" line)) tree in tree @@ -87,8 +88,8 @@ let part2 () = () in let used_space = get_node_size collect_dir_sizes tree in - let total_space = 70000000 in - let required_space = 30000000 in + let total_space = 70_000_000 in + let required_space = 30_000_000 in let free_space = total_space - used_space in match !dir_sizes