From 0f75dd0dff6bfbd004686ba28e94f73af561d0fc Mon Sep 17 00:00:00 2001 From: JonOfUs Date: Tue, 10 Dec 2024 09:02:55 +0100 Subject: [PATCH] d10 - with python --- .gitignore | 1 + python/10.py | 53 +++++++++++++++++++++++++++++++++++++++++++ res/10/example | 8 +++++++ res/10/input | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ src/days/d10.rs | 19 +++++----------- 5 files changed, 128 insertions(+), 13 deletions(-) create mode 100644 python/10.py create mode 100644 res/10/example create mode 100644 res/10/input diff --git a/.gitignore b/.gitignore index ea8c4bf..6c9e1e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +*/venv/ diff --git a/python/10.py b/python/10.py new file mode 100644 index 0000000..9002498 --- /dev/null +++ b/python/10.py @@ -0,0 +1,53 @@ +import networkx as nx + +# load file contents from ../res/10/input +with open("../res/10/input") as f: + lines = [[int(c) for c in line.strip()] for line in f] + +# create a directed graph +G = nx.DiGraph() + +starts = [] +ends = [] + +for i, line in enumerate(lines): + for j, c in enumerate(line): + G.add_node((i, j), pos=(i, j)) + +# add all edges to the graph +for i, line in enumerate(lines): + for j, c in enumerate(line): + if i > 0 and lines[i-1][j] == c+1: + G.add_edge((i, j), (i-1, j), weight=c) + if j > 0 and lines[i][j-1] == c+1: + G.add_edge((i, j), (i, j-1), weight=c) + if i < len(lines)-1 and lines[i+1][j] == c+1: + G.add_edge((i, j), (i+1, j), weight=c) + if j < len(line)-1 and lines[i][j+1] == c+1: + G.add_edge((i, j), (i, j+1), weight=c) + if c == 0: + starts.append((i, j)) + if c == 9: + ends.append((i, j)) + + + +#pos = nx.get_node_attributes(G, 'pos') +#nx.draw(G, pos, with_labels=True, node_color='lightblue', node_size=500, font_size=16) +#plt.show() + +# check if ends are reachable for starts +result = 0 +for start in starts: + for end in ends: + if nx.has_path(G, start, end): + result += 1 + +print("Result 1:",result) + +result = 0 +for start in starts: + for end in ends: + result += len(list(nx.all_simple_paths(G, start, end))) + +print("Result 2:",result) diff --git a/res/10/example b/res/10/example new file mode 100644 index 0000000..cada9b3 --- /dev/null +++ b/res/10/example @@ -0,0 +1,8 @@ +89010123 +78121874 +87430965 +96549874 +45678903 +32019012 +01329801 +10456732 diff --git a/res/10/input b/res/10/input new file mode 100644 index 0000000..6bf37ef --- /dev/null +++ b/res/10/input @@ -0,0 +1,60 @@ +101672126789567871215432345692104565432149876543210210165210 +234583005412345960104301876783213078985034987034324321894321 +898794112303876450201256969654532123834125870125495696765015 +347105678938989321345347238767651054923456678066780787892196 +256234569821672105496598123698743267812167569876541256543087 +101246541230543236787687034521650167600098452210434347893076 +765487430945650123689874501210010458321899321320345898432125 +812392121876210154318965678342181309456786870451256567589034 +906783032944301269203454369453892211034567965960127655678122 +105498893235692378112103223964763789123998234872198543987901 +012347710145785489023078114875654621001870198101083234896872 +143256601056676521032169005676910538912560187232172105765463 +650101542387234982349850321987823447103451276543432156710354 +781256730498105673256741410212345656458967345210589054801267 +292349821567834984109632565401298765367898721125678963987658 +143218762438921015678541078980345101210787430034567672108349 +012109687921089210010699012899876012101656526540545689019232 +123298796810874391523788743765210023089017017651656567567101 +264567345765965487654345651054382104672108978562347458478210 +876501210054056506961296652567893965543223489478998749309654 +930432011123143215870387743456894876654316554300145632218723 +121098723498234010965436892147765989969807621210239841012212 +032127854567872127830124321038967857878898210345678012332301 +343456965432963236543012430121456546545678149814798763441012 +856547876501454345632101569870367630430789034709823454456787 +987456989982321056567832678971278921821018125616710569344596 +898323678776545963498943267089101012900329870125211678233678 +543214549845034872167654154172112307812234568934303234102189 +670100234012123521015673013263001478703105567078450145221078 +789121124112345677893589234654816589654876432169867276098985 +458012043207654986712432102898987678723997654250178780127676 +367123856978943105403398701787676789210788023443239693234565 +212344987821012312314499652012565478943679117652108012345898 +101055678736703432965488343403454567652543208941032101056789 +567167899645898501876670912212343089801411012432143322765603 +498656018514567610154561801101232156787302347823434410810512 +301565323403078921210432765432320165696213456916565567923443 +212174321012169872321983656430110234345012387107896988019654 +143089102100110165434672106321025693230103896217867899528743 +056345243098241234345543567432234787121212344356956187634454 +167216356784356768766965498589105676034789651243445010546763 +098307458965349889457874321678210987345678760342336729867892 +123478969870221232356301230898121236789860987651029838758981 +874554878561110321001234454567033345652101456787010745643230 +965765467892025412120985213476542101543032321897655678994101 +259854326743476501031276302985433297890125670998556767783210 +108765012654389416542333421874344387743234987123449854654325 +237894121001274327985421510123435674659143498001230123087654 +367123432114565878876430678054510523438032567143456732192123 +458023449323676969216567869067823410547621052232109865434012 +569016558701987854107854952154996564678543121218987774325678 +652345667632234543236943343043287643789431030101876789810089 +421298796545105652345012894332106452100396545012765012102168 +310359687698236701489431765012987367011287236923454324303258 +210541056789127892376320012343456218983470147876565465214549 +323478145632036767665411289854387609322561228945456778927678 +416569230541145678547802398761296543211032310239843899818765 +507858121230978789236921234210435234508943001108702016709654 +698943034567879601105210123456520123677654112345611025212123 +787012347898765432434341038987011038989543233434323134321010 diff --git a/src/days/d10.rs b/src/days/d10.rs index 0bb7f55..d8b98d2 100644 --- a/src/days/d10.rs +++ b/src/days/d10.rs @@ -1,16 +1,9 @@ -use std::fs; - pub fn solve() { - let path = "res/10/input"; + println!("Running external Python program..."); - #[allow(unused)] - let contents = fs::read_to_string(path).expect("Something went wrong reading the file"); - - let result: usize = 0; - - println!("Result 1: {}", result); - - let result: usize = 0; - - println!("Result 2: {}", result); + std::process::Command::new("venv/bin/python") + .current_dir("python/") + .arg("10.py") + .status() + .unwrap(); }