Advent of Code 2018 Day 1 in OCaml: Find length of cycles when accumulating deltas
I just started Advent of Code 2018 Advent of Code 2018. This is exercise 2 of day 1exercise 2 of day 1, and I can't understand why my code is so slow (4 minutes 37 seconds, compared to 200ms for a friend who did it in Clojure with the same computer).
Basically, it accumulates integers from a provided list of ints, named input
. Whenever the current value of the accumulator is equal to one of its previous values, it is returned. We might have to loop many times over the provided list before we find such an occurrence. Here is the code:
let main () =
let rec my_slow_code lst history =
if lst == [] then my_slow_code input history
else
let current = List.hd lst + List.hd history in
if List.mem current history then current
else my_slow_code (List.tl lst) (current :: history)
in
my_slow_code input [0]
I can't understand what mistake here makes such a slow code, maybe the use of List.mem? I would appreciate any insight from OCaml practitioners. Thanks a lot !
Advent of Code OCaml
I just started Advent of Code 2018. This is exercise 2 of day 1, and I can't understand why my code is so slow (4 minutes 37 seconds, compared to 200ms for a friend who did it in Clojure with the same computer).
Basically, it accumulates integers from a provided list of ints, named input
. Whenever the current value of the accumulator is equal to one of its previous values, it is returned. We might have to loop many times over the provided list before we find such an occurrence. Here is the code:
let main () =
let rec my_slow_code lst history =
if lst == [] then my_slow_code input history
else
let current = List.hd lst + List.hd history in
if List.mem current history then current
else my_slow_code (List.tl lst) (current :: history)
in
my_slow_code input [0]
I can't understand what mistake here makes such a slow code, maybe the use of List.mem? I would appreciate any insight from OCaml practitioners. Thanks a lot !
Advent of Code 2018 Day 1 in OCaml: Find length of cycles when accumulating deltas
I just started Advent of Code 2018. This is exercise 2 of day 1, and I can't understand why my code is so slow (4 minutes 37 seconds, compared to 200ms for a friend who did it in Clojure with the same computer).
Basically, it accumulates integers from a provided list of ints, named input
. Whenever the current value of the accumulator is equal to one of its previous values, it is returned. We might have to loop many times over the provided list before we find such an occurrence. Here is the code:
let main () =
let rec my_slow_code lst history =
if lst == [] then my_slow_code input history
else
let current = List.hd lst + List.hd history in
if List.mem current history then current
else my_slow_code (List.tl lst) (current :: history)
in
my_slow_code input [0]
I can't understand what mistake here makes such a slow code, maybe the use of List.mem?
Super slow Advent of Code OCaml code
Super slow OCaml code
I just started Advent of Code 2018. This is exercise 2 of day 1, and I can't understand why my code is so slow (4 minutes 37 seconds, compared to 200ms for a friend who did it in Clojure with the same computer).
Basically, it accumulates integers from a provided list of ints, named input
. Whenever the current value of the accumulator is equal to one of its previous values, it is returned. We might have to loop many times over the provided list before we find such an occurrence. Here is the code:
let main () =
let rec my_slow_code lst history =
if lst == [] then my_slow_code input history
else
let current = List.hd lst + List.hd history in
if List.mem current history then current
else my_slow_code (List.tl lst) (current :: history)
in
my_slow_code input [0]
I can't understand what mistake here makes such a slow code, maybe the use of List.mem? I would appreciate any insight from OCaml practitioners. Thanks a lot !