0 Comments

Here’s my solutions to day 2 of the Advent of Code challenge, once again using LINQ in C# and then solving in F#:

If you want to see the code. Here’s part a in C#

File.ReadAllLines("day2.txt")
.Select(s => s.Split('x'))
.Select(x => x.Select(Int32.Parse))
.Select(w => w.OrderBy(x => x).ToArray())
.Select(w => 3 * w[0] * w[1] + 2 * w[0] * w[2] + 2 * w[1] * w[2])
.Sum()

Part b in C#

File.ReadAllLines("day2.txt")
.Select(s => s.Split('x'))
.Select(x => x.Select(Int32.Parse))
.Select(w => w.OrderBy(x => x).ToArray())
.Select(w => 2 * w[0] + 2 * w[1] + w[0] * w[1] * w[2])
.Sum()

Part a in F#

File.ReadAllLines("day2.txt")
|> Seq.map (fun s -> s.Split('x') |> Seq.map int |> Seq.sort |> Seq.toArray)
|> Seq.map (fun w -> 3 * w.[0] * w.[1] + 2 * w.[0] * w.[2] + 2 * w.[1] * w.[2])
|> Seq.sum

Part b in F#

File.ReadAllLines("day2.txt")
|> Seq.map(fun s->s.Split('x') |> Seq.map int |> Seq.sort |> Seq.toArray)
|> Seq.map(fun w-> 2 * w.[0] + 2 * w.[1] + w.[0] * w.[1] * w.[2])
|> Seq.sum
Want to learn more about LINQ? Be sure to check out my Pluralsight course More Effective LINQ.
Vote on HN
comments powered by Disqus