Skip to content

Instantly share code, notes, and snippets.

@joshsharp
Created December 11, 2015 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshsharp/1f3bb50680117da6be94 to your computer and use it in GitHub Desktop.
Save joshsharp/1f3bb50680117da6be94 to your computer and use it in GitHub Desktop.
Advent of code 2015, day 1 answer
let example = "" (* actual string of parentheses goes here *)
let accumulate_parens acc item =
match item with
| '(' -> acc + 1
| ')' -> acc - 1
| _ -> acc
let parse_parens x = String.fold x ~init:0
~f:accumulate_parens
let rec parse_parens_basement (lst: char List.t) (acc: int) (f: int -> char -> int) : int =
match lst with
| [] -> 0
| x :: xs ->
if acc = -1 then List.length xs else
parse_parens_basement xs (f acc x) f
let () =
let result = parse_parens example in
printf "Example length: %d\n" (String.length example);
printf "Final floor: %d\n" result;;
let () =
let result = parse_parens_basement (String.to_list example) 0 accumulate_parens in
printf "First basement pos: %d\n" (List.length (String.to_list example) - result);;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment