Skip to content

Commit 4930b31

Browse files
author
Simon Hessner
committed
Add solution for day 18
1 parent aa9ce8b commit 4930b31

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

day18/example

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.#.#...|#.
2+
.....#|##|
3+
.|..|...#.
4+
..|#.....#
5+
#.#|||#|#|
6+
...#.||...
7+
.|....|...
8+
||...#|.#|
9+
|.||||..|.
10+
...#.|..|.

day18/input

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
..##|##....#|.||#..#........##.#......||...#.|....

day18/solution.py

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# https://adventofcode.com/2018/day/18
2+
3+
from collections import Counter
4+
OPEN = "."
5+
TREES = "|"
6+
LUMBERYARD = "#"
7+
8+
def get_neighbors(x,y):
9+
ans = []
10+
for _x in range(x-1, x+2):
11+
for _y in range(y-1, y+2):
12+
if (_x,_y) != (x,y) and _x in range(0,50) and _y in range(0,50):
13+
ans.append((_x,_y))
14+
return ans
15+
16+
def get_types(grid, coords):
17+
ans = []
18+
for x,y in coords:
19+
ans.append(grid[y][x])
20+
return ans
21+
22+
def get_counts(elems):
23+
c = Counter(elems)
24+
return c[OPEN], c[TREES], c[LUMBERYARD]
25+
26+
def convert(grid, x,y):
27+
nbrs = get_types(grid, get_neighbors(x,y))
28+
n_open, n_trees, n_lumberyard = get_counts(nbrs)
29+
30+
if grid[y][x] == OPEN:
31+
if n_trees >= 3:
32+
return TREES
33+
elif grid[y][x] == TREES:
34+
if n_lumberyard >= 3:
35+
return LUMBERYARD
36+
elif grid[y][x] == LUMBERYARD:
37+
if n_trees < 1 or n_lumberyard < 1:
38+
return OPEN
39+
return grid[y][x]
40+
41+
mapgrid = lambda grid: [[convert(grid,x,y) for x in range(50)] for y in range(50)]
42+
43+
def part1():
44+
with open('input') as f:
45+
grid = list(map(list, f.read().splitlines()))
46+
for _ in range(10):
47+
grid = mapgrid(grid)
48+
49+
_, n_trees, n_lumberyard = get_counts(sum(grid, []))
50+
return n_trees*n_lumberyard
51+
52+
def part2():
53+
seen = []
54+
lastlen = 0
55+
56+
with open('input') as f:
57+
grid = list(map(list, f.read().splitlines()))
58+
repeating = False
59+
60+
t = 0
61+
offset = 0
62+
while not repeating:
63+
t += 1
64+
grid = mapgrid(grid)
65+
_, wood, lumb = get_counts(sum(grid, []))
66+
v = wood*lumb
67+
68+
if v in seen:
69+
if len(seen) == lastlen:
70+
repeating = True
71+
tmpv = v
72+
else:
73+
offset = t
74+
lastlen = len(seen)
75+
else:
76+
seen.append(v)
77+
78+
period = False
79+
periodic_values = {}
80+
while not period:
81+
t += 1
82+
grid = mapgrid(grid)
83+
_, wood, lumb = get_counts(sum(grid, []))
84+
v = wood*lumb
85+
if v == tmpv:
86+
period = True
87+
period_length = t-offset-1
88+
periodic_values[t] = v
89+
90+
periodic_values = {(t-offset)%period_length:v for t,v in periodic_values.items()}
91+
92+
return periodic_values[(1000000000-offset)%period_length]
93+
94+
print("Part 1", part1())
95+
print("Part 2", part2())

0 commit comments

Comments
 (0)