Parcourir la source

Solve 2020 day 3

Jonatan Gezelius il y a 3 ans
Parent
commit
e0091a2bbf
4 fichiers modifiés avec 430 ajouts et 0 suppressions
  1. 44 0
      2020/03/a.py
  2. 52 0
      2020/03/b.py
  3. 11 0
      2020/03/example.txt
  4. 323 0
      2020/03/infile.txt

+ 44 - 0
2020/03/a.py

@@ -0,0 +1,44 @@
+import os, argparse
+tree = '#'
+
+def hit_function(map, x, y):
+    wrapped_x = x % len(map[0])
+
+    if (map[y][wrapped_x] == tree):
+        return True
+    else:
+        return False
+
+def check_stage(map, n):
+    x = 3*n
+    y = n
+    return hit_function(map, x, y)
+
+def solve_task(filename):
+    toboggan_slope = []
+    with open(filename) as infile:
+        for raw_line in infile:
+            line = raw_line.rstrip()
+            toboggan_slope.append(line)
+
+    trees_hit = 0
+    for n in range(len(toboggan_slope)):
+        if check_stage(toboggan_slope, n):
+            trees_hit += 1
+    
+    print(f"Totally {trees_hit} trees hit")
+
+    os.system("pause")
+
+def parse_arguments():
+    parser = argparse.ArgumentParser(description="Script that solves the case",epilog="Have a nice day!")
+    parser.add_argument('filename', nargs='?', default="example.txt", help='Input file')
+    args = parser.parse_args()
+    return args
+
+def main():
+    args = parse_arguments()
+    solve_task(args.filename)
+
+if __name__ == "__main__":
+    main()

+ 52 - 0
2020/03/b.py

@@ -0,0 +1,52 @@
+import os, argparse
+tree = '#'
+
+def hit_function(map, x, y):
+    wrapped_x = x % len(map[0])
+
+    if (map[y][wrapped_x] == tree):
+        return 1
+    else:
+        return 0
+
+def number_of_hits(map, right, down):
+    trees_hit = 0
+    stages = len(map)//down
+    for n in range(stages):
+        trees_hit += hit_function(map, n*right, n*down)
+    return trees_hit
+
+def solve_task(filename):
+    toboggan_slope = []
+    with open(filename) as infile:
+        for raw_line in infile:
+            line = raw_line.rstrip()
+            toboggan_slope.append(line)
+
+    tactics = [(1,1),
+    (3,1),
+    (5,1),
+    (7,1),
+    (1,2)]
+
+    score = 1
+    for tactic in tactics:
+        hits = number_of_hits(toboggan_slope, tactic[0], tactic[1])
+        score *= hits
+        print(f"Hit {hits} trees")
+
+    print(f"Score: {score}")
+    os.system("pause")
+
+def parse_arguments():
+    parser = argparse.ArgumentParser(description="Script that solves the case",epilog="Have a nice day!")
+    parser.add_argument('filename', nargs='?', default="example.txt", help='Input file')
+    args = parser.parse_args()
+    return args
+
+def main():
+    args = parse_arguments()
+    solve_task(args.filename)
+
+if __name__ == "__main__":
+    main()

+ 11 - 0
2020/03/example.txt

@@ -0,0 +1,11 @@
+..##.......
+#...#...#..
+.#....#..#.
+..#.#...#.#
+.#...##..#.
+..#.##.....
+.#.#.#....#
+.#........#
+#.##...#...
+#...##....#
+.#..#...#.#

+ 323 - 0
2020/03/infile.txt

