Exemplo n.º 1
0
def countTrees(r, d):
    pattern = utils.getLinesFromFile("day3.input")
    trees = 0
    offset = 0

    for i in range(0, len(pattern), d):
        if pattern[i][offset] == '#':
            trees += 1
        offset += r
        offset %= len(pattern[i])
    return trees
Exemplo n.º 2
0
import utils

yesCount1 = 0
yesCount2 = 0
groupcount1 = []
groupcount2 = []


for a in utils.getLinesFromFile("day6.input"):
	if len(a) == 0:
		yesCount1 += len(groupcount1)
		yesCount2 += len(groupcount2)
		groupcount1 = []
		groupcount2 = []
		continue
	if len(groupcount1) == 0:
		groupcount2 = set(a)
	else:
		groupcount2 = groupcount2&set(a)
	for l in a:
		if not l in groupcount1:
			groupcount1.append(l)
# last line
yesCount1 += len(groupcount1)
yesCount2 += len(groupcount2)

print(f"Part 1: {yesCount1}")
print(f"Part 2: {yesCount2}")
Exemplo n.º 3
0
from statistics import median
import utils, sys
data = utils.getLinesFromFile("day7.input")

# data = ["16,1,2,0,4,2,7,1,2,14"]


def getCostPart2(a, b):
    dist = abs(a - b)
    return round((dist * (dist + 1)) / 2)


def part1():
    pos = [int(x) for x in data[0].split(',')]
    target = round(median(pos))
    ret = 0
    for p in pos:
        ret += abs(p - target)
    return ret


def part2():
    pos = [int(x) for x in data[0].split(',')]
    leastCost = sys.maxsize
    for target in range(min(pos), max(pos)):
        cost = 0
        for p in pos:
            cost += getCostPart2(target, p)
        # print(target, cost)
        leastCost = min(leastCost, cost)
    return leastCost
Exemplo n.º 4
0
Arquivo: day8.py Projeto: Bug38/AoC
import utils
bootCode = utils.getLinesFromFile("day8.input")

#bootCode = ['nop +0','acc +1','jmp +4','acc +3','jmp -3','acc -99','acc +1','jmp -4','acc +6']


def exec(codeLine, PC, accumulator):
    inst, value = codeLine.split()
    if inst == 'nop':
        PC += 1
        return PC, accumulator
    if inst == "acc":
        PC += 1
        accumulator += int(value)
        return PC, accumulator
    if inst == 'jmp':
        PC += int(value)
        return PC, accumulator


