Ejemplo n.º 1
0
def finde_neue_richtung(pos, richt):
    x, y = richt
    for drehung in ['L', 'R']:
        if drehung == 'L':
            x1, y1 = y * 1, x * -1
        else:
            x1, y1 = y * -1, x * 1
        if pos + Vec(x1, y1) in karte:
            return drehung, Vec(x1, y1)
Ejemplo n.º 2
0
def baue_map(pos):
    map = set()
    while True:
        ascii = intcode.run(0)
        if ascii == None: return map, robot
        if chr(ascii) in richtungen:
            robot = pos, chr(ascii)
        if ascii == 35: map.add(pos)
        x, y = pos
        pos = Vec(x + 1, y)
        if ascii == 10: pos = Vec(0, y + 1)
    return map
Ejemplo n.º 3
0
def baue_karte(output, pos):
    karte = set()
    for o in output:
        o = chr(o)
        if o in richtungen:
            robot = pos, o
        if o == '#':
            karte.add(pos)
        x, y = pos
        pos = Vec(x + 1, y)
        if o == '\n':
            pos = Vec(0, y + 1)
    return karte, robot
Ejemplo n.º 4
0
 def spawn_ball(self, i):
     angle = random() * tau
     radius = int(self.RADIUS * (2 * random() + 0.2))
     speed = Vec(self.VELOCITY * cos(angle) / self.SUPER_SAMPLING,
                 self.VELOCITY * sin(angle) / self.SUPER_SAMPLING)
     position = Vec(
         randint(self.BORDER + radius, self.WIDTH - self.BORDER - radius),
         randint(self.BORDER + radius, self.HEIGHT - self.BORDER - radius))
     ident = f"ball{i}"
     for obj in self.objects:
         if obj.get_hitbox().contains(Ball.ball_hitbox(position, radius)):
             self.spawn_ball(i)
             return
     ball = Ball(self.screen, position, speed, radius, self.fg_color,
                 self.BG_COLOR, ident)
     self.objects.update({ball: ball})
Ejemplo n.º 5
0
 def __init__(self, screen, position, width, height, color, ident=None):
     self.position = position
     if width < 0:
         self.position = self.position + Vec(width, 0)
         width *= -1
     if height < 0:
         self.position = self.position + Vec(0, height)
         height *= -1
     self.width = width
     self.height = height
     self.screen = screen
     self.color = color
     pygame.draw.rect(
         self.screen, self.color,
         pygame.Rect(tuple(self.position), (self.width, self.height)))
     super().__init__(self.position, self.position + Vec(width, height),
                      ident)
Ejemplo n.º 6
0
 def ball_wall_collision(self, other):
     vx, vy = self.speed
     emit = self.radius
     if other.ident == 'bottom_wall':
         self.position = Vec(self.position[0], other.position.y - emit)
         vy = -vy
     elif other.ident == 'top_wall':
         self.position = Vec(self.position[0],
                             other.position.y + other.height + emit)
         vy = -vy
     elif other.ident == 'left_wall':
         self.position = Vec(other.position.x + other.width + emit,
                             self.position[1])
         vx = -vx
     else:
         self.position = Vec(other.position.x - emit, self.position[1])
         vx = -vx
     self.speed = Vec(vx, vy)
     self.inside.remove(other)
Ejemplo n.º 7
0
    def resolve_collision(self, other):
        x1 = self.position
        v1 = self.speed
        m1 = self.radius**3
        try:
            x2 = other.position
            v2 = other.speed
            m2 = other.radius**3
            if norm(x2 -
                    x1) > (self.radius + other.radius
                           ) or other in self.inside or self in other.inside:
                return
            if not (other in self.inside or self in other.inside):
                self.inside.append(other)
                other.inside.append(self)
            self.speed = v1 - ((v1 - v2) @ (x1 - x2) /
                               (norm(x1 - x2)**2)) * (x1 - x2) * (2 * m2 /
                                                                  (m1 + m2))
            other.speed = v2 - ((v2 - v1) @ (x2 - x1) /
                                (norm(x2 - x1)**2)) * (x2 - x1) * (2 * m1 /
                                                                   (m1 + m2))
        except (TypeError, AttributeError):
            # s_hitbox = self.get_hitbox()
            s_vx, s_vy = self.speed
            # if s_hitbox.x1 > o_hitbox.x1 and s_hitbox.x2 < o_hitbox.x2: # Hitting a horizontal edge
            #     s_vy = -s_vy
            # if s_hitbox.y1 > o_hitbox.y1 and s_hitbox.y2 < o_hitbox.y2: # Hitting a vertical edge
            #     s_vx = -s_vx
            emit = self.radius
            if other.ident == 'bottom_wall':
                self.position = Vec(self.position[0], other.position.y - emit)
                s_vy = -s_vy
            elif other.ident == 'top_wall':
                self.position = Vec(self.position[0], other.position.y + emit)
                s_vy = -s_vy
            elif other.ident == 'left_wall':
                self.position = Vec(other.position.x + emit, self.position[1])
                s_vx = -s_vx
            else:
                self.position = Vec(other.position.x - emit, self.position[1])
                s_vx = -s_vx

            self.speed = Vec(s_vx, s_vy)
