Add Gilded Rose exercise

This commit is contained in:
Maiste 2023-10-18 21:10:26 +02:00
parent a204601429
commit c7bdcb974c
No known key found for this signature in database
GPG Key ID: 030C4ED753794A0E
2 changed files with 55 additions and 0 deletions

View File

@ -1,2 +1,3 @@
(library (library
(preprocess (pps ppx_deriving.show))
(name gilded_rose)) (name gilded_rose))

54
ocaml/lib/gilded_rose.ml Normal file
View File

@ -0,0 +1,54 @@
module Item = struct
type t = { name : string; sell_in : int; quality : int } [@@deriving show]
let v name sell_in quality = { name; sell_in; quality }
end
module Items = struct
type items = Item.t list [@@deriving show]
let v ?(items = []) () = items
let show items : string = show_items items
let update_quality items =
let update_quality_items ({ name; sell_in; quality } as item : Item.t) =
let quality' =
if
name <> "Aged Brie"
&& name <> "Backstage passes to a TAFKAL80ETC concert"
then
if quality > 0 then
if name <> "Sulfuras, Hand of Ragnaros" then quality - 1
else quality
else quality
else if quality < 50 then
quality + 1
+
if name = "Backstage passes to a TAFKAL80ETC concert" then
if sell_in < 11 then
if quality < 49 then
1 + if sell_in < 6 then if quality < 48 then 1 else 0 else 0
else 0
else 0
else 0
else quality
in
let sell_in' =
if name <> "Sulfuras, Hand of Ragnaros" then sell_in - 1 else sell_in
in
if sell_in' < 0 then
if name <> "Aged Brie" then
if name <> "Backstage passes to a TAFKAL80ETC concert" then
if quality' > 0 then
if name <> "Sulfuras, Hand of Ragnaros" then
{ item with sell_in = sell_in'; quality = quality' - 1 }
else { item with sell_in = sell_in'; quality = quality' }
else { item with sell_in = sell_in'; quality = quality' }
else { item with sell_in = sell_in'; quality = quality' - quality' }
else if quality' < 50 then
{ item with sell_in = sell_in'; quality = quality' + 1 }
else { item with sell_in = sell_in'; quality = quality' }
else { item with sell_in = sell_in'; quality = quality' }
in
List.map update_quality_items items
end