def execProgram(lineToChange):
    accumulator = 0
    PC = 0
    executedLines = []
    lowestLine = 0
    while (not PC in executedLines) and (PC <= len(bootCode) - 1):
        executedLines.append(PC)
        if PC >= lowestLine:
            lowestLine = PC
        if lineToChange and PC == lineToChange:
            PC, accumulator = exec(
Exemplo n.º 5
0
import utils

passwords = utils.getLinesFromFile("day2.input")

okPwdPart1 = []
okPwdPart2 = []

# PART 1
for p in passwords:
	limits, letter, pwd = p.split()
	limits = [int(x) for x in limits.split('-')]
	#print(limits, letter, pwd)
	nb = pwd.count(letter[0])
	if nb <= limits[1] and nb >= limits[0]:
		okPwdPart1.append(p)
print("Part 1:", len(okPwdPart1))


# PART 2
for p in passwords:
	pos, letter, pwd = p.split()
	pos = [int(x) for x in pos.split('-')]
	nb = (pwd[pos[0] -1] == letter[0]) + (pwd[pos[1] -1] == letter[0])
	if nb == 1:
		okPwdPart2.append(p)
print("Part 2:", len(okPwdPart2))


Exemplo n.º 6
0
Arquivo: day11.py Projeto: Bug38/AoC
import utils
inputSeats = utils.getLinesFromFile("day11.input")

# inputSeats = ['L.LL.LL.LL','LLLLLLL.LL','L.L.L..L..','LLLL.LL.LL','L.LL.LL.LL','L.LLLLL.LL','..L.L.....','LLLLLLLLLL','L.LLLLLL.L','L.LLLLL.LL']

rows = len(inputSeats)
columns = len(inputSeats[1])
seats = [['.' for c in range(columns)] for r in range(rows)]

def getNextState(r, c, isPart2):
	# Part 1
	if not isPart2:
		adjacentOccupied = 0
		for i, j in [(-1,-1),(0,-1),(1,-1),(-1,0),(1,0),(-1,1),(0,1),(1,1)]:
			if r+i >= 0 and c+j >= 0 and r+i < rows and c+j < columns:
				adjacentOccupied += (seats[r+i][c+j] == '#')
		if seats[r][c] == 'L' and not adjacentOccupied:
			return '#'
		elif seats[r][c] == '#' and adjacentOccupied >= 4:
			return 'L'
		return seats[r][c]
	# Part 2
	adjacentOccupied = 0
	for i, j in [(-1,-1),(0,-1),(1,-1),(-1,0),(1,0),(-1,1),(0,1),(1,1)]:
		_r, _c = r+i, c+j
		foundOccupied = False
		while _r >= 0 and _c >= 0 and _r < rows and _c < columns and seats[_r][_c] != 'L' and not foundOccupied:
			foundOccupied = seats[_r][_c] == '#'
			_r, _c = _r+i, _c+j
		adjacentOccupied += foundOccupied
	if seats[r][c] == 'L' and not adjacentOccupied:
Exemplo n.º 7
0
import utils

lines = utils.getLinesFromFile("day4.input")

def getPassports(l):
	p = []
	_p = {}
	for _l in l:
		if not len(_l):
			if len(_p):
				p.append(_p)
				_p = {}
		for e in _l.split():
			k,v = e.split(':')
			_p[k] = v
	if len(_p):
		p.append(_p)
		_p = {}
	return p

def isValid(p, securityLevel):
	if securityLevel == 1:
		if len(p) == 8:
			return True
		else:
			if len(p) == 7 and not 'cid' in p:
				return True
		return False
	if securityLevel == 2:
		if not isValid(p, 1):
			return False
Exemplo n.º 8
0
Arquivo: day12.py Projeto: Bug38/AoC
import utils
instructions = utils.getLinesFromFile("day12.input")

# instructions = ['F10','N3','F7','R90','F11']

def part1():
	direction = 'E'
	cardinals = ['N', 'E', 'S', 'W']
	posEW, posNS = 0,0

	def applyInstruction(instruction):
		nonlocal posNS, posEW, direction
		i, nb = instruction[0], int(instruction[1:])
		if i == 'F':
			i = direction
		if i == 'N':
			posNS += nb
		if i == 'S':
			posNS -= nb
		if i == 'E':
			posEW += nb
		if i == 'W':
			posEW -= nb
		if i == 'R':
			direction = cardinals[(cardinals.index(direction) + int(nb / 90)) % 4]
		if i == 'L':
			direction = cardinals[(cardinals.index(direction) - int(nb / 90)) % 4]

	for i in instructions:
		applyInstruction(i)
	return abs(posEW) + abs(posNS)
Exemplo n.º 9
0
Arquivo: day5.py Projeto: Bug38/AoC
import utils

boardingPasses = utils.getLinesFromFile("day5.input")

def getPlace(bp):
	bp = bp.replace('F', '0').replace('B', '1').replace('L', '0').replace('R', '1')
	return int(bp, 2)

boardingPasses = sorted([getPlace(x) for x in boardingPasses])

print(f"Part 1: {max(boardingPasses)}")
print(f"Part 2: {sorted(set(range(boardingPasses[0], boardingPasses[-1])) - set(boardingPasses))[0]}")