-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuelinjection.py
More file actions
105 lines (102 loc) · 3.48 KB
/
fuelinjection.py
File metadata and controls
105 lines (102 loc) · 3.48 KB
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
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 04 11:38:55 2017
@author: jasonros
"""
def answer(n):
from collections import deque
class Node:
def __init__(self, value,down,up):
self.value = value
self.down = down
self.up = up
def __hash__(self):
return self.value
def __eq__(self, other):
return self.value == other.value
def get_neighbors(self):
neighbors = []
value = self.value
up = self.up
down = self.down
if not (value % 2):
neighbors.append(Node(value >> 1,1,1))
else:
if up:
neighbors.append(Node(value+1,0,1))
if down:
neighbors.append(Node(value-1,1,0))
return neighbors
if int(n) == 0:
return 1
if int(n) == 1:
return 0
source = Node(int(n),1,1)
queue = deque([source])
step_map = {source.value: 0}
while queue:
current_node = queue.popleft()
for child_node in current_node.get_neighbors():
if child_node.value not in step_map.keys():
step_map[child_node.value] = step_map[current_node.value] + 1
if child_node.value == 1:
return step_map[child_node.value]
else:
queue.append(child_node)
print(answer("9"*309))
#python -m cProfile fuelinjection.py
#maze = [
#[0,1,1,0],
#[0,0,0,1],
#[1,1,0,0],
#[1,1,1,0]
#]
#print answer(maze)
#maze = [
#[0, 0, 0, 0, 0, 0],
#[1, 1, 1, 1, 1, 0],
#[0, 0, 0, 0, 0, 0],
#[0, 1, 1, 1, 1, 1],
#[0, 1, 1, 1, 1, 1],
#[0, 0, 0, 0, 0, 0]
#]
#print answer(maze)
#maze = [
# [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,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