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 rec is_safe_adj = function
|
||||
| x :: y :: tl -> if Int.abs (x - y) > 2 then true else is_safe_adj (y :: tl)
|
||||
| _ :: [] | [] -> false
|
||||
let is_safe l =
|
||||
let order l =
|
||||
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
|
||||
let is_safe_order l =
|
||||
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
|
||||
List.filter is_safe data |> List.length
|
||||
;;
|
||||
|
||||
let second data =
|
||||
|
|
Loading…
Reference in a new issue