Day 4 Part 1

This commit is contained in:
Marcel Fries
2019-12-04 13:49:33 +01:00
parent 43e317a598
commit c8d7b67888
8 changed files with 46 additions and 0 deletions

46
day-04/solver.nim Normal file
View File

@@ -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)