summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBrian Chu <brianmchu42@gmail.com>2022-12-01 21:47:36 -0800
committerBrian Chu <brianmchu42@gmail.com>2022-12-01 21:47:36 -0800
commit399d44ddfd2c5050959fcdb6e88e1cfecf55f2b6 (patch)
tree8a441882eaa29e250ce6b7425703c77779612426
parent217582a34d4c47c5b0bc6e137e6019f2485b741f (diff)
downloadAdventOfCode2022-399d44ddfd2c5050959fcdb6e88e1cfecf55f2b6.tar.gz
solution for day 2
-rw-r--r--AoC2022.fsproj1
-rw-r--r--Program.fs5
-rw-r--r--solutions/day2.fs31
3 files changed, 35 insertions, 2 deletions
diff --git a/AoC2022.fsproj b/AoC2022.fsproj
index ab87952..5a20356 100644
--- a/AoC2022.fsproj
+++ b/AoC2022.fsproj
@@ -5,6 +5,7 @@
   </PropertyGroup>
   <ItemGroup>
     <Compile Include="solutions/day1.fs" />
+    <Compile Include="solutions/day2.fs" />
     <Compile Include="Program.fs" />
   </ItemGroup>
 </Project>
\ No newline at end of file
diff --git a/Program.fs b/Program.fs
index a16ec03..f720b0c 100644
--- a/Program.fs
+++ b/Program.fs
@@ -10,5 +10,6 @@ let day, part = int args[1], int args[2]
 match (day, part) with
 | (1, 1) -> Day1.part1 () |> printf "%A\n"
 | (1, 2) -> Day1.part2 () |> printf "%A\n"
-| _ -> raise (NotImplementedYet("not implemented yet"))
-|> printf "%A\n"
\ No newline at end of file
+| (2, 1) -> Day2.part1 () |> printf "%A\n"
+| (2, 2) -> Day2.part2 () |> printfn "%A\n"
+| _ -> raise (NotImplementedYet("not implemented yet"))
\ No newline at end of file
diff --git a/solutions/day2.fs b/solutions/day2.fs
new file mode 100644
index 0000000..5822d00
--- /dev/null
+++ b/solutions/day2.fs
@@ -0,0 +1,31 @@
+namespace Solutions
+
+module Day2 =
+    open System.IO
+    let lines = File.ReadLines("inputs/day2.txt") |> Seq.map (fun x -> x[0], x[2])
+
+    let outcomes1 = Map.empty
+                        .Add(('A', 'X'), 4) // rock: 1 + tie: 3
+                        .Add(('A', 'Y'), 8) // paper: 2 + rock win: 6
+                        .Add(('A', 'Z'), 3) // scissors: 3 + rock lose: 0
+                        .Add(('B', 'X'), 1) // rock: 1 + paper lose: 0
+                        .Add(('B', 'Y'), 5) // paper: 2 + tie: 3
+                        .Add(('B', 'Z'), 9) // scissors: 3 + paper win: 6
+                        .Add(('C', 'X'), 7) // rock: 1 + scissors win: 6
+                        .Add(('C', 'Y'), 2) // paper: 2 + scissors lose: 0
+                        .Add(('C', 'Z'), 6) // scissors: 3 + tie: 3
+
+    let part1 () = lines |> Seq.map outcomes1.TryFind |> Seq.map (function | Some value -> value | None -> 0) |> Seq.sum
+
+    let outcomes2 = Map.empty
+                        .Add(('A', 'X'), 3) // lose to rock: 0 pts + 3 for scissors
+                        .Add(('A', 'Y'), 4) // draw to rock: 3 pts + 1 for rock
+                        .Add(('A', 'Z'), 8) // win to rock: 6 pts + 2 for paper
+                        .Add(('B', 'X'), 1) // lose to paper: 0 pts + 1 for rock
+                        .Add(('B', 'Y'), 5) // draw to paper: 3 pts + 2 for paper
+                        .Add(('B', 'Z'), 9) // win to paper: 6 pts + 3 for scissors
+                        .Add(('C', 'X'), 2) // lose to scissors: 0 pts + 2 for paper
+                        .Add(('C', 'Y'), 6) // draw to scissors: 3 pts + 3 for scissors
+                        .Add(('C', 'Z'), 7) // win to scissors: 6 pts + 1 for rock
+    
+    let part2 () = lines |> Seq.map outcomes2.TryFind |> Seq.map (function | Some value -> value | None -> 0) |> Seq.sum