コード例 #1
0
ファイル: day2.py プロジェクト: SickanK/advent-of-code
import math
import re
from tools.input import Input, strToArr, strToInt, intToStr, arrToStr

i = Input(2020, 2).getData()


def PartOne(info):
    info = strToArr(info)
    count = 0

    for i in info:
        arr = re.split('-|:| ', i)

        minc = int(arr[0])
        maxc = int(arr[1])
        c = arr[2]
        text = list(arr[4])
        ccount = 0

        for x in text:
            if (x == c):
                ccount += 1

        if (ccount <= maxc and ccount >= minc):
            count += 1

        ccount = 0

    return count
コード例 #2
0
import timeit
import math
import re
from tools.input import Input, strToArr, strToInt, intToStr, arrToStr, newlineParse

i = Input(2020, 15).getData()
# i = Input(2020, 15).getFromExample()


def PartOne(info, num=2020):
    # noParseInfo = info
    info = strToArr(info)
    lastTurns = {}
    lastNum = int(info[0])
    firstNum = True

    for i in range(len(info)):
        s = str(info[i])
        lastTurns[s] = [i + 1]
        lastNum = str(s)

    for i in range(len(info) + 1, num + 1):
        if (firstNum):
            lastNum = "0"
            firstNum = False
            if (lastTurns.get(lastNum) is None):
                lastTurns[lastNum] = [i]
            else:
                turnBeforeLast = lastTurns[lastNum][-1]
                lastTurns[lastNum] = [int(turnBeforeLast), i]
        else:
コード例 #3
0
ファイル: day3.py プロジェクト: SickanK/advent-of-code
import math
from tools.input import Input, strToArr, strToInt, intToStr, arrToStr

i = Input(2019, 3).getData()


def PartOne(info):
    info = strToArr(info)
    """
        Create whole path with vectors for one
        Do the same with other path, comparing if any value goes over or under the other
        Compare the vector with highest sum
    """
    l = int(len(info))

    arr1 = info[0:int(l/2)]
    path1 = [0, 0]
    path1Snapshots = []

    arr2 = info[int(l/2):int(l)]
    path2 = [0, 0]
    path2Snapshots = []

    intersections = []

    for i in arr1:
        num = int(i[1:])
        if(i[:1] == "U"):
            path1[0] += num
        if(i[:1] == "D"):
            path1[0] -= num
コード例 #4
0
import timeit
import math
import re
from tools.input import Input, strToArr, strToInt, intToStr, arrToStr, newlineParse

i = Input(2020, 16).getData()
# i = Input(2020, 16).getFromExample()


def PartOne(info):
    # noParseInfo = info
    info = strToArr(info)
    info = newlineParse(info)
    validRange = {}

    for i in info[0]:
        ranges = re.findall(r"\d*\-\d*", i)
        if (ranges):
            for r in ranges:
                o = r.split("-")
                for i in range(int(o[0]), int(o[1]) + 1):
                    validRange[str(i)] = ""

    errorRate = 0

    for i in info[2][1::]:
        n = int(i)
        if (validRange.get(i) is None):
            errorRate += n

    return errorRate
コード例 #5
0
import timeit
import math
import re
from tools.input import Input, strToArr, strToInt, intToStr, arrToStr, newlineParse

i = Input(2020, 20).getData()[::-1][1::][::-1]
# i = Input(2020, 20).getFromExample()


def rotateX(block):
    return block[::-1]


def rotateY(block):
    flipped = []
    for i in block:
        flipped.append(i[::-1])
    return flipped


def rotate(matrix, degree):
    if abs(degree) not in [0, 90, 180, 270, 360]:
        return matrix
    if degree == 0:
        return matrix
    elif degree > 0:
        return rotate(zip(*matrix[::-1]), degree-90)
    else:
        return rotate(zip(*matrix[::-1]), degree+90)

コード例 #6
0
ファイル: day17.py プロジェクト: SickanK/advent-of-code
import numpy as np
import timeit
import math
import re
from tools.input import Input, strToArr, strToInt, intToStr, arrToStr, newlineParse

