~soapdog/desafio-4-ocaml

4ea80180223efb3618f06ace602e0ad707ba08cb — Andre Alves Garzia 3 years ago
working version
7 files changed, 105 insertions(+), 0 deletions(-)

A .gitignore
A README.md
A caso1.txt
A caso2.txt
A desafio.ml
A jbuild
A setup.sh
A  => .gitignore +2 -0
@@ 1,2 @@
_build
.merlin
\ No newline at end of file

A  => README.md +22 -0
@@ 1,22 @@
## Desafio 04

Para [esse desafio 04](https://osprogramadores.com/desafios/d04/) você precisa de:

* [OCaml](http://ocaml.org) - A Linguagem
* [OPAM](http://opam.ocaml.org) - O gerenciador de pacotes

Depois precisa rodar o ```setup.sh``` para instalar os módulos ```core``` e ```jbuilder```

Para compilar rode:

```
$ jbuilder build desafio.exe
```

É isso mesmo, tem .exe em qualquer plataforma, mesmo Linux.

Para rodar use:

```
$ ./_build/default/desafio.exe ./caso2.txt
```

A  => caso1.txt +8 -0
@@ 1,8 @@
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 1 1 0 0 0
0 0 0 1 1 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
\ No newline at end of file

A  => caso2.txt +8 -0
@@ 1,8 @@
4 3 2 5 6 2 3 4
1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1
4 3 2 5 6 2 3 4
\ No newline at end of file

A  => desafio.ml +54 -0
@@ 1,54 @@
open Core

let tabuleiro_para_chars s =
  let rec exp i l =
    if i < 0 then l else exp (i - 1) (s.[i] :: l) in
    exp (String.length s - 1) []

let verifica_peca peca_atual peca_desejada =
  let peca_str = String.of_char peca_atual in
  let r = compare peca_str peca_desejada in
    match r with
      0 -> 1
    | _ -> 0
 

let calcula_saida entrada =
  let chars = tabuleiro_para_chars entrada in
  let peoes = List.fold ~init: 0 ~f:(fun acc p -> acc + (verifica_peca p "1")) chars in
  let bispos = List.fold ~init: 0 ~f:(fun acc p -> acc + (verifica_peca p "2")) chars in
  let cavalos = List.fold ~init: 0 ~f:(fun acc p -> acc + (verifica_peca p "3")) chars in
  let torres = List.fold ~init: 0 ~f:(fun acc p -> acc + (verifica_peca p "4")) chars in
  let rainhas = List.fold ~init: 0 ~f:(fun acc p -> acc + (verifica_peca p "5")) chars in
  let reis = List.fold ~init: 0 ~f:(fun acc p -> acc + (verifica_peca p "6")) chars in
  
    printf "peoes: %d\n" peoes;
    printf "bispos: %d\n" bispos;
    printf "cavalos: %d\n" cavalos;
    printf "torres: %d\n" torres;
    printf "rainhas: %d\n" rainhas;
    printf "reis: %d\n" reis
    

let processa file =
  let entrada = In_channel.with_file file ~f:(fun ic -> In_channel.input_all ic) in
    print_endline "Entrada:";
    print_endline entrada;
    printf "\nSaida:\n";
    calcula_saida entrada

let spec =
  let open Command.Spec in
    empty
    +> anon ("arquivo" %: string)
  
let cli =
  Command.basic
    ~summary:"Calcula desafio 04"
    ~readme:(fun () -> "Mais info em https://osprogramadores.com/desafios/d04/")
    spec
    (fun filename () -> processa filename)

let () =
  Command.run ~version:"1.0" ~build_info:"AAG" cli
(* normal exit: all channels are flushed and closed *)
\ No newline at end of file

A  => jbuild +7 -0
@@ 1,7 @@
(jbuild_version 1)

(executable
 ((name desafio)
  (libraries (core))
  (preprocess (pps (ppx_jane)))
 ))
\ No newline at end of file

A  => setup.sh +4 -0
@@ 1,4 @@
#!/bin/bash

opam update
opam install jbuilder core
\ No newline at end of file