Пример #1
0
        public override long Part1(string input)
        {
            var fields  = RegexDeserializable.Deserialize <Field>(input).ToArray();
            var tickets = Utils.splitLines(input).Where(l => l.Length > 0 && char.IsDigit(l[0])).Select(l => l.Split(',').Select(n => int.Parse(n)).ToArray()).ToArray();

            return(tickets.Sum(t => InvalidFieldSum(t, fields)));
        }
Пример #2
0
        public override long Part2(string input)
        {
            var fields       = RegexDeserializable.Deserialize <Field>(input).ToArray();
            var tickets      = Utils.splitLines(input).Where(l => l.Length > 0 && char.IsDigit(l[0])).Select(l => l.Split(',').Select(n => int.Parse(n)).ToArray()).ToArray();
            var validTickets = tickets.Where(t => MaybeValid(t, fields)).ToArray();

            var assigned = new Queue <int>();

            foreach (var field in fields)
            {
                field.InitIndices(validTickets[0].Length);
                foreach (var ticket in validTickets)
                {
                    field.eliminateIndices(ticket, assigned);
                }
            }

            while (assigned.Count > 0)
            {
                var index = assigned.Dequeue();

                foreach (var field in fields)
                {
                    if (field.possibleIndices.Count > 1 && field.possibleIndices.Contains(index))
                    {
                        field.possibleIndices.Remove(index);
                        if (field.possibleIndices.Count == 1)
                        {
                            assigned.Enqueue(field.possibleIndices.First());
                        }
                    }
                }
            }


            var departureFields = fields.Where(f => f.Name.StartsWith("departure"));

            return(departureFields.Aggregate(1L, (a, f) => a * tickets[0][f.possibleIndices.First()]));
        }
Пример #3
0
        public override long Part1(string input)
        {
            var passwords = RegexDeserializable.Deserialize <PasswordPolicy>(input).ToArray();

            return(passwords.Count(p => p.IsValid));
        }