Skip to content

Commit

Permalink
Use letops rather than ppx_let
Browse files Browse the repository at this point in the history
  • Loading branch information
drewolson committed Jan 4, 2024
1 parent a256cad commit 660f198
Show file tree
Hide file tree
Showing 26 changed files with 114 additions and 97 deletions.
8 changes: 8 additions & 0 deletions lib/util/list.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module Syntax = struct
let ( let* ) t f = Core.List.bind t ~f
let ( and* ) = Core.List.Let_syntax.Let_syntax.both
let ( let+ ) t f = Core.List.map t ~f
let ( and+ ) = Core.List.Let_syntax.Let_syntax.both
let return = Core.List.return
end

let max_int l = Core.List.max_elt ~compare l |> Option.value_exn
let min_int l = Core.List.min_elt ~compare l |> Option.value_exn
let replicate a ~n = Core.List.init n ~f:(const a)
Expand Down
1 change: 1 addition & 0 deletions lib/util/parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Syntax = struct
( >>= ), ( >>| ), ( *> ), ( <* ), ( <|> ), ( <?> ), ( <$> )
;;

let ( let* ), ( let+ ), ( and+ ) = ( let* ), ( let+ ), ( and+ )
let ( $> ) p a = p >>| const a
let ( <$ ) a p = p >>| const a
end
Expand Down
8 changes: 8 additions & 0 deletions lib/util/sequence.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module Syntax = struct
let ( let* ) t f = Core.Sequence.bind t ~f
let ( and* ) = Core.Sequence.Let_syntax.Let_syntax.both
let ( let+ ) t f = Core.Sequence.map t ~f
let ( and+ ) = Core.Sequence.Let_syntax.Let_syntax.both
let return = Core.Sequence.Let_syntax.Let_syntax.return
end

let nats = Core.Sequence.unfold ~init:0 ~f:(fun i -> Some (i, i + 1))

let fold_result_exn s ~init ~f =
Expand Down
8 changes: 4 additions & 4 deletions lib/year2022/day04.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ type range =
}

let range_p =
let%map start = P.integer <* P.char '-'
and stop = P.integer in
let+ start = P.integer <* P.char '-'
and+ stop = P.integer in
{ start; stop }
;;

let range_pair_p =
let%map a = range_p <* P.char ','
and b = range_p in
let+ a = range_p <* P.char ','
and+ b = range_p in
a, b
;;

Expand Down
10 changes: 5 additions & 5 deletions lib/year2022/day05.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ let crate_line_p = P.sep_by1 (P.char ' ') crate_p
let crate_lines_p = P.sep_by1 P.end_of_line crate_line_p <* P.end_of_line