@@ -0,0 +1,323 @@
+...#....#.#...##......#.#...##.
+.#..#...##..#....##........##..
+..##.##...##.#.#....#..#......#
+....#....#..#..#.#....#..###...
+####.....##.#.##...##..#....#..
+#........##...#..###..#.#.#.##.
+.......###........##...#...#...
+#.#...#..#..#...#...##.##......
+..#...........#......##.#.#....
+#..#.#......##.#...#..#.#..#...
+.#....#....#..#.....#..###..#.#
+.##....................#.###.#.
+..#......#.#......#..###.......
+#.###..##..###....#........#..#
+.....#...#.......#.#.#...#....#
+....##..#.##..#.##..#.#....#...
+.#..#..#......#..##.#.#....##.#
+.....#.....#.##...#.#.##.#.....
+.#...#..##.....#.........#.#...
+.....#........#........#..#..#.
+#......##....#..#.#...#...#....
+#......#.#.......##.#..#.#.#...
+...###...##.#..........#...#...
+...#.##...#......#.##....#.#...
+#...#.....#........#..#....##.#
+##.#....##...##.####.##.......#
+..#..#...#.#.......#.##.#......
+##.........#.##....#.#...#.....
+##..#......#....#.....#...#....
+......##.#.#.....#.#.###...#...
+##.......#......#.#..#...#.##..
+#....#....#..#.#..#.#..........
+...#....#.#.#.#.....##.#..#.##.
+......#.#.#....#...##.#..#.....
+..##..#.##....#...#.....#..#.##
+.#...#.#.##..##........#.#...#.
+##.##.##....#.#....##.###......
+#...........#....##..#......#..
+...##..#.....#....#....#.#..##.
+.#..#.##..##..#.........#.....#
+.##......##.#.#......##....#...
+.....##.#....#...#...........##
+..#.....#..##...#.#..#.........
+...#.####..#.###.....#.....##..
+.....#..##..#.###........#.#...
+#..#.#.....#.....#...#...#....#
+.#...........#..#.........##...
+...#.#.#.......#..............#
+.##.#..#...#........#.##.#.#.#.
+....#....#.......#......#.#....
+##.#..##.....##..#...#....#..#.
+#.........#.#..###...#....#..#.
+....##.###...#........##...#.##
+.#...#..##.##...........##...##
+.....#.##........#.#.......##..
+.....#......#..#..#...#.#...#..
+.....#.....#.#..#.###......####
+........#.......#...........#..
+...#.##..#....######..#.#...#..
+#...##...####..##.....##...#...
+.#.......#.###..#.##..#..#.#.#.
+....#..#....##.....#........#.#
+.#..#....##..#........#...#...#
+#.#..#.#...##...#..........#.#.
+##.####....##....#...##.####...
+..##......##....#.....#...#..#.
+...#......#..#...#..#.#....#.#.
+......#...#....##....#.##......
+.##...#.........####..#....#..#
+...#..#.#......#.....#.#.#.#...
+...........#.......#........##.
+#.#....#....#.##.#...#...#.#...
+#..#.....#..#..#.#...#........#
+.....#.#.##...#............#.##
+..#.#.#...#...#..###.#.....#...
+....#.....##.......#......#....
+.#.#....#.##......##.#.#.....#.
+.....#.##.....#.#...#....#....#
+...#.......#..#.#.###.#.###....
+.#...#..#..#....#...###.#.##...
+.#....#........#..........##...
+##.#......#..#.#..........#.#..
+...#..#.......#.##..###....#.#.
+.#.....#..........#..#.........
+...##...##.#.......#..#....#...
+.....#.##....#.#.#...#.......#.
+...#.#..#.#.......#.....##..#..
+.#.#.......#.#..........#...#.#
+#........#...#....###.#...#....
+..##....#.#.#.....###.#.#......
+..#.....#.#...#.#.....#.#......
+.#.....##......#....##.#.#....#
+...##..........#.#####...#..#..
+#....#......##.............#...
+......#.....#...##..#.#....#..#
+..###.#.###.#.##.#.##..#.....#.
+#...#....#.#....#..##...#...#.#
+...#....#...#.#......#......#..
+#......#.....#.#..#........#...
+...##...##...##..###...###.#...
+#..#...#...#.....#..###...##...
+.#.....#.......#....#....##....
+......#......##.#.#......#.#...
+..#....#.#..........#..#...#..#
+###...#..............#.........
+.###.##..#.#..#.....#.#.##....#
+..#......#.##....##......#....#
+.##....###.#..#.#.............#
+..##..#..#.#.#....#..##.#..#..#
+##.#.....##...#..#...#..#.#.#..
+.#.#..............#.#...#......
+#....##...#....#..#...#........
+...#..#..#.#........#..#..#..##
+...##.#.#.#....#.......#....#..
+.##.#......#.##........#...##..
+.#.##..#.....###..#.#......#..#
+#....#..#......#....##...#..#.#
+..##....#.##...#..#.##....##..#
+#....#..#...#.......#...#......
+#.#......#....#.#..............
+..##..#..#.#.......#.#.#...##..
+...##.##......#..#.##.#.......#
+.##.........#............#.#...
+........#...#.....#....#....#..
+#....##...#........#......##...
+.###....#..#.#......#....#.#...
+#.#...........#...#....#..#.#.#
+.#....##.###...#.##....##..#..#
+........#.............#.#...#.#
+#.##..#.##....##.......#.......
+.#.....#..#.##..##.....#.......
+....#.#......#.#.............#.
+..#.#.......#...#......##..#...
+......##..###....##.#.###..##.#
+..#..#.##...#...#....#.##..#..#
+#.##...##.##.#.#........#......
+...#.#.......##....#.......#...
+.#....#.##.#..#.......#.....##.
+#..#...##.####..###.....#......
+..#...##...#...#.#......#...#.#
+.##.##.#...#.....#.##..##......
+.#...#.#.##.###..#...#...#.....
+.#..##..#....##.##....##....##.
+..#...##....#..###........##...
+.#..#..#.#....#.#...#.#......#.
+.##.....#...#..#..#..#...###...
+.#...#....#..#...........###...
+.....#...........##.#......#...
+.....#....##......##..#.#......
+.#.#..#..#...#.#..#..##..#.#...
+..#..#####.#..#.#.#.....#.#....
+#......#.##.#......#.#......#..
+.#...............#..#.#.......#
+...##...##.....##.##......#..##
+#.......##.#...........#....#..
+#...##.#...#.#..#..###...##....
+#..##..#..#......#.#...###..#.#
+.#.#..###.##..##.....#.........
+##.#..##.#..........#.......#..
+.###..##....##..#..#...##......
+........#..##.##....#....#.....
+.....#.#.#........#...#....#.#.
+..#..#.#.....#.##....##........
+...#....................#....#.
+..#....#..###.##......#..#.#.##
+.....#.#....#......#...#......#
+###...##.......#.#.#.....#....#
+..#..###.##..#..#....##.......#
+#...#.###..#...#.##..##........
+..#..#....#...........##..#....
+.#............##..#...##.#..#..
+.#.......#.#.........##..#..#..
+....#.###.#...##....#.#.##.....
+#.......#........#..#.##......#
+#.....###....#....#..#....#....
+...........#...#........#.....#
+..##...#...#.##...##.#.#..#...#
+##.##..#.......##..........#..#
+............#.....#...#..#....#
+.....#.#......#...#..#.#...#..#
+............##.##.....##...#..#
+##.#..#..#.#......#......#..##.
+..#..#.........#...#..#.#.#....
+#..#........#.......###.#......
+...#.#.#....#......#.#...#.....
+#...............#...#......##..
+###.#...#......##.....#..##.##.
+#.##...##.......###.##.........
+.#.#.........#..#.....#........
+..##..#............#.....#...##
+......###...#.#...#..#...##....
+#.........#.#.##.........##....
+.#....##....#....##....#.##....
+.#...##.#...#.......#...#....##
+#................#......##.....
+##..#......#.#.........##...##.
+...#.#.#...#..#..##..#....#.#..
+.##........#.#......##...#..#..
+...##....#.#.....##..#.........
+........###.#.#....###.##.#....
+.............#....###.#..#.#...
+.#...........##....#...#.......
+#..#.......#.....##..#.........
+..#.#..#....##...##.#.......#.#
+#.###.#.........#..#.........#.
+.......#.........##....##.#..##
+.##...###..........#....#.#..##
+.#.....#......##..#...#........
+##..#..###........#.......#....
+........#...#.##...........#.#.
+.....#..#..###.............#..#
+...######.##.....#...#......#..
+.#...#....###...###......#.##..
+...##.####.........#...#...##..
+..##....#.......#.....#..#.....
+#.##....#.#........#.....##....
+.....#.#.###...........#...##.#
+.............###........#......
+...#..#...##.#....#..##.#.....#
+....#.#.....#...#......###..##.
+........#.......#..#...........
+....#.#...#......#.#.....###...
+..#.............####.##.....#..
+..........#...#.#..............
+..#.#...........#.#..##...##..#
+.......#...#........#..##....##
+#...#.....#.#.##...#.#.#...#..#
+.....#..#....#.###.#.....#.#..#
+....#.#.#...#.................#
+#..#........##.###....#....#...
+..##...##..............##....#.
+...#.#...#.#...##........#...#.
+##..##.#..#.#.#..#.#...#.#.#.##
+..#.#..#....#..#.###.........#.
+..#.....#..#..#........#.......
+...#...#......##...###.....##..
+#....#.##.....#.##..##..#..#...
+......##..#.#.#..#.###...#...#.
+.##.....##............#.#......
+..#.#........##....#..#.####.#.
+.#....###..#.......#...........
+##...........#..#...#.....#.#..
+.#..#....#..#....#.#.........#.
+.#.###....#.....##.....##......
+.#.#.......#.......#.###.#...#.
+...###..#..#....#..####.##....#
+..........##...#...#...........
+.#.....#.#..##..##...##..#.....
+##.......#.#...#..###..#..##...
+..#.##......###..#........#..##
+....###...#..........#.#..#.#..
+#..#.#..##.......##....#.#..##.
+.#...#...#..#....#...###.......
+.##.#.#.#.....#.....#.#.#.....#
+...##.#.....#..#.#...#.####.#..
+#...##.....####..#.###.##..#.##
+..###..##....#####......##.....
+###..............#.#..........#
+#...##......##....#...#..##..#.
+.#..#...#....##.##.....#....##.
+...#.#...#..#..#.......#.##....
+.#...#..#.......#.##.#..#...#.#
+.#..#.##...#...#.............#.
+.#.#.#....##..#.#.#.#.#......##
+..#....#.#...##.##.#...#.#.#.#.
+...#..#..##.........##..#.....#
+....#..........#.#.......#.....
+........#....#.#..#.....#.#.#..
+.#...#.......#..#.##.........#.
+##......#.....#...........#.#..
+....#....##...#..#####.####...#
+.....##....#....#..#.....#..##.
+.#...#.#..###....#......#...#..
+.#.#.#.#.....##..........#....#
+...#.....#.....#..#............
+...#.#..#.....#.............#..
+......#....#.#.....#.##...#..#.
+...#....#.#...##..#....###.....
+...#..#............##...##.##..
+..#....#..............#.....#..
+.#..#.......#.#.##......#.###..
+..#...#.........#..#.....#.....
+#..........#.....##............
+##.#.#.#....##......#..#....#.#
+.#..#.....#.#..#.....#..##.....
+...#.#.....#..#..#..#.##.###...
+...##.#.....#.....#.......#....
+.....................##.#...#..
+...#.#...#...#...###..#..#.#...
+#....##..###.#...##.#......#..#
+.##...##..##...##......##.#....
+............#.....#.##.#.##.#.#
+.#...##.#..........###......#..
+.....##.....##....#.#....##..#.
+...#....#.....#....#.....#.#...
+................##.#...#....##.
+#..###.......#......###......#.
+###..#.........#.#.#.....#....#
+..#.#......#....#.#......##...#
+#...#......#.#.#.#..#...#..###.
+.#...#..#...##..#...##....###..
+#.#.#.#..###................#.#
+...#.####....##.#.....#..#.....
+#.#...##..#.#...###.#..#...#...
+#..####........#.#..#...##..##.
+..#..###..#..........##.###...#
+.....#..###.#.#....#....##....#
+.###..#.#.#....#...##.......##.
+#..#..........#...............#
+.#.#...#.....#.###..##......##.
+#...#...#...##...##.....##..#..
+.....##..###...........#.......
+........###..##........#..##..#
+#....#...##..##..#.#.#.###...#.
+.#..#......##.#..#...#.........
+.#....#.##.....................
+#.##.##...........#.####..#....
+......##....#..#.....#.#....#..
+....##.#........#.#.#.#.#..#..#
+.#..##..##..#.....#...#.#.....#
+...#..#.#.....#..#..##....##..#
+..#....#.....#........#.....#..
+#.#.#..#..#.#..#...###.#.#....#