Koltin solutions for the 2021 edition of AoC

Related tags

App AdventOfCode2021
Overview

AdventOfCode2021

This is the code developed for Advent of Code 2021. My primary goals are:

  • have fun
  • learn something new about Kotlin
  • don't spend more than 1 hour each day

I thus favor short and readable code over optimized code. As a rule of thumb, any code which finishes in under 1 minute is "good enough". I also will not try to handle possible corner cases if the provided input file does not contain them.

AoC provides 2 coding challenges for every day in December until Christmas. Every daily challenge also has one file containing the data for the challenges.

Principles

This project setup uses a unit test class per day to provide the solution code. Every class will contain 2 unit tests (one per challenge), and one or more functions which implement the solution. I chose the unit test approach because that allows to code the expected/correct result in a standardized way, and because IntelliJ can directly execute either all or individual tests.

To simplify the test setup, this project provides a Junit5 extension which automates the passing the content of the daily input file to the unit tests. For this to work, the input files must be named exactly like the classes which contain the test which consume them. A straight-forward approach is thus to create an input file input/Day12 and a test class Day12 for the challenge of day 12.

Given the day range, it is advised to use 0-padded 2 digits for the day number (e.g. the input for file day 3 should be named Day03).

Implementation Details

The input test data handling is implemented using a Junit5 extension for test parameters, implemented in InputParameterResolver. To avoid having to annotate test cases, this extension is automatically enabled by using the files resources/META-INF/services/org.junit.jupiter.api.extension.Extension which lists the extension and resources/junit-platform.properties which enables the auto-detection of extensions.

Example / Template

This repository contains the input file input/Day00 and the solution file src/test/kotlin/Day00.kt as an example. This Day00.kt is a good candidate to be copied for the daily challenges.

  • input/Day00
1
2
3
4
5
  • src/test/kotlin/Day00.kt
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test

class Day00 {
    @Test
    fun testOne(input: List<String>) {
        // provide explicit lists for testing other cases than the actual test input
        one(listOf("3", "1", "4")) shouldBe 8
        one(input) shouldBe 15
    }

    @Test
    fun testTwo(input: List<String>) {
        two(input) shouldBe 120L
    }

    // This should return the sum of the input
    private fun one(input: List<String>): Int = input.map(String::toInt).sumOf { it }

    // This should return the product of the input
    private fun two(input: List<String>): Long {
        return input.map(String::toLong).reduce { acc, i -> acc * i }
    }
}
You might also like...
Solutions to advent of code 2021 in the gen-Z programming language known as kotlin

Advent Of Code 2021 - Kotlin Edition How to run? Get the kotlin SDK using the sdkman tool: https://sdkman.io/sdks#kotlin Run the commands: ./gradlew

My solutions for advent of code challenges 2021.

advent-of-code-2021 Welcome to the Advent of Code1 Kotlin project created by hiteshchalise using the Advent of Code Kotlin Template delivered by JetBr

🚧 A fully open-source project for creating and maintaining a Kotlin-based Minecraft: Java Edition server.

Hexalite: Java Edition ⚠️ WARNING: The Hexalite Network is a work in progress. It is not yet ready for production. You may encounter bugs and other is

🚧 A fully open-source project for creating and maintaining a Kotlin-based Minecraft: Java Edition server.

Hexalite: Java Edition ⚠️ WARNING: The Hexalite Network is a work in progress. It is not yet ready for production. You may encounter bugs and other is

Android + Kotlin Project for the 2022 edition of the Open Source Contribution competition, JGEC Winter of Code aka JWoC.

QuotesApp-JWoC Android + Kotlin Project for the 2022 edition of the Open Source Contribution competition, JGEC Winter of Code aka JWoC. 🎯 Project Obj

PhotoGallery app from 'Android Programming (4th edition)' By Big Nerd Ranch
PhotoGallery app from 'Android Programming (4th edition)' By Big Nerd Ranch

PhotoGallery app An app from the book 'Android Programming (4th edition)' by Big Nerd Ranch Learned and worked with: Retrofit: Flickr API Calls @GET,

