normalize points in input

this way i can print the full input in as small a space as possible!
This commit is contained in:
ryan 2023-10-31 18:06:12 -07:00
parent 1ca2a95e1d
commit 2418d2ff0d

View File

@ -4,7 +4,14 @@ type tile =
| Empty
| Rock
| Sand
[@@deriving show]
| Start
let char_of_tile = function
| Empty -> '.'
| Rock -> '#'
| Sand -> 'o'
| Start -> 'v'
;;
let parse_line line =
let open Utils.Parse in
@ -13,6 +20,33 @@ let parse_line line =
Result.get_or_failwith @@ parse_string ~consume:All vec_list line
;;
let parse_lines lines start =
let points = List.map parse_line lines in
let lx, hx, ly, hy =
points
|> List.flatten
|> List.append [ start ]
|> List.fold_left
(fun (lx, hx, ly, hy) Vec2.{ x; y } ->
let lx = min lx x in
let hx = max hx x in
let ly = min ly y in
let hy = max hy y in
lx, hx, ly, hy)
(Int.max_int, 0, Int.max_int, 0)
in
let pad_x = 2 in
let extra = Vec2.of_tuple (lx - pad_x, ly) in
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 draw_rocks grid points =
let open Grid in
let _ =
@ -25,24 +59,190 @@ let draw_rocks grid points =
;;
let%expect_test "Day 14 example" =
let grid = Grid.init ~width:20 ~height:10 (fun _ -> Empty) in
[ "8,4 -> 8,6 -> 6,6"; "13,4 -> 12,4 -> 12,9 -> 4,9" ]
|> List.iter (fun line -> parse_line line |> draw_rocks grid);
Printf.printf "%s"
@@ Grid.draw grid (function
| Empty -> '.'
| Rock -> '#'
| Sand -> 'o');
(* 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;
Printf.printf "%s" @@ Grid.draw grid char_of_tile;
[%expect
{|
....................
....................
....................
....................
........#...##......
........#...#.......
......###...#.......
............#.......
............#.......
....#########....... |}]
w 75 h 174
..........v................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
........#..#...............................................................
........#..#...............................................................
..#######..######..........................................................
..#.............#..........................................................
..#.............#..........................................................
..#.............#..........................................................
..#.............#..........................................................
..#.............#..........................................................
..#.............#..........................................................
..#.............#..........................................................
..###############..........................................................
...........................................................................
...........................................................................
..............######.......................................................
...........................................................................
...........######.######...................................................
...........................................................................
........######.######.######...............................................
...........................................................................
.....######.######.######.######...........................................
...........................................................................
..######.######.######.######.######.......................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
....................................#.#....................................
....................................#.#....................................
..................................#.#.#....................................
..................................#.#.#....................................
................................#.#.#.#....................................
................................#.#.#.#....................................
................................#.#.#.#....................................
................................#.#.#.#....................................
................................#######....................................
...........................................................................
...........................................................................
.............................#.............................................
.............................#.............................................
.........................#...#.............................................
.........................#...#...#.#.......................................
.........................#...#...#.#.......................................
.........................#.#.#...#.#.......................................
.........................#.#.#.#.#.#.......................................
.........................#.#.#.#.#.#.......................................
.........................#.#.#.#.#.#.......................................
.........................#.#.#.#.#.#.......................................
.........................###########.......................................
...........................................................................
...........................................................................
.................................#....#....................................
.................................#....#....................................
.................................#....#....................................
.........................#########....##...................................
.........................#.............#...................................
.........................#.............#...................................
.........................#.............#...................................
.........................#.............#...................................
.........................###############...................................
...........................................................................
...........................................................................
......................................#....................................
..................................#...#....................................
................................#.#...#....................................
................................#.#...#...#................................
................................#.#...#...#.#..............................
................................#.#...#.#.#.#..............................
................................#.#...#.#.#.#.#............................
................................#.#.#.#.#.#.#.#............................
................................#.#.#.#.#.#.#.#............................
................................#.#.#.#.#.#.#.#............................
................................###############............................
...........................................................................
...........................................................................
.........................................#.................................
.........................................#############.....................
...........................................................................
...........................................................................
...................................................######..................
...........................................................................
...........................................................................
................................................######.######..............
...........................................................................
...........................................................................
.............................................######.######.######..........
...........................................................................
...........................................................................
..........................................######.######.######.######......
...........................................................................
...........................................................................
.......................................######.######.######.######.######..
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................#....#..........................
...........................................######..........................
...........................................................................
...........................................................................
.......................................#.....#.............................
.......................................#.....#.............................
.......................................#.....#.............................
.......................................#.....#.............................
....................................####.....#######.......................
....................................#..............#.......................
....................................#..............#.......................
....................................#..............#.......................
....................................#..............#.......................
....................................#..............#.......................
....................................################.......................
...........................................................................
...........................................................................
...........................................................................
..................................#...#....................................
..................................#####....................................
...........................................................................
...........................................................................
..............................#....#.......................................
..............................#....#.......................................
..............................#....#.......................................
.........................######....#########...............................
.........................#.................#...............................
.........................#.................#...............................
.........................#.................#...............................
.........................#.................#...............................
.........................###################...............................
...........................................................................
...........................................................................
....................######.................................................
...........................................................................
...........................................................................
.......................######..............................................
...........................................................................
....................######.######..........................................
...........................................................................
.................######.######.######......................................
...........................................................................
...........................................................................
...........................................................................
...........#...............................................................
...........#...............................................................
...........#.....#.........................................................
...........#...#.#.#.......................................................
...........#...#.#.#.......................................................
...........#.#.#.#.#.......................................................
...........#.#.#.#.#.......................................................
...........#.#.#.#.#.......................................................
...........#.#.#.#.#.......................................................
...........#########.......................................................
...........................................................................
...........................................................................
.................######....................................................
...........................................................................
...........................................................................
..............######.######................................................
...........................................................................
...........................................................................
...........######.######.######............................................
...........................................................................
...........................................................................
........######.######.######.######........................................ |}]
;;