Ejemplo n.º 8
0
 def __init__(self,
              screen,
              position,
              speed,
              radius=10,
              fg_color=pygame.Color('White'),
              bg_color=pygame.Color('Black'),
              ident=None):
     self.position = position
     self._last_position = Vec(int(position.x), int(position.y))
     self.speed = speed
     self.acc = Vec(0, 0)  # Unused
     self.fg_color = fg_color
     self.bg_color = bg_color
     self.radius = radius
     self.screen = screen
     self.show(self.position, self.fg_color)
     # self.ident = ident
     self.inside = []
     super().__init__(
         (position[0] - self.radius, position[1] - self.radius),
         (position[0] + self.radius, position[1] + self.radius), ident)
Ejemplo n.º 9
0
 def __init__(self, fg_color):
     self.screen = pygame.display.set_mode((self.WIDTH, self.HEIGHT))
     Wall(self.screen, Vec(0, 0), self.WIDTH, self.HEIGHT,
          pygame.Color('Blue'))
     self.fg_color = fg_color
     left_wall = Wall(self.screen,
                      Vec(0, 0),
                      self.BORDER,
                      self.HEIGHT,
                      fg_color,
                      ident='left_wall')
     top_wall = Wall(self.screen,
                     Vec(0, 0),
                     self.WIDTH,
                     self.BORDER,
                     fg_color,
                     ident='top_wall')
     right_wall = Wall(self.screen,
                       Vec(self.WIDTH, 0),
                       -self.BORDER,
                       self.HEIGHT,
                       fg_color,
                       ident='right_wall')
     bottom_wall = Wall(self.screen,
                        Vec(0, self.HEIGHT),
                        self.WIDTH,
                        -self.BORDER,
                        fg_color,
                        ident='bottom_wall')
     # self.objects = [left_border, top_border, right_border, bottom_border]
     self.objects = {
         left_wall: left_wall,
         top_wall: top_wall,
         right_wall: right_wall,
         bottom_wall: bottom_wall
     }
     for i in range(0, self.BALLCOUNT):
         self.spawn_ball(i)
Ejemplo n.º 10
0
# Code original by https://www.reddit.com/user/FogleMonster/
# https://www.reddit.com/r/adventofcode/comments/e85b6d/2019_day_9_solutions/faajddr/?context=3

from collections import defaultdict
from Vector import Vec
import time

#north (1), south (2), west (3), and east (4).
richtungen = {1:Vec(0,-1), 2:Vec(0,1), 3:Vec(-1,0), 4:Vec(1,0)}
richt_invers = {1:2, 2:1, 3:4, 4:3} 

class State:
  def __init__(self, program):
    self.mem = defaultdict(int, enumerate(program))
    self.ip = 0
    self.rb = 0
    

start = time.perf_counter()

with open('Tag15.txt') as f:
  program = list(map(int, f.readline().split(',')))

