diff options
Diffstat (limited to '2024')
| -rw-r--r-- | 2024/OCaml/AoC_2024.opam | 2 | ||||
| -rw-r--r-- | 2024/OCaml/lib/day_04/part_01.ml | 5 | ||||
| -rw-r--r-- | 2024/OCaml/lib/grid/cell.ml | 18 | ||||
| -rw-r--r-- | 2024/OCaml/lib/grid/cell.mli | 18 | ||||
| -rw-r--r-- | 2024/OCaml/lib/grid/grid.ml | 14 | ||||
| -rw-r--r-- | 2024/OCaml/lib/grid/grid.mli | 6 | ||||
| -rw-r--r-- | 2024/OCaml/test/day_04_part_01.ml | 30 | ||||
| -rw-r--r-- | 2024/OCaml/test/dune | 3 | ||||
| -rw-r--r-- | 2024/OCaml/test/grid.ml | 5 |
9 files changed, 87 insertions, 14 deletions
diff --git a/2024/OCaml/AoC_2024.opam b/2024/OCaml/AoC_2024.opam index 4676487..6be14e1 100644 --- a/2024/OCaml/AoC_2024.opam +++ b/2024/OCaml/AoC_2024.opam @@ -4,7 +4,7 @@ synopsis: "Advent of Code 2024" description: "Solutions to the Advent of Code puzzles for 2024" maintainer: ["D.J. O'Leary"] authors: ["D.J. O'Leary"] -license: "MIT" +license: "Unlicense" tags: ["aoc2024" "Advent of Code 2024"] homepage: "https://github.com/DJOLEARY/Advent_of_Code" doc: diff --git a/2024/OCaml/lib/day_04/part_01.ml b/2024/OCaml/lib/day_04/part_01.ml index 63c1cfd..acb3e3b 100644 --- a/2024/OCaml/lib/day_04/part_01.ml +++ b/2024/OCaml/lib/day_04/part_01.ml @@ -6,8 +6,9 @@ type direction = N | NE | E | SE | S | SW | W | NW let all_directions = [ N; NE; E; SE; S; SW; W; NW ] let needle = "XMAS" -(** [calculate_row_offset lines] is the offset used to move between the rows of the concatenated variant of [lines] as though it were a grid. - Requires: values of [lines] all have the same length *) +(** [calculate_row_offset lines] is the offset used to move between the rows of + the concatenated variant of [lines] as though it were a grid. Requires: + values of [lines] all have the same length *) let calculate_row_offset lines = match lines with [] -> 0 | h :: _ -> String.length h diff --git a/2024/OCaml/lib/grid/cell.ml b/2024/OCaml/lib/grid/cell.ml new file mode 100644 index 0000000..186e451 --- /dev/null +++ b/2024/OCaml/lib/grid/cell.ml @@ -0,0 +1,18 @@ +type t = { + value : string; + up : t option; + down : t option; + left : t option; + right : t option; +} + +let create value = { value; up = None; down = None; left = None; right = None } +let with_up up c = { c with up = Some up } +let with_down down c = { c with down = Some down } +let with_left left c = { c with left = Some left } +let with_right right c = { c with right = Some right } +let move_up co = match co with Some c -> c.up | None -> None +let move_down co = match co with Some c -> c.down | None -> None +let move_left co = match co with Some c -> c.left | None -> None +let move_right co = match co with Some c -> c.right | None -> None +let ( = ) c1 c2 = c1.value = c2.value diff --git a/2024/OCaml/lib/grid/cell.mli b/2024/OCaml/lib/grid/cell.mli new file mode 100644 index 0000000..e61e34a --- /dev/null +++ b/2024/OCaml/lib/grid/cell.mli @@ -0,0 +1,18 @@ +type t = { + value : string; + up : t option; + down : t option; + left : t option; + right : t option; +} + +val create : string -> t +val with_up : t -> t -> t +val with_down : t -> t -> t +val with_left : t -> t -> t +val with_right : t -> t -> t +val move_up : t option -> t option +val move_down : t option -> t option +val move_left : t option -> t option +val move_right : t option -> t option +val ( = ) : t -> t -> bool diff --git a/2024/OCaml/lib/grid/grid.ml b/2024/OCaml/lib/grid/grid.ml new file mode 100644 index 0000000..0d6bc4c --- /dev/null +++ b/2024/OCaml/lib/grid/grid.ml @@ -0,0 +1,14 @@ +let rec to_grid_aux tokens max_width acc = + match tokens with + | [] -> acc + | h :: t -> + let new_cell = Cell.create h |> Cell.with_left acc in + let new_acc = acc |> Cell.with_right new_cell in + to_grid_aux t max_width new_acc + +let to_grid tokens max_width : Cell.t option = + match tokens with + | [] -> None + | h :: t -> + let first_cell = Cell.create h in + Some (to_grid_aux t max_width first_cell) diff --git a/2024/OCaml/lib/grid/grid.mli b/2024/OCaml/lib/grid/grid.mli new file mode 100644 index 0000000..31c4329 --- /dev/null +++ b/2024/OCaml/lib/grid/grid.mli @@ -0,0 +1,6 @@ +val to_grid : string list -> int -> Cell.t option +(** The cell in the top left corner of the grid is returned where each element + of [tokens] is converted into rows with [width]. An empty list of [tokens] + returns None. Requires: [width] is greater than 0 **) + +val print : Cell.t -> () diff --git a/2024/OCaml/test/day_04_part_01.ml b/2024/OCaml/test/day_04_part_01.ml index 9f43df0..9550e5b 100644 --- a/2024/OCaml/test/day_04_part_01.ml +++ b/2024/OCaml/test/day_04_part_01.ml @@ -36,7 +36,8 @@ let calculate_num_of_matches_tests = "is 0 when no matches present" >:: test_calculate_num_of_matches 0 "SMMX" 1 { index = 3; character = 'X' }; "is 1 when only one match present - top to bottom" - >:: (* + >:: + (* |.|X|.| |.|M|.| |.|A|.| @@ -45,7 +46,8 @@ let calculate_num_of_matches_tests = test_calculate_num_of_matches 1 ".X..M..A..S." 3 { index = 1; character = 'X' }; "is 1 when only one match present - top to bottom edge" - >:: (* + >:: + (* |X|.|.| |M|.|.| |A|.|.| @@ -54,7 +56,8 @@ let calculate_num_of_matches_tests = test_calculate_num_of_matches 1 "X..M..A..S.." 3 { index = 0; character = 'X' }; "is 1 when only one match present - bottom to top" - >:: (* + >:: + (* |.|S|.| |.|A|.| |.|M|.| @@ -63,17 +66,20 @@ let calculate_num_of_matches_tests = test_calculate_num_of_matches 1 ".S..A..M..X." 3 { index = 10; character = 'X' }; "is 1 when only one match present - left to right" - >:: (* + >:: + (* |X|M|A|S| *) test_calculate_num_of_matches 1 "XMAS" 4 { index = 0; character = 'X' }; "is 1 when only one match present - right to left" - >:: (* + >:: + (* |S|A|M|X| *) test_calculate_num_of_matches 1 "SAMX" 4 { index = 3; character = 'X' }; "is 1 when only one match present - bottom right to top left" - >:: (* + >:: + (* |S|.|.|.| |.|A|.|.| |.|.|M|.| @@ -82,7 +88,8 @@ let calculate_num_of_matches_tests = test_calculate_num_of_matches 1 "S....A....M....X" 4 { index = 15; character = 'X' }; "is 1 when only one match present - top left to bottom right" - >:: (* + >:: + (* |X|.|.|.| |.|M|.|.| |.|.|A|.| @@ -91,7 +98,8 @@ let calculate_num_of_matches_tests = test_calculate_num_of_matches 1 "X....M....A....S" 4 { index = 0; character = 'X' }; "is 1 when only one match present - bottom left to top right" - >:: (* + >:: + (* |.|.|.|S| |.|.|A|.| |.|M|.|.| @@ -100,7 +108,8 @@ let calculate_num_of_matches_tests = test_calculate_num_of_matches 1 "...S..A..M..X..." 4 { index = 12; character = 'X' }; "is 1 when only one match present - top right to bottom left" - >:: (* + >:: + (* |.|.|.|X| |.|.|M|.| |.|A|.|.| @@ -109,7 +118,8 @@ let calculate_num_of_matches_tests = test_calculate_num_of_matches 1 "...X..M..A..S..." 4 { index = 3; character = 'X' }; "is 8 when matches present in all possible directions" - >:: (* + >:: + (* |S|.|.|S|.|.|S| |.|A|.|A|.|A|.| |.|.|M|M|M|.|.| diff --git a/2024/OCaml/test/dune b/2024/OCaml/test/dune index 8d01938..d4a5b26 100644 --- a/2024/OCaml/test/dune +++ b/2024/OCaml/test/dune @@ -3,6 +3,7 @@ day_01_part_02 day_02_part_01 day_03_part_01 - day_04_part_01) + day_04_part_01 + grid) (modes byte exe) (libraries AoC_2024 ounit2)) diff --git a/2024/OCaml/test/grid.ml b/2024/OCaml/test/grid.ml new file mode 100644 index 0000000..bd384cd --- /dev/null +++ b/2024/OCaml/test/grid.ml @@ -0,0 +1,5 @@ +open OUnit2 +open AoC_2024.Grid + +let tests = "" >::: [] +let _ = run_test_tt_main tests |
