refactor day 14 a bit
This commit is contained in:
parent
ceaebf895d
commit
724576d120
98
src/day14.ml
98
src/day14.ml
|
|
@ -10,7 +10,7 @@ type tile =
|
|||
| Path_R
|
||||
|
||||
let char_of_tile = function
|
||||
| Empty -> '_'
|
||||
| Empty -> '-'
|
||||
| Rock -> '#'
|
||||
| Sand -> 'o'
|
||||
| Start -> 'v'
|
||||
|
|
@ -73,26 +73,32 @@ let parse_grid lines start =
|
|||
grid, start
|
||||
;;
|
||||
|
||||
type is_done =
|
||||
| Continue
|
||||
| Done
|
||||
|
||||
let rec drop_sand_abyss draw_path grid start =
|
||||
let d = Vec2.(start + down) in
|
||||
let l = Vec2.(d + left) in
|
||||
let r = Vec2.(d + right) in
|
||||
let open Grid in
|
||||
match grid.%(start), grid.%(l), grid.%(d), grid.%(r) with
|
||||
| _, _, Some Empty, _ ->
|
||||
match grid.%(l), grid.%(d), grid.%(r) with
|
||||
| _, None, _ -> Done
|
||||
| _, Some Empty, _ ->
|
||||
if draw_path then grid.%(d) <- Path_D;
|
||||
drop_sand_abyss draw_path grid d
|
||||
| _, Some Empty, _, _ ->
|
||||
| Some Empty, _, _ ->
|
||||
if draw_path then grid.%(l) <- Path_L;
|
||||
drop_sand_abyss draw_path grid l
|
||||
| _, _, _, Some Empty ->
|
||||
| _, _, Some Empty ->
|
||||
if draw_path then grid.%(r) <- Path_R;
|
||||
drop_sand_abyss draw_path grid r
|
||||
| _, _, None, _ -> Error ()
|
||||
| Some Empty, _, _, _ ->
|
||||
grid.%(start) <- Sand;
|
||||
Ok ()
|
||||
| _, _, _, _ -> failwith "oh no!"
|
||||
| _, _, _ ->
|
||||
(match grid.%(start) with
|
||||
| Some Empty ->
|
||||
grid.%(start) <- Sand;
|
||||
Continue
|
||||
| _ -> failwith "oh no!")
|
||||
;;
|
||||
|
||||
let rec drop_sand_floor grid start =
|
||||
|
|
@ -100,23 +106,25 @@ let rec drop_sand_floor grid start =
|
|||
let l = Vec2.(d + left) in
|
||||
let r = Vec2.(d + right) in
|
||||
let open Grid in
|
||||
match grid.%(start), grid.%(l), grid.%(d), grid.%(r) with
|
||||
| _, _, Some Empty, _ -> drop_sand_floor grid d
|
||||
| _, Some Empty, _, _ -> drop_sand_floor grid l
|
||||
| _, _, _, Some Empty -> drop_sand_floor grid r
|
||||
| Some Empty, _, _, _ ->
|
||||
grid.%(start) <- Sand;
|
||||
Ok ()
|
||||
| Some Start, _, _, _ ->
|
||||
grid.%(start) <- Sand;
|
||||
Error ()
|
||||
| _, _, _, _ -> failwith "ooooo!"
|
||||
match grid.%(l), grid.%(d), grid.%(r) with
|
||||
| _, Some Empty, _ -> drop_sand_floor grid d
|
||||
| Some Empty, _, _ -> drop_sand_floor grid l
|
||||
| _, _, Some Empty -> drop_sand_floor grid r
|
||||
| _, _, _ ->
|
||||
(match grid.%(start) with
|
||||
| Some Empty ->
|
||||
grid.%(start) <- Sand;
|
||||
Continue
|
||||
| Some Start ->
|
||||
grid.%(start) <- Sand;
|
||||
Done
|
||||
| _ -> failwith "oh no!")
|
||||
;;
|
||||
|
||||
let rec drop_all_sand drop grid start =
|
||||
match drop grid start with
|
||||
| Ok () -> drop_all_sand drop grid start
|
||||
| Error _ -> ()
|
||||
| Continue -> drop_all_sand drop grid start
|
||||
| Done -> ()
|
||||
;;
|
||||
|
||||
let%expect_test "Day 14.1 example" =
|
||||
|
|
@ -130,17 +138,17 @@ let%expect_test "Day 14.1 example" =
|
|||
[%expect
|
||||
{|
|
||||
24 grains
|
||||
_____________v_____________
|
||||
_____________|_____________
|
||||
____________/o_____________
|
||||
___________/ooo____________
|
||||
__________/#ooo##__________
|
||||
_________/o#ooo#___________
|
||||
________/###ooo#___________
|
||||
________|__oooo#___________
|
||||
_______/o_ooooo#___________
|
||||
______/#########___________
|
||||
______|____________________ |}]
|
||||
-------------v-------------
|
||||
-------------|-------------
|
||||
------------/o-------------
|
||||
-----------/ooo------------
|
||||
----------/#ooo##----------
|
||||
---------/o#ooo#-----------
|
||||
--------/###ooo#-----------
|
||||
--------|--oooo#-----------
|
||||
-------/o-ooooo#-----------
|
||||
------/#########-----------
|
||||
------|-------------------- |}]
|
||||
;;
|
||||
|
||||
let%expect_test "Day 14.2 example" =
|
||||
|
|
@ -153,17 +161,17 @@ let%expect_test "Day 14.2 example" =
|
|||
[%expect
|
||||
{|
|
||||
93 grains
|
||||
_____________o_____________
|
||||
____________ooo____________
|
||||
___________ooooo___________
|
||||
__________ooooooo__________
|
||||
_________oo#ooo##o_________
|
||||
________ooo#ooo#ooo________
|
||||
_______oo###ooo#oooo_______
|
||||
______oooo_oooo#ooooo______
|
||||
_____oooooooooo#oooooo_____
|
||||
____ooo#########ooooooo____
|
||||
___ooooo_______ooooooooo___ |}]
|
||||
-------------o-------------
|
||||
------------ooo------------
|
||||
-----------ooooo-----------
|
||||
----------ooooooo----------
|
||||
---------oo#ooo##o---------
|
||||
--------ooo#ooo#ooo--------
|
||||
-------oo###ooo#oooo-------
|
||||
------oooo-oooo#ooooo------
|
||||
-----oooooooooo#oooooo-----
|
||||
----ooo#########ooooooo----
|
||||
---ooooo-------ooooooooo--- |}]
|
||||
;;
|
||||
|
||||
let%expect_test "Day 14.1" =
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user