From c8d7b67888d74f792ff7f9b65af3e388d132b52e Mon Sep 17 00:00:00 2001 From: Marcel Fries Date: Wed, 4 Dec 2019 13:49:33 +0100 Subject: [PATCH] Day 4 Part 1 --- {day01 => day-01}/input.txt | 0 {day01 => day-01}/main.nim | 0 {day02 => day-02}/computer.nim | 0 {day02 => day-02}/input.txt | 0 {day02 => day-02}/main.nim | 0 {day03 => day-03}/input.txt | 0 {day03 => day-03}/main.nim | 0 day-04/solver.nim | 46 ++++++++++++++++++++++++++++++++++ 8 files changed, 46 insertions(+) rename {day01 => day-01}/input.txt (100%) rename {day01 => day-01}/main.nim (100%) rename {day02 => day-02}/computer.nim (100%) rename {day02 => day-02}/input.txt (100%) rename {day02 => day-02}/main.nim (100%) rename {day03 => day-03}/input.txt (100%) rename {day03 => day-03}/main.nim (100%) create mode 100644 day-04/solver.nim diff --git a/day01/input.txt b/day-01/input.txt similarity index 100% rename from day01/input.txt rename to day-01/input.txt diff --git a/day01/main.nim b/day-01/main.nim similarity index 100% rename from day01/main.nim rename to day-01/main.nim diff --git a/day02/computer.nim b/day-02/computer.nim similarity index 100% rename from day02/computer.nim rename to day-02/computer.nim diff --git a/day02/input.txt b/day-02/input.txt similarity index 100% rename from day02/input.txt rename to day-02/input.txt diff --git a/day02/main.nim b/day-02/main.nim similarity index 100% rename from day02/main.nim rename to day-02/main.nim diff --git a/day03/input.txt b/day-03/input.txt similarity index 100% rename from day03/input.txt rename to day-03/input.txt diff --git a/day03/main.nim b/day-03/main.nim similarity index 100% rename from day03/main.nim rename to day-03/main.nim diff --git a/day-04/solver.nim b/day-04/solver.nim new file mode 100644 index 0000000..74f41d6 --- /dev/null +++ b/day-04/solver.nim @@ -0,0 +1,46 @@ +import math + +# The puzzle input +const + inputLow = 171309 + inputHigh = 643603 + +iterator digitsLowToHight(number: int): int = + var num = number + while (num != 0): + yield num mod 10 + num = num.floorDiv(10) + +proc hasAdjacentSame(number: int): bool = + var previous = int.high + for digit in number.digitsLowToHight: + if (previous == digit): + return true + previous = digit + return false + +proc digitsAscending(number: int): bool = + var previous = int.high + for digit in number.digitsLowToHight: + if previous < digit: + return false + previous = digit + return true + +proc noLargerGroups(number: int): bool = + discard + +proc countCombinations(lower, upper: int): int = + ## Count all possible combinations between two bounds + for num in lower .. upper: + if (num.hasAdjacentSame and num.digitsAscending): + result.inc + +proc countCombinationsPart2(lower, upper: int): int = + ## Count all possible combinations between two bounds + for num in lower .. upper: + if (num.hasAdjacentSame and num.digitsAscending and num.noLargerGroups): + result.inc + +echo "Part 1: Number of combinations with in the range -> ", countCombinations(inputLow, inputHigh) +echo "Part 2: Number of combinations with in the range -> ", countCombinationsPart2(inputLow, inputHigh) \ No newline at end of file