-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvent2017_22.py
151 lines (128 loc) · 4.55 KB
/
advent2017_22.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python3
# http://adventofcode.com/2017
import math
from itertools import groupby
from functools import lru_cache
from collections import Counter, defaultdict, deque
from pprint import pprint
import csv
import logging as log
import os
import pytest
import queue
import re
LOGLEVEL = os.environ.get("LOGLEVEL", "DEBUG").upper()
log.basicConfig(level=LOGLEVEL)
class Carrier:
grid = defaultdict(lambda: ".")
directions = deque(["up", "right", "down", "left"])
def __init__(self, x, y):
self.x = x
self.y = y
self.direction = Carrier.directions[0]
self.infections = 0
def current_possition_value(self):
return Carrier.grid[(self.x, self.y)]
def get_new_direction(self):
if self.current_possition_value() == "#":
Carrier.directions.rotate(-1)
self.direction = Carrier.directions[0]
elif self.current_possition_value() == ".":
Carrier.directions.rotate(1)
self.direction = Carrier.directions[0]
def infect_clean(self):
if self.current_possition_value() == "#":
Carrier.grid[(self.x, self.y)] = "."
elif self.current_possition_value() == ".":
Carrier.grid[(self.x, self.y)] = "#"
self.infections += 1
def move(self):
if self.direction == "up":
self.y -= 1
elif self.direction == "right":
self.x += 1
elif self.direction == "down":
self.y += 1
elif self.direction == "left":
self.x -= 1
def burst(self):
self.get_new_direction()
log.debug(f"New direction is {self.direction}")
self.infect_clean()
log.debug(f"Current infections {self.infections}")
self.move()
log.debug(f"Current coordinates x = {self.x}; y = {self.y}")
class Carrier2:
grid = defaultdict(lambda: ".")
directions = deque(["up", "right", "down", "left"])
def __init__(self, x, y):
self.x = x
self.y = y
self.direction = Carrier.directions[0]
self.infections = 0
def current_possition_value(self):
return Carrier.grid[(self.x, self.y)]
def get_new_direction(self):
if self.current_possition_value() == "#":
Carrier.directions.rotate(-1)
self.direction = Carrier.directions[0]
elif self.current_possition_value() == ".":
Carrier.directions.rotate(1)
self.direction = Carrier.directions[0]
elif self.current_possition_value() == "f":
Carrier.directions.rotate(2)
self.direction = Carrier.directions[0]
def infect_clean(self):
if self.current_possition_value() == "#":
Carrier.grid[(self.x, self.y)] = "f"
elif self.current_possition_value() == ".":
Carrier.grid[(self.x, self.y)] = "w"
elif self.current_possition_value() == "w":
Carrier.grid[(self.x, self.y)] = "#"
self.infections += 1
elif self.current_possition_value() == "f":
Carrier.grid[(self.x, self.y)] = "."
def move(self):
if self.direction == "up":
self.y -= 1
elif self.direction == "right":
self.x += 1
elif self.direction == "down":
self.y += 1
elif self.direction == "left":
self.x -= 1
def burst(self):
self.get_new_direction()
# log.debug(f"New direction is {self.direction}")
self.infect_clean()
# log.debug(f"Current infections {self.infections}")
self.move()
# log.debug(f"Current coordinates x = {self.x}; y = {self.y}")
if __name__ == "__main__":
with open("input_advent2017_22.txt") as file:
lines = [list(line.strip()) for line in file.readlines()]
# grid_X = grid_Y = 1500
for y, li in enumerate(lines):
for x, char in enumerate(li):
Carrier.grid[(x, y)] = char
print(Carrier.grid)
# Carrier.grid = lines
# Carrier.grid = massage(Carrier.grid, 6)
carrier_X = len(lines) // 2
carrier_Y = len(lines) // 2
# Carrier.grid[carrier_Y][carrier_X - 1] = "#"
# Carrier.grid[carrier_Y - 1][carrier_X + 1] = "#"
c = Carrier2(carrier_X, carrier_Y)
number_of_bursts = 10_000_000
# pprint(lines, width=80, depth=80)
for _ in range(number_of_bursts):
# for line in Carrier.grid:
# print("".join(line))
c.burst()
log.debug(f"Current infections {c.infections}")
# 2512599
# pprint(Carrier.grid)
# not 6046
# not 5017
# a = [["a"], ["b"]]
# print(massage(a, 1))