# Idiomatic Kotlin: Solving Advent of Code Puzzles, Passport Validation

DevFeed: [Idiomatic Kotlin: Solving Advent of Code Puzzles, Passport Validation](<https://devfeed.tech/articles/idiomatic-kotlin-solving-advent-of-code-puzzles-passport-validation-22986.md>)

Original publisher: [Read original article](<https://dev.to/kotlin/idiomatic-kotlin-solving-advent-of-code-puzzles-passport-validation-1425>)

Author: Sebastian Aigner

Published: 2021-09-01T14:52:59Z

Content type: tutorial

Language: en

Sources: [Kotlin](<https://devfeed.tech/sources/kotlin-2.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Advent of Code](<https://devfeed.tech/topics/advent-of-code.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [Sanitization](<https://devfeed.tech/topics/sanitization.md>)

Tags: [100daysofcode](<https://devfeed.tech/tags/100daysofcode.md>), [advent-of-code](<https://devfeed.tech/tags/advent-of-code.md>), [adventofcode](<https://devfeed.tech/tags/adventofcode.md>), [code](<https://devfeed.tech/tags/code.md>), [codenewbie](<https://devfeed.tech/tags/codenewbie.md>), [coding](<https://devfeed.tech/tags/coding.md>), [community](<https://devfeed.tech/tags/community.md>), [development](<https://devfeed.tech/tags/development.md>), [engineering](<https://devfeed.tech/tags/engineering.md>), [files](<https://devfeed.tech/tags/files.md>), [inclusive](<https://devfeed.tech/tags/inclusive.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [linux](<https://devfeed.tech/tags/linux.md>), [macos](<https://devfeed.tech/tags/macos.md>), [software](<https://devfeed.tech/tags/software.md>), [validation](<https://devfeed.tech/tags/validation.md>)

## AI overview

A tutorial on solving Advent of Code 2020 Day 4, Passport Processing, with Kotlin. It explains how to parse passport records separated by blank lines, extract key-value pairs, and count passports containing all required fields.

## Source excerpt

Today in "Idiomatic Kotlin", we're looking at day 4 of the Advent of Code 2020 challenges, in which we tackle a problem that feels as old as programming itself: input sanitization and validation. Day 4. Passport processing We need to build a passport scanner that, given a batch of input text, can count how many passports are valid. You can find the complete task description at https://adventofcode.com/2020/day/4. Like many challenges, we first inspect our input: ecl:gry pid:860033327 eyr:2020 hcl:#fffffd byr:1937 iyr:2017 cid:147 hgt:183cm iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884 hcl:#cfa07d byr:1929 hcl:#ae17e1 iyr:2013 eyr:2024 ecl:brn pid:760753108 byr:1931 hgt:179cm The input is a batch of travel documents in a text file, separated by blank lines. Each passport is represented as a sequence of key-colon-value pairs separated by spaces or newlines. Our challenge is finding out how many passports are valid. For part one, "valid" means that they need to have all the required fields outlined by the security personnel: byr, iyr, eyr, hgt, hcl, ecl and pid (we conveniently ignore their request to validate the cid field). Solving Day 4, Part 1 Like many challenges, we start by reading our puzzle input as text and trim off any extraneous whitespace at the beginning and the end of the file. As per the description, passports are always separated by blank lines. A blank line is just two "returns", or newlines, in a row, so we'll use this to split our input string into the individual passports: val passports = File("src/day04/input.txt") .readText() .trim() .split("\n\n", "\r\n\r\n") (Note that depending on your operating system, the line separator in text files is different: On Windows, it is \r\n, on Linux and macOS, it's \n. Kotlin's split method takes an arbitrary number of delimiters, allowing us to cover both cases directly.) We now have a list of passport strings. However, working with lists of raw strings can quickly get confusing. Let's use Kotlin's expressiv