def PartTwo(info): info = strToArr(info) output = [] lastOutput = [] state = [] for i in info: output.append(list(i)) state.append(list(i)) while output != lastOutput: for i in range(len(output)): for j in range(len(output[i])): changedVal = output[i][j] neighbours = findValidNeighbours([j, i], output) if(changedVal == "L" and neighbours == 0): state[i][j] = "#" if(changedVal == "#" and neighbours >= 5): state[i][j] = "L" lastOutput = [*output] for i in range(len(state)): output[i] = list("".join(state[i])) occupiedSeats = 0 for i in output: for j in list(i): if(j == "#"): occupiedSeats += 1 return occupiedSeats
def PartTwo(info): info = strToArr(info) info = newlineParse(info) t1 = 0 t2 = [] for i in info: t2.append(i) for l in t2: d = list(dict.fromkeys(l)) if(len(l) == 1): t1 += len(l[0]) elif(len(d) == 1): t1 += len(d[0]) else: e = [] for q in l: for x in q: e.append(x) de = list(dict.fromkeys(e)) for f in range(len(list(de))): if(e.count(de[f]) == len(l)): t1 += 1 return t1
def PartOne(info): info = strToArr(info) info = strToInt(info) sum = 0 for i in info: sum += math.floor(i / 3) - 2 return sum
def PartOne(info, changeIndex=-1, changeInfo=""): info = strToArr(info) acc = 0 i = 0 knownValues = [] if(changeIndex != -1): print(changeIndex) info[changeIndex] = changeInfo while i < len(info): a = info[i].split(" ") if(a[0] == "acc"): o = re.split(r"\-|\+", a[1]) if(a[1][0] == "-"): acc -= int(o[1]) else: acc += int(o[1]) if(a[0] == "jmp"): o = re.split(r"\-|\+", a[1]) if(a[1][0] == "-"): i -= int(o[1]) else: i += int(o[1]) else: i += 1 knownValues.append(i) for e in knownValues: if knownValues.count(e) > 1: return acc return acc
def PartOne(info): info = strToArr(info) info = newlineParse(info) arr = [] tempArr = [] for a in info: if(a == ""): arr.append(tempArr) tempArr = [] else: tempArr.append(a) arr.append(tempArr) info = arr t1 = 0 for i in info: t2 = [] for a in i: for l in a: t2.append(l) t1 += len(list(dict.fromkeys(t2))) t2 = [] return t1
def PartOne(info): info = strToArr(info) arr = [] tempArr = [] for a in info: if (a == ""): arr.append(tempArr) tempArr = [] else: tempArr.append(a) info = arr valid = 0 for i in info: c = 0 j = " ".join(i) j = j.split(":") for x in j: l = re.findall(r"ecl|iyr|eyr|hgt|hcl|byr|pid", x) if (len(l) > 0): c += 1 if (c >= 7): print("Valid") valid += 1 c = 0 return valid + 1
def PartTwo(info): info = strToArr(info) ids = [] for i in info: fbmin = 0 fbmax = 127 lrmin = 0 lrmax = 7 for x in i: if(x == "F"): d = fbmax-fbmin fbmax -= math.floor(d/2) if(x == "B"): a = fbmax-fbmin fbmin += math.ceil(a/2) if(x == "L"): s = lrmax-lrmin lrmax -= math.floor(s/2) if(x == "R"): y = lrmax-lrmin lrmin += math.ceil(y/2) ids.append(fbmin * 8 + lrmin) ids = sorted(ids) missing = 0 for i in range(1, len(ids)-1): if(ids[i+1]-1 != ids[i]): missing = ids[i] + 1 return missing
def PartOne(info): info = strToArr(info) directions = ["E", "S", "W", "N"] instructions = { "E": [1, 0], "S": [0, 1], "W": [-1, 0], "N": [0, -1], "R": 1, "L": -1 } direction = 0 vector = [0, 0] for i in info: arguments = re.findall(r"[a-zA-Z]|[\d]*", i) if (re.match(r"[ESWN]", i)): vector[0] += instructions[arguments[0]][0] * int(arguments[1]) vector[1] += instructions[arguments[0]][1] * int(arguments[1]) elif (re.match(r"[RL]", i) != None): direction += instructions[arguments[0]] * (int(arguments[1]) / 90) direction = int((direction + 4) % 4) else: dire = directions[direction] vector[0] += instructions[dire][0] * int(arguments[1]) vector[1] += instructions[dire][1] * int(arguments[1]) return abs(vector[0]) + abs(vector[1])
def PartOne(info): info = strToArr(info) highid = 0 for i in info: fbmin = 0 fbmax = 127 lrmin = 0 lrmax = 7 for x in i: if(x == "F"): d = fbmax-fbmin fbmax -= math.floor(d/2) if(x == "B"): a = fbmax-fbmin fbmin += math.ceil(a/2) if(x == "L"): s = lrmax-lrmin lrmax -= math.floor(s/2) if(x == "R"): y = lrmax-lrmin lrmin += math.ceil(y/2) tid = fbmin * 8 + lrmin if(tid > highid): highid = tid return highid
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: turnBeforeLast = lastTurns[lastNum][-2] turnLast = lastTurns[lastNum][-1] lastNum = str(int(turnLast) - int(turnBeforeLast)) if (lastTurns.get(lastNum) is None): lastTurns[lastNum] = [i] firstNum = True else: turnBeforeLast = lastTurns[lastNum][-1] lastTurns[lastNum] = [int(turnBeforeLast), i] return lastNum
def PartTwo(info): info = strToArr(info) sa = [] for i in info: sa.append(i.split("contain")) pa = [] tempPa = [] for s in sa: if (len(s) < 2): tempPa.append(s[0]) else: pa.append(tempPa) tempPa = [] tempPa.append(s[0]) tempPa.append(s[1]) pa.append(tempPa) cts = [] recursiveP2(pa[1::], "shiny gold bags ", cts) total = 0 for i in cts: total += i return total
def PartTwo(info): info = strToArr(info) obj = {} tempArr = [] total = {"p": 0, "SAN": [], "YOU": []} for i in info: s = i.split(")") for j in info: c = j.split(")") if (c[1] == s[1]): tempArr.append(c[0]) obj[s[1]] = tempArr tempArr = [] SAN = [] YOU = [] a = [] recursiveP2("YOU", obj, total, None, YOU) recursiveP2("SAN", obj, total, SAN, None) for i in range(0, len(SAN)): for j in range(0, len(YOU)): if (SAN[i] == YOU[j]): a.append([SAN[i], YOU[j], [i, j]]) return SAN, YOU
def PartOne(info): info = strToArr(info) # info = newlineParse(info) sa = [] res = [] for i in info: sa.append(i.split("contain")) pa = [] tempPa = [] for s in sa: if (len(s) < 2): tempPa.append(s[0]) else: pa.append(tempPa) tempPa = [] tempPa.append(s[0]) tempPa.append(s[1]) pa.append(tempPa) containsShiny = [] tempActive = "" for a in pa: for o in a: if (tempActive == ""): tempActive = o else: r = re.findall(r"shiny gold", o) if (len(r) > 0): nTempActive = tempActive[::-1][2::][::-1] containsShiny.append(nTempActive) tempActive = "" tempActive2 = "" c = False for _ in range(0, 6): for a in pa: for o in a: if (tempActive2 == ""): tempActive2 = o else: for p in containsShiny: r = re.findall(rf"{p}", o) if (len(r) > 0): c = True if (c == True): nTempActive2 = tempActive2[::-1][2::][::-1] containsShiny.append(nTempActive2) c = False [res.append(x) for x in containsShiny if x not in res] tempActive2 = "" return len(res)
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): if(ut[row][indexOfRow+1] == "#"): adjacentSeats += 1 for y in range(-1, 2): if (((y == 1 and indexOfRow < len(ut[row])-1) or y != 1) and row > 0): if(y != -1): if(ut[row-1][indexOfRow+y] == "#"): adjacentSeats += 1 elif(indexOfRow > 0): if(ut[row-1][indexOfRow+y] == "#"): adjacentSeats += 1 for y in range(-1, 2): if (((y == 1 and indexOfRow < len(ut[row])-1) or y != 1) and row < len(ut)-1): if(y != -1): if(ut[row+1][indexOfRow+y] == "#"): adjacentSeats += 1 elif(indexOfRow > 0): if(ut[row+1][indexOfRow+y] == "#"): adjacentSeats += 1 if(ut[row][indexOfRow] == "L" and adjacentSeats == 0): info[row][indexOfRow] = "#" if(ut[row][indexOfRow] == "#" and adjacentSeats >= 4): info[row][indexOfRow] = "L" occupiedSeats = 0 for i in info: for j in list(i): if(j == "#"): occupiedSeats += 1 return occupiedSeats
def PartTwo(info): info = strToArr(info) arr = [] tempArr = [] for a in info: if (a == ""): arr.append(tempArr) tempArr = [] else: tempArr.append(a) info = arr count = 0 valid = 0 for x in info: for l in x: l = l.split(" ") for w in l: u = w.split(":") if (u[0] == "byr"): if (int(u[1]) >= 1920 and int(u[1]) <= 2002): count += 1 if (u[0] == "iyr"): if (int(u[1]) >= 2010 and int(u[1]) <= 2020): count += 1 if (u[0] == "eyr"): if (int(u[1]) >= 2020 and int(u[1]) <= 2030): count += 1 if (u[0] == "hgt"): s = re.findall(r"cm", u[1]) b = int(re.split(r"cm|in", u[1])[0]) if (len(s) > 0): if (b >= 150 and b <= 193): count += 1 else: if (b >= 59 and b <= 76): count += 1 if (u[0] == "hcl"): o = re.match(r"#([A-Fa-f0-9]{6})", u[1]) if (o != None): count += 1 if (u[0] == "ecl"): o = re.findall(r"amb|blu|brn|gry|grn|hzl|oth", u[1]) if (len(o) > 0): count += 1 if (u[0] == "pid"): o = str(u[1]) if (len(o) == 9): count += 1 if (count >= 7): valid += 1 count = 0 return valid
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
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 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): noParsedInfo = info info = strToArr(info) i = 0 changeIndex = -1 knownValues = [] changedInfo = [*info] restarted = False while i < len(changedInfo): if(restarted == True): restarted = False changedInfo = [*info] knownValues = [] for j in range(len(changedInfo)): if(j > changeIndex): y = changedInfo[j].split(" ") if(y[0] == "nop"): changedInfo[j] = f"jmp {y[1]}" changeIndex = j break if(y[0] == "jmp"): changedInfo[j] = f"nop {y[1]}" changeIndex = j break a = changedInfo[i].split(" ") if(a[0] == "jmp"): o = re.split(r"\-|\+", a[1]) if(a[1][0] == "-"): i -= int(o[1]) else: i += int(o[1]) else: i += 1 knownValues.append(i) for e in knownValues: if knownValues.count(e) > 1: restarted = True return PartOne(noParsedInfo, changeIndex, changedInfo[changeIndex])
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 if(i[:1] == "R"): path1[1] += num if(i[:1] == "L"): path1[1] -= num path1Snapshots.append([path1[0], path1[1]]) for i in arr2: num = int(i[1:]) if(i[:1] == "U"): path2[0] += num if(i[:1] == "D"): path2[0] -= num if(i[:1] == "R"): path2[1] += num if(i[:1] == "L"): path2[1] -= num path2Snapshots.append([path2[0], path2[1]]) return intersections
def PartTwo(info): noParseInfo = info info = strToArr(info) info = newlineParse(info) print(PartOne(noParseInfo)[0]) ids = PartOne(noParseInfo)[1] print(ids) corners = PartOne(noParseInfo)[2] leftUpCorner = "" for i in corners: presentCorners = [] print(i) for j in ids[i]: print(j) presentCorners.append(j[1]) if("top" in presentCorners and "right" in presentCorners): leftUpCorner = i sortedInfo = [] height = getLengthInDir(info, 1, ids, leftUpCorner, "top") width = getLengthInDir(info, 1, ids, leftUpCorner, "right") print(width) for s in range(height): sortedInfo.append([]) print(sortedInfo) stripped = [] for i in info: r = i[1::] ts = [] for j in r[1::][:: -1][1::][:: -1]: s = j[1::][:: -1][1::][:: -1] ts.append(s) stripped.append(ts) info = stripped # for i in info: # for j in i: # print(j) return 0
def PartTwo(info): noParseInfo = info info = strToArr(info) info = strToInt(info) nst = PartOne(noParseInfo, 25) r, i, j = [], 0, 0 while i < len(info): if (sumOfRange(info, i, j) < nst): j += 1 elif (sumOfRange(info, i, j) > nst): i += 1 else: for u in range(i, j): r.append(info[u]) break r = sorted(r) return r[0] + r[len(r) - 1]
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 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 PartOne(info): info = strToArr(info) fInfo = [] for i in info: if (i != "x"): fInfo.append(int(i)) info = fInfo earlyDep = fInfo[0] margin = [0, 0] tMargin = 0 for i in info[1::]: while tMargin < earlyDep: tMargin += i if (tMargin - earlyDep < margin[1] or margin[1] == 0): margin[0] = i margin[1] = tMargin - earlyDep tMargin = 0 return margin[0] * margin[1]
def PartOne(info): info = strToArr(info) obj = {} tempArr = [] total = {"p": 0} for i in info: s = i.split(")") for j in info: c = j.split(")") if (c[1] == s[1]): tempArr.append(c[0]) obj[s[1]] = tempArr tempArr = [] for i in info: s = i.split(")")[1] recursiveP1(s, obj, total) return total["p"]
def PartTwo(info): info = strToArr(info) instructions = { "E": [1, 0], "S": [0, 1], "W": [-1, 0], "N": [0, -1], "R": 1, "L": -1 } ship = [0, 0] waypoint = [10, -1] for i in info: arguments = re.findall(r"[a-zA-Z]|[\d]*", i) if (re.match(r"[ESWN]", i)): waypoint[0] += instructions[arguments[0]][0] * int(arguments[1]) waypoint[1] += instructions[arguments[0]][1] * int(arguments[1]) elif (re.match(r"[RL]", i) != None): direction = int(instructions[arguments[0]] * (int(arguments[1]) / 90)) savedWaypoint = [*waypoint] if (direction == -1): direction = 3 elif (direction == -3): direction = 1 if (direction == 1): waypoint[0] = savedWaypoint[1] * -1 waypoint[1] = savedWaypoint[0] elif (abs(direction) == 2): waypoint[0] *= -1 waypoint[1] *= -1 elif (direction == 3): waypoint[0] = savedWaypoint[1] waypoint[1] = savedWaypoint[0] * -1 else: ship[0] += waypoint[0] * int(arguments[1]) ship[1] += waypoint[1] * int(arguments[1]) return abs(ship[0]) + abs(ship[1])
def PartTwo(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] nu = "".join(nums) noadd = re.split(r"[^\d^\+^\d]", nu) toMult = [] for a in noadd: toMult.append(sum(a.split("+"))) tempSumArr.append(str(mult(toMult))) else: tempSumArr.append(s) sumArr = tempSumArr sumArr = "".join(sumArr) sumArr = sumArr.split(" ") nu = "".join(sumArr) noadd = re.split(r"[^\d^\+^\d]", nu) toMult = [] for a in noadd: toMult.append(sum(a.split("+"))) total += mult(toMult) return total
def PartTwo(info): info = strToArr(info) info = strToInt(info) info = sorted(info) arr = [] for i in range(0, len(info) - 1): if (info[i] - info[i - 1] < 3 and info[i + 1] - info[i] < 3): arr.append(info[i]) subArrays = [] tempSubArrays = [] for i in range(0, len(arr)): if (arr[i - 1] + 1 == arr[i]): tempSubArrays.append(arr[i]) else: subArrays.append(tempSubArrays) tempSubArrays = [] tempSubArrays.append(arr[i]) subArrays.append(tempSubArrays) subArrays = subArrays[1::] numArr = [] for i in subArrays: if (len(i) == 3): numArr.append(7) elif (len(i) == 2): numArr.append(4) elif (len(i) == 1): numArr.append(2) total = 1 for i in numArr: total *= i return total
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