Skip to content

Commit 4c7dd50

Browse files
committed
optimizations, formatting (sub 1s 2024)
1 parent af74657 commit 4c7dd50

File tree

5 files changed

+44
-48
lines changed

5 files changed

+44
-48
lines changed

2024/days/Day22.ml

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,39 +29,32 @@ module Sequence = struct
2929
map_quad (fun x -> (s / x mod 19) - 9) (19 * 19 * 19, 19 * 19, 19, 1)
3030
end
3131

32-
let none = 10
33-
34-
let change_table secret =
35-
let m = Array.make (19 * 19 * 19 * 19) none in
36-
let open Iter in
37-
iterate step secret |> take 2001
38-
|> map (fun x -> x mod 10)
39-
|> fold_map
40-
(fun (p, s) v ->
41-
let ns = Sequence.rot s (v - p) in
42-
((v, ns), (ns, v)))
43-
(0, 0)
44-
|> drop 4
45-
|> iter (fun (s, v) -> if m.(s) == none then m.(s) <- v);
46-
47-
m
48-
49-
let profit seq table =
50-
let r = table.(seq) in
51-
if r = none then 0 else r
52-
53-
let total_profit changes seq =
54-
let open Iter in
55-
of_list changes |> map (profit seq) |> sum
56-
57-
let part1 secrets =
58-
let open Iter in
59-
of_list secrets |> map (Utils.repeat step 2000) |> sum
60-
61-
let part2 secrets =
62-
let open Iter in
63-
let changes = on_list (map change_table) secrets in
64-
Sequence.all_sequences |> map (total_profit changes) |> max_exn
32+
let profit_table secrets =
33+
let n = 19 * 19 * 19 * 19 in
34+
let profit = Array.make n 0 in
35+
let seen = Array.make n 0 in
36+
37+
List.iteri
38+
(fun i secret ->
39+
let open Iter in
40+
iterate step secret |> take 2001
41+
|> map (fun x -> x mod 10)
42+
|> fold_map
43+
(fun (p, s) v ->
44+
let ns = Sequence.rot s (v - p) in
45+
((v, ns), (ns, v)))
46+
(0, 0)
47+
|> drop 4
48+
|> iter (fun (seq, v) ->
49+
if seen.(seq) != i then (
50+
profit.(seq) <- profit.(seq) + v;
51+
seen.(seq) <- i)))
52+
secrets;
53+
54+
profit
55+
56+
let part1 secrets = Iter.(of_list secrets |> map (Utils.repeat step 2000) |> sum)
57+
let part2 = Array.fold_left max 0 % profit_table
6558

6659
let day22 input =
6760
let secrets = parse input in

2024/days/Day25.ml

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@ open Utils
22

33
let parse input =
44
String.split_on_char '\n' (input ^ "\n")
5-
|> List.fold_left (fun (xs, b) l -> (
6-
if l = "" && Option.is_some b then
7-
(Option.get b :: xs, None)
8-
else
9-
let b = Option.value ~default:0 b in
10-
let b = String.fold_left (fun b c -> Int.(logor (shift_left b 1) (int_of_bool (c = '#')))) b l in
11-
(xs, Some b)
12-
)) ([], None)
13-
|> fst
14-
|> List.partition (fun b -> Int.logand b 1 = 0)
5+
|> List.fold_left
6+
(fun (xs, b) l ->
7+
if l = "" && Option.is_some b then (Option.get b :: xs, None)
8+
else
9+
let b = Option.value ~default:0 b in
10+
let b =
11+
String.fold_left
12+
(fun b c -> Int.(logor (shift_left b 1) (int_of_bool (c = '#'))))
13+
b l
14+
in
15+
(xs, Some b))
16+
([], None)
17+
|> fst
18+
|> List.partition (fun b -> Int.logand b 1 = 0)
1519

1620
let part1 locks keys =
1721
let open Iter in
1822
product (of_list locks) (of_list keys)
19-
|> filter (fun (l, k) -> Int.logand l k = 0)
20-
|> length
23+
|> filter (fun (l, k) -> Int.logand l k = 0)
24+
|> length
2125

2226
let day25 input =
23-
let (locks, keys) = parse input in
27+
let locks, keys = parse input in
2428
(string_of_int @@ part1 locks keys, "")

2024/lib/Aoc.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ let display_results res total_duration =
118118
(fun day ->
119119
print_endline @@ "[Day " ^ string_of_int day.number ^ "]";
120120
print_endline @@ " Part1: " ^ day.part1;
121-
if day.part2 <> "" then
122-
print_endline @@ " Part2: " ^ day.part2)
121+
if day.part2 <> "" then print_endline @@ " Part2: " ^ day.part2)
123122
res;
124123

125124
print_endline "";

2024/perf.data

-1.05 MB
Binary file not shown.

2024/perf.data.old

7.36 MB
Binary file not shown.

0 commit comments

Comments
 (0)