let move_p =
let%map n = P.string "move " *> P.integer
and from = P.string " from " *> P.integer
and to' = P.string " to " *> P.integer in
let+ n = P.string "move " *> P.integer
and+ from = P.string " from " *> P.integer
and+ to' = P.string " to " *> P.integer in
{ n; from; to' }
;;

Expand All @@ -48,8 +48,8 @@ let to_stacks crates =
;;

let instructions_p =
let%map crate_lines = crate_lines_p <* drop_line_p <* drop_line_p
and moves = moves_p in
let+ crate_lines = crate_lines_p <* drop_line_p <* drop_line_p
and+ moves = moves_p in
let crates = to_stacks crate_lines in
{ moves; crates }
;;
Expand Down
8 changes: 4 additions & 4 deletions lib/year2022/day07.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ let name_p =
;;

let cd_p =
let%map dir = P.string "$ cd " *> name_p in
let+ dir = P.string "$ cd " *> name_p in
Cd dir
;;

let ls_p = Ls <$ P.string "$ ls"

let dir_item_p =
let%map dir = P.string "dir " *> name_p in
let+ dir = P.string "dir " *> name_p in
DirItem dir
;;

let file_item_p =
let%map size = P.integer <* P.char ' '
and file = name_p in
let+ size = P.integer <* P.char ' '
and+ file = name_p in
FileItem (size, file)
;;

Expand Down
7 changes: 4 additions & 3 deletions lib/year2022/day08.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ let grid_size grid =
;;

let coords grid =
let open Util.List.Syntax in
let max_x, max_y = grid_size grid in
let%bind.List x = List.range 0 max_x
and y = List.range 0 max_y in
List.return (x, y)
let* x = List.range 0 max_x in
let+ y = List.range 0 max_y in
x, y
;;

let rays x y grid =
Expand Down
4 changes: 2 additions & 2 deletions lib/year2022/day09.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ let dir_p =
;;

let step_p =
let%map dir = dir_p <* P.char ' '
and num = P.integer in
let+ dir = dir_p <* P.char ' '
and+ num = P.integer in
dir, num
;;

Expand Down
2 changes: 1 addition & 1 deletion lib/year2022/day10.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type inst =
| Noop

let addx_p =
let%map i = P.string "addx " *> P.signed_integer in
let+ i = P.string "addx " *> P.signed_integer in
[ Noop; AddX i ]
;;

Expand Down
12 changes: 6 additions & 6 deletions lib/year2022/day11.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ let div_test_p = P.string "divisible by " *> P.integer >>| Z.of_int
let throw_p = P.string "throw to monkey " *> P.integer

let monkey_p =
let%map id = P.string "Monkey " *> P.integer <* P.string ":" <* P.end_of_line
and items = P.string " Starting items: " *> items_p <* P.end_of_line
and op = P.string " Operation: " *> op_p <* P.end_of_line
and div_test = P.string " Test: " *> div_test_p <* P.end_of_line
and if_true = P.string " If true: " *> throw_p <* P.end_of_line
and if_false = P.string " If false: " *> throw_p <* P.end_of_line in
let+ id = P.string "Monkey " *> P.integer <* P.string ":" <* P.end_of_line
and+ items = P.string " Starting items: " *> items_p <* P.end_of_line
and+ op = P.string " Operation: " *> op_p <* P.end_of_line
and+ div_test = P.string " Test: " *> div_test_p <* P.end_of_line
and+ if_true = P.string " If true: " *> throw_p <* P.end_of_line
and+ if_false = P.string " If false: " *> throw_p <* P.end_of_line in
{ id; items = List.map items ~f:Z.of_int; op; div_test; if_true; if_false }
;;

Expand Down
8 changes: 4 additions & 4 deletions lib/year2023/day02.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ let color_p =
;;

let draw_p =
let%map count = P.integer <* P.char ' '
and color = color_p in
let+ count = P.integer <* P.char ' '
and+ color = color_p in
{ count; color }
;;

let draws_p = P.sep_by1 (P.string ", ") draw_p
let round_p = P.sep_by1 (P.string "; ") draws_p

let game_p =
let%map id = P.string "Game " *> P.integer <* P.string ": "
and round = round_p in
let+ id = P.string "Game " *> P.integer <* P.string ": "
and+ round = round_p in
{ id; round }
;;

Expand Down
6 changes: 3 additions & 3 deletions lib/year2023/day04.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ type card =
let nums_p = P.sep_by1 P.spaces P.integer

let card_p =
let%map id = P.string "Card" *> P.spaces *> P.integer <* P.char ':' <* P.spaces
and winners = nums_p <* P.spaces <* P.char '|' <* P.spaces
and picks = nums_p in
let+ id = P.string "Card" *> P.spaces *> P.integer <* P.char ':' <* P.spaces
and+ winners = nums_p <* P.spaces <* P.char '|' <* P.spaces
and+ picks = nums_p in
{ id; winners = IntSet.of_list winners; picks = IntSet.of_list picks }
;;

Expand Down
14 changes: 7 additions & 7 deletions lib/year2023/day05.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ type almanac =
let seeds_p = P.string "seeds: " *> P.sep_by1 (P.char ' ') P.integer

let mapping_p =
let%map dest = P.integer <* P.char ' '
and source = P.integer <* P.char ' '
and length = P.integer in
let+ dest = P.integer <* P.char ' '
and+ source = P.integer <* P.char ' '
and+ length = P.integer in
{ source; dest; length }
;;

Expand All @@ -34,16 +34,16 @@ let name_p =
;;

let map_p =
let%map name = name_p <* P.string " map:" <* P.end_of_line
and mappings = P.sep_by1 P.end_of_line mapping_p in
let+ name = name_p <* P.string " map:" <* P.end_of_line
and+ mappings = P.sep_by1 P.end_of_line mapping_p in
{ name; mappings }
;;

let maps_p = P.sep_by1 (P.count 2 P.end_of_line) map_p

let almanac_p =
let%map seeds = seeds_p <* P.count 2 P.end_of_line
and maps = maps_p in
let+ seeds = seeds_p <* P.count 2 P.end_of_line
and+ maps = maps_p in
{ seeds; maps }
;;

Expand Down
6 changes: 3 additions & 3 deletions lib/year2023/day06.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ type race =
}

