Advertisement
Armandur

Untitled

Dec 3rd, 2020
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.66 KB | None | 0 0
  1. # You start on the open square (.) in the top-left corner and need to reach the bottom (below the bottom-most row on your map).
  2. #
  3. # The toboggan can only follow a few specific slopes (you opted for a cheaper model that prefers rational numbers); start by counting all the trees you would encounter for the slope right 3, down 1:
  4. #
  5. # From your starting position at the top-left, check the position that is right 3 and down 1. Then, check the position that is right 3 and down 1 from there, and so on until you go past the bottom of the map.
  6. #
  7. # The locations you'd check in the above example are marked here with O where there was an open square and X where there was a tree:
  8. #
  9. # ..##.........##.........##.........##.........##.........##.......  --->
  10. # #..O#...#..#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..
  11. # .#....X..#..#....#..#..#....#..#..#....#..#..#....#..#..#....#..#.
  12. # ..#.#...#O#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#
  13. # .#...##..#..X...##..#..#...##..#..#...##..#..#...##..#..#...##..#.
  14. # ..#.##.......#.X#.......#.##.......#.##.......#.##.......#.##.....  --->
  15. # .#.#.#....#.#.#.#.O..#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#
  16. # .#........#.#........X.#........#.#........#.#........#.#........#
  17. # #.##...#...#.##...#...#.X#...#...#.##...#...#.##...#...#.##...#...
  18. # #...##....##...##....##...#X....##...##....##...##....##...##....#
  19. # .#..#...#.#.#..#...#.#.#..#...X.#.#..#...#.#.#..#...#.#.#..#...#.#  --->
  20. #
  21. # In this example, traversing the map using this slope would cause you to encounter 7 trees.
  22. #
  23. # Starting at the top-left corner of your map and following a slope of right 3 and down 1, how many trees would you encounter?
  24.  
  25. from collections import namedtuple
  26.  
  27.  
  28. map = []
  29. with open("3 - input", 'r') as file:
  30.     lines = file.readlines()
  31.     linenum = 0
  32.     for line in lines:
  33.         linestr = line.strip("\n")
  34.  
  35.         linestr = linestr*11*7 # Multiply to get map wide enough, *11 to get square, * 7 because we move a total of times 7 to the right in the least sloping slope.
  36.  
  37.         lines[linenum] = linestr
  38.  
  39.         linenum += 1
  40.     map = lines
  41.  
  42. width = len(map[0])-1
  43. height = len(map)-1
  44.  
  45. print(f"Map width (X): {width}, Map height (Y): {height}")
  46.  
  47. # Ugly way to be able to do pos[X], pos[Y]
  48. pos = [0, 0]
  49. X = 0
  50. Y = 1
  51.  
  52. tree = '#'
  53. ground = '.'
  54.  
  55. treeCount = []
  56.  
  57. slopes = [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]
  58.  
  59. slopeNum = 0
  60. doPrint = False  # Console slows down alot if you print it all
  61. for slope in slopes:
  62.     print(f"Sledding with stepping {slope[1]} down, {slope[0]} right")
  63.     treeCount.append(0)
  64.  
  65.     pos = [0, 0]
  66.     while pos[Y] <= height:
  67.         printX = 0
  68.  
  69.         # This doesn't print every other line on the last slope, 2 vertical skips.
  70.         if doPrint:
  71.             for char in map[pos[Y]]:
  72.                 if printX == pos[X] and map[pos[Y]][pos[X]] != tree:
  73.                     print("O", end='')
  74.                 elif printX == pos[X] and map[pos[Y]][pos[X]] == tree:
  75.                     print("X", end='')
  76.                 else:
  77.                     print(char, end='')
  78.                 printX += 1
  79.             print()
  80.  
  81.         if map[ pos[Y] ][ pos[X] ] == tree:
  82.             treeCount[slopeNum] += 1
  83.  
  84.         # Only advance position if we stay within the map.
  85.         if pos[X] + slope[0] > width:
  86.             break
  87.         pos[X] += slope[0]
  88.  
  89.         if pos[Y] + slope[1] > height:
  90.             break
  91.         pos[Y] += slope[1]
  92.     slopeNum += 1
  93.     print(f"Ending position: {pos[X]}, {pos[Y]}")
  94.     print()
  95.  
  96. for slope in treeCount:
  97.     print(f"Trees in slope: {slope}")
  98.  
  99. print(f"totalCount multiplied: {treeCount[0]*treeCount[1]*treeCount[2]*treeCount[3]*treeCount[4]}")
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement