Grilled Cheese

ExploreLog inSign up
Terms of UsePrivacy PolicyCommunity StandardsHelpGet the app

Grilled Cheese is a product of Village Compute

Version devBuilt at: 2026-12-03 22:42:33 EST

PhotosVideosAllPeople
LatestRanked
{
  type buttons = App of int | Mul of int | Div of int | Swp of char * string | Oct | Del
  let press screen = function
    | App d -> screen * 10 + d
    | Mul d -> screen * d
    | Div d -> screen / d
    | Swp (a, b) -> int_of_string String.(concat b (split_on_char a (string_of_int screen)))
    | Oct -> int_of_string (Printf.sprintf "%o" screen)
    | Del -> screen / 10
}

let digit = ['0' - '9']

rule buttons = parse
| eof { [] }
| ' ' | '\n' { buttons lexbuf }
| "[" (digit as d) "]" { App (int_of_char d - int_of_char '0') :: buttons lexbuf }
| "[*" (digit as d) "]" { Mul (int_of_char d - int_of_char '0') :: buttons lexbuf }
| "[/" (digit as d) "]" { Div (int_of_char d - int_of_char '0') :: buttons lexbuf }
| "[" (digit as a) "->" (digit as b) "]" { Swp (a, String.make 1 b) :: buttons lexbuf }
| "[oct]" { Oct :: buttons lexbuf }
| "[bksp]" { Del :: buttons lexbuf }

{
  let buttons = buttons (Lexing.from_channel stdin) in
  let smash screen = List.map (press screen) buttons in
  let screens =
    let rec loop n screens =
      if n = 0 then screens else
      loop (n - 1) List.(map smash screens |> concat |> sort_uniq Stdlib.compare)
    in loop 3 [0]
  in
  Printf.printf "%d\n" (List.fold_left (+) 0 screens)
}
Photo, no description
let rec set l i v =
  if i = 0 then v :: List.tl l
  else List.hd l :: set (List.tl l) (i - 1) v

let rec patlen bc =
  let s = String.(trim (replace_all ~sub:"?" ~by:" " bc)) in
  let len = String.length s in
  if len < 2 then 1 else
  let lst = List.of_seq (String.to_seq s) in
  let rec checklen n =
    if n = len then n else
    let rec checkpat pat =
      let rec check i p l =
        match p, l with
        | _, [] -> n
        | [], l -> check 0 pat l
        | x :: p, c :: l when c = ' ' || c = x -> check (i + 1) p l
        | ' ' :: _, c :: _ -> checkpat (set pat i c)
        | _ -> checklen (n + 1)
      in check 0 pat lst
    in checkpat (List.take n lst)
  in checklen 1

let () =
  let barcodes = In_channel.input_lines stdin in
  Printf.printf "%d\n%d\n"
    List.(map patlen barcodes |> fold_left (+) 0)
    (String.concat "" barcodes |> patlen)
{
  module T = Set.Make(struct type t = int * int let compare = compare end)
}

rule map trees i j = parse
| '#' { map (T.add (i, j) trees) i (j+1) lexbuf }
| '.' { map trees i (j+1) lexbuf }
| '\n' { map trees (i+1) 0 lexbuf }
| eof { trees }

{
  let ts = map T.empty 0 0 (Lexing.from_channel stdin) in
  let to_check (i, j) = T.(filter (fun (i', j') -> i' <= i && j' > j) ts |> to_list) in
  let is_square (i, j) (i', j') = T.(mem (i+j'-j, j-i'+i) ts && mem (i'+j'-j, j'-i'+i) ts) in
  let (+^) n b = n + Bool.to_int b in
  let count_squares t = List.(map (is_square t) (to_check t) |> fold_left (+^) 0) in
  Printf.printf "%d\n" List.(map count_squares (T.elements ts) |> fold_left (+) 0)
}
{
  let color = String.index "roygbv"
  let steps c i = match ((color i) - (color c) + 6) mod 6 with 0 -> 6 | d -> d
}

rule move dist cur = parse
| eof | '\n' { dist }
| _ as instr { move (dist + steps cur instr) instr lexbuf }

{
  Printf.printf "%d\n" (move 0 'r' (Lexing.from_channel stdin))
}
let sorted = In_channel.input_lines stdin |> List.map int_of_string
  |> List.sort compare |> List.fold_left
    (fun acc id ->
      match acc with
      | (pre, c) :: r when id = pre -> (id, c+1) :: r
      | r -> (id, 1) :: r) []
  |> List.sort (fun (_, c) (_, c') -> compare c' c)
  |> List.map fst

let () =
  Printf.printf "%d\n%d\n"
    List.(sorted |> take 3 |> fold_left ( * ) 1)
    List.(rev sorted |> take 3 |> fold_left ( * ) 1)
Bash: echo $(($(cat | sort | uniq -c | sort -nr | head -3 | rev | cut -d' ' -f1 | rev | tr '\n' '*')1))

OCaml :

rule ids = parse
| ['0' - '9']+ as id { int_of_string id :: ids lexbuf }
| '\n' { ids lexbuf }
| eof { [] }

{
  ids (Lexing.from_channel stdin)
  |> List.sort compare
  |> List.fold_left (fun acc id ->
    match acc with
    | (pre, c) :: r when id = pre -> (id, c+1) :: r
    | r -> (id, 1) :: r) []
  |> List.sort (fun (_, c) (_, c') -> compare c c')
  |> List.take 3
  |> List.map fst
  |> List.fold_left ( * ) 1
  |> Printf.printf "%d\n"
}