An outdated excellent anticheat for NukkitX / Minecraft Bedrock Edition

WAntiChatPro An outdated or modern anti-cheat plugin for Minecraft: Bedrock Edition servers (NukkitX). Following description is excerpt from previous

LeeCo is an awesome app for (including unlock) problems, solutions, discuss(from leetcode) and comments.
LeeCo is an awesome app for (including unlock) problems, solutions, discuss(from leetcode) and comments.

LeeCo LeeCo is an awesome app for algorithem problems(including unlock), solutions, discuss(from leetcode) and comments. #δΈ­ζ–‡ζ–‡ζ‘£ README-ZH.md #Download

My Advent of Code solutions written in Kotlin

AOC21 My Advent of Code solutions written in Kotlin Why Kotlin? I think Kotlin is a really suitable language for Advent of Code, because it handles li

Comments
  • Could use

    Could use "split" w/o regex

    https://github.com/nkiesel/AdventOfCode2021/blob/dadc1e8e0acfc88382a52c363f1bfc3a4022a785/src/test/kotlin/Day02.kt#L39

    (Just trying out how to comment on code in github....)

    You could use s.split(" ") instead of s.split(" ".toRegex()) (Kotlin has overloaded split for String and Regex argument)

    opened by nkiesel 1
Owner
Norbert Kiesel
Norbert Kiesel
Solving all 25 days of the AOC 2021 event here: https://adventofcode.com

advent-of-code-2021-kotlin Welcome to the Advent of Code1 Kotlin project using the Advent of Code Kotlin Template delivered by JetBrains. In this repo

Mofe Ejegi 1 Dec 17, 2021
AoC in Kotlin for 2021

aoc-kotlin-starter Starter template for solving Advent of Code in Kotlin Features Gradle setup so you can run a specific day or all days on the comman

Joakim H. 0 Dec 8, 2021
My solutions for Advent of Code 2021 puzzles, mainly using Kotlin.

Advent of Code 2021 Featuring Kotlin What's that ? https://adventofcode.com/2021/about Advent of Code is an Advent calendar of small programming puzzl

Petrole 0 Dec 2, 2021
My solutions for Advent of Code 2021, written in Kotlin!

Advent-of-Code-2021 Welcome to the Advent of Code1 Kotlin project created by thijsboehme using the Advent of Code Kotlin Template delivered by JetBrai

Thijs Boehme 0 Dec 15, 2021
Advent of Code 2021: Solutions in Kotlin

Advent of Code 2021 Solutions in Kotlin This repo is my personal attempt at solving the Advent of Code 2021 set of problems with the Kotlin programmin

Todd Ginsberg 33 Nov 28, 2022
πŸŽ… Marry XMas πŸŽ„ Kotlin solutions for my Advent of Code 2021 🀢

Advent of Code 2021 ?? val aMessageFromMe = """ _____________,--, | | | | | | |/ .-.\ HANG IN THERE |_|_|_|_|_|_/ / `. SAN

Nicola Corti 4 Dec 15, 2022
My solutions for the Advent of Code 2021. See the link for a playlist with recordings of me solving each puzzle.

Advent of Code 2021 My solutions for the Advent of Code 2021 solved with Kotlin. What is the Advent of Code? Advent of Code is an online event created

The Self-Taught Software Engineer 6 Jan 5, 2022
My Advent Of Code 2021 solutions πŸŽ„πŸŽ…

AdventOfCode2021 In this repository, i am about to provide solutions for the Advent of Code1 puzzles using Kotlin language. Footnotes Advent of Code –

null 1 Dec 4, 2021
My advent of code 2021 solutions.

Advent of code 2021 These are my kotlin solutions for however long I manage to take part without giving up. How to run Add a Config.kt file locally an

DownloadPizza 1 Dec 14, 2021
πŸŽ„ Solutions to Advent of Code 2021 in Kotlin πŸ’œ

Advent of Code 2021 in Kotlin These are my solutions to Advent of Code 2021 using the Kotlin language. Each day has its own subpackage. Try the proble

James 3 Feb 28, 2022