My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add solution for 2018/18

+113
+61
2018/day18.py
··· 1 + import fileinput 2 + from collections import defaultdict 3 + from itertools import count 4 + 5 + from utils import Point 6 + 7 + 8 + GRID = {} 9 + 10 + for y, line in enumerate(fileinput.input()): 11 + for x, t in enumerate(line.strip()): 12 + GRID[Point(x, y)] = t 13 + 14 + DIM = y + 1 15 + 16 + 17 + seen = defaultdict(list) 18 + resources = {} 19 + period = None 20 + last_period = None 21 + 22 + for minute in count(): 23 + tiles = GRID.values() 24 + trees = tiles.count('|') 25 + yards = tiles.count('#') 26 + 27 + seen[trees, yards].append(minute) 28 + resources[minute] = trees, yards 29 + 30 + if minute == 10: 31 + print "Resource value after 10 minutes:", trees * yards 32 + 33 + # Wait until we see periodic behaviour in the resource values observed 34 + if len(seen[trees, yards]) > 3: 35 + poss_period = minute - seen[trees, yards][-2] 36 + if poss_period == last_period: 37 + period = poss_period 38 + last_period = poss_period 39 + else: 40 + last_period = None 41 + 42 + if period is not None: 43 + if (1000000000 - minute) % period == 0: 44 + print "Resoruce value after 1000000000 minutes:", trees * yards 45 + break 46 + 47 + next_grid = {} 48 + 49 + for y in range(DIM): 50 + for x in range(DIM): 51 + p = Point(x, y) 52 + neighs = [GRID.get(q) for q in Point(x, y).neighbours_8()] 53 + 54 + if GRID.get(p) == '.': 55 + next_grid[p] = '|' if neighs.count('|') >= 3 else '.' 56 + elif GRID.get(p) == '|': 57 + next_grid[p] = '#' if neighs.count('#') >= 3 else '|' 58 + else: 59 + next_grid[p] = '#' if neighs.count('#') >= 1 and neighs.count('|') >= 1 else '.' 60 + 61 + GRID = next_grid
+50
2018/inputs/18.txt
··· 1 + .#|..|.#..|......#.#..#....#..|#.#..#....#.||.#|.| 2 + #...||||.|....#.|......#..|...|.|.|.|.#||......#|. 3 + .|##|#..|||.#..#|.||||.|.#.#|....||....#.#||...... 4 + #..|..##.....#.|.|#.|......||..|..|....|...|...|.| 5 + .#.|..|#|.|#....|...|.#.|.........||..|#|.##|.#|.. 6 + .....#..#.#.#...|.##...|.#.....|..#...#||.##...|.. 7 + ...#...........||##.|.#....|#..|##....|.||#..###.. 8 + #|||...#|.||.#|..|.#|||.........#....#.....|..|#.. 9 + ...||.#...|....|.....#|..#......#..##......||.#... 10 + .#.|.#||#.#...#....##||........|#|.#.#......#..#|| 11 + |.||...|...##|...||#..#.|#||.|#.#.#....#..##....|. 12 + .|...#|..#....##.#.|##|.#.#..||.#..|||.|||||....## 13 + |##.#..|.|...|.#.#...|.|..#..#|#||###|.#..#.#...## 14 + .||....#..#.##..#.|.#..||.#..|#..|..|.........#..| 15 + ###.....#...|...|###.|#.|#.|..|.#|...........#..|. 16 + |..#...#..#.....#.#..#.#.||.....#|.#|.|..#...|..#. 17 + ||.||..|....|.#...|#....|#.#|...|#.#.....|.|....|. 18 + |..|||.|...#...||.|||.||#..#|......#....|..#.....| 19 + .####..#..|#||.#||...#.#.#..#|.|..|....##.....|#.. 20 + ..||.|##|#.........#|......#..|||||........|...#.. 21 + |..#..|#.#..|.##.....#|.#.#...||.#...#.#|..|.....# 22 + .|..##.#.#.|#....#.###.|#|#..#||.#......#...#.||#. 23 + #.....|#.....#.||||.|...||.#.......#..#.#||.|..#.# 24 + ..#|||....||#|##.|#...|##.|....|.##.|.|..|...|.... 25 + .|.....|......||....|.|..|.|......|.|.#.|...|#...| 26 + |.##......#.||....#.#..#.....#...#.#|...#..|..#|.| 27 + .|#||.#|#...|.#|##.|.|.##.#.#....|#|.||.|.#|#..#.. 28 + |...|..|.||.###........|#||..|...#...##.|#..|....| 29 + ##.|...#|.|...#..#.||.|#..#||...#.#||..|.|....###| 30 + .|||.##.....|###...#||..|...#.#|....|.#|.......#|. 31 + .#..#...#.#|.|..#.|#.#..||......|#......|#.##|.##. 32 + .|||.||#|...#|||#.#.||....|...#...|...#.#.#....... 33 + |..|.#..#.........|#.........#.##........|#....#|| 34 + .......#..|#.#.|...|....#.......#.#|#.#....#.|.|#. 35 + .........|#.....|.#|...#...#.#|.#..|.....#..|#|.|. 36 + ....#.|....||||.#..|.|.#..|..|.|....#.|.|||..#.#.| 37 + .|.|....|.|....##.....|....||.#.###|||......|.#... 38 + ...|....|#|.##..##|||###....|..#..|#||.#||#.||...| 39 + .##||#|.###.#|###.#|#.#.##...#|...#........||...#. 40 + #|.#|..|.#....|..|.....|.#..|.|....#..||#.|.#.|.#. 41 + .....#..|.|####....||||...|#..##..#......#....|.|# 42 + .|.....#|#...#|#|.#.#||.#|##.|.#...#.|.|...####... 43 + |.|...||#|..#|.|...||.....#....||.|.|..##..#|..... 44 + |||##....#..#.|...#.|...|..|#|....|...........|#.. 45 + .#....||....#|#||#...|#|.#||.|.....##....#.|..##.. 46 + ...|.#|#|.#||.##.|.#|.##...|#...#|..#.#|.#..||||.. 47 + .#....#...||...|.....|..#...#.....|...#..#|##..... 48 + .....#.....#.|.|##.#..#.##||...###....#.....|#.... 49 + .|#|......|.||..#.#|.|#||....|.##.....#.#|.#..##.. 50 + ...||.#|||.|..||.....#.............|.#.|||#.#..##|
+2
2018/outputs/18.txt
··· 1 + 506385 2 + 215404