~turminal/aoc2020

fedca4b408b2019dff54f93ccd20734416b7f829 — Bor Grošelj Simić 3 years ago 533bd0d
day 3

List.filteri is actually not available on my system
 #MoveFastBreakThings or something
1 files changed, 34 insertions(+), 0 deletions(-)

A day_3.ml
A day_3.ml => day_3.ml +34 -0
@@ 0,0 1,34 @@
let bool_to_int b = match b with | true -> 1 | false -> 0

let rec read_input chan =
	match (try Some (input_line chan) with End_of_file -> None) with
	| Some line -> line :: read_input chan
	| None -> []

let get_path lst offset (x, y) =
	let new_lst = List.filteri (fun i v -> (i mod y) = 0) lst in
	let rec aux lst offset dx =
		match lst with
		| [] -> 0
		| str::tail ->
				let curr = (bool_to_int (str.[offset mod String.length str] = '#')) in
				(+) curr (aux tail (offset + dx) dx)
	in
	aux new_lst offset x

let _ =
	let in_chan = open_in "day_3.in" in
	let lst = read_input in_chan in
	close_in in_chan;
	let slopes = [ (1, 1); (3, 1); (5, 1); (7, 1); (1, 2) ] in

	let val1 = get_path lst 0 (List.nth slopes 1) in
	let out_chan1 = open_out "day_3_1.out" in
	Printf.fprintf out_chan1 "%d\n" val1;
	close_out out_chan1;

	let val2 = List.fold_left ( * ) 1 (List.map (get_path lst 0) slopes) in
	let out_chan2 = open_out "day_3_2.out" in
	Printf.fprintf out_chan2 "%d\n" val2;
	close_out out_chan2;
	()