solve_1.ml 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. open Stdio
  2. (* open Str *)
  3. let input =
  4. let line = In_channel.input_line In_channel.stdin in
  5. match line with
  6. | None -> ""
  7. | Some x -> x
  8. ;;
  9. let expand dm =
  10. let rec aux i acc=
  11. if (i >= String.length dm) then
  12. acc
  13. else (if (i == String.length dm -1)
  14. then
  15. let id = (i/2) in
  16. let file_size = (int_of_char dm.[i] - int_of_char '0') in
  17. let new_encoding = List.init file_size (fun _ -> id) in
  18. new_encoding@acc
  19. else
  20. let id = (i/2) in
  21. let file_size = (int_of_char dm.[i] - int_of_char '0') in
  22. let empty_space = (int_of_char dm.[i+1] - int_of_char '0') in
  23. let new_encoding = (List.init empty_space (fun _ -> -1)) @ (List.init file_size (fun _ -> id)) in
  24. aux (i+2) (new_encoding@acc))
  25. in
  26. aux 0 []
  27. ;;
  28. let reverse_list =
  29. let rec aux acc = function
  30. | [] -> acc
  31. | x :: tl -> aux (x::acc) tl
  32. in
  33. aux []
  34. ;;
  35. (* To be continued *)
  36. let compact dm =
  37. let reversed = (reverse_list dm) in
  38. reversed
  39. ;;
  40. let calculate_checksum dm=
  41. let rec aux accum id = function
  42. | [] -> accum
  43. | x::tl -> (aux (id+1) ((id * x) + accum)) tl
  44. in
  45. aux 0 0 dm;;
  46. let solve = 0
  47. (* let expanded = expand input in *)
  48. (* calculate_checksum *)
  49. (* (compact expanded (reverse_compact expanded)); *)
  50. ;;
  51. let () =
  52. let dm = expand input in
  53. List.iter (printf "%d") (dm);
  54. printf "\n ----------------- \n";
  55. List.iter (printf "%d") (reverse_list dm);
  56. (* printf "%s\n" (reverse_compact (expand input)); *)
  57. (* printf "%s\n" (compact (expand input) (reverse_compact (expand input))); *)
  58. (* printf "Total: %d\n" solve;; *)