about summary refs log tree commit diff
path: root/Day8.cs
blob: cdb3ef6f384651fb0710890e878d77f338b040c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Collections.Generic;
using System.Linq;
using aoc2019.lib;

namespace aoc2019
{
    internal sealed class Day8 : Day
    {
        private readonly List<List<char>> photo;

        public Day8()
        {
            photo = Input.First().Chunk(25 * 6).Select(s => s.ToList()).ToList();
        }

        public override int DayNumber => 8;

        protected 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')}";
        }

        protected 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');
        }
    }
}