Luna Lailatova
3 years ago
5 changed files with 378 additions and 0 deletions
@ -0,0 +1,138 @@
@@ -0,0 +1,138 @@
|
||||
from queue import PriorityQueue |
||||
import sys |
||||
import copy |
||||
|
||||
class grid: |
||||
def __init__(self): |
||||
self.y = [] |
||||
self.attempts = 0 |
||||
self.solutions = 0 |
||||
self.current_smallest_solution_level = None |
||||
self.point_costs = {(0, 0): 0} |
||||
self.next_point_queue = PriorityQueue() |
||||
self.next_point_queue.put((0, (0, 0))) |
||||
self.visited = {} |
||||
|
||||
def addline(self, line): |
||||
self.y.append([int(i) for i in line]) |
||||
self.maxy = len(self.y) - 1 |
||||
self.maxx = len(self.y[0]) - 1 |
||||
|
||||
# print(self.maxx) |
||||
# print(self.maxy) |
||||
|
||||
def largegrid(self): |
||||
for f in self.y: |
||||
temp = copy.copy(f) |
||||
for i in range(1, 5): |
||||
for t in range(len(temp)): |
||||
temp[t] += 1 |
||||
if temp[t] > 9: |
||||
temp[t] -= 9 |
||||
f.extend(temp) |
||||
temp2 = copy.copy(self.y) |
||||
for i in range(1, 5): |
||||
temp3 = copy.copy(temp2) |
||||
for f in temp3: |
||||
self.y.append([(g+i-1) % 9 + 1 for g in f]) |
||||
self.maxy = len(self.y) - 1 |
||||
self.maxx = len(self.y[0]) - 1 |
||||
|
||||
# def initialpath(self): |
||||
|
||||
def findpath(self): |
||||
while not self.next_point_queue.empty(): |
||||
level, poi = self.next_point_queue.get() |
||||
if self.visited.get(poi, False): |
||||
continue |
||||
self.visited[poi] = True |
||||
self.attempts += 1 |
||||
if self.attempts % 100000 == 0: |
||||
print('Attempt %d, solutions: %d, cost: %d, visited: %d, queued: %d' % ( |
||||
self.attempts, self.solutions, self.current_smallest_solution_level or -1, |
||||
len(self.visited), self.next_point_queue.qsize())) |
||||
|
||||
neighbors = self.get_neighbors(poi, level) |
||||
|
||||
for i in neighbors: |
||||
next_level = level + int(self.y[i[0]][i[1]]) |
||||
if (self.current_smallest_solution_level is not None and |
||||
next_level > self.current_smallest_solution_level): |
||||
# We already found a solution that was overall less risky |
||||
# than the current path, so who cares. |
||||
continue |
||||
if i == (self.maxy, self.maxx): |
||||
self.solutions += 1 |
||||
if (self.current_smallest_solution_level is None or |
||||
next_level < self.current_smallest_solution_level): |
||||
self.current_smallest_solution_level = next_level |
||||
# print('Found solution: ', i, ', orig level ', level) |
||||
continue |
||||
elif self.visited.get(i, False) and (i in self.point_costs and self.point_costs[i] <= next_level): |
||||
# We already visited the point on a cheaper path. |
||||
continue |
||||
else: |
||||
# print('Path: %s, next: %s' % (repr(path), i)) |
||||
self.point_costs[i] = next_level |
||||
self.next_point_queue.put((next_level, i)) |
||||
|
||||
# Try to find a path from the current point to the end by going down and right |
||||
# alternatingly, so we can restrict the search to paths less costly than the |
||||
# current one. |
||||
next_level = level |
||||
while poi != (self.maxx, self.maxy): |
||||
if poi[0] < self.maxx: |
||||
poi = (poi[0] + 1, poi[1]) |
||||
next_level += int(self.y[poi[0]][poi[1]]) |
||||
if poi in self.point_costs and next_level > self.point_costs[poi]: |
||||
break |
||||
self.point_costs[poi] = next_level |
||||
|
||||
if poi[1] < self.maxy: |
||||
poi = (poi[0], poi[1] + 1) |
||||
next_level += int(self.y[poi[0]][poi[1]]) |
||||
if poi in self.point_costs and next_level > self.point_costs[poi]: |
||||
break |
||||
self.point_costs[poi] = next_level |
||||
|
||||
if poi == (self.maxx, self.maxy): |
||||
self.solutions += 1 |
||||
if (self.current_smallest_solution_level is None or |
||||
next_level < self.current_smallest_solution_level): |
||||
self.current_smallest_solution_level = next_level |
||||
|
||||
|
||||
def get_neighbors(self, poi, level): |
||||
nex = {} |
||||
rv = [] |
||||
if poi[0] != self.maxy: |
||||
nex.setdefault(self.y[poi[0] + 1][poi[1]], list()).append((poi[0] + 1, poi[1])) |
||||
if poi[1] != self.maxx: |
||||
nex.setdefault(self.y[poi[0]][poi[1] + 1], list()).append((poi[0], poi[1] + 1)) |
||||
if poi[0] != 0: |
||||
nex.setdefault(self.y[poi[0] - 1][poi[1]], list()).append((poi[0] - 1, poi[1])) |
||||
if poi[1] != 0: |
||||
nex.setdefault(self.y[poi[0]][poi[1] - 1], list()).append((poi[0], poi[1] - 1)) |
||||
|
||||
order = sorted(nex.keys()) |
||||
|
||||
for j in order: |
||||
for p in nex[j]: |
||||
if (p not in self.visited and |
||||
(p not in self.point_costs or self.point_costs[p] >= level + j)): |
||||
rv.append(p) |
||||
|
||||
return rv |
||||
|
||||
gri = grid() |
||||
|
||||
with open(sys.argv[1], 'r') as f: |
||||
for i in f.readlines(): |
||||
gri.addline(i.strip()) |
||||
gri.largegrid() |
||||
#for f in gri.y: |
||||
# print("".join(map(str, f))) |
||||
|
||||
gri.findpath() |
||||
|
||||
print(gri.current_smallest_solution_level) |
@ -0,0 +1,80 @@
@@ -0,0 +1,80 @@
|
||||
from queue import PriorityQueue |
||||
import sys |
||||
|
||||
class grid: |
||||
def __init__(self): |
||||
self.y = [] |
||||
self.attempts = 0 |
||||
self.solutions = [] |
||||
self.current_smallest_solution_level = None |
||||
self.point_costs = {(0, 0): 0} |
||||
self.next_point_queue = PriorityQueue() |
||||
self.next_point_queue.put((0, (0, 0))) |
||||
|
||||
def addline(self, line): |
||||
self.y.append([int(i) for i in line]) |
||||
self.maxy = len(self.y) - 1 |
||||
self.maxx = len(self.y[0]) - 1 |
||||
|
||||
# print(self.maxx) |
||||
# print(self.maxy) |
||||
|
||||
def findpath(self): |
||||
while not self.next_point_queue.empty(): |
||||
level, poi = self.next_point_queue.get() |
||||
self.attempts += 1 |
||||
if self.attempts % 100000 == 0: |
||||
print('Attempt %d, solutions: %d, cost: %d' % (self.attempts, len(self.solutions), self.current_smallest_solution_level or -1)) |
||||
|
||||
neighbors = self.get_neighbors(poi) |
||||
|
||||
for i in neighbors: |
||||
next_level = level + int(self.y[i[0]][i[1]]) |
||||
if (self.current_smallest_solution_level is not None and |
||||
next_level > self.current_smallest_solution_level): |
||||
# We already found a solution that was overall less risky |
||||
# than the current path, so who cares. |
||||
continue |
||||
if i == (self.maxy, self.maxx): |
||||
self.solutions.append(next_level) |
||||
if (self.current_smallest_solution_level is None or |
||||
next_level < self.current_smallest_solution_level): |
||||
self.current_smallest_solution_level = next_level |
||||
# print('Found solution: ', i, ', orig level ', level) |
||||
continue |
||||
elif i in self.point_costs and self.point_costs[i] < next_level: |
||||
# We already visited the point on a cheaper path. |
||||
continue |
||||
else: |
||||
# print('Path: %s, next: %s' % (repr(path), i)) |
||||
self.point_costs[i] = next_level |
||||
self.next_point_queue.put((next_level, i)) |
||||
|
||||
def get_neighbors(self, poi): |
||||
nex = {} |
||||
rv = [] |
||||
if poi[0] != self.maxy: |
||||
nex.setdefault(self.y[poi[0] + 1][poi[1]], list()).append((poi[0] + 1, poi[1])) |
||||
if poi[1] != self.maxx: |
||||
nex.setdefault(self.y[poi[0]][poi[1] + 1], list()).append((poi[0], poi[1] + 1)) |
||||
if poi[0] != 0: |
||||
nex.setdefault(self.y[poi[0] - 1][poi[1]], list()).append((poi[0] - 1, poi[1])) |
||||
if poi[1] != 0: |
||||
nex.setdefault(self.y[poi[0]][poi[1] - 1], list()).append((poi[0], poi[1] - 1)) |
||||
|
||||
order = sorted(nex.keys()) |
||||
|
||||
for j in order: |
||||
rv += nex[j] |
||||
|
||||
return rv |
||||
|
||||
gri = grid() |
||||
|
||||
with open(sys.argv[1], 'r') as f: |
||||
for i in f.readlines(): |
||||
gri.addline(i.strip()) |
||||
|
||||
gri.findpath() |
||||
|
||||
print(min(gri.solutions)) |
@ -0,0 +1,100 @@
@@ -0,0 +1,100 @@
|
||||
8656175343119439947311919179279377257689197682985752287249881788527977345924133329967132199899199876 |
||||
4281288396984115627338957853913949146812568775798196289991176529889498798259196976277683981565984526 |
||||
8169838335918963371547591999342191683995972519179929683978126291855319511168245582651697938949414963 |
||||
9911589184911198759466719191189995157885898771177913669999436998997781742478168921196176418888127932 |
||||
2332856729917591158935782911513226531793881992469468248251991718456991551657621249229316797949388969 |
||||
6488738222981127824819626661531168733698299313631126969162927725778914183239351484699921454131481322 |
||||
3456499223985116399353928681116846939429849384129296119943791119497551946593566977823982927391143399 |
||||
9297612841849646148511299569971197927971682598351883892842936977227795411959211782251399118676896328 |
||||
1685688918999598249622886442487951289969917848948815999749154287969297396481891239821819247914959943 |
||||
2855192597719945162116189119381795158862162959133647579913886639586438893695967226719685616432291318 |
||||
3893139873964939791913261811173979825213968972939996181992593113695976322782892995939891783998623789 |
||||
5964671772545917912328949994999514157618987654288269789931394913375316999489275494179994919117119365 |
||||
3216339828898396212733674969295762442389789119366989996931919596282992864471192938639162947919972647 |
||||
7879826444123912118829914949989194219763533694839431198212319893181151696157671388992581982743988967 |
||||
7876122492176935838133394839673532581399474449569117478121989273123412313151952891988698552384251637 |
||||
8786999269197821985731819155537259938991261919979365751539876866146174376911518943778479997131942722 |
||||
7239919942299391891898171771589178983916281118889294494829292752139999754571391999826947791975394972 |
||||
2727617215141959982544247383138498917281833932371546394891963994434638187617528821379728933335772342 |
||||
6712949984729176138535147942577311588399869767114795759883948915177671474313191789814718292236422124 |
||||
8689792116597916131779139899373471282726812338914773516383771158691551851991689929199689197212298649 |
||||
6228175894997691471313338136819712699979449476188196729899628851391971728199959981585798691881191415 |
||||
9491157288112631922166642933446197292729817888649185515198768537925149995158353829229269894798938589 |
||||
5199389142294542461535132814487861987422485947698739199748986652229272998164982399192268248929567161 |
||||
6881119581991538187539159493621999896486112182779617795198955921614753699796171837819179999654231813 |
||||
6996595113784151975521879415898217119639999975184518599369743127684295298268259495226623211591871888 |
||||
1779949977648145993713949919359399998812143513534984334127763318649997689919849157137418761137512977 |
||||
9223292971993981422528813498119387921238194638215641911218326517839287886478991147999899894724396962 |
||||
7451983891767995938972123465939918261682281929689989464297789895588752221375859586787938391672994381 |
||||
1579954176878141996516584483611183926131694919877929897576151469657864935819598781814826881644187861 |
||||
8934595321937914584995177237653199951582515953621782173159453287575529819242531191989113518599339361 |
||||
3214791137819868237121897913928328682928239268681975567994149694512799167598489194267554981611157817 |
||||
3161289322443822496458312797912957538349962328948955764669152944278622928929944477635195993465821714 |
||||
3769985892295265479971729196428585411214871889139731131516448815999122871374996159138497416978598848 |
||||
4926877381218811127939655759733739111889455359949289979523168797571871445191441719179859376969393872 |
||||
7159871151274214979637977982939997337182139971896511115912187982698278842961545125121242636339621282 |
||||
8423118867997391616931374681582965659184916625321536161483641999294114251928796446618889979683993218 |
||||
6824788915976897879897933391998341399228914119912289149511515799142181679965255395897688149876629889 |
||||
4794395837416911945432167619882718499181629123717219977896911277339851349162195299228963994667691947 |
||||
1465122112768881153591399679274839985297692314947884169719151181986711822112796246416415955915718319 |
||||
7114797833579933611249299912348294873921288555119486979959982462111791467394161781467316779796883775 |
||||
9767795467854144194712742294766968549329117618283754959873952489784997597669593796889571883816748771 |
||||
1759974879458746414925558868197241214751687661841649999221178613679929261984417289292556869111324941 |
||||
9557955429999672993612951916598187741611893112948813816999484953994151249931191942493691497559992158 |
||||
1469791219499218437828194894539893191199115998162227659757698173867329816328599398544492677817272998 |
||||
6696472832532492295279572315976884291727999732161858916985514872395367168971969865959582923387918951 |
||||
9722652752119479169481198119314728131429449798379179119171791999189878174319864498112912224658495939 |
||||
9581794449119116165418178711351481129242636919987948291451161695541253977174231393582734959682898495 |
||||
4757727865924728111221629983989222587789999361791192917315287219922119718827676188689966872358484669 |
||||
1982319969915242121935864989286462118395187369193933592791414764965279817872197863615512262358899223 |
||||
1811379712999278717898361849316989959114612857179582117839285399618929182259624916911439331479239488 |
||||
1377519658968884779897799292198595917148591218821239614925192183128211371959295299959221257811239131 |
||||
2627929612263297913663198357736594275199769318118546371628241929618769991214845296132728799271417791 |
||||
7576972138942696587693261199467749912936563131454674591315468112333812659454789393699615918167338715 |
||||
1778985995949591574849625759667137792111923771976687288498928179271746938986792658149697999719931492 |
||||
1497174992519427759719169714342998311421419197412331399469329847692794191181111339933357161599799597 |
||||
7158163932199423259572847121198873491117119999919996985599149129919588579223518511588378639685891315 |
||||
6219361855573892912131718991998196749251949481814844299182119621344738991784149679694476483378729929 |
||||
1398599388779918861183115593522489331874978727922373471991219247115944295163699192776967648639268659 |
||||
8246143812623927544154898631717811662213981175534335932638718171969421398421692892399791789186618487 |
||||
4457134139841167885881924949364884427839314973843366971997383189398399657998961185159781215111619878 |
||||
2889934549617841969369167119338852267887359654637229634658717722728239549585928482358431813296859898 |
||||
9968328746979669249145999585143117898196251944175462877716129989773292878848966999379983436559934934 |
||||
6493994193996595991299891992438695916166928199197254269991979292986895799985938128881229638182945947 |
||||
7266928918675112795879219714929968471352931229592623394713393869269941181318972192169271548316892574 |
||||
2174196776119149368951321419512925818641848291921584561885586189445194773817792112569311611884895234 |
||||
5617593683181229369984148191819568774125471789395181874847932736429929334265516997661987796259692831 |
||||
7882228973393795938729341656949619511272988446634311621871394678132831899299832311614174939261965429 |
||||
1131881592984545192978449637151892697525357924197922698921549791127287595929496515499876251183628176 |
||||
9922954913893559597663824459627321943979599969474976998899879429185892995511392872381956558981867119 |
||||
5166385448694129439797196837181355547743133114829285275149516986685925813416161972979872853196155916 |
||||
3292899922794159291851551956738911971988929383861879921354871787791695284999126375594873179426338749 |
||||
3971894752927483651131869717199971871964396958991799413921821198121691265888799954912311963139425236 |
||||
5778565318498395151591219315499879273943817921588123199611531989674238272218198865913747444419629892 |
||||
1148956123134785152496887782841669964225944272953163717879528194631986997718971481186872943816456591 |
||||
5982998155291779554662769986899318991622943125199743569463317913185991442772122148595119965486877247 |
||||
1592622221626611159996955945922614981222438358385913259412688295754161193293194979914999974739594393 |
||||
1735995389872189896282749844299933869588931688619645726996959149185997128397635694766944945458588326 |
||||
8969839951998899979864239155832982419271118982882915197745124695321464198748765361499779978719441246 |
||||
6625484228731161195646227911299498172947526327866775111148173468799729895761595437743769876911891521 |
||||
6742199381937628999297557371399872217983581247138899399239252389649491949578491998528132446741686888 |
||||
9212328164223895178547771855976415127193137191217188318191949238429129518281919843229126971963111111 |
||||
8589368771155989697277997492471969978917117589192916193887975787949771588121551699971132275182798898 |
||||
1321997349438946615383211392181979153964221243634922639112684529286794272849757186396714797832698913 |
||||
5499455959999492339698359789213323519479768626457859213487938838995348379392231623595879353861382977 |
||||
4612129999347379911982695394862437936254989311441326295994617191963497895691479282499959828989987571 |
||||
3953949985681978999897341375236798381973326952579319464522624134872231113676167279146985521828793431 |
||||
2547232255918288952326674861524529119719429148626615319818611241832868529862499874951986568635799957 |
||||
2181959929952711246913363617991144311613441799129546223661433628933579819732979148127949974997119951 |
||||
1369158892917379123998253242599392893199998853969933983592129813969821127281951849151627687114757833 |
||||
9629699176294886577773394197932998769278865351155516385519239971147785457838922269188195227329168368 |
||||
1788912961677399663173822433232514884848179868666798987368382881226468481869127998236137881926332779 |
||||
4811241997249159619241887142461943467819366815858371379792287198271563986876852729186927761281942192 |
||||
2121993996798639492562961751217922319969898368461919218459919713413366292171628639922554153774327219 |
||||
3489591127775295131639799492971117993637992434115331278511281997587996291111198123428588991837368589 |
||||
1283798883941983912985615991541417994114453747887836936186431822993881123911799962749981389292178586 |
||||
8392631553657936578372199916912973193995216848776811924799769959318889717919172521769363698292447429 |
||||
9587697134368994212916856845984875416273418561198913859115983139967143891991617853289936814265113512 |
||||
9111571922348271733199797953979536915998195916688317991255916216419817129423728913622269119972958686 |
||||
9959599568987335314713638999897919449512991684446347789183915679567141547281548619496728636288445972 |
||||
8391247911139217869276338998734496391173478717461631572924754792241695917352991662734191814376765142 |
@ -0,0 +1,50 @@
@@ -0,0 +1,50 @@
|
||||
11637517422274862853338597396444961841755517295286 |
||||
13813736722492484783351359589446246169155735727126 |
||||
21365113283247622439435873354154698446526571955763 |
||||
36949315694715142671582625378269373648937148475914 |
||||
74634171118574528222968563933317967414442817852555 |
||||
13191281372421239248353234135946434524615754563572 |
||||
13599124212461123532357223464346833457545794456865 |
||||
31254216394236532741534764385264587549637569865174 |
||||
12931385212314249632342535174345364628545647573965 |
||||
23119445813422155692453326671356443778246755488935 |
||||
22748628533385973964449618417555172952866628316397 |
||||
24924847833513595894462461691557357271266846838237 |
||||
32476224394358733541546984465265719557637682166874 |
||||
47151426715826253782693736489371484759148259586125 |
||||
85745282229685639333179674144428178525553928963666 |
||||
24212392483532341359464345246157545635726865674683 |
||||
24611235323572234643468334575457944568656815567976 |
||||
42365327415347643852645875496375698651748671976285 |
||||
23142496323425351743453646285456475739656758684176 |
||||
34221556924533266713564437782467554889357866599146 |
||||
33859739644496184175551729528666283163977739427418 |
||||
35135958944624616915573572712668468382377957949348 |
||||
43587335415469844652657195576376821668748793277985 |
||||
58262537826937364893714847591482595861259361697236 |
||||
96856393331796741444281785255539289636664139174777 |
||||
35323413594643452461575456357268656746837976785794 |
||||
35722346434683345754579445686568155679767926678187 |
||||
53476438526458754963756986517486719762859782187396 |
||||
34253517434536462854564757396567586841767869795287 |
||||
45332667135644377824675548893578665991468977611257 |
||||
44961841755517295286662831639777394274188841538529 |
||||
46246169155735727126684683823779579493488168151459 |
||||
54698446526571955763768216687487932779859814388196 |
||||
69373648937148475914825958612593616972361472718347 |
||||
17967414442817852555392896366641391747775241285888 |
||||
46434524615754563572686567468379767857948187896815 |
||||
46833457545794456865681556797679266781878137789298 |
||||
64587549637569865174867197628597821873961893298417 |
||||
45364628545647573965675868417678697952878971816398 |
||||
56443778246755488935786659914689776112579188722368 |
||||
55172952866628316397773942741888415385299952649631 |
||||
57357271266846838237795794934881681514599279262561 |
||||
65719557637682166874879327798598143881961925499217 |
||||
71484759148259586125936169723614727183472583829458 |
||||
28178525553928963666413917477752412858886352396999 |
||||
57545635726865674683797678579481878968159298917926 |
||||
57944568656815567976792667818781377892989248891319 |
||||
75698651748671976285978218739618932984172914319528 |
||||
56475739656758684176786979528789718163989182927419 |
||||
67554889357866599146897761125791887223681299833479 |
Loading…
Reference in new issue