<TargetFramework>netcoreapp2.1</TargetFramework>\r
</PropertyGroup>\r
<ItemGroup>\r
+ <Compile Include="Day03.fs" />\r
<Compile Include="Day02.fs" />\r
<Compile Include="Day01.fs" />\r
<Compile Include="Program.fs" />\r
+ <Content Include="Data/day03.input">\r
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>\r
+ </Content>\r
+ <Content Include="Data/day02.input">\r
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>\r
+ </Content>\r
<Content Include="Data/day01.input">\r
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>\r
</Content>\r
open System\r
\r
let parseInput (str : string) : int[] =\r
- str.Split ([| "\r\n"; "\r"; "\n"; "," |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int\r
+ str.Split ([| '\r'; '\n'; ','; ' ' |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int\r
\r
let finalFrequency : seq<int> -> int =\r
Seq.sum\r
open System\r
\r
let parseInput (str : string) : string[] =\r
- str.Split ([| "\r\n"; "\r"; "\n"; ","; " " |], StringSplitOptions.RemoveEmptyEntries)\r
+ str.Split ([| '\r'; '\n'; ','; ' ' |], StringSplitOptions.RemoveEmptyEntries)\r
\r
let containsN (n : int) (id : string) : bool = \r
id |> Seq.map (fun c -> id |> Seq.filter ((=) c) |> Seq.length) |> Seq.contains n\r
--- /dev/null
+module AdventOfCode2018.Day03\r
+\r
+open System\r
+open System.Collections.Generic\r
+\r
+type Claim =\r
+ {\r
+ Id : int\r
+ Left : int\r
+ Top : int\r
+ Width : int\r
+ Height : int\r
+ }\r
+ with\r
+ member this.Coordinates : (int * int) list =\r
+ [ \r
+ for x in this.Left .. this.Left + this.Width - 1 do\r
+ for y in this.Top .. this.Top + this.Height - 1 -> x, y\r
+ ]\r
+\r
+let parseInput (str : string) : Claim[] =\r
+ str.Split ([| '\r'; '\n'; |], StringSplitOptions.RemoveEmptyEntries)\r
+ |> Array.map (\r
+ fun claim ->\r
+ let claim' = claim.Split ([| '#'; '@'; ','; ':'; 'x'; ' ' |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int\r
+ { Id = claim'.[0]; Left = claim'.[1]; Top = claim'.[2]; Width = claim'.[3]; Height = claim'.[4] }\r
+ )\r
+\r
+let overlappingSurface (claims : Claim seq) : int * int =\r
+ let occupiedSpace = Dictionary<(int*int), int> ()\r
+ for c in claims do\r
+ for (x, y) in c.Coordinates do\r
+ occupiedSpace.[(x, y)] <- occupiedSpace.GetValueOrDefault ((x, y), 0) + 1\r
+\r
+ occupiedSpace.Values |> Seq.filter ((<) 1) |> Seq.length, // Total surface of overlapped claims.\r
+ (claims |> Seq.find (fun c -> c.Coordinates |> List.forall (fun coord -> occupiedSpace.[coord] = 1))).Id // Id of the first not overlapping claim.\r
+
\ No newline at end of file
let idLength = ids.[0].Length\r
sprintf "part1 = %A, part2 = %A" (Day02.checksum ids) (Day02.findTwoStingsWithCommonChars ids (idLength - 1))\r
\r
+let day03 () =\r
+ let claims = File.ReadAllText "Data/day03.input" |> Day03.parseInput\r
+ let surface, claimId = Day03.overlappingSurface claims\r
+ sprintf "part1 = %A, part2 = %A" surface claimId\r
+\r
let days : (unit -> string) array =\r
[|\r
day01\r
day02\r
+ day03\r
|]\r
\r
let doDay (n : int) =\r
--- /dev/null
+namespace AdventOfCode2018.Tests\r
+\r
+open System\r
+\r
+open Xunit\r
+open Xunit.Abstractions\r
+open Swensen.Unquote\r
+\r
+open AdventOfCode2018\r
+\r
+type ``Day03 tests`` (output : ITestOutputHelper) =\r
+\r
+ [<Fact>]\r
+ let ``(Part1) From web page`` () =\r
+ let input = \r
+ """#1 @ 1,3: 4x4\r
+ #2 @ 3,1: 4x4\r
+ #3 @ 5,5: 2x2"""\r
+ let claims = Day03.parseInput input\r
+ Day03.overlappingSurface claims |> fst =! 4\r
+\r
+ [<Fact>]\r
+ let ``(Part2) From web page`` () =\r
+ let input = \r
+ """#1 @ 1,3: 4x4\r
+ #2 @ 3,1: 4x4\r
+ #3 @ 5,5: 2x2"""\r
+ let claims = Day03.parseInput input\r
+ Day03.overlappingSurface claims |> snd =! 3\r
+\r
<ProjectReference Include="..\AdventOfCode2018.fsproj" />\r
</ItemGroup>\r
<ItemGroup>\r
+ <Compile Include="Day03 tests.fs" />\r
<Compile Include="Day02 tests.fs" />\r
<Compile Include="Day01 tests.fs" />\r
</ItemGroup>\r