finish day 17 part 1
This commit is contained in:
parent
9f1499a1d3
commit
8b54ae782c
File diff suppressed because one or more lines are too long
111
src/day17.ml
111
src/day17.ml
|
|
@ -1,6 +1,14 @@
|
|||
open Containers
|
||||
|
||||
let example_lines = {|>>><<><>><<<>><>>><<<>>><<<><<<>><>><<>>|}
|
||||
let example_lines = [ ">>><<><>><<<>><>>><<<>>><<<><<<>><>><<>>" ]
|
||||
|
||||
let dir_of_char = function
|
||||
| '<' -> Vec2.left
|
||||
| '>' -> Vec2.right
|
||||
| _ -> failwith "oof"
|
||||
;;
|
||||
|
||||
let next_stream_idx stream idx = (idx + 1) mod Array.length stream
|
||||
|
||||
type shape =
|
||||
| Row
|
||||
|
|
@ -9,16 +17,8 @@ type shape =
|
|||
| Col
|
||||
| Square
|
||||
|
||||
let dir_of_char = function
|
||||
| '<' -> Vec2.left
|
||||
| '>' -> Vec2.right
|
||||
| _ -> failwith "oof"
|
||||
;;
|
||||
|
||||
let get_dir stream idx =
|
||||
let idx = idx mod Array.length stream in
|
||||
stream.(idx)
|
||||
;;
|
||||
let next_shape_idx idx = (idx + 1) mod 5
|
||||
let get_shape idx = [| Row; Plus; Angle; Col; Square |].(idx)
|
||||
|
||||
(*[{
|
||||
#
|
||||
|
|
@ -43,20 +43,20 @@ let shape_points shape pos =
|
|||
|
||||
type cave =
|
||||
{ grid : char Grid.t
|
||||
; cache : (int * int * Vec2.t list, string) Hashtbl.t
|
||||
; mutable height : int
|
||||
; mutable rocks : (shape * Vec2.t) list
|
||||
; mutable rock_idx : int
|
||||
; mutable stream_idx : int
|
||||
}
|
||||
|
||||
let init_cave max_height =
|
||||
let grid = Grid.init ~width:7 ~height:max_height (Fun.const '.') in
|
||||
{ grid; height = -1; rocks = []; stream_idx = 0 }
|
||||
{ grid; height = -1; rock_idx = 0; stream_idx = 0; cache = Hashtbl.create max_height }
|
||||
;;
|
||||
|
||||
let draw_cave cave = Grid.draw cave.grid ~rev_y:true (fun _ contents -> contents)
|
||||
|
||||
let place_shape cave shape pos =
|
||||
cave.rocks <- (shape, pos) :: cave.rocks;
|
||||
shape_points shape pos
|
||||
|> List.iter (fun point ->
|
||||
if cave.height < point.Vec2.y then cave.height <- point.y;
|
||||
|
|
@ -73,10 +73,12 @@ let can_place_shape cave shape pos =
|
|||
false
|
||||
;;
|
||||
|
||||
let drop_shape cave stream shape =
|
||||
let drop_shape cave stream =
|
||||
let shape = get_shape cave.rock_idx in
|
||||
cave.rock_idx <- next_shape_idx cave.rock_idx;
|
||||
let rec drop pos =
|
||||
let stream_pos = Vec2.(pos + get_dir stream cave.stream_idx) in
|
||||
cave.stream_idx <- cave.stream_idx + 1;
|
||||
let stream_pos = Vec2.(pos + stream.(cave.stream_idx)) in
|
||||
cave.stream_idx <- next_stream_idx stream cave.stream_idx;
|
||||
let pos = if can_place_shape cave shape stream_pos then stream_pos else pos in
|
||||
let drop_pos = Vec2.(pos + Vec2.up) in
|
||||
if can_place_shape cave shape drop_pos
|
||||
|
|
@ -86,62 +88,21 @@ let drop_shape cave stream shape =
|
|||
drop Vec2.{ x = 2; y = Int.(cave.height + 4) }
|
||||
;;
|
||||
|
||||
let%expect_test "Day 17" =
|
||||
let stream = example_lines |> Iter.of_str |> Iter.map dir_of_char |> Iter.to_array in
|
||||
let cave = init_cave 40 in
|
||||
drop_shape cave stream Row;
|
||||
drop_shape cave stream Plus;
|
||||
drop_shape cave stream Angle;
|
||||
drop_shape cave stream Col;
|
||||
drop_shape cave stream Square;
|
||||
drop_shape cave stream Row;
|
||||
drop_shape cave stream Plus;
|
||||
drop_shape cave stream Angle;
|
||||
drop_shape cave stream Col;
|
||||
drop_shape cave stream Square;
|
||||
Printf.printf "height: %i\n" (cave.height + 1);
|
||||
Printf.printf "%s" @@ draw_cave cave;
|
||||
[%expect
|
||||
{|
|
||||
height: 17
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
....#..
|
||||
....#..
|
||||
....##.
|
||||
##..##.
|
||||
######.
|
||||
.###...
|
||||
..#....
|
||||
.####..
|
||||
....##.
|
||||
....##.
|
||||
....#..
|
||||
..#.#..
|
||||
..#.#..
|
||||
#####..
|
||||
..###..
|
||||
...#...
|
||||
..####. |}]
|
||||
let part1 cave_capacity lines =
|
||||
let stream = lines |> List.hd |> Iter.of_str |> Iter.map dir_of_char |> Iter.to_array in
|
||||
let cave = init_cave cave_capacity in
|
||||
for _ = 1 to 2022 do
|
||||
drop_shape cave stream
|
||||
done;
|
||||
cave.height + 1
|
||||
;;
|
||||
|
||||
let%expect_test "Day 17.1 Example" =
|
||||
Printf.printf "height: %i\n" @@ part1 4000 example_lines;
|
||||
[%expect {| height: 3068 |}]
|
||||
;;
|
||||
|
||||
let%expect_test "Day 17.1" =
|
||||
Printf.printf "height: %i\n" @@ part1 4000 (Utils.lines_of_input 17);
|
||||
[%expect {| height: 3227 |}]
|
||||
;;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user