i = Input(2020, 17).getData()
i = Input(2020, 17).getFromExample()


def findValidNeighbours(vec, arr):
    total = 0
    arrMult = [[0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0],
               [-1, -1]]

    for i in range(8):
        tVec = [*vec]
        tVec[0] += arrMult[i][0]
        tVec[1] += arrMult[i][1]
        if (tVec[0] >= 0 and tVec[1] >= 0 and tVec[1] < len(arr)
                and tVec[0] < len(arr[tVec[1]])):
            cVal = arr[tVec[1]][tVec[0]]
            if (cVal == "#"):
                total += 1
            tVec[0] += arrMult[i][0]
            tVec[1] += arrMult[i][1]
    return total


def PartOne(info):
    # noParseInfo = info
コード例 #7
0
import timeit
import math
import re
from tools.input import Input, strToArr, strToInt, intToStr, arrToStr, newlineParse

i = Input(2020, 14).getData()
# i = Input(2020, 14).getFromExample()


def convertToBinary(val):
    valueLeft = val
    newVal = []
    for i in range(35, -1, -1):
        c = math.floor(valueLeft / (2**i))
        if (c == 1):
            valueLeft -= 2**i
        newVal.append(str(c))
    return "".join(newVal)


def convertToDecimal(val):
    total = 0
    newVal = list(val)[::-1]
    for i in range(0, 36):
        total += int(newVal[i]) * (2**i)

    return total


def maskBinary(val, mask):
    newVal = list(val)
コード例 #8
0
import math
from tools.input import Input, strToArr, strToInt, intToStr, arrToStr

i = Input(2019, 6).getData()

# Create 2 of this recursive function


def recursiveP1(argument, obj, total):
    try:
        for x in obj[argument]:
            total["p"] += 1
            recursiveP1(x, obj, total)
    except:
        return 0

    return 0


def recursiveP2(argument, obj, total, SAN, YOU):
    try:
        if (SAN != None):
            SAN.append(argument)

        if (YOU != None):
            YOU.append(argument)

        for x in obj[argument]:
            total["p"] += 1
            recursiveP2(x, obj, total, SAN, YOU)
    except:
コード例 #9
0
import math
from tools.input import Input, strToArr, strToInt, intToStr, arrToStr

i = Input(2019, 5).getData()


def PartOne(info):
    return 0


def PartTwo(info):
    return 0


print(i)
print(PartOne(i))
print(PartTwo(i))
コード例 #10
0
import timeit
import math
import re
from tools.input import Input, strToArr, strToInt, intToStr, arrToStr, newlineParse

i = Input(2020, 18).getData()
# i = Input(2020, 18).getFromExample()


def PartOne(info):
    # noParseInfo = info
    info = strToArr(info)

    total = 0
    for i in info:
        sumArr = []
        for _ in range(200):
            tempSumArr = []
            if (sumArr == []):
                inside = re.split(r"(\([0-9*+ ]*\))", i)
            else:
                inside = re.split(r"(\([0-9*+ ]*\))", "".join(sumArr))

            toFind = re.findall(r"(\([0-9*+ ]*\))", "".join(inside))
            for s in inside:
                if (s in toFind):
                    nums = re.split(r"\s|[()]", s)[1::][::-1][1::][::-1]
                    totali = 0
                    for n in enumerate(nums):
                        sign = n[1]
                        if (sign == "*"):
コード例 #11
0
ファイル: day2.py プロジェクト: SickanK/advent-of-code
import math
from tools.input import Input, strToArr, strToInt, intToStr, arrToStr

i = Input(2019, 2).getData()


def PartOne(info, first, second):
    info = strToArr(info)
    info = strToInt(info)
    info[1] = first
    info[2] = second
    i = 0
    while (i < len(info)):
        if (info[i] == 1):
            info[info[i + 3]] = info[info[i + 1]] + info[info[i + 2]]
            i += 4
        elif (info[i] == 2):
            info[info[i + 3]] = info[info[i + 1]] * info[info[i + 2]]
            i += 4
        elif (info[i] == 99):
            break

    return info[0]


def PartTwo(info):
    for x in range(0, 100):
        for y in range(0, 50):
            if (PartOne(i, x, y) == 19690720):
                return 100 * x + y
    return 0
コード例 #12
0
import math
import re
from tools.input import Input, strToArr, strToInt, intToStr, arrToStr, newlineParse

i = Input(2020, 11).getData()
# i = Input(2020, 11).getFromExample()


def PartOne(info):
    info = strToArr(info)

    for i in range(len(info)):
        info[i] = list(info[i])

    for _ in range(100):
        arr = []
        for a in info:
            arr.append([*a])

        for row in range(len(info)):

            for indexOfRow in range(len(info[row])):
                ut = []
                for a in arr:
                    ut.append([*a])
                    adjacentSeats = 0

                if(ut[row][indexOfRow-1] == "#" and indexOfRow > 0):
                    adjacentSeats += 1

                if (indexOfRow < len(ut[row])-1):
コード例 #13
0
import math
from tools.input import Input, strToArr, strToInt

i = Input(2019, 1).getData()


def PartOne(info):
    info = strToArr(info)
    info = strToInt(info)
    sum = 0
    for i in info:
        sum += math.floor(i / 3) - 2
    return sum


def PartTwo(info):
    info = strToArr(info)
    info = strToInt(info)
    sum, s = 0, 0

    for i in info:
        s = math.floor(i / 3) - 2
        while (s >= 0):
            sum += s
            s = math.floor(s / 3) - 2

    return sum


print(PartOne(i))
print(PartTwo(i))
コード例 #14
0
ファイル: day3.py プロジェクト: SickanK/advent-of-code
import math
import re
from tools.input import Input, strToArr, strToInt, intToStr, arrToStr

i = Input(2020, 3).getData()


def PartOne(info, right=3, down=1):
    info = strToArr(info)

    i, total, info = 0, 0, info[0:len(info):down]
    for t in list(info):
        if(i >= len(t)):
            i -= len(t)

        if(t[i] == '#'):
            total += 1
        i += right

    return total


def PartTwo(info):
    t1 = PartOne(info, 1, 1)
    t2 = PartOne(info, 3, 1)
    t3 = PartOne(info, 5, 1)
    t4 = PartOne(info, 7, 1)
    t5 = PartOne(info, 1, 2)

    return t1 * t2 * t3 * t4 * t5
コード例 #15
0
ファイル: day10.py プロジェクト: SickanK/advent-of-code
import math
import re
from tools.input import Input, strToArr, strToInt, intToStr, arrToStr, newlineParse

i = Input(2020, 10).getData()
# i = Input(2020, 10).getFromExample()


def PartOne(info):
    info = strToArr(info)
    info = strToInt(info)
    info.append(0)
    info = sorted(info)
    info.append(info[len(info) - 1] + 3)

    one = 0
    three = 0
    last = 0

    for i in info:
        if (i - last == 3):
            three += 1
        if (i - last == 1):
            one += 1
        last = i

    return one * three


def PartTwo(info):
    info = strToArr(info)
コード例 #16
0
import math
import re
from tools.input import Input, strToArr, strToInt, intToStr, arrToStr, newlineParse

i = Input(2020, 9).getData()
# i = Input(2020, 9).getFromExample()


def PartOne(info, step=25):
    info = strToArr(info)
    info = strToInt(info)

    startIndex = step
    currentNum = info[startIndex]
    isSum = False
    for i in range(startIndex, len(info)):
        currentNum = info[i]
        for j in range(i - startIndex, i):
            for u in range(i - startIndex, i):
                if (info[j] + info[u] == info[i]):
                    isSum = True
        if (isSum):
            isSum = False
        else:
            break

    return currentNum


def sumOfRange(arr, start, end):
    total = 0