From f728b02ad6537be8b8634419af196495e343dc87 Mon Sep 17 00:00:00 2001 From: Henk Kalkwater Date: Thu, 10 Dec 2020 09:58:18 +0100 Subject: [PATCH] Day 10 part 1 --- in/10.txt | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++ source/app.d | 2 ++ source/day10.d | 38 ++++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 in/10.txt create mode 100644 source/day10.d diff --git a/in/10.txt b/in/10.txt new file mode 100644 index 0000000..f7374a0 --- /dev/null +++ b/in/10.txt @@ -0,0 +1,95 @@ +76 +12 +97 +28 +132 +107 +145 +121 +84 +34 +115 +127 +22 +23 +11 +135 +113 +82 +140 +119 +69 +77 +83 +36 +13 +37 +92 +133 +122 +99 +147 +112 +42 +62 +65 +40 +123 +139 +33 +129 +149 +68 +41 +16 +48 +109 +5 +27 +142 +81 +90 +9 +78 +103 +26 +100 +141 +59 +55 +120 +126 +1 +35 +2 +20 +114 +58 +54 +10 +51 +116 +93 +6 +134 +108 +47 +70 +91 +138 +63 +19 +64 +148 +106 +21 +98 +43 +30 +146 +46 +128 +73 +94 +87 +29 diff --git a/source/app.d b/source/app.d index 252b291..bb2fd72 100644 --- a/source/app.d +++ b/source/app.d @@ -17,6 +17,7 @@ import day6; import day7; import day8; import day9; +import day10; import dayutil; immutable string progName = "aoc-2020"; @@ -35,6 +36,7 @@ Variant function(int, File, bool, string[])[] programs = [ &day7.run, &day8.run, &day9.run, + &day10.run, ]; void printUsage(string message = null) { diff --git a/source/day10.d b/source/day10.d new file mode 100644 index 0000000..3a5e8a9 --- /dev/null +++ b/source/day10.d @@ -0,0 +1,38 @@ +import std.array; +import std.algorithm; +import std.conv; +import std.range; +import std.stdio; +import std.variant; + +import dayutil; + +Variant run(int part, File input, bool bigboy, string[] args) { + uint[] numbers = input.byLineCopy.map!(to!uint).array ~ [0u]; + auto sortedNumbers = numbers.sort; + + Variant result = parts!size_t(part, + () => part1(sortedNumbers)); + return result; +} + +size_t part1(T)(SortedRange!(T[]) numbers) { + size_t gap1 = 0, gap3 = 0; + + foreach(window; numbers.slide!(No.withPartial)(2)) { + size_t diff = window[1] - window[0]; + if (diff == 3) gap3++; + if (diff == 1) gap1++; + } + // Device adapter is always rated 3 volts higher + gap3++; + writeln(gap1, ", ", gap3); + return gap1 * gap3; +} + +unittest { + uint[] numbers = [28, 33, 18, 42, 31, 14, 46, 20, 48, 47, 24, 23, 49, 45, 19, 38, 39, 11, 1, 32, + 25, 35, 8, 17, 7, 9, 4, 2, 34, 10, 3]; + numbers ~= [0u]; + assert(part1(numbers.sort) == 220); +}