fn
This commit is contained in:
parent
e101e5550e
commit
99c05dbcc2
1 changed files with 12 additions and 10 deletions
|
@ -20,17 +20,19 @@ let from input =
|
||||||
;;
|
;;
|
||||||
|
|
||||||
let first data =
|
let first data =
|
||||||
let rec is_safe_adj = function
|
let is_safe l =
|
||||||
| x :: y :: tl -> if Int.abs (x - y) > 2 then true else is_safe_adj (y :: tl)
|
let order l =
|
||||||
| _ :: [] | [] -> false
|
let sorted = List.fast_sort compare l in
|
||||||
|
List.equal (fun a b -> a = b) l sorted
|
||||||
|
|| List.equal (fun a b -> a = b) l (List.rev sorted)
|
||||||
|
in
|
||||||
|
let rec adjacent = function
|
||||||
|
| x :: y :: tl -> if Int.abs (x - y) >= 3 then true else adjacent (y :: tl)
|
||||||
|
| _ :: [] | [] -> false
|
||||||
|
in
|
||||||
|
order l && adjacent l
|
||||||
in
|
in
|
||||||
let is_safe_order l =
|
List.filter is_safe data |> List.length
|
||||||
let sorted = List.fast_sort compare l in
|
|
||||||
List.equal (fun a b -> a = b) l sorted
|
|
||||||
|| List.equal (fun a b -> a = b) l (List.rev sorted)
|
|
||||||
in
|
|
||||||
let res = List.filter (fun x -> is_safe_order x && is_safe_adj x) data in
|
|
||||||
List.length res
|
|
||||||
;;
|
;;
|
||||||
|
|
||||||
let second data =
|
let second data =
|
||||||
|
|
Loading…
Reference in a new issue