diff --git a/08/input.puzzle b/08/input.puzzle new file mode 100644 index 0000000..53f8cdb --- /dev/null +++ b/08/input.puzzle @@ -0,0 +1,170 @@ +rect 1x1 +rotate row y=0 by 5 +rect 1x1 +rotate row y=0 by 5 +rect 1x1 +rotate row y=0 by 3 +rect 1x1 +rotate row y=0 by 2 +rect 1x1 +rotate row y=0 by 3 +rect 1x1 +rotate row y=0 by 2 +rect 1x1 +rotate row y=0 by 5 +rect 1x1 +rotate row y=0 by 5 +rect 1x1 +rotate row y=0 by 3 +rect 1x1 +rotate row y=0 by 2 +rect 1x1 +rotate row y=0 by 3 +rect 2x1 +rotate row y=0 by 2 +rect 1x2 +rotate row y=1 by 5 +rotate row y=0 by 3 +rect 1x2 +rotate column x=30 by 1 +rotate column x=25 by 1 +rotate column x=10 by 1 +rotate row y=1 by 5 +rotate row y=0 by 2 +rect 1x2 +rotate row y=0 by 5 +rotate column x=0 by 1 +rect 4x1 +rotate row y=2 by 18 +rotate row y=0 by 5 +rotate column x=0 by 1 +rect 3x1 +rotate row y=2 by 12 +rotate row y=0 by 5 +rotate column x=0 by 1 +rect 4x1 +rotate column x=20 by 1 +rotate row y=2 by 5 +rotate row y=0 by 5 +rotate column x=0 by 1 +rect 4x1 +rotate row y=2 by 15 +rotate row y=0 by 15 +rotate column x=10 by 1 +rotate column x=5 by 1 +rotate column x=0 by 1 +rect 14x1 +rotate column x=37 by 1 +rotate column x=23 by 1 +rotate column x=7 by 2 +rotate row y=3 by 20 +rotate row y=0 by 5 +rotate column x=0 by 1 +rect 4x1 +rotate row y=3 by 5 +rotate row y=2 by 2 +rotate row y=1 by 4 +rotate row y=0 by 4 +rect 1x4 +rotate column x=35 by 3 +rotate column x=18 by 3 +rotate column x=13 by 3 +rotate row y=3 by 5 +rotate row y=2 by 3 +rotate row y=1 by 1 +rotate row y=0 by 1 +rect 1x5 +rotate row y=4 by 20 +rotate row y=3 by 10 +rotate row y=2 by 13 +rotate row y=0 by 10 +rotate column x=5 by 1 +rotate column x=3 by 3 +rotate column x=2 by 1 +rotate column x=1 by 1 +rotate column x=0 by 1 +rect 9x1 +rotate row y=4 by 10 +rotate row y=3 by 10 +rotate row y=1 by 10 +rotate row y=0 by 10 +rotate column x=7 by 2 +rotate column x=5 by 1 +rotate column x=2 by 1 +rotate column x=1 by 1 +rotate column x=0 by 1 +rect 9x1 +rotate row y=4 by 20 +rotate row y=3 by 12 +rotate row y=1 by 15 +rotate row y=0 by 10 +rotate column x=8 by 2 +rotate column x=7 by 1 +rotate column x=6 by 2 +rotate column x=5 by 1 +rotate column x=3 by 1 +rotate column x=2 by 1 +rotate column x=1 by 1 +rotate column x=0 by 1 +rect 9x1 +rotate column x=46 by 2 +rotate column x=43 by 2 +rotate column x=24 by 2 +rotate column x=14 by 3 +rotate row y=5 by 15 +rotate row y=4 by 10 +rotate row y=3 by 3 +rotate row y=2 by 37 +rotate row y=1 by 10 +rotate row y=0 by 5 +rotate column x=0 by 3 +rect 3x3 +rotate row y=5 by 15 +rotate row y=3 by 10 +rotate row y=2 by 10 +rotate row y=0 by 10 +rotate column x=7 by 3 +rotate column x=6 by 3 +rotate column x=5 by 1 +rotate column x=3 by 1 +rotate column x=2 by 1 +rotate column x=1 by 1 +rotate column x=0 by 1 +rect 9x1 +rotate column x=19 by 1 +rotate column x=10 by 3 +rotate column x=5 by 4 +rotate row y=5 by 5 +rotate row y=4 by 5 +rotate row y=3 by 40 +rotate row y=2 by 35 +rotate row y=1 by 15 +rotate row y=0 by 30 +rotate column x=48 by 4 +rotate column x=47 by 3 +rotate column x=46 by 3 +rotate column x=45 by 1 +rotate column x=43 by 1 +rotate column x=42 by 5 +rotate column x=41 by 5 +rotate column x=40 by 1 +rotate column x=33 by 2 +rotate column x=32 by 3 +rotate column x=31 by 2 +rotate column x=28 by 1 +rotate column x=27 by 5 +rotate column x=26 by 5 +rotate column x=25 by 1 +rotate column x=23 by 5 +rotate column x=22 by 5 +rotate column x=21 by 5 +rotate column x=18 by 5 +rotate column x=17 by 5 +rotate column x=16 by 5 +rotate column x=13 by 5 +rotate column x=12 by 5 +rotate column x=11 by 5 +rotate column x=3 by 1 +rotate column x=2 by 5 +rotate column x=1 by 5 +rotate column x=0 by 1 diff --git a/08/input.sample01 b/08/input.sample01 new file mode 100644 index 0000000..a9df2bc --- /dev/null +++ b/08/input.sample01 @@ -0,0 +1,4 @@ +rect 3x2 +rotate column x=1 by 1 +rotate row y=0 by 4 +rotate column x=1 by 1 diff --git a/08/part01.py b/08/part01.py new file mode 100755 index 0000000..7b74676 --- /dev/null +++ b/08/part01.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python + +import re +import argparse + +SIZE_X = 50 +SIZE_Y = 6 + +RE_RECT = re.compile(r'rect (\d+)x(\d+)$') +RE_ROTATE_C = re.compile(r'rotate column x=(\d+) by (\d+)$') +RE_ROTATE_R = re.compile(r'rotate row y=(\d+) by (\d+)$') + +def load_file(filename): + with open(filename) as f: + for line in f: + yield line.strip() + +def do_rect(display, size_x, size_y): + for x in range(size_x): + for y in range(size_y): + display[y][x] = 1 + +def do_rotate_row(display, y, p): + p = SIZE_X - p + display[y] = display[y][p:] + display[y][:p] + +def do_rotate_column(display, x, p): + data = [display[i][x] for i in range(SIZE_Y)] + p = SIZE_Y - p + data = data[p:] + data[:p] + for i in range(SIZE_Y): + display[i][x] = data[i] + +def main(args): + DISPLAY = [] + for y in range(SIZE_Y): + line = [] + for x in range(SIZE_X): + line.append(0) + DISPLAY.append(line) + + for LINE in load_file(args.input): + match = RE_RECT.search(LINE) + if match: + do_rect(DISPLAY, int(match.group(1)), int(match.group(2))) + continue + match = RE_ROTATE_C.search(LINE) + if match: + do_rotate_column(DISPLAY, int(match.group(1)), int(match.group(2))) + continue + match = RE_ROTATE_R.search(LINE) + if match: + do_rotate_row(DISPLAY, int(match.group(1)), int(match.group(2))) + continue + raise Exception("unkown instruction: {}".format(LINE)) + + count = 0 + for y in range(SIZE_Y): + for x in range(SIZE_X): + count += DISPLAY[y][x] + print(count) + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='AdventOfCode 2016 Day 01') + parser.add_argument('--input', '-i', action='store', required=True, help='input file') + args = parser.parse_args() + + main(args) diff --git a/08/part02.py b/08/part02.py new file mode 100755 index 0000000..c538dbf --- /dev/null +++ b/08/part02.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python + +import re +import argparse + +SIZE_X = 50 +SIZE_Y = 6 + +RE_RECT = re.compile(r'rect (\d+)x(\d+)$') +RE_ROTATE_C = re.compile(r'rotate column x=(\d+) by (\d+)$') +RE_ROTATE_R = re.compile(r'rotate row y=(\d+) by (\d+)$') + +def load_file(filename): + with open(filename) as f: + for line in f: + yield line.strip() + +def do_rect(display, size_x, size_y): + for x in range(size_x): + for y in range(size_y): + display[y][x] = 'X' + +def do_rotate_row(display, y, p): + p = SIZE_X - p + display[y] = display[y][p:] + display[y][:p] + +def do_rotate_column(display, x, p): + data = [display[i][x] for i in range(SIZE_Y)] + p = SIZE_Y - p + data = data[p:] + data[:p] + for i in range(SIZE_Y): + display[i][x] = data[i] + +def main(args): + DISPLAY = [] + for y in range(SIZE_Y): + line = [] + for x in range(SIZE_X): + line.append(" ") + DISPLAY.append(line) + + for LINE in load_file(args.input): + match = RE_RECT.search(LINE) + if match: + do_rect(DISPLAY, int(match.group(1)), int(match.group(2))) + continue + match = RE_ROTATE_C.search(LINE) + if match: + do_rotate_column(DISPLAY, int(match.group(1)), int(match.group(2))) + continue + match = RE_ROTATE_R.search(LINE) + if match: + do_rotate_row(DISPLAY, int(match.group(1)), int(match.group(2))) + continue + raise Exception("unkown instruction: {}".format(LINE)) + + for y in range(SIZE_Y): + print("".join(DISPLAY[y])) + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='AdventOfCode 2016 Day 01') + parser.add_argument('--input', '-i', action='store', required=True, help='input file') + args = parser.parse_args() + + main(args) diff --git a/README.md b/README.md index 005f0ba..ceb9fe1 100644 --- a/README.md +++ b/README.md @@ -11,3 +11,4 @@ - **Day 5**: How About a Nice Game of Chess? - **Day 6**: Signals and Noise - **Day 7**: Internet Protocol Version 7 +- **Day 8**: Two-Factor Authentication