Here is my solution to adventcode problem 7 using F#.
I am using the adventofcode to improve my functional programming skills. The gist of the problem is to solve the logic gates which is represented as 16 bit integer’s and these depend on other gates.
The puzzle starts of with simple problem
123 -> x
456 -> y
x AND y -> d
x OR y -> e
x LSHIFT 2 -> f
y RSHIFT 2 ->
g NOT x -> h
NOT y -> i
where the if you solve the above the code should work. But the complexity comes in when the order of the gates are not as expected and so the code would have solve the dependent ones before solving the entire solution.
One of the things that I really love about the FP code is union types and pattern matching.
That is the composite type that could hold multiple different data types. In a non-functional static language I would have ended up creating nested types for each one of these.
My solutions to the adventofcode is here https://github.com/naveensrinivasan/adventcode/
After finishing I realized that I could have used the Tree structure which could help in traversal of the tree.
which would have made this problem simpler.
Here is my solution.
The fsharp project has a official docker image https://github.com/fsprojects/docker-fsharp . The one issue with that is it is based on mono 4.0.4 which is buggy and fsharp does not work very well. The latest alpha release of the mono with which fsharp works well is 4.2.0. The 4.2.0 isn’t available in stable channels.
So I created a docker image with the latest mono from their alpha repo and using the latest fsharp from the github.
It is also available in the docker hub. You could get it by
docker pull naveensrinivasan/fsharp
I guess most of the .NET Devs read The Morning Brew, if not you should. It is a morning newspaper for the dev, so I end up reading it first thing when I go to work. I like to try and automate most of the stuff . So I thought why not write a script that reads the Morning Brew feed, filter the excluded content that I am not interested in and open the urls before I come in. The reason behind using F# is I don’t have to compile the code. I could use it with FSI.exe. Incase if the extraction logic changes and I don’t have to recompile , it is just fixing the script. The best part of writing in F# is I avoid all the ceremony. Here is the F# code, it is nothing fancy. And this code can easily be modified for other link collection sites.
let formatter = new Rss20FeedFormatter()
let sw = new StringWriter();
// Excluded Keywords
let exists (item:string) = File.ReadAllLines @"c:Usersnaveenexclude.txt" |>
Seq.exists(fun e -> item.ToLower().Contains(e.ToLower()))
formatter.Feed.Items |> Seq.nth 0 |> fun i -> i.SaveAsRss20(new XmlTextWriter(sw))
let data = HttpUtility.HtmlDecode(sw.ToString())
Seq.filter(fun i -> not (exists i.Value)) |>
Seq.map(fun i -> i.Descendants(XName.Get("a"))) |>
Seq.iter(fun x -> x |> Seq.iter(fun y ->
System.Diagnostics.Process.Start (y.Attribute(XName.Get("href")).Value) |> ignore
|ex -> printfn "Error : %s" ex.Message))
"C:Program Files (x86)Microsoft F#v4.0Fsi.exe" --quiet --exec "C:Usersnaveenmorningbrew.fsx"
I have been hacking F# lately and I am big fan of ReSharper. So why not create a few Live Templates for the things i do daily in F#.
Download R# live template for F#. I am sure there are few more things that can be part of this template. I have posted the code in MSDN Code gallery so that others can find and contribute.