def run(s, program_input):
  while True:
    op = s.mem[s.ip] % 100
    if op == 99:
      return
    size = [0, 4, 4, 2, 2, 3, 3, 4, 4, 2][op]
    args = [s.mem[s.ip+i] for i in range(1, size)]
    modes = [(s.mem[s.ip] // 10 ** i) % 10 for i in range(2, 5)]
Ejemplo n.º 11
0
from Vector import Vec
from collections import defaultdict
import time

time_start = time.perf_counter()

map = set()
with open("tag10.txt") as f:
    for y, zeile in enumerate(f):
        for x, zeichen in enumerate(zeile):
            if zeichen != '#': continue
            map.add(Vec(x, y))

winkels = defaultdict(set)
for asteroid1 in map:
    for asteroid2 in map:
        if asteroid1 == asteroid2: continue
        winkels[asteroid1].add(asteroid1.winkel(asteroid2))

lösung = sorted([(len(winkel), pos) for pos, winkel in winkels.items()])[-1]

print(
    f'Lösung: {lösung} ermittelt in {time.perf_counter()-time_start:0.3f} Sek.'
)
Ejemplo n.º 12
0
# Intcode Class Code original by https://www.reddit.com/user/FogleMonster/
# https://www.reddit.com/r/adventofcode/comments/e85b6d/2019_day_9_solutions/faajddr/?context=3

from collections import defaultdict, deque
from Vector import Vec
import time
import itertools as iter

# ^, v, <, or > for directions
richtungen = {'^': Vec(0, -1), 'v': Vec(0, 1), '<': Vec(-1, 0), '>': Vec(1, 0)}


class Intcode:
    def __init__(self, program):
        self.mem = defaultdict(int, enumerate(program))
        self.ip = 0
        self.rb = 0
        self.input = []
        self.output = []

    def load_input(self, data):
        self.input.extend(data)

    def run(self):
        while True:
            op = self.mem[self.ip] % 100
            if op == 99:
                return
            size = [0, 4, 4, 2, 2, 3, 3, 4, 4, 2][op]
            args = [self.mem[self.ip + i] for i in range(1, size)]
            modes = [(self.mem[self.ip] // 10**i) % 10 for i in range(2, 5)]
Ejemplo n.º 13
0
from Vector import Vec
central_port = Vec(1,1)

directions = {'R': Vec(1,0), 'L': Vec(-1,0), 'U': Vec(0,1), 'D':Vec(0,-1)}
wires = []
with open("tag03.txt") as f:
  for line in f:
    wires.append(line.strip().split(","))

positionen = []
for wire in wires:
  position = central_port
  pos_wire = set()
  for wert in wire:
    dir = wert[0]
    steps = int(wert[1:])
    for i in range(steps):
      position += directions[dir]
      pos_wire.add(position)
  positionen.append(pos_wire)

kreuzungen = positionen[0].intersection(positionen[1])

min_abstand = 99999999
for kreuzung in kreuzungen:
  abst = central_port.abstand(kreuzung)
  min_abstand = min(min_abstand, abst)

print(min_abstand)

Ejemplo n.º 14
0
# Code original by https://www.reddit.com/user/FogleMonster/
# https://www.reddit.com/r/adventofcode/comments/e85b6d/2019_day_9_solutions/faajddr/?context=3

from collections import defaultdict, deque
from Vector import Vec
import time

# north (1), south (2), west (3), and east (4).
richtungen = {1: Vec(0, -1), 2: Vec(0, 1), 3: Vec(-1, 0), 4: Vec(1, 0)}
richt_invers = {1: 2, 2: 1, 3: 4, 4: 3}


class State:
  def __init__(self, program):
    self.mem = defaultdict(int, enumerate(program))
    self.ip = 0
    self.rb = 0


start = time.perf_counter()

with open('Tag15.txt') as f:
  program = list(map(int, f.readline().split(',')))


def run(s, program_input):
  while True:
    op = s.mem[s.ip] % 100
    if op == 99:
      return
    size = [0, 4, 4, 2, 2, 3, 3, 4, 4, 2][op]
Ejemplo n.º 15
0
 def show(self, position, color):
     x, y = position
     pygame.draw.circle(self.screen, color, (int(x), int(y)), self.radius)
     self._last_position = Vec(int(x), int(y))
Ejemplo n.º 16
0
from Vector import Vec
central_port = Vec(1, 1)

directions = {'R': Vec(1, 0), 'L': Vec(-1, 0), 'U': Vec(0, 1), 'D': Vec(0, -1)}
wires = []
with open("tag03.txt") as f:
    for line in f:
        wires.append(line.strip().split(","))

positionen = []
for wire in wires:
    position = central_port
    pos_wire = {}
    count_steps = 0
    for wert in wire:
        dir = wert[0]
        steps = int(wert[1:])
        for i in range(steps):
            count_steps += 1
            position += directions[dir]
            pos_wire[position] = count_steps
    positionen.append(pos_wire)

wire1 = {x for x in positionen[0]}
wire2 = {x for x in positionen[1]}
kreuzungen = wire1.intersection(wire2)

min_steps = 99999999
for kreuzung in kreuzungen:
    steps = positionen[0][kreuzung] + positionen[1][kreuzung]
    min_steps = min(min_steps, steps)
Ejemplo n.º 17
0
# Code original by https://www.reddit.com/user/FogleMonster/
# https://www.reddit.com/r/adventofcode/comments/e85b6d/2019_day_9_solutions/faajddr/?context=3

from collections import defaultdict, Counter
from Vector import Vec
import time

richt = {'u': Vec(0, -1), 'd': Vec(0, 1), 'r': Vec(1, 0), 'l': Vec(-1, 0)}
richt_wechsel = {
    'u': ['l', 'r'],
    'd': ['r', 'l'],
    'r': ['u', 'd'],
    'l': ['d', 'u']
}


class State:
    def __init__(self, program):
        self.mem = defaultdict(int, enumerate(program))
        self.ip = 0
        self.rb = 0


start = time.perf_counter()

with open('Tag11.txt') as f:
    program = list(map(int, f.readline().split(',')))

s = State(program)

Ejemplo n.º 18
0
import re 
import time

start_time = time.perf_counter()

class Moon:
  def __init__(self, pos):
    self.pos = pos
    self.vel = Vec(0,0,0)

puzzle_input = []
with open('Tag12.txt') as f:
  for line in f:
    line = re.sub('[<>x=yz,]','',line)
    line = list(map(int,line.split()))
    puzzle_input.append(Vec(*line))

moons = []
for pos in puzzle_input:
  moons.append(Moon(pos))
for _ in range(1000):
  for moon1 in moons:
    for moon2 in moons:
      if moon1 == moon2: continue
      diff = moon2.pos - moon1.pos
      vel_list = list(moon1.vel)
      for i,achse in enumerate(diff):
        if achse == 0: continue
        if achse > 0:
          vel_list[i] += 1
        if achse < 0:
Ejemplo n.º 19
0
from Vector import Vec
from collections import defaultdict
import time

time_start = time.perf_counter()

map = set()
with open("tag10.txt") as f:
    for y, zeile in enumerate(f):
        for x, zeichen in enumerate(zeile):
            if zeichen != '#': continue
            map.add(Vec(x, y))

start = Vec(14, 17)
map.remove(start)

winkels = defaultdict(list)

for asteroid in map:
    winkel = 360 - start.winkel(asteroid)
    winkel = 0 if winkel == 360 else winkel
    winkels[winkel].append(asteroid)

sortiert_nach_winkel = [winkels[winkel] for winkel in sorted(winkels.keys())]

zerstört = 0
fertig = False
while not fertig:
    for asteroiden in sortiert_nach_winkel:
        asteroid = asteroiden.pop()
        zerstört += 1
Ejemplo n.º 20
0
 def __init__(self, pos):
   self.pos = pos
   self.vel = Vec(0,0,0)
Ejemplo n.º 21
0
# Intcode Class Code original by https://www.reddit.com/user/FogleMonster/
# https://www.reddit.com/r/adventofcode/comments/e85b6d/2019_day_9_solutions/faajddr/?context=3

from collections import defaultdict, deque
from Vector import Vec
import time

#^, v, <, or > for directions
richtungen = {'^': Vec(0, -1), 'v': Vec(0, 1), '<': Vec(-1, 0), '>': Vec(1, 0)}


class Intcode:
    def __init__(self, program):
        self.mem = defaultdict(int, enumerate(program))
        self.ip = 0
        self.rb = 0

    def run(self, program_input):
        while True:
            op = self.mem[self.ip] % 100
            if op == 99:
                return
            size = [0, 4, 4, 2, 2, 3, 3, 4, 4, 2][op]
            args = [self.mem[self.ip + i] for i in range(1, size)]
            modes = [(self.mem[self.ip] // 10**i) % 10 for i in range(2, 5)]
            reads = [(self.mem[x], x, self.mem[x + self.rb])[m]
                     for x, m in zip(args, modes)]
            writes = [(x, None, x + self.rb)[m] for x, m in zip(args, modes)]
            self.ip += size
            if op == 1:
                self.mem[writes[2]] = reads[0] + reads[1]