diff options
author | Brian Chu <brianmchu42@gmail.com> | 2022-12-02 21:46:23 -0800 |
---|---|---|
committer | Brian Chu <brianmchu42@gmail.com> | 2022-12-02 21:46:23 -0800 |
commit | 778f61bc932c96c4860d69c80ef002d86b22073c (patch) | |
tree | 61984ae4e1719eb40ddec5c331e2e8f42dfb9fb7 | |
parent | 91645b267eec14b2d80da55f3df53a6e88cb011c (diff) | |
download | AdventOfCode2022-778f61bc932c96c4860d69c80ef002d86b22073c.tar.gz |
solution for day 3
-rw-r--r-- | Program.fs | 2 | ||||
-rw-r--r-- | solutions/day3.fs | 26 |
2 files changed, 28 insertions, 0 deletions
diff --git a/Program.fs b/Program.fs index e85d15f..7e7cb55 100644 --- a/Program.fs +++ b/Program.fs @@ -12,5 +12,7 @@ match (day, part) with | (1, 2) -> Day1.part2 () |> printf "%A\n" | (2, 1) -> Day2.part1 () |> printf "%A\n" | (2, 2) -> Day2.part2 () |> printf "%A\n" +| (3, 1) -> Day3.part1 () |> printf "%A\n" +| (3, 2) -> Day3.part2 () |> printf "%A\n" | (x, y) when (1 <= x && x <= 25) && (y = 1 || y = 2) -> raise (NotImplemented("not implemented yet")) | _ -> raise (NotImplemented("invalid values")) \ No newline at end of file diff --git a/solutions/day3.fs b/solutions/day3.fs new file mode 100644 index 0000000..cebfde5 --- /dev/null +++ b/solutions/day3.fs @@ -0,0 +1,26 @@ +namespace Solutions + +module Day3 = + open System.IO + + let lines = File.ReadLines("inputs/day3.txt") + let rucksacks = lines + |> Seq.map (fun x -> (Set.ofSeq x[0..x.Length / 2 - 1], Set.ofSeq x[x.Length/2..x.Length])) + let groups = lines + |> Seq.map Set.ofSeq + |> Seq.splitInto (Seq.length lines / 3) + + let prioritize ch = + match ch with + | x when 'a' <= x && x <= 'z' -> int x - int 'a' + 1 + | x when 'A' <= x && x <= 'Z' -> int x - int 'A' + 27 + | _ -> 0 + + // extract the single element from the set with maxElement + let part1 () = rucksacks + |> Seq.map ((fun (x, y) -> Set.intersect x y |> Set.maxElement) >> prioritize) + |> Seq.sum + + let part2 () = groups + |> Seq.map (Set.intersectMany >> Set.maxElement >> prioritize) + |> Seq.sum \ No newline at end of file |