# Idiomatic Kotlin: Solving Advent of Code Puzzles, Day 2

DevFeed: [Idiomatic Kotlin: Solving Advent of Code Puzzles, Day 2](<https://devfeed.tech/articles/idiomatic-kotlin-solving-advent-of-code-puzzles-day-2-22985.md>)

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

Author: Svetlana Isakova

Published: 2021-07-21T16:25:30Z

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>), [Code](<https://devfeed.tech/topics/code.md>), [passwords](<https://devfeed.tech/topics/passwords.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>), [how-to](<https://devfeed.tech/tags/how-to.md>), [inclusive](<https://devfeed.tech/tags/inclusive.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [password](<https://devfeed.tech/tags/password.md>), [passwords](<https://devfeed.tech/tags/passwords.md>), [software](<https://devfeed.tech/tags/software.md>)

## AI overview

A tutorial on writing idiomatic Kotlin while solving Advent of Code 2020 Day 2. It explains the two password-policy interpretations: counting letter occurrences within a range, and requiring the letter in exactly one of two specified positions, then begins implementing input parsing and a Kotlin data class for the policy and password.

## Source excerpt

Idiomatic Kotlin: Solving Advent of Code Puzzles, Day 2 Let's continue learning how to write idiomatic Kotlin code by solving the AdventOfCode tasks! Today, we're discussing the solution for the day 2 task. Day 2. Password philosophy We need to confirm that passwords meet the corporate policy. Find the full task description at https://adventofcode.com/2020/day/2*. First, we need to read the input: 1-3 a: abcde 1-3 b: cdefg 2-9 c: ccccccccc Each line contains the password policy and the password. Our task is to check that the password is valid and conforms to the given policy. The policies are different in the first and the second parts of the task. In the first part, the password policy indicates the lowest and highest number of times a given letter must appear for the password to be valid. For example, 1-3 a means that the password must contain a at least once and at most 3 times. In the example, two passwords, the first and the third ones, are valid. The first contains one a, and the third contains nine cs, both within the limits of their respective policies. The second password, cdefg, is invalid, as it contains no instances of b but needs at least 1. In the second part, the policy describes two positions in the password, where 1 means the first character, 2 means the second character, and so on (indexing starts at 1, not 0). Exactly one of these positions must contain the given letter. Other occurrences of the letter are irrelevant. Given the same example list from above: 1-3 a: abcde is valid: position 1 contains a and position 3 does not. 1-3 b: cdefg is invalid: neither position 1 nor position 3 contains b. 2-9 c: ccccccccc is invalid: both positions 2 and position 9 contain c. We should count the number of passwords from the given input that are valid according to the interpretations of the policies. As usual, if you haven't done it, please solve the task yourself first. Here's how you can set up Kotlin for this purpose. Solution First, we should read and pa