From: Greg Burri Date: Mon, 3 Dec 2018 21:27:00 +0000 (+0100) Subject: Day 3. X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=46d3b5cbb6273c2598239a7601140102ca9bc954;p=advent_of_code_2018.git Day 3. --- diff --git a/AdventOfCode2018.fsproj b/AdventOfCode2018.fsproj index 81a24c8..66bd76c 100644 --- a/AdventOfCode2018.fsproj +++ b/AdventOfCode2018.fsproj @@ -4,9 +4,16 @@ netcoreapp2.1 + + + PreserveNewest + + + PreserveNewest + PreserveNewest diff --git a/Day01.fs b/Day01.fs index 2c75701..483a779 100644 --- a/Day01.fs +++ b/Day01.fs @@ -3,7 +3,7 @@ module AdventOfCode2018.Day01 open System let parseInput (str : string) : int[] = - str.Split ([| "\r\n"; "\r"; "\n"; "," |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int + str.Split ([| '\r'; '\n'; ','; ' ' |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int let finalFrequency : seq -> int = Seq.sum diff --git a/Day02.fs b/Day02.fs index b0c55c4..a151d77 100644 --- a/Day02.fs +++ b/Day02.fs @@ -3,7 +3,7 @@ module AdventOfCode2018.Day02 open System let parseInput (str : string) : string[] = - str.Split ([| "\r\n"; "\r"; "\n"; ","; " " |], StringSplitOptions.RemoveEmptyEntries) + str.Split ([| '\r'; '\n'; ','; ' ' |], StringSplitOptions.RemoveEmptyEntries) let containsN (n : int) (id : string) : bool = id |> Seq.map (fun c -> id |> Seq.filter ((=) c) |> Seq.length) |> Seq.contains n diff --git a/Day03.fs b/Day03.fs new file mode 100644 index 0000000..7f15456 --- /dev/null +++ b/Day03.fs @@ -0,0 +1,37 @@ +module AdventOfCode2018.Day03 + +open System +open System.Collections.Generic + +type Claim = + { + Id : int + Left : int + Top : int + Width : int + Height : int + } + with + member this.Coordinates : (int * int) list = + [ + for x in this.Left .. this.Left + this.Width - 1 do + for y in this.Top .. this.Top + this.Height - 1 -> x, y + ] + +let parseInput (str : string) : Claim[] = + str.Split ([| '\r'; '\n'; |], StringSplitOptions.RemoveEmptyEntries) + |> Array.map ( + fun claim -> + let claim' = claim.Split ([| '#'; '@'; ','; ':'; 'x'; ' ' |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int + { Id = claim'.[0]; Left = claim'.[1]; Top = claim'.[2]; Width = claim'.[3]; Height = claim'.[4] } + ) + +let overlappingSurface (claims : Claim seq) : int * int = + let occupiedSpace = Dictionary<(int*int), int> () + for c in claims do + for (x, y) in c.Coordinates do + occupiedSpace.[(x, y)] <- occupiedSpace.GetValueOrDefault ((x, y), 0) + 1 + + occupiedSpace.Values |> Seq.filter ((<) 1) |> Seq.length, // Total surface of overlapped claims. + (claims |> Seq.find (fun c -> c.Coordinates |> List.forall (fun coord -> occupiedSpace.[coord] = 1))).Id // Id of the first not overlapping claim. + \ No newline at end of file diff --git a/Program.fs b/Program.fs index 4905062..21b5ab7 100644 --- a/Program.fs +++ b/Program.fs @@ -12,10 +12,16 @@ let day02 () = let idLength = ids.[0].Length sprintf "part1 = %A, part2 = %A" (Day02.checksum ids) (Day02.findTwoStingsWithCommonChars ids (idLength - 1)) +let day03 () = + let claims = File.ReadAllText "Data/day03.input" |> Day03.parseInput + let surface, claimId = Day03.overlappingSurface claims + sprintf "part1 = %A, part2 = %A" surface claimId + let days : (unit -> string) array = [| day01 day02 + day03 |] let doDay (n : int) = diff --git a/Tests/Day03 tests.fs b/Tests/Day03 tests.fs new file mode 100644 index 0000000..8515806 --- /dev/null +++ b/Tests/Day03 tests.fs @@ -0,0 +1,30 @@ +namespace AdventOfCode2018.Tests + +open System + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2018 + +type ``Day03 tests`` (output : ITestOutputHelper) = + + [] + let ``(Part1) From web page`` () = + let input = + """#1 @ 1,3: 4x4 + #2 @ 3,1: 4x4 + #3 @ 5,5: 2x2""" + let claims = Day03.parseInput input + Day03.overlappingSurface claims |> fst =! 4 + + [] + let ``(Part2) From web page`` () = + let input = + """#1 @ 1,3: 4x4 + #2 @ 3,1: 4x4 + #3 @ 5,5: 2x2""" + let claims = Day03.parseInput input + Day03.overlappingSurface claims |> snd =! 3 + diff --git a/Tests/Tests.fsproj b/Tests/Tests.fsproj index 44bb5aa..7ed0d16 100644 --- a/Tests/Tests.fsproj +++ b/Tests/Tests.fsproj @@ -6,6 +6,7 @@ +