logo CodeStepByStep logo


Write a method named TicketValidation that processes a file of travel tickets to find ones that contain invalid values. Your method accepts as its parameter a string representing the name of a file of ticket data.

The input file contains three sections. The first section is a list of fields, each of which has a name and one or more ranges of valid integer values for that field. The second section contains your ticket, which has a one-line heading followed by a single line of comma-separated list of values for your ticket. The third section contains other people's tickets, which has a one-line heading followed by one or more lines of comma-separated lists of values for each ticket. For example, consider the following ticket data, stored in a file named tickets.txt:

class: 1-3 or 5-7
row: 6-11 or 33-44
seat: 13-40 or 45-50

your ticket:

nearby tickets:

Your task is to find all values in any of the "nearby tickets" that are invalid. (Do not check the validity of "your ticket" for this problem.) An invalid value is one that cannot fit into the ranges for any of the fields in the first section of the input file. For example, in the input file above, the second nearby ticket has a value of 4, which does not fit into the ranges for a class, row, nor seat. Similarly, the third ticket has a 55 and the fourth ticket has a 12, both of which cannot fit into any of the fields' ranges.

Your method should find and return the sum of all invalid values in all nearby tickets in the input file. So the call of TicketValidation("tickets.txt") would return 4 + 55 + 12 or 71.

You may assume that the file exists and is readable, that it follows the format described above, and that there will be at least one field, exactly one "your ticket" entry, and at least one "nearby tickets" entry.

(This exercise is based on the Advent of Code 2020, day 16.)

Method: Write a C# method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.