Kaynağa Gözat

Add ugly solusion to day 2

Jonatan Gezelius 4 yıl önce
ebeveyn
işleme
64afa4ea0f
2 değiştirilmiş dosya ile 32 ekleme ve 4 silme
  1. 4 4
      02/a.py
  2. 28 0
      02/a_ugly.py

+ 4 - 4
02/a.py

@@ -6,12 +6,12 @@ def solve_task(filename):
     depth = 0
     
     match_string = r"(?P<command>[a-z]*) (?P<distance>\d*)"
-    matcher = re.compile(match_string)
+    #matcher = re.compile(match_string)
     
     with open(filename) as infile:
-        for raw_line in infile:
-            line = raw_line.rstrip()
-            m = matcher.match(line)
+        for line in infile:
+            #m = matcher.match(line)
+            m = re.search(match_string, line)
             command = m.group('command')
             distance = int(m.group('distance'))
             

+ 28 - 0
02/a_ugly.py

@@ -0,0 +1,28 @@
+import os, argparse, re
+
+
+def solve_task(filename):
+    horizontal_position = 0
+    depth = 0
+        
+    with open(filename) as infile:
+        for line in infile:
+            distance = int(re.search(r"(?P<command>[a-z]*) (?P<distance>\d*)", line).group('distance'))
+            horizontal_position += distance if 'forward' in line else 0
+            depth += distance if 'down' in line else -distance if 'up' in line else 0
+    
+    print("Product: " + str(depth * horizontal_position))
+    os.system("pause")
+
+def parse_arguments():
+    parser = argparse.ArgumentParser(description="Solves a part of advent of code.",epilog="Have a nice day!")
+    parser.add_argument('filename', nargs='?', default="infile.txt", help='Input file')
+    args = parser.parse_args()
+    return args
+
+def main():
+    args = parse_arguments()
+    solve_task(args.filename)
+
+if __name__ == "__main__":
+    main()