not passing around count any more

master
Dustin Swan 4 years ago
parent cd90cf840b
commit d8bd75f016

@ -29,17 +29,17 @@ let read_cell map x y = (* return Some char at these coordinates, or None if bel
then Some (List.nth (List.nth map x) (wrapped_y y map))
else None
let rec go map position slope count = (* move across the map, counting trees *)
let rec go map position slope = (* move across the map, counting trees *)
let (x, y) = position in
let (dy, dx) = slope in (* suddenly regretting having x be the vertical axis.. *)
match read_cell map x y with
| None -> count (* off the map. stop counting *)
| Some '#' -> go map (x+dx, y+dy) slope (count+1)
| Some a -> go map (x+dx, y+dy) slope count
| None -> 0 (* off the map! stop counting *)
| Some '#' -> 1 + go map (x+dx, y+dy) slope
| Some a -> go map (x+dx, y+dy) slope
let map = read_file "day3.txt" |> List.map explode
let slopes = [(1, 1); (3, 1); (5, 1); (7, 1); (1, 2)]
let tree_counts = slopes |> List.map (fun slope -> go map (0, 0) slope 0);;
let tree_counts = slopes |> List.map (fun slope -> go map (0, 0) slope);;
List.iter (fun count -> Printf.printf "Number of trees: %i\n" count) tree_counts;;

Loading…
Cancel
Save