ソースを参照

D13.Bonus solution in video :)

bastien.monsarrat 6 年 前
コミット
9a791a727a

+ 6 - 0
Adv2018.sln

@@ -57,6 +57,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D13.1", "D13.1\D13.1.csproj
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D13.2", "D13.2\D13.2.csproj", "{50E6029A-7019-44A3-B790-D2F556A0FF2E}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D13.Bonus", "D13.Bonus\D13.Bonus.csproj", "{34DBA00B-F1A6-438B-B610-19FC8436F7DE}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -171,6 +173,10 @@ Global
 		{50E6029A-7019-44A3-B790-D2F556A0FF2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{50E6029A-7019-44A3-B790-D2F556A0FF2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{50E6029A-7019-44A3-B790-D2F556A0FF2E}.Release|Any CPU.Build.0 = Release|Any CPU
+		{34DBA00B-F1A6-438B-B610-19FC8436F7DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{34DBA00B-F1A6-438B-B610-19FC8436F7DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{34DBA00B-F1A6-438B-B610-19FC8436F7DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{34DBA00B-F1A6-438B-B610-19FC8436F7DE}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 6 - 4
D13.2/Program.cs

@@ -22,15 +22,17 @@ namespace D13._2
             FillTrack(file, track, carts);
 
             int? last = null;
-            while (last == null)
+            do
             {
                 carts.Sort((a, b) => a.x.CompareTo(b.x));
                 carts.Sort((a, b) => a.y.CompareTo(b.y));
 
-                tick(track, carts);
-
                 last = lastCart(carts);
-            }
+
+                if (last == null) tick(track, carts);
+
+            } while (last == null);
+
             tick(track, carts);
 
             var l = carts[last.Value];

+ 6 - 0
D13.Bonus/App.config

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+    <startup> 
+        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
+    </startup>
+</configuration>

+ 86 - 0
D13.Bonus/D13.Bonus.csproj

@@ -0,0 +1,86 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProjectGuid>{1FC3EF39-4B1A-4792-801C-560A83E9461C}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <RootNamespace>D13.Bonus</RootNamespace>
+    <AssemblyName>D13.Bonus</AssemblyName>
+    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
+    <Deterministic>true</Deterministic>
+    <NuGetPackageImportStamp>
+    </NuGetPackageImportStamp>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <PlatformTarget>AnyCPU</PlatformTarget>
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <LangVersion>7.3</LangVersion>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <PlatformTarget>AnyCPU</PlatformTarget>
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <LangVersion>7.3</LangVersion>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="Accord, Version=3.8.0.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
+      <HintPath>..\packages\Accord.3.8.0\lib\net462\Accord.dll</HintPath>
+    </Reference>
+    <Reference Include="Accord.Video, Version=3.8.0.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
+      <HintPath>..\packages\Accord.Video.3.8.0\lib\net462\Accord.Video.dll</HintPath>
+    </Reference>
+    <Reference Include="Accord.Video.FFMPEG, Version=3.8.0.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=x86">
+      <HintPath>..\packages\Accord.Video.FFMPEG.3.8.0\lib\net462\Accord.Video.FFMPEG.dll</HintPath>
+    </Reference>
+    <Reference Include="AForge, Version=2.2.5.0, Culture=neutral, PublicKeyToken=c1db6ff4eaa06aeb, processorArchitecture=MSIL">
+      <HintPath>..\packages\AForge.2.2.5\lib\AForge.dll</HintPath>
+    </Reference>
+    <Reference Include="AForge.Imaging.Formats, Version=2.2.4.0, Culture=neutral, PublicKeyToken=8065ed5a639199a0, processorArchitecture=MSIL">
+      <HintPath>..\packages\AForge.Imaging.Formats.2.2.5\lib\AForge.Imaging.Formats.dll</HintPath>
+    </Reference>
+    <Reference Include="AForge.Video, Version=2.2.5.0, Culture=neutral, PublicKeyToken=cbfb6e07d173c401, processorArchitecture=MSIL">
+      <HintPath>..\packages\AForge.Video.2.2.5\lib\AForge.Video.dll</HintPath>
+    </Reference>
+    <Reference Include="System" />
+    <Reference Include="System.Core" />
+    <Reference Include="System.Drawing" />
+    <Reference Include="System.Xml.Linq" />
+    <Reference Include="System.Data.DataSetExtensions" />
+    <Reference Include="Microsoft.CSharp" />
+    <Reference Include="System.Data" />
+    <Reference Include="System.Net.Http" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="App.config" />
+    <None Include="packages.config" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <Import Project="..\packages\Accord.3.8.0\build\Accord.targets" Condition="Exists('..\packages\Accord.3.8.0\build\Accord.targets')" />
+  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
+    <PropertyGroup>
+      <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
+    </PropertyGroup>
+    <Error Condition="!Exists('..\packages\Accord.3.8.0\build\Accord.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Accord.3.8.0\build\Accord.targets'))" />
+    <Error Condition="!Exists('..\packages\Accord.Video.FFMPEG.3.8.0\build\Accord.Video.FFMPEG.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Accord.Video.FFMPEG.3.8.0\build\Accord.Video.FFMPEG.targets'))" />
+  </Target>
+  <Import Project="..\packages\Accord.Video.FFMPEG.3.8.0\build\Accord.Video.FFMPEG.targets" Condition="Exists('..\packages\Accord.Video.FFMPEG.3.8.0\build\Accord.Video.FFMPEG.targets')" />
+</Project>

+ 197 - 0
D13.Bonus/Program.cs

@@ -0,0 +1,197 @@
+using Accord.Video.FFMPEG;
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.IO;
+using System.Linq;
+
+namespace D13._3
+{
+    class Track : Dictionary<(int x, int y), char> { };
+    class Carts : List<(int x, int y, int vx, int vy, int t, Color c)> { };
+
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            if (args.Length < 1) return;
+            if (File.Exists(args[0]) == false) return;
+            var file = File.OpenText(args[0]);
+
+            var track = new Track();
+            var carts = new Carts();
+
+            FillTrack(file, track, carts);
+
+            int width = track.Max(c => c.Key.x) + 1;
+            int height = track.Max(c => c.Key.y) + 1;
+            VideoFileWriter writer = new VideoFileWriter();
+            writer.Open("out.avi", width, height);
+
+            int? last = null;
+            do
+            {
+                last = lastCart(carts);
+
+                Bitmap image = new Bitmap(width, height);
+                foreach (var t in track) image.SetPixel(t.Key.x, t.Key.y, Color.White);
+                foreach (var t in carts) image.SetPixel(t.x, t.y, t.c);
+
+                writer.WriteVideoFrame(image);
+
+                carts.Sort((a, b) => a.x.CompareTo(b.x));
+                carts.Sort((a, b) => a.y.CompareTo(b.y));
+
+                tick(track, carts);
+            
+            } while (last == null) ;
+
+            writer.Close();
+        }
+
+        private static void FillTrack(StreamReader file, Track track, Carts carts)
+        {
+            int y = 0;
+            do
+            {
+                var line = file.ReadLine();
+                if (line == null) break;
+
+                for (int x = 0; x < line.Length; ++x)
+                {
+                    char c = line[x];
+                    if (c == ' ') continue;
+
+                    if (isCart(c))
+                    {
+                        carts.Add(getCart(c, x, y));
+                        c = c == '>' || c == '<' ? '-' : '|';
+                    }
+
+                    track[(x, y)] = c;
+                }
+
+                y++;
+
+            } while (true);
+        }
+
+        private static void tick(Track track, Carts carts)
+        {
+            for (var i = 0; i < carts.Count; ++i)
+            {
+                var cart = carts[i];
+                if (cart == default) continue;
+
+                cart.x += cart.vx;
+                cart.y += cart.vy;
+
+                if (inter(carts, cart, i)) continue;
+
+                var c = track[(cart.x, cart.y)];
+
+                var ncart = getCart(c, cart);
+                carts[i] = ncart;
+            }
+        }
+
+        static int? lastCart(Carts carts)
+        {
+            int count = 0;
+            int? cart = null;
+
+            for (var i = 0; i < carts.Count; ++i)
+            {
+                if (carts[i] == default) continue;
+
+                cart = i;
+                count++;
+            }
+            return count == 1 ? cart : null;
+        }
+
+        static bool inter(Carts carts, (int x, int y, int vx, int vy, int t, Color c) cj, int skip)
+        {
+            for (var i = 0; i < carts.Count; ++i)
+            {
+                if (i == skip) continue;
+
+                var ci = carts[i];
+                if (ci == default) continue;
+
+                if (ci.x == cj.x && ci.y == cj.y)
+                {
+                    carts[i] = default;
+                    carts[skip] = default;
+                    return true;
+                }
+            }
+
+            return false;
+        }
+
+        static bool isCart(char c)
+        {
+            return c == '>' || c == '<' || c == '^' || c == 'v';
+        }
+
+        static (int x, int y, int vx, int vy, int t, Color c) getCart(char c, int x, int y)
+        {
+            var r = new Random();
+            switch (c)
+            {
+                case '>': return (x, y, +1, +0, 0, Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255)));
+                case '<': return (x, y, -1, +0, 0, Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255)));
+                case '^': return (x, y, +0, -1, 0, Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255)));
+                case 'v': return (x, y, +0, +1, 0, Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255)));
+            }
+            return default;
+        }
+
+        static (int x, int y, int vx, int vy, int t, Color c) getCart(char c, (int x, int y, int vx, int vy, int t, Color c) cart)
+        {
+            if (c == '|' || c == '-') return cart;
+            if (c == '+' && cart.t == 1)
+            {
+                cart.t++;
+                return cart;
+            }
+
+            switch (cart)
+            {
+                case var t when t.vx == 1 && t.vy == 0:
+
+                    if (c == '/') return (cart.x, cart.y, +0, -1, cart.t, cart.c);
+                    if (c == '\\') return (cart.x, cart.y, +0, +1, cart.t, cart.c);
+                    if (c == '+' && cart.t == 0) return (cart.x, cart.y, +0, -1, 1, cart.c);
+                    if (c == '+' && cart.t == 2) return (cart.x, cart.y, +0, +1, 0, cart.c);
+                    return default;
+
+                case var t when t.vx == -1 && t.vy == 0:
+
+                    if (c == '/') return (cart.x, cart.y, +0, +1, cart.t, cart.c);
+                    if (c == '\\') return (cart.x, cart.y, +0, -1, cart.t, cart.c);
+                    if (c == '+' && cart.t == 0) return (cart.x, cart.y, +0, +1, 1, cart.c);
+                    if (c == '+' && cart.t == 2) return (cart.x, cart.y, +0, -1, 0, cart.c);
+                    return default;
+
+                case var t when t.vx == 0 && t.vy == 1:
+
+                    if (c == '/') return (cart.x, cart.y, -1, +0, cart.t, cart.c);
+                    if (c == '\\') return (cart.x, cart.y, +1, +0, cart.t, cart.c);
+                    if (c == '+' && cart.t == 0) return (cart.x, cart.y, +1, +0, 1, cart.c);
+                    if (c == '+' && cart.t == 2) return (cart.x, cart.y, -1, +0, 0, cart.c);
+                    return default;
+
+                case var t when t.vx == 0 && t.vy == -1:
+
+                    if (c == '/') return (cart.x, cart.y, +1, +0, cart.t, cart.c);
+                    if (c == '\\') return (cart.x, cart.y, -1, +0, cart.t, cart.c);
+                    if (c == '+' && cart.t == 0) return (cart.x, cart.y, -1, +0, 1, cart.c);
+                    if (c == '+' && cart.t == 2) return (cart.x, cart.y, +1, +0, 0, cart.c);
+                    return default;
+            }
+            return default;
+        }
+    }
+}

+ 36 - 0
D13.Bonus/Properties/AssemblyInfo.cs

@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("D13.Bonus")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("D13.Bonus")]
+[assembly: AssemblyCopyright("Copyright ©  2019")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components.  If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("1fc3ef39-4b1a-4792-801c-560a83e9461c")]
+
+// Version information for an assembly consists of the following four values:
+//
+//      Major Version
+//      Minor Version
+//      Build Number
+//      Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]

+ 9 - 0
D13.Bonus/packages.config

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+  <package id="Accord" version="3.8.0" targetFramework="net472" />
+  <package id="Accord.Video" version="3.8.0" targetFramework="net472" />
+  <package id="Accord.Video.FFMPEG" version="3.8.0" targetFramework="net472" />
+  <package id="AForge" version="2.2.5" targetFramework="net472" />
+  <package id="AForge.Imaging.Formats" version="2.2.5" targetFramework="net472" />
+  <package id="AForge.Video" version="2.2.5" targetFramework="net472" />
+</packages>