mirror of
https://github.com/thib8956/advent-of-code.git
synced 2024-12-26 05:46:29 +00:00
import more stuff
This commit is contained in:
parent
d9be62b784
commit
9c9ac3bf3f
22
2018/day1/day1.py
Normal file
22
2018/day1/day1.py
Normal file
@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env python3
|
||||
import itertools
|
||||
|
||||
|
||||
def main(inp):
|
||||
# Part 1
|
||||
changes = [int(n) for n in inp]
|
||||
print(sum(changes))
|
||||
freq = 0
|
||||
seen = {0}
|
||||
for num in itertools.cycle(changes):
|
||||
freq += num
|
||||
if freq in seen:
|
||||
print(freq)
|
||||
break
|
||||
seen.add(freq)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
with open("input.txt") as inp:
|
||||
main(inp.readlines())
|
||||
|
1025
2018/day1/input.txt
Normal file
1025
2018/day1/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env python3
|
||||
from rules import part1_rules
|
||||
from rules import part1_rules, part2_rules
|
||||
|
||||
|
||||
def main(grid, rules):
|
||||
@ -9,6 +9,8 @@ def main(grid, rules):
|
||||
generation += 1
|
||||
grid = next_grid
|
||||
assert generation < 1000
|
||||
if generation % 10 == 0:
|
||||
print(f"Generation {generation}, changes: {changes}")
|
||||
if changes == 0:
|
||||
return next_grid.count("#")
|
||||
|
||||
@ -28,5 +30,4 @@ if __name__ == "__main__":
|
||||
with open("input.txt") as infile:
|
||||
grid = list("".join(infile.read().splitlines()))
|
||||
print("Part 1 ", main(grid, rules=part1_rules))
|
||||
|
||||
# print("Part 2 ", main(grid, rules={"L": handle_empty_2, "#": handle_occupied_2}))
|
||||
print("Part 2 ", main(grid, rules=part2_rules))
|
||||
|
@ -73,8 +73,8 @@ def handle_empty_2(index, grid, next_grid):
|
||||
|
||||
if neighbors == 0:
|
||||
next_grid[index] = "#"
|
||||
return 1
|
||||
return 0
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def handle_occupied_2(index, grid, next_grid):
|
||||
@ -87,16 +87,16 @@ def handle_occupied_2(index, grid, next_grid):
|
||||
y = index // grid_width
|
||||
for direction in directions:
|
||||
for xx, yy in takewhile(in_bounds, move(x, y, direction)):
|
||||
print(xx, yy)
|
||||
cell = grid[yy * grid_width + xx]
|
||||
|
||||
if cell == "#":
|
||||
occupied += 1
|
||||
elif cell != ".":
|
||||
break
|
||||
|
||||
if occupied >= 5:
|
||||
next_grid[index] = "L"
|
||||
return 1
|
||||
return 0
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def in_bounds(pos):
|
||||
@ -105,10 +105,12 @@ def in_bounds(pos):
|
||||
|
||||
|
||||
def move(x, y, direction):
|
||||
pos = x, y
|
||||
xx = x
|
||||
yy = y
|
||||
while True:
|
||||
yield pos
|
||||
pos = x + direction[0], y + direction[1]
|
||||
yield xx, yy
|
||||
xx += direction[0]
|
||||
yy += direction[1]
|
||||
|
||||
|
||||
part1_rules = {"L": handle_empty, "#": handle_occupied}
|
||||
|
Loading…
Reference in New Issue
Block a user