~turminal/aoc2022

d3d0bdf3a8dc9d6086abd41585bb61e47cad762d — Bor Grošelj Simić 1 year, 10 months ago 9e263d5
day 4
3 files changed, 47 insertions(+), 0 deletions(-)

A 4/lex.mll
A 4/t1.ml
A 4/t2.ml
A 4/lex.mll => 4/lex.mll +13 -0
@@ 0,0 1,13 @@
{
type token =
	| Line of (int * int) * (int * int)
  | Eof

let i = int_of_string
}

rule token = parse
	| (['0'-'9']+ as fl)'-'(['0'-'9']+ as fr)','(['0'-'9']+ as sl)'-'(['0'-'9']+ as sr)'\n' {
    Line ((i fl, i fr), (i sl, i sr))
  }
  | eof { Eof }

A 4/t1.ml => 4/t1.ml +18 -0
@@ 0,0 1,18 @@
open Lex

let rec f lexbuf =
	match Lex.token lexbuf with
	| Eof -> []
	| Line (a, b) -> (a, b) :: f lexbuf

let fully_contains ((l1, r1), (l2, r2)) = l1 <= l2 && r2 <= r1

let both_ways (a, b) = fully_contains (a, b) || fully_contains (b, a)

let () =
	stdin
		|> Lexing.from_channel
		|> f
		|> List.filter both_ways
		|> List.length
		|> Printf.printf "%d\n"

A 4/t2.ml => 4/t2.ml +16 -0
@@ 0,0 1,16 @@
open Lex

let rec f lexbuf =
	match Lex.token lexbuf with
	| Eof -> []
	| Line (a, b) -> (a, b) :: f lexbuf

let overlaps ((l1, r1), (l2, r2)) = l1 <= l2 && l2 <= r1 || l2 <= l1 && l1 <= r2

let () =
	stdin
		|> Lexing.from_channel
		|> f
		|> List.filter overlaps
		|> List.length
		|> Printf.printf "%d\n"