implement sand dropping logic & pretty visuals
This commit is contained in:
parent
c5c9b584c0
commit
ceb0f04a78
458
src/day14.ml
458
src/day14.ml
|
|
@ -5,12 +5,18 @@ type tile =
|
|||
| Rock
|
||||
| Sand
|
||||
| Start
|
||||
| Path_D
|
||||
| Path_L
|
||||
| Path_R
|
||||
|
||||
let char_of_tile = function
|
||||
| Empty -> '.'
|
||||
| Empty -> ' '
|
||||
| Rock -> '#'
|
||||
| Sand -> 'o'
|
||||
| Start -> 'v'
|
||||
| Path_D -> '|'
|
||||
| Path_L -> '/'
|
||||
| Path_R -> '\\'
|
||||
;;
|
||||
|
||||
let parse_line line =
|
||||
|
|
@ -20,7 +26,18 @@ let parse_line line =
|
|||
Result.get_or_failwith @@ parse_string ~consume:All vec_list line
|
||||
;;
|
||||
|
||||
let parse_lines lines start =
|
||||
let draw_rocks grid points =
|
||||
let open Grid in
|
||||
let _ =
|
||||
points
|
||||
|> List.reduce (fun a b ->
|
||||
iter_region grid a b (fun pos -> grid.%(pos) <- Rock);
|
||||
b)
|
||||
in
|
||||
()
|
||||
;;
|
||||
|
||||
let parse_grid lines start =
|
||||
let points = List.map parse_line lines in
|
||||
let lx, hx, ly, hy =
|
||||
points
|
||||
|
|
@ -40,209 +57,258 @@ let parse_lines lines start =
|
|||
let width = hx - lx + (pad_x * 2) + 1 in
|
||||
let height = hy - ly + 1 in
|
||||
let start = Vec2.(start - extra) in
|
||||
let points =
|
||||
points
|
||||
|> List.map (fun points -> points |> List.map (fun point -> Vec2.(point - extra)))
|
||||
in
|
||||
points, start, width, height
|
||||
let open Grid in
|
||||
let grid = init ~width ~height (fun _ -> Empty) in
|
||||
grid.%(start) <- Start;
|
||||
points
|
||||
|> List.iter Fun.(List.map (fun point -> Vec2.(point - extra)) %> draw_rocks grid);
|
||||
grid, start
|
||||
;;
|
||||
|
||||
let draw_rocks grid points =
|
||||
let rec drop_sand grid start draw_path =
|
||||
let d = Vec2.(start + down) in
|
||||
let l = Vec2.(d + left) in
|
||||
let r = Vec2.(d + right) in
|
||||
let open Grid in
|
||||
let _ =
|
||||
points
|
||||
|> List.reduce (fun a b ->
|
||||
iter_region grid a b (fun pos -> grid.%(pos) <- Rock);
|
||||
b)
|
||||
in
|
||||
()
|
||||
match grid.%(start), grid.%(l), grid.%(d), grid.%(r) with
|
||||
| _, _, Some Empty, _ ->
|
||||
if draw_path then grid.%(d) <- Path_D;
|
||||
drop_sand grid d draw_path
|
||||
| _, Some Empty, _, _ ->
|
||||
if draw_path then grid.%(l) <- Path_L;
|
||||
drop_sand grid l draw_path
|
||||
| _, _, _, Some Empty ->
|
||||
if draw_path then grid.%(r) <- Path_R;
|
||||
drop_sand grid r draw_path
|
||||
| _, _, None, _ -> Error ()
|
||||
| Some Empty, _, _, _ ->
|
||||
grid.%(start) <- Sand;
|
||||
Ok ()
|
||||
| _, _, _, _ -> failwith "pain"
|
||||
;;
|
||||
|
||||
let rec drop_all_sand grid start =
|
||||
match drop_sand grid start false with
|
||||
| Ok () -> drop_all_sand grid start
|
||||
| Error _ -> ()
|
||||
;;
|
||||
|
||||
let%expect_test "Day 14 example" =
|
||||
let lines = [ "498,4 -> 498,6 -> 496,6"; "503,4 -> 502,4 -> 502,9 -> 494,9" ] in
|
||||
let start = Vec2.of_tuple (500, 0) in
|
||||
let grid, start = parse_grid lines start in
|
||||
let show_grid () = Printf.printf "%s" @@ Grid.draw grid char_of_tile in
|
||||
show_grid ();
|
||||
[%expect
|
||||
{|
|
||||
v
|
||||
|
||||
|
||||
|
||||
# ##
|
||||
# #
|
||||
### #
|
||||
#
|
||||
#
|
||||
######### |}];
|
||||
drop_all_sand grid start;
|
||||
let _ = drop_sand grid start true in
|
||||
show_grid ();
|
||||
[%expect
|
||||
{|
|
||||
v
|
||||
|
|
||||
/o
|
||||
/ooo
|
||||
/#ooo##
|
||||
/o#ooo#
|
||||
/###ooo#
|
||||
| oooo#
|
||||
/o ooooo#
|
||||
/######### |}]
|
||||
;;
|
||||
|
||||
let%expect_test "Day 14 example" =
|
||||
(* let lines = [ "498,4 -> 498,6 -> 496,6"; "503,4 -> 502,4 -> 502,9 -> 494,9" ] in *)
|
||||
let lines = Utils.lines_of_input 14 in
|
||||
let start = Vec2.of_tuple (500, 0) in
|
||||
let points, start, width, height = parse_lines lines start in
|
||||
let grid = Grid.init ~width ~height (fun _ -> Empty) in
|
||||
points |> List.iter @@ draw_rocks grid;
|
||||
grid.Grid.%(start) <- Start;
|
||||
Printf.printf "w %d h %d\n" width height;
|
||||
let grid, start = parse_grid lines start in
|
||||
drop_all_sand grid start;
|
||||
let _ = drop_sand grid start true in
|
||||
Printf.printf "%s" @@ Grid.draw grid char_of_tile;
|
||||
[%expect
|
||||
{|
|
||||
w 75 h 174
|
||||
..........v................................................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
........#..#...............................................................
|
||||
........#..#...............................................................
|
||||
..#######..######..........................................................
|
||||
..#.............#..........................................................
|
||||
..#.............#..........................................................
|
||||
..#.............#..........................................................
|
||||
..#.............#..........................................................
|
||||
..#.............#..........................................................
|
||||
..#.............#..........................................................
|
||||
..#.............#..........................................................
|
||||
..###############..........................................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
..............######.......................................................
|
||||
...........................................................................
|
||||
...........######.######...................................................
|
||||
...........................................................................
|
||||
........######.######.######...............................................
|
||||
...........................................................................
|
||||
.....######.######.######.######...........................................
|
||||
...........................................................................
|
||||
..######.######.######.######.######.......................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
....................................#.#....................................
|
||||
....................................#.#....................................
|
||||
..................................#.#.#....................................
|
||||
..................................#.#.#....................................
|
||||
................................#.#.#.#....................................
|
||||
................................#.#.#.#....................................
|
||||
................................#.#.#.#....................................
|
||||
................................#.#.#.#....................................
|
||||
................................#######....................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
.............................#.............................................
|
||||
.............................#.............................................
|
||||
.........................#...#.............................................
|
||||
.........................#...#...#.#.......................................
|
||||
.........................#...#...#.#.......................................
|
||||
.........................#.#.#...#.#.......................................
|
||||
.........................#.#.#.#.#.#.......................................
|
||||
.........................#.#.#.#.#.#.......................................
|
||||
.........................#.#.#.#.#.#.......................................
|
||||
.........................#.#.#.#.#.#.......................................
|
||||
.........................###########.......................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
.................................#....#....................................
|
||||
.................................#....#....................................
|
||||
.................................#....#....................................
|
||||
.........................#########....##...................................
|
||||
.........................#.............#...................................
|
||||
.........................#.............#...................................
|
||||
.........................#.............#...................................
|
||||
.........................#.............#...................................
|
||||
.........................###############...................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
......................................#....................................
|
||||
..................................#...#....................................
|
||||
................................#.#...#....................................
|
||||
................................#.#...#...#................................
|
||||
................................#.#...#...#.#..............................
|
||||
................................#.#...#.#.#.#..............................
|
||||
................................#.#...#.#.#.#.#............................
|
||||
................................#.#.#.#.#.#.#.#............................
|
||||
................................#.#.#.#.#.#.#.#............................
|
||||
................................#.#.#.#.#.#.#.#............................
|
||||
................................###############............................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
.........................................#.................................
|
||||
.........................................#############.....................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
...................................................######..................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
................................................######.######..............
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
.............................................######.######.######..........
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
..........................................######.######.######.######......
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
.......................................######.######.######.######.######..
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
...........................................#....#..........................
|
||||
...........................................######..........................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
.......................................#.....#.............................
|
||||
.......................................#.....#.............................
|
||||
.......................................#.....#.............................
|
||||
.......................................#.....#.............................
|
||||
....................................####.....#######.......................
|
||||
....................................#..............#.......................
|
||||
....................................#..............#.......................
|
||||
....................................#..............#.......................
|
||||
....................................#..............#.......................
|
||||
....................................#..............#.......................
|
||||
....................................################.......................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
..................................#...#....................................
|
||||
..................................#####....................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
..............................#....#.......................................
|
||||
..............................#....#.......................................
|
||||
..............................#....#.......................................
|
||||
.........................######....#########...............................
|
||||
.........................#.................#...............................
|
||||
.........................#.................#...............................
|
||||
.........................#.................#...............................
|
||||
.........................#.................#...............................
|
||||
.........................###################...............................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
....................######.................................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
.......................######..............................................
|
||||
...........................................................................
|
||||
....................######.######..........................................
|
||||
...........................................................................
|
||||
.................######.######.######......................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
...........#...............................................................
|
||||
...........#...............................................................
|
||||
...........#.....#.........................................................
|
||||
...........#...#.#.#.......................................................
|
||||
...........#...#.#.#.......................................................
|
||||
...........#.#.#.#.#.......................................................
|
||||
...........#.#.#.#.#.......................................................
|
||||
...........#.#.#.#.#.......................................................
|
||||
...........#.#.#.#.#.......................................................
|
||||
...........#########.......................................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
.................######....................................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
..............######.######................................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
...........######.######.######............................................
|
||||
...........................................................................
|
||||
...........................................................................
|
||||
........######.######.######.######........................................ |}]
|
||||
v
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
oo\
|
||||
oooo\
|
||||
oooooo\
|
||||
oooooooo\
|
||||
ooo#oo#ooo\
|
||||
oooo#oo#oooo\
|
||||
#######oo######\
|
||||
# oooo #o\
|
||||
# oooooo #oo\
|
||||
# oooooooo #ooo\
|
||||
# oooooooooo #oooo\
|
||||
# oooooooooooo#ooooo\
|
||||
#ooooooooooooo#oooooo\
|
||||
#ooooooooooooo#ooooooo\
|
||||
###############oooooooo\
|
||||
oooooooooo\
|
||||
oooooooooooo\
|
||||
######oooooooo\
|
||||
oooooooooo\
|
||||
###### ######oooooo\
|
||||
oooooooo\
|
||||
###### ###### ######oooo\
|
||||
oooooo\
|
||||
###### ###### ###### ######oo\
|
||||
oooo\
|
||||
###### ###### ###### ###### ######\
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
/# #
|
||||
/o# #
|
||||
/#o# #
|
||||
/o#o# #
|
||||
/#o#o# #
|
||||
|#o#o# #
|
||||
|#o#o# #
|
||||
|#o#o# #
|
||||
|#######
|
||||
|
|
||||
oo\
|
||||
#ooo\
|
||||
#oooo\
|
||||
# #ooooo\
|
||||
# #ooo#o#\
|
||||
# #ooo#o#|
|
||||
# # #ooo#o#|
|
||||
# # #o#o#o#|
|
||||
# # #o#o#o#|
|
||||
# # #o#o#o#|
|
||||
# # #o#o#o#|
|
||||
###########|
|
||||
oo\
|
||||
oooo\
|
||||
#oooo#\
|
||||
#oooo#|
|
||||
#oooo#|
|
||||
#########oooo##\
|
||||
# oooooo#|
|
||||
# ooooooo#|
|
||||
# oooooooo#|
|
||||
# ooooooooo#|
|
||||
###############|
|
||||
|
|
||||
|
|
||||
#oo\
|
||||
# #ooo\
|
||||
# # #oooo\
|
||||
# # #ooo#o\
|
||||
# # #ooo#o#\
|
||||
# # #o#o#o#o\
|
||||
# # #o#o#o#o#\
|
||||
# # # #o#o#o#o#|
|
||||
# # # #o#o#o#o#o\
|
||||
# # # #o#o#o#o#oo\
|
||||
###############ooo\
|
||||
ooooo\
|
||||
ooooooo\
|
||||
# ooooooooo\
|
||||
#############\
|
||||
oo\
|
||||
oooo\
|
||||
######\
|
||||
/o
|
||||
/ooo
|
||||
######/######
|
||||
/o
|
||||
/ooo
|
||||
######/###### ######
|
||||
/o
|
||||
/ooo
|
||||
######/###### ###### ######
|
||||
/o
|
||||
/ooo
|
||||
######/###### ###### ###### ######
|
||||
|
|
||||
|
|
||||
/o
|
||||
/ooo
|
||||
/#oooo#
|
||||
/o######
|
||||
/ooo
|
||||
/ooooo
|
||||
/#ooooo#
|
||||
|#ooooo#
|
||||
/o#ooooo#
|
||||
/oo#ooooo#
|
||||
/####ooooo#######
|
||||
|# ooooooo #
|
||||
|# ooooooooo #
|
||||
|#ooooooooooo #
|
||||
|#oooooooooooo #
|
||||
|#ooooooooooooo #
|
||||
|################
|
||||
/o
|
||||
/ooo
|
||||
/ooooo
|
||||
/oo#ooo#
|
||||
/ooo#####
|
||||
/ooooo
|
||||
/ooooooo
|
||||
/oo#oooo#o
|
||||
/ooo#oooo#oo
|
||||
/oooo#oooo#ooo
|
||||
/######oooo#########
|
||||
|# oooooo #
|
||||
|# oooooooo #
|
||||
|# oooooooooo #
|
||||
/o# oooooooooooo #
|
||||
/oo###################
|
||||
/oooo
|
||||
/oooooo
|
||||
/######oo
|
||||
| oooo
|
||||
| oooooo
|
||||
| ######oo
|
||||
| oooo
|
||||
/o###### ######
|
||||
/ooo
|
||||
/###### ###### ######
|
||||
|
|
||||
|
|
||||
|
|
||||
# oo\
|
||||
# oooo\
|
||||
# oooo#o\
|
||||
#ooo#o#o#\
|
||||
#ooo#o#o#|
|
||||
#o#o#o#o#|
|
||||
#o#o#o#o#|
|
||||
#o#o#o#o#|
|
||||
#o#o#o#o#|
|
||||
#########|
|
||||
oo\
|
||||
oooo\
|
||||
######\
|
||||
/o
|
||||
/ooo
|
||||
######/######
|
||||
/o
|
||||
/ooo
|
||||
######/###### ######
|
||||
/o
|
||||
/ooo
|
||||
######/###### ###### ###### |}]
|
||||
;;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user