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