Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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).
- #
- # 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:
- #
- # 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.
- #
- # 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:
- #
- # ..##.........##.........##.........##.........##.........##....... --->
- # #..O#...#..#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..
- # .#....X..#..#....#..#..#....#..#..#....#..#..#....#..#..#....#..#.
- # ..#.#...#O#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#
- # .#...##..#..X...##..#..#...##..#..#...##..#..#...##..#..#...##..#.
- # ..#.##.......#.X#.......#.##.......#.##.......#.##.......#.##..... --->
- # .#.#.#....#.#.#.#.O..#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#
- # .#........#.#........X.#........#.#........#.#........#.#........#
- # #.##...#...#.##...#...#.X#...#...#.##...#...#.##...#...#.##...#...
- # #...##....##...##....##...#X....##...##....##...##....##...##....#
- # .#..#...#.#.#..#...#.#.#..#...X.#.#..#...#.#.#..#...#.#.#..#...#.# --->
- #
- # In this example, traversing the map using this slope would cause you to encounter 7 trees.
- #
- # 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?
- from collections import namedtuple
- map = []
- with open("3 - input", 'r') as file:
- lines = file.readlines()
- linenum = 0
- for line in lines:
- linestr = line.strip("\n")
- 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.
- lines[linenum] = linestr
- linenum += 1
- map = lines
- width = len(map[0])-1
- height = len(map)-1
- print(f"Map width (X): {width}, Map height (Y): {height}")
- # Ugly way to be able to do pos[X], pos[Y]
- pos = [0, 0]
- X = 0
- Y = 1
- tree = '#'
- ground = '.'
- treeCount = []
- slopes = [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]
- slopeNum = 0
- doPrint = False # Console slows down alot if you print it all
- for slope in slopes:
- print(f"Sledding with stepping {slope[1]} down, {slope[0]} right")
- treeCount.append(0)
- pos = [0, 0]
- while pos[Y] <= height:
- printX = 0
- # This doesn't print every other line on the last slope, 2 vertical skips.
- if doPrint:
- for char in map[pos[Y]]:
- if printX == pos[X] and map[pos[Y]][pos[X]] != tree:
- print("O", end='')
- elif printX == pos[X] and map[pos[Y]][pos[X]] == tree:
- print("X", end='')
- else:
- print(char, end='')
- printX += 1
- print()
- if map[ pos[Y] ][ pos[X] ] == tree:
- treeCount[slopeNum] += 1
- # Only advance position if we stay within the map.
- if pos[X] + slope[0] > width:
- break
- pos[X] += slope[0]
- if pos[Y] + slope[1] > height:
- break
- pos[Y] += slope[1]
- slopeNum += 1
- print(f"Ending position: {pos[X]}, {pos[Y]}")
- print()
- for slope in treeCount:
- print(f"Trees in slope: {slope}")
- print(f"totalCount multiplied: {treeCount[0]*treeCount[1]*treeCount[2]*treeCount[3]*treeCount[4]}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement