finish 11 and start 12
This commit is contained in:
parent
2f9d118169
commit
09bdd6d669
@ -1,13 +1,13 @@
|
|||||||
from collections import deque
|
from collections import deque, Counter
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
||||||
def simulate(num_players, last_marble):
|
def simulate(num_players, last_marble):
|
||||||
circle = deque([0])
|
circle = deque([0])
|
||||||
players = [0 for i in range(num_players)]
|
players = Counter()
|
||||||
current_turn = 1
|
current_turn = 1
|
||||||
while current_turn <= last_marble:
|
while current_turn <= last_marble:
|
||||||
current_player = (current_turn % len(players)) - 1
|
current_player = (current_turn % num_players) - 1
|
||||||
|
|
||||||
if current_turn % 23 == 0:
|
if current_turn % 23 == 0:
|
||||||
players[current_player] += current_turn
|
players[current_player] += current_turn
|
||||||
@ -17,7 +17,7 @@ def simulate(num_players, last_marble):
|
|||||||
circle.rotate(-2)
|
circle.rotate(-2)
|
||||||
circle.appendleft(current_turn)
|
circle.appendleft(current_turn)
|
||||||
current_turn += 1
|
current_turn += 1
|
||||||
return max(players)
|
return players.most_common()[0][1]
|
||||||
|
|
||||||
|
|
||||||
with open('data/09.txt') as f:
|
with open('data/09.txt') as f:
|
||||||
|
51
2018/11.py
51
2018/11.py
@ -26,51 +26,53 @@ class Grid:
|
|||||||
|
|
||||||
|
|
||||||
class Cursor:
|
class Cursor:
|
||||||
def __init__(self, parent_grid):
|
def __init__(self, parent_grid, cursor_size):
|
||||||
self.grid = parent_grid
|
self.grid = parent_grid
|
||||||
|
self.size = cursor_size
|
||||||
self.x = self.y = 1
|
self.x = self.y = 1
|
||||||
self.calc_power()
|
self.total_power = self.calc_power()
|
||||||
|
|
||||||
def step(self, direction):
|
def step(self, direction):
|
||||||
if direction in ('right', 'left'):
|
if direction in ('right', 'left'):
|
||||||
axis = 'vertical'
|
axis = 'vertical'
|
||||||
new_x, new_y = 1, 0
|
delta_x, delta_y = 1, 0
|
||||||
elif direction in ('up', 'down'):
|
elif direction in ('up', 'down'):
|
||||||
axis = 'horizontal'
|
axis = 'horizontal'
|
||||||
new_x, new_y = 0, 1
|
delta_x, delta_y = 0, 1
|
||||||
|
|
||||||
if direction in ('right', 'down'):
|
if direction in ('right', 'down'):
|
||||||
delta_new, delta_old = 3, 0
|
offset_new, offset_old = self.size, 0
|
||||||
elif direction in ('left', 'up'):
|
elif direction in ('left', 'up'):
|
||||||
delta_new, delta_old = -1, 2
|
offset_new, offset_old = -1, self.size - 1
|
||||||
new_x, new_y = new_x * -1, new_y * -1
|
delta_x, delta_y = delta_x * -1, delta_y * -1
|
||||||
|
|
||||||
new_points = self.get_row(delta_new, axis)
|
new_points = self.get_row(offset_new, axis)
|
||||||
old_points = self.get_row(delta_old, axis)
|
old_points = self.get_row(offset_old, axis)
|
||||||
self.total_power += sum(new_points)
|
self.total_power += sum(new_points)
|
||||||
self.total_power -= sum(old_points)
|
self.total_power -= sum(old_points)
|
||||||
self.x, self.y = self.x + new_x, self.y + new_y
|
self.x, self.y = self.x + delta_x, self.y + delta_y
|
||||||
|
|
||||||
def get_row(self, delta, axis):
|
def get_row(self, delta, axis):
|
||||||
points = []
|
points = []
|
||||||
if axis == 'vertical':
|
if axis == 'vertical':
|
||||||
for y in range(self.y, self.y + 3):
|
for y in range(self.y, self.y + self.size):
|
||||||
points.append(self.grid.get(self.x + delta, y))
|
points.append(self.grid.get(self.x + delta, y))
|
||||||
elif axis == 'horizontal':
|
elif axis == 'horizontal':
|
||||||
for x in range(self.x, self.x + 3):
|
for x in range(self.x, self.x + self.size):
|
||||||
points.append(self.grid.get(x, self.y + delta))
|
points.append(self.grid.get(x, self.y + delta))
|
||||||
return points
|
return points
|
||||||
|
|
||||||
def jump(self, x, y):
|
def jump(self, x, y):
|
||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
self.calc_power()
|
self.total_power = self.calc_power()
|
||||||
|
|
||||||
def calc_power(self):
|
def calc_power(self):
|
||||||
self.total_power = 0
|
total = 0
|
||||||
for y in range(3):
|
for y in range(self.size):
|
||||||
for x in range(3):
|
for x in range(self.size):
|
||||||
self.total_power += self.grid.get(self.x + x, self.y + y)
|
total += self.grid.get(self.x + x, self.y + y)
|
||||||
|
return total
|
||||||
|
|
||||||
def find_max(self):
|
def find_max(self):
|
||||||
coords, max_power = (0, 0), 0
|
coords, max_power = (0, 0), 0
|
||||||
@ -79,7 +81,7 @@ class Cursor:
|
|||||||
if self.total_power > max_power:
|
if self.total_power > max_power:
|
||||||
coords, max_power = (self.x, self.y), self.total_power
|
coords, max_power = (self.x, self.y), self.total_power
|
||||||
|
|
||||||
if self.x in (1, 298) and prev_dir != 'down': # cursor is at edge of grid
|
if self.x in (1, 301 - self.size) and prev_dir != 'down': # cursor is at edge of grid
|
||||||
direction = 'down'
|
direction = 'down'
|
||||||
elif self.y % 2 == 0: # go left on even lines
|
elif self.y % 2 == 0: # go left on even lines
|
||||||
direction = 'left'
|
direction = 'left'
|
||||||
@ -96,6 +98,15 @@ class Cursor:
|
|||||||
|
|
||||||
|
|
||||||
grid = Grid(7857)
|
grid = Grid(7857)
|
||||||
cursor = Cursor(grid)
|
cursor = Cursor(grid, 3)
|
||||||
coords, max_power = cursor.find_max()
|
coords, max_power = cursor.find_max()
|
||||||
print(f'Part 1: {coords[0]}, {coords[1]}, {max_power}')
|
print(f'Part 1: {coords[0]},{coords[1]}, ({max_power})\n')
|
||||||
|
|
||||||
|
coords = max_power = grid_size = 0
|
||||||
|
for s in range(1, 301):
|
||||||
|
cursor = Cursor(grid, s)
|
||||||
|
c, m = cursor.find_max()
|
||||||
|
if m > max_power:
|
||||||
|
coords, max_power, grid_size = c, m, s
|
||||||
|
|
||||||
|
print(f'Part 2: {coords[0]},{coords[1]},{grid_size} ({max_power})\n')
|
||||||
|
52
2018/12.py
Normal file
52
2018/12.py
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
def to_int(bitlist):
|
||||||
|
out = 0
|
||||||
|
for bit in bitlist:
|
||||||
|
out = out * 2 + bit
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def get_slice(index, pots):
|
||||||
|
for i in range(index - 2, index + 3):
|
||||||
|
if i < 0:
|
||||||
|
yield 0
|
||||||
|
yield pots[i]
|
||||||
|
|
||||||
|
|
||||||
|
def grow(init, num_gens):
|
||||||
|
current_gen = init
|
||||||
|
for g in range(num_gens):
|
||||||
|
while current_gen[:2] != [0, 0]:
|
||||||
|
current_gen.insert(0, 0)
|
||||||
|
while current_gen[-2:] != [0, 0]:
|
||||||
|
current_gen.append(0)
|
||||||
|
|
||||||
|
next_gen = []
|
||||||
|
for i, b in enumerate(current_gen):
|
||||||
|
key = to_int(get_slice(i, current_gen))
|
||||||
|
if key in positives:
|
||||||
|
next_gen.append(1)
|
||||||
|
else:
|
||||||
|
next_gen.append(0)
|
||||||
|
current_gen = next_gen
|
||||||
|
|
||||||
|
return current_gen
|
||||||
|
|
||||||
|
|
||||||
|
with open('data/12.txt') as f:
|
||||||
|
initial = list(next(f)[15:-1])
|
||||||
|
initial = list(map(lambda p: int(p == '#'), initial))
|
||||||
|
next(f)
|
||||||
|
positives, negatives = set(), set()
|
||||||
|
for line in f:
|
||||||
|
state, result = line.strip().split(' => ')
|
||||||
|
s = to_int(int(c == '#') for c in state)
|
||||||
|
if result == '#':
|
||||||
|
positives.add(s)
|
||||||
|
else:
|
||||||
|
negatives.add(s)
|
||||||
|
|
||||||
|
|
||||||
|
def test(n):
|
||||||
|
result = grow(initial, n)
|
||||||
|
print(''.join('#' if p else '.' for p in result))
|
||||||
|
|
34
2018/data/12.txt
Normal file
34
2018/data/12.txt
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
initial state: ##.#.####..#####..#.....##....#.#######..#.#...........#......##...##.#...####..##.#..##.....#..####
|
||||||
|
|
||||||
|
#..#. => #
|
||||||
|
.###. => .
|
||||||
|
..##. => .
|
||||||
|
....# => .
|
||||||
|
#...# => .
|
||||||
|
.#.#. => .
|
||||||
|
#.#.# => #
|
||||||
|
#.... => .
|
||||||
|
#.#.. => #
|
||||||
|
###.# => .
|
||||||
|
.#... => #
|
||||||
|
#.### => .
|
||||||
|
.#.## => #
|
||||||
|
..#.. => #
|
||||||
|
.#### => .
|
||||||
|
..### => #
|
||||||
|
...#. => .
|
||||||
|
##.#. => #
|
||||||
|
##.## => #
|
||||||
|
.##.# => #
|
||||||
|
###.. => .
|
||||||
|
..#.# => .
|
||||||
|
...## => #
|
||||||
|
##... => #
|
||||||
|
##### => .
|
||||||
|
#.##. => .
|
||||||
|
.#..# => #
|
||||||
|
##..# => .
|
||||||
|
..... => .
|
||||||
|
####. => #
|
||||||
|
#..## => .
|
||||||
|
.##.. => #
|
Loading…
x
Reference in New Issue
Block a user