Skip to content

Commit

Permalink
Day 22, both parts.
Browse files Browse the repository at this point in the history
  • Loading branch information
pak21 committed Dec 22, 2017
1 parent c99ddd4 commit 372f14c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
36 changes: 36 additions & 0 deletions 22/22-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/python3

import collections
import sys

grid = collections.defaultdict(lambda: collections.defaultdict(bool))

x_moves = [0, 1, 0, -1]
y_moves = [-1, 0, 1, 0]

with open(sys.argv[1]) as input_file:
linenum = 0
for line in [l.rstrip() for l in input_file]:
infected = [c == '#' for c in list(line)]
offset = (len(infected) - 1) // 2
for i in range(0, len(infected)):
grid[linenum - offset][i - offset] = infected[i]
linenum += 1

x = 0
y = 0
direction = 0

counts = collections.defaultdict(int)

for step in range(0, 10000):
dir_change = 1 if grid[y][x] else -1
direction = (direction + dir_change) % 4

counts[grid[y][x]] += 1
grid[y][x] = not grid[y][x]

x += x_moves[direction]
y += y_moves[direction]

print(counts)
36 changes: 36 additions & 0 deletions 22/22-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/python3

import collections
import sys

grid = collections.defaultdict(lambda: collections.defaultdict(int))

x_moves = [0, 1, 0, -1]
y_moves = [-1, 0, 1, 0]

with open(sys.argv[1]) as input_file:
linenum = 0
for line in [l.rstrip() for l in input_file]:
infected = [2 if c == '#' else 0 for c in list(line)]
offset = (len(infected) - 1) // 2
for i in range(0, len(infected)):
grid[linenum - offset][i - offset] = infected[i]
linenum += 1

x = 0
y = 0
direction = 0

counts = collections.defaultdict(int)

for step in range(0, 10000000):
dir_change = grid[y][x] - 1
direction = (direction + dir_change) % 4

counts[grid[y][x]] += 1
grid[y][x] = (grid[y][x] + 1) % 4

x += x_moves[direction]
y += y_moves[direction]

print(counts)
9 changes: 9 additions & 0 deletions 22/testcase.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.........
.........
.........
.....#...
...#.....
.........
.........
.........
.........

0 comments on commit 372f14c

Please sign in to comment.