let data_p p =
let%map times = P.string "Time:" *> P.spaces *> P.sep_by1 P.spaces p <* P.end_of_line
and distances = P.string "Distance:" *> P.spaces *> P.sep_by1 P.spaces p in
let+ times = P.string "Time:" *> P.spaces *> P.sep_by1 P.spaces p <* P.end_of_line
and+ distances = P.string "Distance:" *> P.spaces *> P.sep_by1 P.spaces p in
times, distances
;;

Expand All @@ -22,7 +22,7 @@ let races_p =
;;

let race_p =
let%map times, distances = data_p P.digits in
let+ times, distances = data_p P.digits in
{ time = times |> String.concat |> Int.of_string
; distance = distances |> String.concat |> Int.of_string
}
Expand Down
4 changes: 2 additions & 2 deletions lib/year2023/day07.ml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ let card_p =
;;

let hand_p =
let%map cards = P.many1 card_p <* P.spaces
and score = P.integer in
let+ cards = P.many1 card_p <* P.spaces
and+ score = P.integer in
cards, score
;;

Expand Down
12 changes: 6 additions & 6 deletions lib/year2023/day08.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ let token_p =
;;

let pair_p =
let%map left = P.char '(' *> token_p <* P.string ", "
and right = token_p <* P.char ')' in
let+ left = P.char '(' *> token_p <* P.string ", "
and+ right = token_p <* P.char ')' in
left, right
;;

let entry_p =
let%map key = token_p <* P.string " = "
and value = pair_p in
let+ key = token_p <* P.string " = "
and+ value = pair_p in
key, value
;;

let map_p = P.sep_by1 P.end_of_line entry_p >>| StrMap.of_alist_exn

let input_p =
let%map dirs = dirs_p <* P.end_of_line
and map = map_p in
let+ dirs = dirs_p <* P.end_of_line
and+ map = map_p in
{ dirs; map }
;;

Expand Down
4 changes: 2 additions & 2 deletions lib/year2023/day12.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ let is_token = function
;;

let line_p =
let%map tokens = P.take_while is_token <* P.char ' '
and counts = P.sep_by1 (P.char ',') P.integer in
let+ tokens = P.take_while is_token <* P.char ' '
and+ counts = P.sep_by1 (P.char ',') P.integer in
String.to_list tokens, counts
;;

Expand Down
6 changes: 3 additions & 3 deletions lib/year2023/day13.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ let reflection_score' g =
result
in
let coords =
let open Sequence.Let_syntax in
let%bind x = Sequence.range 0 (Array.length g) in
let%map y = Sequence.range 0 (Array.length g.(x)) in
let open Util.Sequence.Syntax in
let* x = Sequence.range 0 (Array.length g) in
let+ y = Sequence.range 0 (Array.length g.(x)) in
x, y
in
coords
Expand Down
4 changes: 2 additions & 2 deletions lib/year2023/day15.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ let token_p =
let inst_p = P.choice [ Drop <$ P.char '-'; (P.char '=' *> P.integer >>| fun i -> Add i) ]

let op_p =
let%map label = token_p
and inst = inst_p in
let+ label = token_p
and+ inst = inst_p in
{ label; hash = hash label; inst }
;;

Expand Down
3 changes: 1 addition & 2 deletions lib/year2023/day17.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ let solve max drop grid =
let expand_steps dir init_loss coords =
coords
|> List.filter_map ~f:(fun coord ->
let%map.Option loss = Map.find grid coord in
loss, coord)
coord |> Map.find grid |> Option.map ~f:(fun loss -> loss, coord))
|> List.folding_map ~init:init_loss ~f:(fun sum (loss, coord) ->
let loss' = sum + loss in
loss', (loss', coord, dir))
Expand Down
6 changes: 3 additions & 3 deletions lib/year2023/day18.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ let hex_p =
;;

let command_p =
let%map dir = dir_p <* P.char ' '
and length = P.integer <* P.char ' '
and color = P.string "(#" *> hex_p <* P.char ')' in
let+ dir = dir_p <* P.char ' '
and+ length = P.integer <* P.char ' '
and+ color = P.string "(#" *> hex_p <* P.char ')' in
{ dir; length; color }
;;

Expand Down
Loading

0 comments on commit 660f198

Please sign in to comment.