Skip to content

Instantly share code, notes, and snippets.

@pvik
Last active December 5, 2019 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pvik/b44c0f9960db6ae934f1bf06159fb3fe to your computer and use it in GitHub Desktop.
Save pvik/b44c0f9960db6ae934f1bf06159fb3fe to your computer and use it in GitHub Desktop.
let increasing (chr_lst : char list) =
List.fold_left
(fun last_int chr ->
if last_int >= 0 then
let chr_int = int_of_string (String.make 1 chr) in
if last_int <= chr_int then
chr_int
else
(-1)
else
last_int)
0
chr_lst
let consecutive (chr_lst : char list) =
List.fold_left
(fun (last_int, tmp_cnt, cnt, cnt_lst) chr ->
let chr_int = int_of_string (String.make 1 chr) in
(* Printf.printf "last_int: %d ; tmp_cnt: %d ; cnt: %d | cur_chr: %d\n"
* last_int tmp_cnt cnt chr_int ; *)
if last_int = chr_int then
if cnt <= (tmp_cnt + 1) then
(last_int, (tmp_cnt + 1), (tmp_cnt + 1), cnt_lst)
else
(last_int, (tmp_cnt + 1), cnt, cnt_lst)
else
(chr_int, 0, cnt, tmp_cnt :: cnt_lst)
)
(0, 0, 0, [])
chr_lst
let satisfy_rules (p : string) =
if (String.length p) = 6 then
let p_lst = List.init (String.length p) (String.get p) in
let is_increasing = increasing p_lst and
(_, tmp_cnt, consec_cnt, consec_cnt_lst1) = consecutive p_lst in
let consec_cnt_lst = tmp_cnt :: (consec_cnt :: consec_cnt_lst1) in
let double_grp = List.filter
(fun v -> if v = 1 then true else false)
consec_cnt_lst in
(* Printf.printf "increasing: %d consec_cnt: %d\n" is_increasing consec_cnt ; *)
(* List.iter (Printf.printf "%d ") consec_cnt_lst ; *)
if is_increasing >= 0 && (List.length double_grp) > 0 then
true
else
false
else
false
let main (start : int) (fin : int) =
let cnt = ref 0 in
for num = start to fin do
if satisfy_rules (string_of_int num) then
cnt := !cnt + 1
done ;
Printf.printf "count: %d\n" !cnt
let () = main 138241 674034
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment