Example #1
0
from statistics import median, mean
from aocd.models import Puzzle
p = Puzzle(year=2021, day=7).input_data

input = [ int(i) for i in p.split(',') ]

med = median(input)

fuel_needed = sum([abs(med - i) for i in input])
def fuel_use(n):
    use = 0
    for i in range(1, n + 1):
        use += i
    return use

# the median is the closest point to all points, right?
print(fuel_needed)

# brute force way of doing this, but it works
print(min([sum([fuel_use(abs(x - n)) for n in input]) for x in range( min(input), max(input) + 1)]))
Example #2
0
from aocd.models import Puzzle
import numpy as np
from numpy import array, where, zeros
p = Puzzle(year=2021, day=4).input_data

numbers = [int(n) for n in p.split('\n\n')[0].split(',')]


class Card:
    rows = array([])
    grid = array([])

    def __init__(self, rows: list):
        self.rows = array(rows)
        self.grid = zeros((5, 5), dtype=int)

    def __str__(self):
        return str(self.rows)

    def state(self):
        return str(self.grid)

    def mark_if_present(self, number: int):
        locs = where(self.rows == number)
        if locs:
            x, y = locs
            if x.size > 0 and y.size > 0:
                self.grid[x[0]][y[0]] = 1

        return self
Example #3
0
        self.start, self.end = points

    def __str__(self):
        return f"Line: {self.start} -> {self.end}"

    def x(self):
        return [self.start.x, self.end.x]

    def y(self):
        return [self.start.y, self.end.y]


input = [
    Line(
        [Point([int(k) for k in j.strip().split(",")]) for j in i.split("->")])
    for i in p.split("\n")
]
part1 = [l for l in input if (l.start.x == l.end.x or l.start.y == l.end.y)]


def ambirange(a, b):
    if b >= a:
        return range(a, b + 1, 1)
    else:
        return reversed(range(b, a + 1, 1))


def markgrid(lines: list) -> numpy.ndarray:
    x = [i.start.x for i in lines]
    x.extend([i.end.x for i in lines])
    y = [i.start.y for i in lines]
Example #4
0
from aocd.models import Puzzle
p = Puzzle(year=2021, day=6).input_data
# p = "3,4,3,1,2"
initial_state = [int(i) for i in p.split(",")]

fish_age = [0, 0, 0, 0, 0, 0, 0, 0, 0]

for fish in initial_state:
    fish_age[fish] += 1

for i in range(80):
    fish_age.append(fish_age.pop(0))
    fish_age[6] += fish_age[8]

print(sum(fish_age))

for i in range(176):
    fish_age.append(fish_age.pop(0))
    fish_age[6] += fish_age[8]

print(sum(fish_age))
Example #5
0
    astroids = []
    for y2 in range(len(data)):
        for x2 in range(len(data[y])):
            field = data[y2][x2]
            if (not (y2 == y and x2 == x) and field == '#'):
                angle2 = get_angle(x, x2, y, y2)
                if (angle2 == angle):
                    print(x2, y2)


data = Puzzle(year=2019, day=10).input_data
# data = '#..\n.#.\n..#'
# data = '.#..#\n.....\n#####\n....#\n...##'
# data = '.#..#..###\n####.###.#\n....###.#.\n..###.##.#\n##.##.#.#.\n....###..#\n..#.#..#.#\n#..#.#.###\n.##...##.#\n.....#.#..'
# data = '.#..##.###...#######\n##.############..##.\n.#.######.########.#\n.###.#######.####.#.\n#####.##.#.##.###.##\n..#####..#.#########\n####################\n#.####....###.#.#.##\n##.#################\n#####.##.###..####..\n..######..##.#######\n####.##.####...##..#\n.#####..#.######.###\n##...#.##########...\n#.##########.#######\n.####.#.###.###.#.##\n....##.##.###..#####\n.#.#.###########.###\n#.#.#.#####.####.###\n###.##.####.##.#..##'
data = data.split('\n')

result = data.copy()
result = list(map(lambda x: list(x), result))

counts = []

for y in range(len(data)):
    for x in range(len(data[y])):
        # print("at "+str(x)+','+str(y))
        field = data[y][x]
        if (field == '#'):
            angles = visible_astroid_angles(x, y, data)
            count = len(angles)
            if (count == 276):
                print(x)