<PlatformTarget>AnyCPU</PlatformTarget>
<DocumentationFile>bin\$(Configuration)\$(AssemblyName).XML</DocumentationFile>
<Prefer32Bit>true</Prefer32Bit>
- <StartArguments>4</StartArguments>
+ <StartArguments>5</StartArguments>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Compile Include="Day2.fs" />
<Compile Include="Day3.fs" />
<Compile Include="Day4.fs" />
+ <Compile Include="Day5.fs" />
<Compile Include="Program.fs" />
<None Include="App.config" />
<None Include="Data\day1.input">
<None Include="Data\day4.input">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
+ <None Include="Data\day5.input">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
<Content Include="packages.config" />
</ItemGroup>
<ItemGroup>
for b in a + 1 .. words.Length - 1 -> f words.[a] words.[b]
] |> List.forall not
+let isAnagram w1 w2 = Seq.compareWith compare (Seq.sort w1) (Seq.sort w2) = 0
+
let passphraseValid = forallDistinctPairs (=)
-let isAnagram w1 w2 = Seq.compareWith (compare) (Seq.sort w1) (Seq.sort w2) = 0
let passphraseValidAnagram = forallDistinctPairs isAnagram
+
let nbPassphrasesValid (f : string -> bool) = Seq.map f >> Seq.sumBy (fun v -> if v then 1 else 0)
\ No newline at end of file
--- /dev/null
+module AdventOfCode2017.Day5
+
+open System
+
+let parseInput (str : string) : int[] =
+ str.Split ([| '\r'; '\t'; ' ' |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int
+
+let nbSteps (next : int -> int) (instructions : int[]) =
+ let is = instructions.[*]
+ let mutable cursor, steps = 0, 0
+ while cursor >= 0 && cursor < instructions.Length do
+ let i = is.[cursor]
+ is.[cursor] <- next is.[cursor]
+ cursor <- cursor + i
+ steps <- steps + 1
+ steps
+
+let nbSteps1 = nbSteps ((+) 1)
+let nbSteps2 = nbSteps (fun i -> if i >= 3 then i - 1 else i + 1)
\ No newline at end of file
let input = File.ReadAllLines "Data/day4.input"
sprintf "part1 = %A, part2 = %A" (Day4.nbPassphrasesValid Day4.passphraseValid input) (Day4.nbPassphrasesValid Day4.passphraseValidAnagram input)
+let day5 () =
+ let input = File.ReadAllText "Data/day5.input"
+ sprintf "part1 = %A, part2 = %A" (Day5.nbSteps1 (Day5.parseInput input)) (Day5.nbSteps2 (Day5.parseInput input))
+
let doDay (n : int) =
let result =
match n with
| 2 -> day2 ()
| 3 -> day3 ()
| 4 -> day4 ()
+ | 5 -> day5 ()
| _ -> raise <| NotImplementedException ()
printfn "Result of day %i: %s" n result
--- /dev/null
+namespace AdventOfCode2017.Tests
+
+open Xunit
+open Xunit.Abstractions
+open Swensen.Unquote
+
+open AdventOfCode2017
+
+type ``Day5 tests`` (output : ITestOutputHelper) =
+
+ [<Fact>]
+ let ``(Part1) From web page`` () =
+ Day5.nbSteps1 [| 0; 3; 0; 1; -3 |] =! 5
+
+ [<Fact>]
+ let ``(Part2) From web page`` () =
+ Day5.nbSteps2 [| 0; 3; 0; 1; -3 |] =! 10
\ No newline at end of file
<Compile Include="Day2 tests.fs" />
<Compile Include="Day3 tests.fs" />
<Compile Include="Day4 tests.fs" />
+ <Compile Include="Day5 tests.fs" />
<Content Include="packages.config" />
<Content Include="App.config" />
</ItemGroup>