예제 #1
0
def main():
  start = time.time()
  data = AOC.Advent(2020,3).input_data
  matrix = [line.replace('#','1').replace('.','0') for line in data]
  matrix = [[int(value) for value in row] for row in matrix]
  
  def slope(horizontal, vertical):
    trees = 0
    for i in range( int( len(matrix) / vertical // 1 ) ):
      trees += matrix[vertical * i][horizontal * i % len(matrix[0])]
    return trees

  if __name__ == "__main__":
    print("The solution to problem 1 is: {}".format(slope(3,1)))
    print("The solution to problem 2 is: {}".format(slope(1,1) * slope(3,1) * slope(5,1) * slope(7,1) * slope(1,2)))
    print('Completed in {} seconds.'.format(time.time() - start))
예제 #2
0
import AOC
import time

data = AOC.Advent(2020,5).input_data

def main():
  def seat_number(id):
    row, col = id[:7], id[7:]
    row = int(row.replace('B','1').replace('F','0'), 2)
    col = int(col.replace('L','0').replace('R','1'), 2)
    return 8 * row + col

  x = {seat_number(line) for line in data}

  if __name__ == "__main__":
    print("The solution to problem 1 is: {}".format(max(x)))
    print("The solution to problem 2 is: {}".format( set(range(min(x), max(x)+1)) - x ))

start = time.time()
main()
print('Completed in {} seconds.'.format(time.time() - start))
예제 #3
0
import AOC
import time

AOC.Advent(2020, 6)
with open('input/input06.txt') as f:
    data = f.readlines()
data = ''.join(data).split('\n\n')


def main():
    def count(group: str) -> int:
        s = set('a b c d e f g h i j k l m n o p q r s t u v w x y z'.split())
        for question in group.split():
            s = s.intersection(set(question))
        return len(s)

    if __name__ == "__main__":
        print("The solution to problem 1 is: {}".format(
            sum((len(set(i.replace('\n', ''))) for i in data))))
        print("The solution to problem 2 is: {}".format(
            sum(count(group) for group in data)))


start = time.time()
main()
print('Completed in {} seconds.'.format(time.time() - start))
예제 #4
0
import AOC

AOC.Advent(2020, 1)


def main():
    with open('input/input01.txt') as f:
        expense = [int(i) for i in f.read().splitlines()]

    def loopsum(looplist, val=2020) -> int:
        for index, item1 in enumerate(looplist):
            for item2 in looplist[index:]:
                if item1 + item2 == val:
                    return item1, item2

    def loopsum2(looplist, val=2020) -> int:
        for idx1, item1 in enumerate(looplist):
            for idx2, item2 in enumerate(looplist[idx1:]):
                for item3 in looplist[idx2:]:
                    if item1 + item2 + item3 == val:
                        return item1, item2, item3

    if __name__ == "__main__":
        item1, item2 = loopsum(expense)
        print("The solution to problem 1 is: {}".format(item1 * item2))

        item1, item2, item3 = loopsum2(expense)
        print("The solution to problem 2 is: {}".format(item1 * item2 * item3))


main()