added comment & got rid of match%pcre

This commit is contained in:
ryan 2023-09-30 23:20:47 -07:00
parent 1fdb1ebc36
commit 3d2a2f2c4d

View File

@ -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 (?<name>.*)|} ->
(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
| {|(?<size>\d+) (?<name>.*)|} ->
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 (?<name>.*)|} ->
| [ "$"; "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