Last active
December 29, 2016 17:25
-
-
Save b4284/9b20bce8dbb6cb13d5915e5791dc22b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (* To build: corebuild digit5.native *) | |
| (* To test-run: seq 10000 99999 |./digit5.native *) | |
| open Core.Std | |
| type t = int Char.Map.t | |
| let empty = Char.Map.empty | |
| let rec read_loop proc = | |
| match In_channel.input_line In_channel.stdin with | |
| | Some str -> proc str; read_loop proc | |
| | None -> () | |
| ;; | |
| let look_for_pair str = | |
| let rec f m l pair_flag = | |
| if List.is_empty l then | |
| pair_flag | |
| else | |
| let h = List.hd_exn l in | |
| match Map.find m h with | |
| | Some n -> | |
| (match n with | |
| | 2 -> false | |
| | 1 -> if pair_flag | |
| then false | |
| else f (Map.add m ~key:h ~data:2) | |
| (List.tl_exn l) true | |
| | _ -> false) | |
| | None -> f (Map.add m ~key:h ~data:1) (List.tl_exn l) false | |
| in f Char.Map.empty (String.to_list str) false | |
| ;; | |
| let lfp_front_end str = | |
| if look_for_pair str | |
| then printf "%s: good\n%!" str | |
| else printf "%s: bad\n%!" str | |
| ;; | |
| read_loop lfp_front_end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment