01.sml 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. (* open IntInf; *)
  2. val newline_tokenizer = String.tokens (fn c => c = #"\n");
  3. val points_tokenizer = StringExtra.split " -> ";
  4. val comma_tokenizer = String.tokens (fn c => c = #",");
  5. val str = FileExtra.read "input";
  6. val lines = newline_tokenizer str;
  7. val points_as_str = map points_tokenizer lines;
  8. fun string_to_point str =
  9. let
  10. val parts = comma_tokenizer str;
  11. in
  12. if (length parts) = 2
  13. then
  14. let
  15. val a = hd parts;
  16. val b = hd (tl parts);
  17. val x = case Int.fromString a of
  18. SOME num => num
  19. | NONE => raise Fail "should not happen";
  20. val y = case Int.fromString b of
  21. SOME num => num
  22. | NONE => raise Fail "should not happen";
  23. in
  24. (x,y) : LineSegment.point
  25. end
  26. else
  27. raise Fail "not a point"
  28. end;
  29. fun make_segments nil = []
  30. | make_segments (points_as_str as (ps_as_strs::rest)) =
  31. let
  32. val (p1_str :: p2_str :: nil) = ps_as_strs;
  33. val p1 = string_to_point p1_str;
  34. val p2 = string_to_point p2_str;
  35. in
  36. LineSegment (p1, p2) :: (make_segments rest)
  37. end;
  38. val line_segments = make_segments points_as_str;