From 24b03319e57cf244f260eaa0b96cbb6379d0a29d Mon Sep 17 00:00:00 2001 From: zmj Date: Tue, 12 Aug 2025 21:36:22 -0400 Subject: [PATCH 1/2] logic --- zmj/readme.md | 5 +++++ zmj/roman.fsx | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 zmj/readme.md create mode 100755 zmj/roman.fsx diff --git a/zmj/readme.md b/zmj/readme.md new file mode 100644 index 0000000..b81822a --- /dev/null +++ b/zmj/readme.md @@ -0,0 +1,5 @@ +# How do I run this? + +It's a script. `./roman.fsx ../input.txt` should work with the .NET SDK installed. Make sure the file is executable: `chmod +x roman.fsx`. + +If that's not working, invoke the F# REPL directly: `dotnet fsi roman.fsx ../input.txt`. \ No newline at end of file diff --git a/zmj/roman.fsx b/zmj/roman.fsx new file mode 100755 index 0000000..392ddff --- /dev/null +++ b/zmj/roman.fsx @@ -0,0 +1,18 @@ +#!/usr/bin/env -S dotnet fsi + +let numerals = + [ "M"; "CM"; "D"; "CD"; "C"; "XC"; "L"; "XL"; "X"; "IX"; "V"; "IV"; "I" ] + +let values = [ 1000; 900; 500; 400; 100; 90; 50; 40; 10; 9; 5; 4; 1 ] + +let canHasDigit n (v, d) = + if n - v >= 0 then Some(n - v, d) else None + +let digit n = + List.pick (canHasDigit n) (List.zip values numerals) + +let rec convert (n, s) = + if n = 0 then s else s + convert (digit n) + +for line in System.IO.File.ReadLines(fsi.CommandLineArgs[1]) do + printfn "%s" (convert (int line, "")) From 4f6e51bd1d84e20b56624cbdd41af5c647fa5a09 Mon Sep 17 00:00:00 2001 From: zmj Date: Wed, 13 Aug 2025 06:27:46 -0400 Subject: [PATCH 2/2] minify --- zmj/roman.fsx | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/zmj/roman.fsx b/zmj/roman.fsx index 392ddff..7b4ca8f 100755 --- a/zmj/roman.fsx +++ b/zmj/roman.fsx @@ -1,18 +1,3 @@ #!/usr/bin/env -S dotnet fsi - -let numerals = - [ "M"; "CM"; "D"; "CD"; "C"; "XC"; "L"; "XL"; "X"; "IX"; "V"; "IV"; "I" ] - -let values = [ 1000; 900; 500; 400; 100; 90; 50; 40; 10; 9; 5; 4; 1 ] - -let canHasDigit n (v, d) = - if n - v >= 0 then Some(n - v, d) else None - -let digit n = - List.pick (canHasDigit n) (List.zip values numerals) - -let rec convert (n, s) = - if n = 0 then s else s + convert (digit n) - -for line in System.IO.File.ReadLines(fsi.CommandLineArgs[1]) do - printfn "%s" (convert (int line, "")) +let rec c(n,s)=if n=0 then s else s+c(List.pick(fun(v,d)->if n-v>=0 then Some(n-v,d) else None)(List.zip [1000;900;500;400;100;90;50;40;10;9;5;4;1] ["M";"CM";"D";"CD";"C";"XC";"L";"XL";"X";"IX";"V";"IV";"I"])) +for l in System.IO.File.ReadLines fsi.CommandLineArgs[1] do printfn"%s"(c(int l,""))