import more stuff

This commit is contained in:
Thibaud Gasser 2024-12-03 16:21:15 +01:00
parent d9be62b784
commit 9c9ac3bf3f
4 changed files with 1063 additions and 13 deletions

22
2018/day1/day1.py Normal file
View 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

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from rules import part1_rules from rules import part1_rules, part2_rules
def main(grid, rules): def main(grid, rules):
@ -9,6 +9,8 @@ def main(grid, rules):
generation += 1 generation += 1
grid = next_grid grid = next_grid
assert generation < 1000 assert generation < 1000
if generation % 10 == 0:
print(f"Generation {generation}, changes: {changes}")
if changes == 0: if changes == 0:
return next_grid.count("#") return next_grid.count("#")
@ -27,6 +29,5 @@ def step(grid, rules):
if __name__ == "__main__": if __name__ == "__main__":
with open("input.txt") as infile: with open("input.txt") as infile:
grid = list("".join(infile.read().splitlines())) grid = list("".join(infile.read().splitlines()))
print("Part 1 ", main(grid, rules=part1_rules)) print("Part 1 ", main(grid, rules=part1_rules))
print("Part 2 ", main(grid, rules=part2_rules))
# print("Part 2 ", main(grid, rules={"L": handle_empty_2, "#": handle_occupied_2}))

View File

@ -73,8 +73,8 @@ def handle_empty_2(index, grid, next_grid):
if neighbors == 0: if neighbors == 0:
next_grid[index] = "#" next_grid[index] = "#"
return 1 return True
return 0 return False
def handle_occupied_2(index, grid, next_grid): def handle_occupied_2(index, grid, next_grid):
@ -87,16 +87,16 @@ def handle_occupied_2(index, grid, next_grid):
y = index // grid_width y = index // grid_width
for direction in directions: for direction in directions:
for xx, yy in takewhile(in_bounds, move(x, y, direction)): for xx, yy in takewhile(in_bounds, move(x, y, direction)):
print(xx, yy)
cell = grid[yy * grid_width + xx] cell = grid[yy * grid_width + xx]
if cell == "#": if cell == "#":
occupied += 1 occupied += 1
elif cell != ".":
break
if occupied >= 5: if occupied >= 5:
next_grid[index] = "L" next_grid[index] = "L"
return 1 return True
return 0 return False
def in_bounds(pos): def in_bounds(pos):
@ -105,10 +105,12 @@ def in_bounds(pos):
def move(x, y, direction): def move(x, y, direction):
pos = x, y xx = x
yy = y
while True: while True:
yield pos yield xx, yy
pos = x + direction[0], y + direction[1] xx += direction[0]
yy += direction[1]
part1_rules = {"L": handle_empty, "#": handle_occupied} part1_rules = {"L": handle_empty, "#": handle_occupied}