day 8
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Ben Harris 2019-12-08 01:56:33 -05:00
parent 270cac64a4
commit cd816151aa
4 changed files with 54 additions and 0 deletions

39
Day8.cs Normal file
View File

@ -0,0 +1,39 @@
using aoc2019.lib;
using System;
using System.Collections.Generic;
using System.Linq;
namespace aoc2019
{
internal class Day8 : Day
{
public override int DayNumber => 8;
private readonly List<List<char>> photo;
public Day8()
{
photo = Input.First().Chunk(25 * 6).Select(s => s.ToList()).ToList();
}
public override string Part1()
{
var l = photo.OrderBy(layer => layer.Count(pixel => pixel == '0')).First();
return $"{l.Count(p => p == '1') * l.Count(p => p == '2')}";
}
public override string Part2()
{
return Enumerable.Range(0, 25 * 6)
.Select(p => Enumerable.Range(0, photo.Count)
.Select(l => photo[l][p])
.Aggregate('2', (acc, next) =>
acc != '2' ? acc : next == '0' ? ' ' : next
)
)
.ToDelimitedString()
.Chunk(25)
.ToDelimitedString(Environment.NewLine)
.Replace('1', 'x');
}
}
}

View File

@ -27,6 +27,9 @@
<None Update="input\day7.in">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="input\day8.in">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

1
input/day8.in Normal file

File diff suppressed because one or more lines are too long

View File

@ -11,5 +11,16 @@ namespace aoc2019.lib
if (list.Count() == 1) return new[] { list };
return list.SelectMany(t => Permute(list.Where(x => !x.Equals(t))), (v, p) => p.Prepend(v));
}
public static IEnumerable<string> Chunk(this string str, int chunkSize)
{
for (int i = 0; i < str.Length; i += chunkSize)
yield return str.Substring(i, chunkSize);
}
public static string ToDelimitedString<T>(this IEnumerable<T> enumerable, string delimiter = "")
{
return string.Join(delimiter, enumerable);
}
}
}