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
|
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 =
|
type shape =
|
||||||
| Row
|
| Row
|
||||||
|
|
@ -9,16 +17,8 @@ type shape =
|
||||||
| Col
|
| Col
|
||||||
| Square
|
| Square
|
||||||
|
|
||||||
let dir_of_char = function
|
let next_shape_idx idx = (idx + 1) mod 5
|
||||||
| '<' -> Vec2.left
|
let get_shape idx = [| Row; Plus; Angle; Col; Square |].(idx)
|
||||||
| '>' -> Vec2.right
|
|
||||||
| _ -> failwith "oof"
|
|
||||||
;;
|
|
||||||
|
|
||||||
let get_dir stream idx =
|
|
||||||
let idx = idx mod Array.length stream in
|
|
||||||
stream.(idx)
|
|
||||||
;;
|
|
||||||
|
|
||||||
(*[{
|
(*[{
|
||||||
#
|
#
|
||||||
|
|
@ -43,20 +43,20 @@ let shape_points shape pos =
|
||||||
|
|
||||||
type cave =
|
type cave =
|
||||||
{ grid : char Grid.t
|
{ grid : char Grid.t
|
||||||
|
; cache : (int * int * Vec2.t list, string) Hashtbl.t
|
||||||
; mutable height : int
|
; mutable height : int
|
||||||
; mutable rocks : (shape * Vec2.t) list
|
; mutable rock_idx : int
|
||||||
; mutable stream_idx : int
|
; mutable stream_idx : int
|
||||||
}
|
}
|
||||||
|
|
||||||
let init_cave max_height =
|
let init_cave max_height =
|
||||||
let grid = Grid.init ~width:7 ~height:max_height (Fun.const '.') in
|
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 draw_cave cave = Grid.draw cave.grid ~rev_y:true (fun _ contents -> contents)
|
||||||
|
|
||||||
let place_shape cave shape pos =
|
let place_shape cave shape pos =
|
||||||
cave.rocks <- (shape, pos) :: cave.rocks;
|
|
||||||
shape_points shape pos
|
shape_points shape pos
|
||||||
|> List.iter (fun point ->
|
|> List.iter (fun point ->
|
||||||
if cave.height < point.Vec2.y then cave.height <- point.y;
|
if cave.height < point.Vec2.y then cave.height <- point.y;
|
||||||
|
|
@ -73,10 +73,12 @@ let can_place_shape cave shape pos =
|
||||||
false
|
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 rec drop pos =
|
||||||
let stream_pos = Vec2.(pos + get_dir stream cave.stream_idx) in
|
let stream_pos = Vec2.(pos + stream.(cave.stream_idx)) in
|
||||||
cave.stream_idx <- cave.stream_idx + 1;
|
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 pos = if can_place_shape cave shape stream_pos then stream_pos else pos in
|
||||||
let drop_pos = Vec2.(pos + Vec2.up) in
|
let drop_pos = Vec2.(pos + Vec2.up) in
|
||||||
if can_place_shape cave shape drop_pos
|
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) }
|
drop Vec2.{ x = 2; y = Int.(cave.height + 4) }
|
||||||
;;
|
;;
|
||||||
|
|
||||||
let%expect_test "Day 17" =
|
let part1 cave_capacity lines =
|
||||||
let stream = example_lines |> Iter.of_str |> Iter.map dir_of_char |> Iter.to_array in
|
let stream = lines |> List.hd |> Iter.of_str |> Iter.map dir_of_char |> Iter.to_array in
|
||||||
let cave = init_cave 40 in
|
let cave = init_cave cave_capacity in
|
||||||
drop_shape cave stream Row;
|
for _ = 1 to 2022 do
|
||||||
drop_shape cave stream Plus;
|
drop_shape cave stream
|
||||||
drop_shape cave stream Angle;
|
done;
|
||||||
drop_shape cave stream Col;
|
cave.height + 1
|
||||||
drop_shape cave stream Square;
|
;;
|
||||||
drop_shape cave stream Row;
|
|
||||||
drop_shape cave stream Plus;
|
let%expect_test "Day 17.1 Example" =
|
||||||
drop_shape cave stream Angle;
|
Printf.printf "height: %i\n" @@ part1 4000 example_lines;
|
||||||
drop_shape cave stream Col;
|
[%expect {| height: 3068 |}]
|
||||||
drop_shape cave stream Square;
|
;;
|
||||||
Printf.printf "height: %i\n" (cave.height + 1);
|
|
||||||
Printf.printf "%s" @@ draw_cave cave;
|
let%expect_test "Day 17.1" =
|
||||||
[%expect
|
Printf.printf "height: %i\n" @@ part1 4000 (Utils.lines_of_input 17);
|
||||||
{|
|
[%expect {| height: 3227 |}]
|
||||||
height: 17
|
|
||||||
.......
|
|
||||||
.......
|
|
||||||
.......
|
|
||||||
.......
|
|
||||||
.......
|
|
||||||
.......
|
|
||||||
.......
|
|
||||||
.......
|
|
||||||
.......
|
|
||||||
.......
|
|
||||||
.......
|
|
||||||
.......
|
|
||||||
.......
|
|
||||||
.......
|
|
||||||
.......
|
|
||||||
.......
|
|
||||||
.......
|
|
||||||
.......
|
|
||||||
.......
|
|
||||||
.......
|
|
||||||
.......
|
|
||||||
.......
|
|
||||||
.......
|
|
||||||
....#..
|
|
||||||
....#..
|
|
||||||
....##.
|
|
||||||
##..##.
|
|
||||||
######.
|
|
||||||
.###...
|
|
||||||
..#....
|
|
||||||
.####..
|
|
||||||
....##.
|
|
||||||
....##.
|
|
||||||
....#..
|
|
||||||
..#.#..
|
|
||||||
..#.#..
|
|
||||||
#####..
|
|
||||||
..###..
|
|
||||||
...#...
|
|
||||||
..####. |}]
|
|
||||||
;;
|
;;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user