CINXE.COM

Advent of Code puzzles in idiomatic Kotlin | Kotlin Documentation

<!DOCTYPE html SYSTEM "about:legacy-compat"> <html lang="en-US" data-preset="contrast" data-primary-color="#307FFF"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta charset="UTF-8"><meta name="built-on" content="2024-11-18T21:26:13.705585235"><meta name="build-number" content="3272"><script> (function (w, d, s, l, i) { w[l] = w[l] || []; w[l].push({'gtm.start': new Date().getTime(), event: 'gtm.js'}); var f = d.getElementsByTagName(s)[0], j = d.createElement(s), dl = l != 'dataLayer' ? '&amp;l=' + l : ''; j.async = true; j.src = '//www.googletagmanager.com/gtm.js?id=' + i + dl; f.parentNode.insertBefore(j, f); })(window, document, 'script', 'dataLayer', 'GTM-5P98'); </script> <script src="static/v3/analytics.js"></script> <title>Advent of Code puzzles in idiomatic Kotlin | Kotlin Documentation</title><script type="application/json" id="virtual-toc-data">[{"id":"get-ready-for-advent-of-code","level":0,"title":"Get ready for Advent of Code","anchor":"#get-ready-for-advent-of-code"},{"id":"advent-of-code-2022","level":0,"title":"Advent of Code 2022","anchor":"#advent-of-code-2022"},{"id":"day-1-calorie-counting","level":1,"title":"Day 1: Calorie counting","anchor":"#day-1-calorie-counting"},{"id":"day-2-rock-paper-scissors","level":1,"title":"Day 2: Rock paper scissors","anchor":"#day-2-rock-paper-scissors"},{"id":"day-3-rucksack-reorganization","level":1,"title":"Day 3: Rucksack reorganization","anchor":"#day-3-rucksack-reorganization"},{"id":"day-4-camp-cleanup","level":1,"title":"Day 4: Camp cleanup","anchor":"#day-4-camp-cleanup"},{"id":"day-5-supply-stacks","level":1,"title":"Day 5: Supply stacks","anchor":"#day-5-supply-stacks"},{"id":"day-6-tuning-trouble","level":1,"title":"Day 6: Tuning trouble","anchor":"#day-6-tuning-trouble"},{"id":"day-7-no-space-left-on-device","level":1,"title":"Day 7: No space left on device","anchor":"#day-7-no-space-left-on-device"},{"id":"day-8-treetop-tree-house","level":1,"title":"Day 8: Treetop tree house","anchor":"#day-8-treetop-tree-house"},{"id":"day-9-rope-bridge","level":1,"title":"Day 9: Rope bridge","anchor":"#day-9-rope-bridge"},{"id":"day-10-cathode-ray-tube","level":1,"title":"Day 10: Cathode-ray tube","anchor":"#day-10-cathode-ray-tube"},{"id":"day-11-monkey-in-the-middle","level":1,"title":"Day 11: Monkey in the middle","anchor":"#day-11-monkey-in-the-middle"},{"id":"day-12-hill-climbing-algorithm","level":1,"title":"Day 12: Hill Climbing algorithm","anchor":"#day-12-hill-climbing-algorithm"},{"id":"advent-of-code-2021","level":0,"title":"Advent of Code 2021","anchor":"#advent-of-code-2021"},{"id":"day-1-sonar-sweep","level":1,"title":"Day 1: Sonar sweep","anchor":"#day-1-sonar-sweep"},{"id":"day-2-dive","level":1,"title":"Day 2: Dive!","anchor":"#day-2-dive"},{"id":"day-3-binary-diagnostic","level":1,"title":"Day 3: Binary diagnostic","anchor":"#day-3-binary-diagnostic"},{"id":"day-4-giant-squid","level":1,"title":"Day 4: Giant squid","anchor":"#day-4-giant-squid"},{"id":"advent-of-code-2020","level":0,"title":"Advent of Code 2020","anchor":"#advent-of-code-2020"},{"id":"day-1-report-repair","level":1,"title":"Day 1: Report repair","anchor":"#day-1-report-repair"},{"id":"day-2-password-philosophy","level":1,"title":"Day 2: Password philosophy","anchor":"#day-2-password-philosophy"},{"id":"day-3-toboggan-trajectory","level":1,"title":"Day 3: Toboggan trajectory","anchor":"#day-3-toboggan-trajectory"},{"id":"day-4-passport-processing","level":1,"title":"Day 4: Passport processing","anchor":"#day-4-passport-processing"},{"id":"day-5-binary-boarding","level":1,"title":"Day 5: Binary boarding","anchor":"#day-5-binary-boarding"},{"id":"day-6-custom-customs","level":1,"title":"Day 6: Custom customs","anchor":"#day-6-custom-customs"},{"id":"day-7-handy-haversacks","level":1,"title":"Day 7: Handy haversacks","anchor":"#day-7-handy-haversacks"},{"id":"day-8-handheld-halting","level":1,"title":"Day 8: Handheld halting","anchor":"#day-8-handheld-halting"},{"id":"day-9-encoding-error","level":1,"title":"Day 9: Encoding error","anchor":"#day-9-encoding-error"},{"id":"what-s-next","level":0,"title":"What\u0027s next?","anchor":"#what-s-next"}]</script><script type="application/json" id="topic-shortcuts"></script><link href="static/v3/app.css?v=6.11.0-footer" rel="stylesheet"><link rel="icon" type="image/svg" sizes="16x16" href="https://kotlinlang.org/assets/images/favicon.svg?v2"><link rel="icon" type="image/x-icon" sizes="32x32" href="https://kotlinlang.org/assets/images/favicon.ico?v2"><link rel="icon" type="image/png" sizes="96x96" href="https://kotlinlang.org/assets/images/apple-touch-icon.png?v2"><link rel="icon" type="image/png" sizes="300x300" href="https://kotlinlang.org/assets/images/apple-touch-icon-72x72.png?v2"><link rel="icon" type="image/png" sizes="500x500" href="https://kotlinlang.org/assets/images/apple-touch-icon-114x114.png?v2"><meta name="image" content="https://kotlinlang.org/assets/images/open-graph/docs.png"><!-- Open Graph --><meta property="og:title" content="Advent of Code puzzles in idiomatic Kotlin | Kotlin"><meta property="og:description" content=""><meta property="og:image" content="https://kotlinlang.org/assets/images/open-graph/docs.png"><meta property="og:site_name" content="Kotlin Help"><meta property="og:type" content="website"><meta property="og:locale" content="en_US"><meta property="og:url" content="https://kotlinlang.org/docs/advent-of-code.html"><!-- End Open Graph --><!-- Twitter Card --><meta name="twitter:card" content="summary_large_image"><meta name="twitter:site" content="@kotlin"><meta name="twitter:title" content="Advent of Code puzzles in idiomatic Kotlin | Kotlin"><meta name="twitter:description" content=""><meta name="twitter:creator" content="@kotlin"><meta name="twitter:image:src" content="https://kotlinlang.org/assets/images/open-graph/docs.png"><!-- End Twitter Card --><!-- Schema.org WebPage --><script type="application/ld+json">{ "@context": "http://schema.org", "@type": "WebPage", "@id": "https://kotlinlang.org/docs/advent-of-code.html#webpage", "url": "https://kotlinlang.org/docs/advent-of-code.html", "name": "Advent of Code puzzles in idiomatic Kotlin | Kotlin", "description": "", "image": "https://kotlinlang.org/assets/images/open-graph/docs.png", "inLanguage":"en-US" }</script><!-- End Schema.org --><!-- Schema.org WebSite --><script type="application/ld+json">{ "@type": "WebSite", "@id": "https://kotlinlang.org/docs/#website", "url": "https://kotlinlang.org/docs/", "name": "Kotlin Help" }</script><!-- End Schema.org --><style>a[href="test-page.html"] { visibility: hidden; }</style></head><body data-id="advent-of-code" data-main-title="Advent of Code puzzles in idiomatic Kotlin" data-article-props="{&quot;seeAlsoStyle&quot;:&quot;links&quot;}" data-template="article" data-breadcrumbs="Learning materials" data-edit-url="https://github.com/JetBrains/kotlin-web-site/edit/master/docs/topics/advent-of-code.md"><div class="wrapper"><main class="panel _main"><header class="panel__header"><div class="container"><h3>Kotlin Help</h3><div class="panel-trigger"></div></div></header><section class="panel__content"><div class="container"><article class="article" data-shortcut-switcher="inactive"><h1 data-toc="advent-of-code" id="advent-of-code.md">Advent of Code puzzles in idiomatic Kotlin</h1><p id="-comhfw_2"><a href="https://adventofcode.com/" id="-comhfw_9" data-external="true" rel="noopener noreferrer">Advent of Code</a> is an annual December event, where holiday-themed puzzles are published every day from December 1 to December 25. With the permission of <a href="http://was.tl/" id="-comhfw_10" data-external="true" rel="noopener noreferrer">Eric Wastl</a>, creator of Advent of Code, we'll show how to solve these puzzles using the idiomatic Kotlin style:</p><ul class="list _bullet" id="-comhfw_3"><li class="list__item" id="-comhfw_11"><p><a href="https://www.youtube.com/playlist?list=PLlFc5cFwUnmzk0wvYW4aTl57F2VNkFisU" id="-comhfw_15" data-external="true" rel="noopener noreferrer">Advent of Code 2023</a></p></li><li class="list__item" id="-comhfw_12"><p><a href="#advent-of-code-2022" id="-comhfw_16">Advent of Code 2022</a></p></li><li class="list__item" id="-comhfw_13"><p><a href="#advent-of-code-2021" id="-comhfw_17">Advent of Code 2021</a></p></li><li class="list__item" id="-comhfw_14"><p><a href="#advent-of-code-2020" id="-comhfw_18">Advent of Code 2020</a></p></li></ul><section class="chapter"><h2 id="get-ready-for-advent-of-code" data-toc="get-ready-for-advent-of-code">Get ready for Advent of Code</h2><p id="-comhfw_19">We'll take you through the basic tips on how to get up and running with solving Advent of Code challenges with Kotlin:</p><ul class="list _bullet" id="-comhfw_20"><li class="list__item" id="-comhfw_22"><p>Use <a href="https://github.com/kotlin-hands-on/advent-of-code-kotlin-template" id="-comhfw_24" data-external="true" rel="noopener noreferrer">this GitHub template</a> to create projects</p></li><li class="list__item" id="-comhfw_23"><p>Check out the welcome video by Kotlin Developer Advocate, Sebastian Aigner:</p></li></ul><div class="video-player" id="-comhfw_21"><object class="article__bordered-element" width="560" height="315" data-theme="light" type="application/x-shockwave-flash" data="https://www.youtube.com/v/6-XSehwRgSY?color2=FBE9EC&amp;rel=0&amp;hd=1&amp;autoplay=0&amp;version=3&amp;modestbranding=1&amp;fs=1"></object></div></section><section class="chapter"><h2 id="advent-of-code-2022" data-toc="advent-of-code-2022">Advent of Code 2022</h2><section class="chapter"><h3 id="day-1-calorie-counting" data-toc="day-1-calorie-counting">Day 1: Calorie counting</h3><p id="-comhfw_37">Learn about the <a href="https://github.com/kotlin-hands-on/advent-of-code-kotlin-template" id="-comhfw_40" data-external="true" rel="noopener noreferrer">Kotlin Advent of Code template</a> and convenience functions for working with strings and collections in Kotlin, such as <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/max-of.html" id="-comhfw_41" data-external="true" rel="noopener noreferrer"><code class="code" id="-comhfw_43">maxOf()</code></a> and <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/sum-of.html" id="-comhfw_42" data-external="true" rel="noopener noreferrer"><code class="code" id="-comhfw_44">sumOf()</code></a>. See how extension functions can help you structure your solution in a nice manner.</p><ul class="list _bullet" id="-comhfw_38"><li class="list__item" id="-comhfw_45"><p>Read the puzzle description on <a href="https://adventofcode.com/2022/day/1" id="-comhfw_47" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_46"><p>Check out the solution in the video:</p></li></ul><p id="-comhfw_39"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_48" width="25" height="25"> <a href="https://www.youtube.com/watch?v=ntbsbqLCKDs" id="-comhfw_49" data-external="true" rel="noopener noreferrer">Advent of Code 2022 Day 1 | Kotlin</a></p></section><section class="chapter"><h3 id="day-2-rock-paper-scissors" data-toc="day-2-rock-paper-scissors">Day 2: Rock paper scissors</h3><p id="-comhfw_50">Understand operations on the <code class="code" id="-comhfw_53">Char</code> type in Kotlin, see how the <code class="code" id="-comhfw_54">Pair</code> type and the <code class="code" id="-comhfw_55">to</code> constructor work well with pattern matching. Understand how to order your own objects using the <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-comparable/compare-to.html" id="-comhfw_56" data-external="true" rel="noopener noreferrer"><code class="code" id="-comhfw_57">compareTo()</code></a> function.</p><ul class="list _bullet" id="-comhfw_51"><li class="list__item" id="-comhfw_58"><p>Read the puzzle description on <a href="https://adventofcode.com/2022/day/2" id="-comhfw_60" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_59"><p>Check out the solution in the video:</p></li></ul><p id="-comhfw_52"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_61" width="25" height="25"> <a href="https://www.youtube.com/watch?v=Fn0SY2yGDSA" id="-comhfw_62" data-external="true" rel="noopener noreferrer">Advent of Code 2022 Day 2 | Kotlin</a></p></section><section class="chapter"><h3 id="day-3-rucksack-reorganization" data-toc="day-3-rucksack-reorganization">Day 3: Rucksack reorganization</h3><p id="-comhfw_63">Learn how the <a href="https://github.com/Kotlin/kotlinx-benchmark" id="-comhfw_66" data-external="true" rel="noopener noreferrer">kotlinx.benchmark</a> library helps you understand the performance characteristics of your code. See how set operations like <code class="code" id="-comhfw_67">intersect</code> can help you select overlapping data, and see performance comparisons between different implementations of the same solution.</p><ul class="list _bullet" id="-comhfw_64"><li class="list__item" id="-comhfw_68"><p>Read the puzzle description on <a href="https://adventofcode.com/2022/day/3" id="-comhfw_70" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_69"><p>Check out the solution in the video:</p></li></ul><p id="-comhfw_65"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_71" width="25" height="25"> <a href="https://www.youtube.com/watch?v=IPLfo4zXNjk" id="-comhfw_72" data-external="true" rel="noopener noreferrer">Advent of Code 2022 Day 3 | Kotlin</a></p></section><section class="chapter"><h3 id="day-4-camp-cleanup" data-toc="day-4-camp-cleanup">Day 4: Camp cleanup</h3><p id="-comhfw_73">See how <code class="code" id="-comhfw_76">infix</code> and <code class="code" id="-comhfw_77">operator</code> functions can make your code more expressive, and how extension functions for the <code class="code" id="-comhfw_78">String</code> and <code class="code" id="-comhfw_79">IntRange</code> types make it easy to parse input.</p><ul class="list _bullet" id="-comhfw_74"><li class="list__item" id="-comhfw_80"><p>Read the puzzle description on <a href="https://adventofcode.com/2022/day/4" id="-comhfw_82" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_81"><p>Check out the solution in the video:</p></li></ul><p id="-comhfw_75"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_83" width="25" height="25"> <a href="https://www.youtube.com/watch?v=dBIbr55YS0A" id="-comhfw_84" data-external="true" rel="noopener noreferrer">Advent of Code 2022 Day 4 | Kotlin</a></p></section><section class="chapter"><h3 id="day-5-supply-stacks" data-toc="day-5-supply-stacks">Day 5: Supply stacks</h3><p id="-comhfw_85">Learn about constructing more complex objects with factory functions, how to use regular expressions, and the double-ended <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-array-deque/" id="-comhfw_88" data-external="true" rel="noopener noreferrer"><code class="code" id="-comhfw_89">ArrayDeque</code></a> type.</p><ul class="list _bullet" id="-comhfw_86"><li class="list__item" id="-comhfw_90"><p>Read the puzzle description on <a href="https://adventofcode.com/2022/day/5" id="-comhfw_92" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_91"><p>Check out the solution in the video:</p></li></ul><p id="-comhfw_87"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_93" width="25" height="25"> <a href="https://www.youtube.com/watch?v=lKq6r5Nt8Yo" id="-comhfw_94" data-external="true" rel="noopener noreferrer">Advent of Code 2022 Day 5 | Kotlin</a></p></section><section class="chapter"><h3 id="day-6-tuning-trouble" data-toc="day-6-tuning-trouble">Day 6: Tuning trouble</h3><p id="-comhfw_95">See more in-depth performance investigations with the <a href="https://github.com/Kotlin/kotlinx-benchmark" id="-comhfw_98" data-external="true" rel="noopener noreferrer">kotlinx.benchmark</a> library, comparing the characteristics of 16 different variations of the same solution.</p><ul class="list _bullet" id="-comhfw_96"><li class="list__item" id="-comhfw_99"><p>Read the puzzle description on <a href="https://adventofcode.com/2022/day/6" id="-comhfw_101" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_100"><p>Check out the solution in the video:</p></li></ul><p id="-comhfw_97"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_102" width="25" height="25"> <a href="https://www.youtube.com/watch?v=VbBhaQhW0zk" id="-comhfw_103" data-external="true" rel="noopener noreferrer">Advent of Code 2022 Day 6 | Kotlin</a></p></section><section class="chapter"><h3 id="day-7-no-space-left-on-device" data-toc="day-7-no-space-left-on-device">Day 7: No space left on device</h3><p id="-comhfw_104">Learn how to model tree structures, and see a demo of generating Kotlin code programmatically.</p><ul class="list _bullet" id="-comhfw_105"><li class="list__item" id="-comhfw_107"><p>Read the puzzle description on <a href="https://adventofcode.com/2022/day/7" id="-comhfw_109" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_108"><p>Check out the solution in the video:</p></li></ul><p id="-comhfw_106"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_110" width="25" height="25"> <a href="https://www.youtube.com/watch?v=Q819VW8yxFo" id="-comhfw_111" data-external="true" rel="noopener noreferrer">Advent of Code 2022 Day 7 | Kotlin</a></p></section><section class="chapter"><h3 id="day-8-treetop-tree-house" data-toc="day-8-treetop-tree-house">Day 8: Treetop tree house</h3><p id="-comhfw_112">See the <code class="code" id="-comhfw_115">sequence</code> builder in action, and how far a first draft of a program and an idiomatic Kotlin solution can differ (with special guest Roman Elizarov!).</p><ul class="list _bullet" id="-comhfw_113"><li class="list__item" id="-comhfw_116"><p>Read the puzzle description on <a href="https://adventofcode.com/2022/day/8" id="-comhfw_118" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_117"><p>Check out the solution in the video:</p></li></ul><p id="-comhfw_114"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_119" width="25" height="25"> <a href="https://www.youtube.com/watch?v=6d6FXFh-UdA" id="-comhfw_120" data-external="true" rel="noopener noreferrer">Advent of Code 2022 Day 8 | Kotlin</a></p></section><section class="chapter"><h3 id="day-9-rope-bridge" data-toc="day-9-rope-bridge">Day 9: Rope bridge</h3><p id="-comhfw_121">See the <code class="code" id="-comhfw_124">run</code> function, labeled returns, and convenient standard library functions like <code class="code" id="-comhfw_125">coerceIn</code>, or <code class="code" id="-comhfw_126">zipWithNext</code>. See how you can construct lists of given sizes using the <code class="code" id="-comhfw_127">List</code> and <code class="code" id="-comhfw_128">MutableList</code> constructors, and get a peek at a Kotlin-based visualization of the problem statement.</p><ul class="list _bullet" id="-comhfw_122"><li class="list__item" id="-comhfw_129"><p>Read the puzzle description on <a href="https://adventofcode.com/2022/day/9" id="-comhfw_131" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_130"><p>Check out the solution in the video:</p></li></ul><p id="-comhfw_123"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_132" width="25" height="25"> <a href="https://www.youtube.com/watch?v=ShU9dNUa_3g" id="-comhfw_133" data-external="true" rel="noopener noreferrer">Advent of Code 2022 Day 9 | Kotlin</a></p></section><section class="chapter"><h3 id="day-10-cathode-ray-tube" data-toc="day-10-cathode-ray-tube">Day 10: Cathode-ray tube</h3><p id="-comhfw_134">Learn how ranges and the <code class="code" id="-comhfw_137">in</code> operator make checking ranges natural, how function parameters can be turned into receivers, and a brief exploration of the <code class="code" id="-comhfw_138">tailrec</code> modifier.</p><ul class="list _bullet" id="-comhfw_135"><li class="list__item" id="-comhfw_139"><p>Read the puzzle description on <a href="https://adventofcode.com/2022/day/10" id="-comhfw_141" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_140"><p>Check out the solution in the video:</p></li></ul><p id="-comhfw_136"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_142" width="25" height="25"> <a href="https://www.youtube.com/watch?v=KVyeNmFHoL4" id="-comhfw_143" data-external="true" rel="noopener noreferrer">Advent of Code 2022 Day 10 | Kotlin</a></p></section><section class="chapter"><h3 id="day-11-monkey-in-the-middle" data-toc="day-11-monkey-in-the-middle">Day 11: Monkey in the middle</h3><p id="-comhfw_144">See how you can move from mutable, imperative code to a more functional approach that makes use of immutable and read-only data structures. Learn about context receivers and how our guest built his own visualization library just for Advent of Code.</p><ul class="list _bullet" id="-comhfw_145"><li class="list__item" id="-comhfw_147"><p>Read the puzzle description on <a href="https://adventofcode.com/2022/day/11" id="-comhfw_149" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_148"><p>Check out the solution in the video:</p></li></ul><p id="-comhfw_146"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_150" width="25" height="25"> <a href="https://www.youtube.com/watch?v=1eBSyPe_9j0" id="-comhfw_151" data-external="true" rel="noopener noreferrer">Advent of Code 2022 Day 11 | Kotlin</a></p></section><section class="chapter"><h3 id="day-12-hill-climbing-algorithm" data-toc="day-12-hill-climbing-algorithm">Day 12: Hill Climbing algorithm</h3><p id="-comhfw_152">Use queues, <code class="code" id="-comhfw_155">ArrayDeque</code>, function references, and the <code class="code" id="-comhfw_156">tailrec</code> modifier to solve path finding problems with Kotlin.</p><ul class="list _bullet" id="-comhfw_153"><li class="list__item" id="-comhfw_157"><p>Read the puzzle description on <a href="https://adventofcode.com/2022/day/12" id="-comhfw_159" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_158"><p>Check out the solution in the video:</p></li></ul><p id="-comhfw_154"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_160" width="25" height="25"> <a href="https://www.youtube.com/watch?v=tJ74hi_3sk8" id="-comhfw_161" data-external="true" rel="noopener noreferrer">Advent of Code 2022 Day 12 | Kotlin</a></p></section></section><section class="chapter"><h2 id="advent-of-code-2021" data-toc="advent-of-code-2021">Advent of Code 2021</h2><aside class="prompt" data-type="tip" data-title="" id="-comhfw_162"><p id="-comhfw_167">Read our <a href="https://blog.jetbrains.com/kotlin/2021/11/advent-of-code-2021-in-kotlin/" id="-comhfw_168" data-external="true" rel="noopener noreferrer">blog post about Advent of Code 2021</a></p></aside><section class="chapter"><h3 id="day-1-sonar-sweep" data-toc="day-1-sonar-sweep">Day 1: Sonar sweep</h3><p id="-comhfw_169">Apply windowed and count functions to work with pairs and triplets of integers.</p><ul class="list _bullet" id="-comhfw_170"><li class="list__item" id="-comhfw_172"><p>Read the puzzle description on <a href="https://adventofcode.com/2021/day/1" id="-comhfw_174" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_173"><p>Check out the solution from Anton Arhipov on the <a href="https://blog.jetbrains.com/kotlin/2021/12/advent-of-code-2021-in-kotlin-day-1" id="-comhfw_175" data-external="true" rel="noopener noreferrer">Kotlin Blog</a> or watch the video:</p></li></ul><p id="-comhfw_171"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_176" width="25" height="25"> <a href="https://www.youtube.com/watch?v=76IzmtOyiHw" id="-comhfw_177" data-external="true" rel="noopener noreferrer">Advent of Code 2021 in Kotlin, Day 1: Sonar Sweep</a></p></section><section class="chapter"><h3 id="day-2-dive" data-toc="day-2-dive">Day 2: Dive!</h3><p id="-comhfw_178">Learn about destructuring declarations and the <code class="code" id="-comhfw_181">when</code> expression.</p><ul class="list _bullet" id="-comhfw_179"><li class="list__item" id="-comhfw_182"><p>Read the puzzle description on <a href="https://adventofcode.com/2021/day/2" id="-comhfw_184" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_183"><p>Check out the solution from Pasha Finkelshteyn on <a href="https://github.com/asm0dey/aoc-2021/blob/main/src/Day02.kt" id="-comhfw_185" data-external="true" rel="noopener noreferrer">GitHub</a> or watch the video:</p></li></ul><p id="-comhfw_180"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_186" width="25" height="25"> <a href="https://www.youtube.com/watch?v=4A2WwniJdNc" id="-comhfw_187" data-external="true" rel="noopener noreferrer">Advent of Code 2021 in Kotlin, Day 2: Dive!</a></p></section><section class="chapter"><h3 id="day-3-binary-diagnostic" data-toc="day-3-binary-diagnostic">Day 3: Binary diagnostic</h3><p id="-comhfw_188">Explore different ways to work with binary numbers.</p><ul class="list _bullet" id="-comhfw_189"><li class="list__item" id="-comhfw_191"><p>Read the puzzle description on <a href="https://adventofcode.com/2021/day/3" id="-comhfw_193" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_192"><p>Check out the solution from Sebastian Aigner on <a href="https://blog.jetbrains.com/kotlin/2021/12/advent-of-code-2021-in-kotlin-day-3/" id="-comhfw_194" data-external="true" rel="noopener noreferrer">Kotlin Blog</a> or watch the video:</p></li></ul><p id="-comhfw_190"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_195" width="25" height="25"> <a href="https://www.youtube.com/watch?v=mF2PTnnOi8w" id="-comhfw_196" data-external="true" rel="noopener noreferrer">Advent of Code 2021 in Kotlin, Day 3: Binary Diagnostic</a></p></section><section class="chapter"><h3 id="day-4-giant-squid" data-toc="day-4-giant-squid">Day 4: Giant squid</h3><p id="-comhfw_197">Learn how to parse the input and introduce some domain classes for more convenient processing.</p><ul class="list _bullet" id="-comhfw_198"><li class="list__item" id="-comhfw_200"><p>Read the puzzle description on <a href="https://adventofcode.com/2021/day/4" id="-comhfw_202" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_201"><p>Check out the solution from Anton Arhipov on the <a href="https://github.com/antonarhipov/advent-of-code-2021/blob/main/src/Day04.kt" id="-comhfw_203" data-external="true" rel="noopener noreferrer">GitHub</a> or watch the video:</p></li></ul><p id="-comhfw_199"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_204" width="25" height="25"> <a href="https://www.youtube.com/watch?v=wL6sEoLezPQ" id="-comhfw_205" data-external="true" rel="noopener noreferrer">Advent of Code 2021 in Kotlin, Day 4: Giant Squid</a></p></section></section><section class="chapter"><h2 id="advent-of-code-2020" data-toc="advent-of-code-2020">Advent of Code 2020</h2><aside class="prompt" data-type="tip" data-title="" id="-comhfw_206"><p id="-comhfw_216">You can find all the solutions for the Advent of Code 2020 puzzles in our <a href="https://github.com/kotlin-hands-on/advent-of-code-2020/" id="-comhfw_217" data-external="true" rel="noopener noreferrer">GitHub repository</a>.</p></aside><section class="chapter"><h3 id="day-1-report-repair" data-toc="day-1-report-repair">Day 1: Report repair</h3><p id="-comhfw_218">Explore input handling, iterating over a list, different ways of building a map, and using the <a href="scope-functions.html#let" id="-comhfw_221"><code class="code" id="-comhfw_222">let</code></a> function to simplify your code.</p><ul class="list _bullet" id="-comhfw_219"><li class="list__item" id="-comhfw_223"><p>Read the puzzle description on <a href="https://adventofcode.com/2020/day/1" id="-comhfw_225" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_224"><p>Check out the solution from Svetlana Isakova on the <a href="https://blog.jetbrains.com/kotlin/2021/07/advent-of-code-in-idiomatic-kotlin/" id="-comhfw_226" data-external="true" rel="noopener noreferrer">Kotlin Blog</a> or watch the video:</p></li></ul><p id="-comhfw_220"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_227" width="25" height="25"> <a href="https://www.youtube.com/watch?v=o4emra1xm88" id="-comhfw_228" data-external="true" rel="noopener noreferrer">Learn Kotlin With the Kotlin Team: Advent of Code 2020 #1</a></p></section><section class="chapter"><h3 id="day-2-password-philosophy" data-toc="day-2-password-philosophy">Day 2: Password philosophy</h3><p id="-comhfw_229">Explore string utility functions, regular expressions, operations on collections, and how the <a href="scope-functions.html#let" id="-comhfw_232"><code class="code" id="-comhfw_233">let</code></a> function can be helpful to transform your expressions.</p><ul class="list _bullet" id="-comhfw_230"><li class="list__item" id="-comhfw_234"><p>Read the puzzle description on <a href="https://adventofcode.com/2020/day/2" id="-comhfw_236" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_235"><p>Check out the solution from Svetlana Isakova on the <a href="https://blog.jetbrains.com/kotlin/2021/07/advent-of-code-in-idiomatic-kotlin-day2/" id="-comhfw_237" data-external="true" rel="noopener noreferrer">Kotlin Blog</a> or watch the video:</p></li></ul><p id="-comhfw_231"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_238" width="25" height="25"> <a href="https://www.youtube.com/watch?v=MyvJ7G6aErQ" id="-comhfw_239" data-external="true" rel="noopener noreferrer">Learn Kotlin with The Kotlin Team: Advent of Code 2020 #2</a></p></section><section class="chapter"><h3 id="day-3-toboggan-trajectory" data-toc="day-3-toboggan-trajectory">Day 3: Toboggan trajectory</h3><p id="-comhfw_240">Compare imperative and more functional code styles, work with pairs and the <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/reduce.html" id="-comhfw_243" data-external="true" rel="noopener noreferrer"><code class="code" id="-comhfw_244">reduce()</code></a> function, edit code in the column selection mode, and fix integer overflows.</p><ul class="list _bullet" id="-comhfw_241"><li class="list__item" id="-comhfw_245"><p>Read the puzzle description on <a href="https://adventofcode.com/2020/day/3" id="-comhfw_247" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_246"><p>Check out the solution from Mikhail Dvorkin on <a href="https://github.com/kotlin-hands-on/advent-of-code-2020/blob/master/src/day03/day3.kt" id="-comhfw_248" data-external="true" rel="noopener noreferrer">GitHub</a> or watch the video:</p></li></ul><p id="-comhfw_242"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_249" width="25" height="25"> <a href="https://www.youtube.com/watch?v=ounCIclwOAw" id="-comhfw_250" data-external="true" rel="noopener noreferrer">Learn Kotlin with the Kotlin Team: Advent of Code 2020 #3</a></p></section><section class="chapter"><h3 id="day-4-passport-processing" data-toc="day-4-passport-processing">Day 4: Passport processing</h3><p id="-comhfw_251">Apply the <a href="control-flow.html#when-expressions-and-statements" id="-comhfw_254"><code class="code" id="-comhfw_255">when</code></a> expression and explore different ways of how to validate the input: utility functions, working with ranges, checking set membership, and matching a particular regular expression.</p><ul class="list _bullet" id="-comhfw_252"><li class="list__item" id="-comhfw_256"><p>Read the puzzle description on <a href="https://adventofcode.com/2020/day/4" id="-comhfw_258" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_257"><p>Check out the solution from Sebastian Aigner on the <a href="https://blog.jetbrains.com/kotlin/2021/09/validating-input-advent-of-code-in-kotlin/" id="-comhfw_259" data-external="true" rel="noopener noreferrer">Kotlin Blog</a> or watch the video:</p></li></ul><p id="-comhfw_253"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_260" width="25" height="25"> <a href="https://www.youtube.com/watch?v=-kltG4Ztv1s" id="-comhfw_261" data-external="true" rel="noopener noreferrer">Learn Kotlin with the Kotlin Team: Advent of Code 2020 #4</a></p></section><section class="chapter"><h3 id="day-5-binary-boarding" data-toc="day-5-binary-boarding">Day 5: Binary boarding</h3><p id="-comhfw_262">Use the Kotlin standard library functions (<code class="code" id="-comhfw_265">replace()</code>, <code class="code" id="-comhfw_266">toInt()</code>, <code class="code" id="-comhfw_267">find()</code>) to work with the binary representation of numbers, explore powerful local functions, and learn how to use the <code class="code" id="-comhfw_268">max()</code> function in Kotlin 1.5.</p><ul class="list _bullet" id="-comhfw_263"><li class="list__item" id="-comhfw_269"><p>Read the puzzle description on <a href="https://adventofcode.com/2020/day/5" id="-comhfw_271" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_270"><p>Check out the solution from Svetlana Isakova on the <a href="https://blog.jetbrains.com/kotlin/2021/09/idiomatic-kotlin-binary-representation/" id="-comhfw_272" data-external="true" rel="noopener noreferrer">Kotlin Blog</a> or watch the video:</p></li></ul><p id="-comhfw_264"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_273" width="25" height="25"> <a href="https://www.youtube.com/watch?v=XEFna3xyxeY" id="-comhfw_274" data-external="true" rel="noopener noreferrer">Learn Kotlin with the Kotlin Team: Advent of Code 2020 #5</a></p></section><section class="chapter"><h3 id="day-6-custom-customs" data-toc="day-6-custom-customs">Day 6: Custom customs</h3><p id="-comhfw_275">Learn how to group and count characters in strings and collections using the standard library functions: <code class="code" id="-comhfw_278">map()</code>, <code class="code" id="-comhfw_279">reduce()</code>, <code class="code" id="-comhfw_280">sumOf()</code>, <code class="code" id="-comhfw_281">intersect()</code>, and <code class="code" id="-comhfw_282">union()</code>.</p><ul class="list _bullet" id="-comhfw_276"><li class="list__item" id="-comhfw_283"><p>Read the puzzle description on <a href="https://adventofcode.com/2020/day/6" id="-comhfw_285" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_284"><p>Check out the solution from Anton Arhipov on the <a href="https://blog.jetbrains.com/kotlin/2021/09/idiomatic-kotlin-set-operations/" id="-comhfw_286" data-external="true" rel="noopener noreferrer">Kotlin Blog</a> or watch the video:</p></li></ul><p id="-comhfw_277"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_287" width="25" height="25"> <a href="https://www.youtube.com/watch?v=QLAB0kZ-Tqc" id="-comhfw_288" data-external="true" rel="noopener noreferrer">Learn Kotlin with the Kotlin Team: Advent of Code 2020 #6</a></p></section><section class="chapter"><h3 id="day-7-handy-haversacks" data-toc="day-7-handy-haversacks">Day 7: Handy haversacks</h3><p id="-comhfw_289">Learn how to use regular expressions, use Java's <code class="code" id="-comhfw_292">compute()</code> method for HashMaps from Kotlin for dynamic calculations of the value in the map, use the <code class="code" id="-comhfw_293">forEachLine()</code> function to read files, and compare two types of search algorithms: depth-first and breadth-first.</p><ul class="list _bullet" id="-comhfw_290"><li class="list__item" id="-comhfw_294"><p>Read the puzzle description on <a href="https://adventofcode.com/2020/day/7" id="-comhfw_296" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_295"><p>Check out the solution from Pasha Finkelshteyn on the <a href="https://blog.jetbrains.com/kotlin/2021/09/idiomatic-kotlin-traversing-trees/" id="-comhfw_297" data-external="true" rel="noopener noreferrer">Kotlin Blog</a> or watch the video:</p></li></ul><p id="-comhfw_291"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_298" width="25" height="25"> <a href="https://www.youtube.com/watch?v=KyZiveDXWHw" id="-comhfw_299" data-external="true" rel="noopener noreferrer">Learn Kotlin with the Kotlin Team: Advent of Code 2020 #7</a></p></section><section class="chapter"><h3 id="day-8-handheld-halting" data-toc="day-8-handheld-halting">Day 8: Handheld halting</h3><p id="-comhfw_300">Apply sealed classes and lambdas to represent instructions, apply Kotlin sets to discover loops in the program execution, use sequences and the <code class="code" id="-comhfw_303">sequence { }</code> builder function to construct a lazy collection, and try the experimental <code class="code" id="-comhfw_304">measureTimedValue()</code> function to check performance metrics.</p><ul class="list _bullet" id="-comhfw_301"><li class="list__item" id="-comhfw_305"><p>Read the puzzle description on <a href="https://adventofcode.com/2020/day/8" id="-comhfw_307" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_306"><p>Check out the solution from Sebastian Aigner on the <a href="https://blog.jetbrains.com/kotlin/2021/10/idiomatic-kotlin-simulating-a-console/" id="-comhfw_308" data-external="true" rel="noopener noreferrer">Kotlin Blog</a> or watch the video:</p></li></ul><p id="-comhfw_302"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_309" width="25" height="25"> <a href="https://www.youtube.com/watch?v=0GWTTSMatO8" id="-comhfw_310" data-external="true" rel="noopener noreferrer">Learn Kotlin with the Kotlin Team: Advent of Code 2020 #8</a></p></section><section class="chapter"><h3 id="day-9-encoding-error" data-toc="day-9-encoding-error">Day 9: Encoding error</h3><p id="-comhfw_311">Explore different ways to manipulate lists in Kotlin using the <code class="code" id="-comhfw_314">any()</code>, <code class="code" id="-comhfw_315">firstOrNull()</code>, <code class="code" id="-comhfw_316">firstNotNullOfOrNull()</code>, <code class="code" id="-comhfw_317">windowed()</code>, <code class="code" id="-comhfw_318">takeIf()</code>, and <code class="code" id="-comhfw_319">scan()</code> functions, which exemplify an idiomatic Kotlin style.</p><ul class="list _bullet" id="-comhfw_312"><li class="list__item" id="-comhfw_320"><p>Read the puzzle description on <a href="https://adventofcode.com/2020/day/9" id="-comhfw_322" data-external="true" rel="noopener noreferrer">Advent of Code</a></p></li><li class="list__item" id="-comhfw_321"><p>Check out the solution from Svetlana Isakova on the <a href="https://blog.jetbrains.com/kotlin/2021/10/idiomatic-kotlin-working-with-lists/" id="-comhfw_323" data-external="true" rel="noopener noreferrer">Kotlin Blog</a> or watch the video:</p></li></ul><p id="-comhfw_313"><img alt="YouTube" src="images/youtube.svg" class="inline-icon-" title="YouTube" id="-comhfw_324" width="25" height="25"> <a href="https://www.youtube.com/watch?v=vj3J9MuF1mI" id="-comhfw_325" data-external="true" rel="noopener noreferrer">Learn Kotlin with the Kotlin Team: Advent of Code 2020 #9</a></p></section></section><section class="chapter"><h2 id="what-s-next" data-toc="what-s-next">What's next?</h2><ul class="list _bullet" id="-comhfw_326"><li class="list__item" id="-comhfw_327"><p>Complete more tasks with <a href="koans.html" id="-comhfw_329">Kotlin Koans</a></p></li><li class="list__item" id="-comhfw_328"><p>Create working applications with the free <a href="https://hyperskill.org/tracks?category=4&amp;utm_source=jbkotlin_hs&amp;utm_medium=referral&amp;utm_campaign=kotlinlang-docs&amp;utm_content=button_1&amp;utm_term=22.03.23" id="-comhfw_330" data-external="true" rel="noopener noreferrer">Kotlin Core track</a> by JetBrains Academy</p></li></ul></section><div class="last-modified">Last modified: 06 November 2024</div><div data-feedback-placeholder="true"></div><div class="navigation-links _bottom"><a href="books.html" class="navigation-links__prev">Kotlin books</a><a href="edu-tools-learner.html" class="navigation-links__next">Learning Kotlin with JetBrains Academy plugin</a></div></article><div id="disqus_thread"></div></div></section></main></div><script src="static/v3/app.js"></script></body></html>

Pages: 1 2 3 4 5 6 7 8 9 10