From: Ummon Date: Wed, 6 Dec 2017 07:53:26 +0000 (+0100) Subject: Day 6 X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=aea2fca8acb6c7a392e646002bfc30b18665b50a;p=advent_of_code_2017.git Day 6 --- diff --git a/AdventOfCode2017/AdventOfCode2017.fsproj b/AdventOfCode2017/AdventOfCode2017.fsproj index 5a9b3ef..0af4157 100644 --- a/AdventOfCode2017/AdventOfCode2017.fsproj +++ b/AdventOfCode2017/AdventOfCode2017.fsproj @@ -25,7 +25,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - 5 + 6 pdbonly @@ -61,6 +61,7 @@ + @@ -75,6 +76,9 @@ PreserveNewest + + PreserveNewest + diff --git a/AdventOfCode2017/Day6.fs b/AdventOfCode2017/Day6.fs new file mode 100644 index 0000000..85fac4c --- /dev/null +++ b/AdventOfCode2017/Day6.fs @@ -0,0 +1,25 @@ +module AdventOfCode2017.Day6 + +open System + +type Blocks = int[] + +let parseInput (str : string) : int[] = + str.Split ([| '\r'; '\t'; ' ' |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int + +let nbRedistribution (blocks : Blocks) = + let rec next (previous : Blocks list) = + let blocks = List.head previous |> Array.copy + let i, max = blocks |> Array.indexed |> Array.maxBy snd + blocks.[i] <- 0 + for offset = i + 1 to i + max do + let i' = offset % blocks.Length + blocks.[i'] <- blocks.[i'] + 1 + + match previous |> List.tryFindIndex ((=) blocks) with + | Some i -> previous, i + 1 + | None -> next (blocks :: previous) + + let blockList, cycleLength = next [ blocks ] + + List.length blockList, cycleLength diff --git a/AdventOfCode2017/Program.fs b/AdventOfCode2017/Program.fs index 574d6d9..b5116bb 100644 --- a/AdventOfCode2017/Program.fs +++ b/AdventOfCode2017/Program.fs @@ -23,6 +23,11 @@ let day5 () = let input = File.ReadAllText "Data/day5.input" |> Day5.parseInput sprintf "part1 = %A, part2 = %A" (Day5.nbSteps1 input) (Day5.nbSteps2 input) +let day6 () = + let input = File.ReadAllText "Data/day6.input" |> Day6.parseInput + let part1, part2 = Day6.nbRedistribution input + sprintf "part1 = %A, part2 = %A" part1 part2 + let doDay (n : int) = let result = match n with @@ -31,6 +36,7 @@ let doDay (n : int) = | 3 -> day3 () | 4 -> day4 () | 5 -> day5 () + | 6 -> day6 () | _ -> raise <| NotImplementedException () printfn "Result of day %i: %s" n result diff --git a/Tests/Day6 tests.fs b/Tests/Day6 tests.fs new file mode 100644 index 0000000..9d2d0ad --- /dev/null +++ b/Tests/Day6 tests.fs @@ -0,0 +1,19 @@ +namespace AdventOfCode2017.Tests + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +type ``Day6 tests`` (output : ITestOutputHelper) = + + [] + let ``(Part1) From web page`` () = + let l, _ = Day6.nbRedistribution [| 0; 2; 7; 0 |] + l =! 5 + + [] + let ``(Part2) From web page`` () = + let _, cycleLength = Day6.nbRedistribution [| 0; 2; 7; 0 |] + cycleLength =! 4 \ No newline at end of file diff --git a/Tests/Tests.fsproj b/Tests/Tests.fsproj index 1b36559..fe51460 100644 --- a/Tests/Tests.fsproj +++ b/Tests/Tests.fsproj @@ -60,6 +60,7 @@ +