From 6e366d7ade526266929eb82871d16e420238e047 Mon Sep 17 00:00:00 2001 From: Peter Hudec Date: Mon, 21 Dec 2015 00:44:35 +0100 Subject: [PATCH] another day of coding --- 17/part1.py | 51 +++++++++++++++++++++++++++ 17/part2.py | 51 +++++++++++++++++++++++++++ 17/readme | 7 ++++ 18/input | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 18/input1 | 6 ++++ 18/part1.py | 71 +++++++++++++++++++++++++++++++++++++ 18/part2.py | 78 ++++++++++++++++++++++++++++++++++++++++ 19/input | 45 +++++++++++++++++++++++ 8 files changed, 409 insertions(+) create mode 100644 17/part1.py create mode 100644 17/part2.py create mode 100644 17/readme create mode 100644 18/input create mode 100644 18/input1 create mode 100644 18/part1.py create mode 100644 18/part2.py create mode 100644 19/input diff --git a/17/part1.py b/17/part1.py new file mode 100644 index 0000000..3cbc64c --- /dev/null +++ b/17/part1.py @@ -0,0 +1,51 @@ +#!/usr/bin/python + +import re + +RE_INGRADIENT_INFO = re.compile(r'^(.*): capacity (-?\d+), durability (-?\d+), flavor (-?\d+), texture (-?\d+), calories (-?\d+)$') + + +def read_file(filename): + file = open(filename, 'r') + while True: + line = file.readline() + if not line: + break + yield line + + +def bottle_usage_positions(minCount, maxCount, pos, usage): + if pos == len(usage): + yield usage + return + + for x in range(minCount, maxCount+1): + usage[pos] = x + for b in bottle_usage_positions(minCount, maxCount, pos+1, usage): + yield b + usage[pos] = minCount + +def calculate_liters(bottles, usage): + sum = 0 + bottles_used = [] + for x in range(len(bottles)): + sum += bottles[x] * usage[x] + bottles_used.append(bottles[x] * usage[x]) + return (sum, bottles_used) + + +def main(): + input = 150 + bottles = [] + bottle_usage = [] + for line in read_file('input'): + bottles.append(int(line)) + bottle_usage.append(0) + + for usage in bottle_usage_positions(0, 1, 0, bottle_usage): + (litres, used) = calculate_liters(bottles, usage) + if litres == input: + print used + +if __name__ == "__main__": + main() diff --git a/17/part2.py b/17/part2.py new file mode 100644 index 0000000..6b879ff --- /dev/null +++ b/17/part2.py @@ -0,0 +1,51 @@ +#!/usr/bin/python + +import re + +RE_INGRADIENT_INFO = re.compile(r'^(.*): capacity (-?\d+), durability (-?\d+), flavor (-?\d+), texture (-?\d+), calories (-?\d+)$') + + +def read_file(filename): + file = open(filename, 'r') + while True: + line = file.readline() + if not line: + break + yield line + + +def bottle_usage_positions(minCount, maxCount, pos, usage): + if pos == len(usage): + yield usage + return + + for x in range(minCount, maxCount+1): + usage[pos] = x + for b in bottle_usage_positions(minCount, maxCount, pos+1, usage): + yield b + usage[pos] = minCount + +def calculate_liters(bottles, usage): + sum = 0 + bottles_used = 0 + for x in range(len(bottles)): + sum += bottles[x] * usage[x] + bottles_used += usage[x] + return (sum, bottles_used) + + +def main(): + input = 150 + bottles = [] + bottle_usage = [] + for line in read_file('input'): + bottles.append(int(line)) + bottle_usage.append(0) + + for usage in bottle_usage_positions(0, 1, 0, bottle_usage): + (litres, used) = calculate_liters(bottles, usage) + if litres == input: + print used + +if __name__ == "__main__": + main() diff --git a/17/readme b/17/readme new file mode 100644 index 0000000..fdb7491 --- /dev/null +++ b/17/readme @@ -0,0 +1,7 @@ +## part1 + + python part1.py | wc -l + +## part2 + + python part2.py | sort | uniq -c diff --git a/18/input b/18/input new file mode 100644 index 0000000..e91a2a7 --- /dev/null +++ b/18/input @@ -0,0 +1,100 @@ +###.##..##.#..#.##...#..#.####..#.##.##.##..###...#....#...###..#..###..###.#.#.#..#.##..#...##.#..# +.#...##.#####..##.......#..####.###.##.#..###.###.....#.#.####.##.###..##...###....#.##.....#.#.#.## +.....#.#.....#..###..####..#.....##.#..###.####.#.######..##......#####.#.##.#########.###..#.##.#.# +...###......#.#..###..#.#.....#.##..#.##..###...#.##.#..#..#.##.#..##......##.##.##.######...#....## +.###.....#...#.#...####.#.###..#..####.#..#.##..####...##.#...#..###...###...####..##....####.##..#. +..#....#...#.......#..###.###....#.##..#.....###.#.##.#....#.#....##.##..#.##.#..###.###.##.##..##.# +##..#####.#.#....#.#...#.#.####..#....#..#....#.#..#.#####...#..##.#.....#.##..##.####......#.#.##.. +.#..##..#.#.###..##..##...#....##...#..#.#..##.##..###.####.....#.####.#.....##.#.##...#..####..#... +#.#####.......#####...#...####.#.#.#....#.###.#.##.#####..#.###.#..##.##.#.##....#.##..#....####.#.# +#.##...#####....##.#.#.....##......##.##...#.##.##...##...###.###.##.#.####.####.##..#.##.#.#.####.. +#.##.##....###.###.#..#..##.##.#..#.#..##..#.#...#.##........###..#...##.#.#.##.......##.....#...### +###..#.#..##.##.#.#.#...#..#...##.##.#.########.......#.#...#....########..#.#.###..#.#..#.##..##### +####.#.#...#.##.##..#.#...#....#..###..#.#.#.####.#.##.##.#..##..##..#..#####.####.##..########..##. +.#.#...#..##.#..#..###.#..####.......##.#.#.#.##.#####..#..##...#.##...#..#....#..#..###..####.#.... +..#.#...#....##...#####..#..#...###.###.....#.###.#....#.#..##...#.##.##.####.#.#.#..#.##.#....#.#.. +#....###.####.##..#.#.###..###.##.##..#.#...###..#.##.#####.##.#######..#.#...##.#..........####.### +#.#####.#......#.#......#.....##...##.#.#########.#......##..##..##.#..##.##..#....##...###...#.#... +#..#..##..###.#.#.#.#.....###.#.####.##.##....#.#..##....#.#..#.####..###.##...#######.#####.##.#.#. +..###.#........##.#...###..#.##..#.#....##.#......#..#.##..#.#..#.#..#.####.#####..###.##..#.##.#... +##.###....#..##...#..#.#......##..#...#..#.####..#.##...##.####.#...#..###...#.#.#....###.##..#.#... +..##.##.#.##..##.#..#.###...##..##..#....##..##...####.#..####.###...#.....#..#.##..##..###..#.#...# +#.#....#.....#...##.#...####..#..##..##.####..##..##...####...#....##.#.#######..##.#......######.#. +#.#...###.######.######..##..##....#.#......#......#.#.##.#.##.#.#.#...#...#....#.#.#.#..#.##..#...# +####.###.#.#.##..#.##.#...#.##...#.##.##...#.....#.#..#.####.##..######.#..#.#..##....#.#.#..#.#.#.# +..##......#.#...#.##.##..##..##..#..##..#########.#..###..###.##...#..##.#..#.#.#.######..#....#.#.. +..##.##.#...###.#...##..######.##.#..####..#..#.#.##.####.##.##.#...##....#...###.##.####..#....#.#. +####...###..#.#.##.#.#....###..##.#.#..########..#...#.#...#.##....##.##...#.....#.#.....#.....#.... +.#.###############....#.##..###..#.####.#.##.##..#..#.#...###...##..##.##.#.....##...###.###.....#.. +.###..#..##.##..####.#.###.##.##..#..##....#.#......#......##.#...#.#...#..##.#.#...#...#.##..#.##.. +###.#.#.########.#.#..####.#..##.#.##.##.###.##..######...#..##.##.#..#.#...#.##..#####.....#.#.#..# +.##.##..#.#...#####.#.#.###...##...####...#......#...#..####..#.##..........#..#.#..###....######.## +..#####...#.#.#.#..#.##..#...#.#..#.##...##..##.##.#.##.#..#.#...#.......##.#...###.....#...#.#.#.## +##.##.#..######.##...#.....#.###.#..##.#.#.#..####.#....##.#....####...##....#.#.##.#..###.##.##..## +.###.##.#..#.###.####..#.##..####.#.#.##..###.#######.###.###...####........##....###.#...#.#.####.# +........#..#.#..##..########..........#.##.#..##.#...#.....####....##..#..#.#####.###...#...#.##.### +.....#..##.####...##.#####..######.##.#.###.####.##.##.#..##.##.######.##......#..#.####..##....#.## +##...####....#.##.##.###....#.#...#.####..##.#.##.#.#...####.#.#.#.#...##.###...##...###...######.## +.#....#.#.####...#.##.....##...###.#.#.##...##.#####....#.######.#.#....##..##...##....##.#.##.#.#.# +.###..###.#.......#.#######..#.#.#.######....#.#####.#.....#.#########...#....##...##.####.#..#..... +##.#..##..##.....#..##...#..##.##.#..#.#####.##.##.#.##.##...##.######.####..#.##..#####.##...##..#. +#.###...##.#.#.#.##....#.#.##.##..#....#...#.#.........#..#..####..####.####..#.##.##.#....####..##. +.#..######..#####.####.##.#.....#.#.#####..##..###.#.#.#..#.#...#.#######..##....##.##...#######..#. +#...#....#.#.##..#####..#########..#.....#...##.#.#.###...#####..##...##...####.......#######.#..### +.#......#...##.###..#....#...#.#.....#.#...##.#.#..#..###.##.###.#.##..##...#.##......#.###..#.#..## +.#....####...###..#.....##..#...#.#.###.#.#.##...#.##.##.#.#.#..####..###.#.#.#.##.#.#...#..#...#### +......##.##.#...#####.##..#.###..#.#####..##.#..##.###......#...#...#..#......###.######...#.#.##..# +###..#...#.##..###.#....##...#..#####.#.#..#.###...#####.#....##..####.#.##...#.#...##..#.#.#.#..#.# +...##.#.##.##..#.#.#.###.#.#...#.....###.###.##...#.###.##...##..#..###.#..##.##..###.#....###..##.. +.##.#..###..###.##.##...#..#####...#.....#####.##..####...#.##.#.#..##.#.#.#....###.....#....##..... +######.#..#.#..#....#.###...####.####.#.........#..##.#..##..##.....#..#.##.##...#...#####.#.##..#.# +.##.###...####....#.####...#####..#..#...#..#.....###.#..#.###..#.###.#.......##.####..#.##.#...##.. +........#.#.##.#.....#####.###......##..#.##.#..#...####.#...#..###.#.#...##..#.#...#.####...#.#.### +.#..#.##..##...######.###.##.#.#...#.#.#.#.##..##..##.#.##..#....#.##...#.##.##...##....##.###.##.#. +##...#...#...###.#.#...#...#..###......##.#.#....##..##.#..##.#.######...#..##.#.##.#.#....#.##.##.. +...#..###.#....#...#.##..##.#.##.#..###.##..#.##..####.#########....#.....##.#.##.##..##.##.######.# +#.##.#..##.......###...#.###....###.#..####..##.#####.##.###....##....#.###...####..#.#.#.##.....### +.......#...#...##.#...##.#.#..#.##..##.#....###...##.#####...#.........#.......###.##.#.#.###....##. +###.#.##.##.....#.#..#.#..####.####..#..###..........####.#.##...#######.###..#####..#.....#..###..# +#...##.##..####.##.###.#.#######..###.#..#######..#.##.####...#..#.##.####..####.#.#.......####.#... +...#.##..#..#..##........#.#..#..#.#....#.###.#.###..#.......###..#.....#....#..##.#...#.###...##.#. +###.##..#.##.#.#####..#.##.####....#####..###.#.#..#...#...###.#.##..#.#.#.....#.####.#.#.#.#.#.#... +..##..##..#..##.##.#...#..#....####....#...#..####..#.....######.###.####.#....##....##.#.#.###....# +.#.#.#.##..####..#.....#.####.#....#.....#....#.##..#.#..#.#...#.#.#.#..#..#..##.#....####.......#.. +..##.##..###......#...#..##...#.###.####.#...#.####..#.#.#.....#.#...####...#.########.##.#.#.#..### +#....#.##.....##.###.##.###..#.####.....####.##...#..##.###...###..###.#....####.#..#..#..#.#..##.#. +.#.#.##....#.##......#.#..###.#....###....#......#.#.##.##.#########..##..#...#.####..#...####..#..# +.#.#.......##.#.##.#...#...#.##.#..#.#.#.##....#..###.###.##.#.#...##.#..#..##....#..###.#...#.#.##. +#.##.#....####...#..##..#.#.#.#.##.#...#####.#...#..#..#.####.####.#.#....#......##..##..###...#..## +..##.###..##.####..#..#..##...###.#.#.#######.####...####......##.##..#...#.##...##....#..#..#.....# +....#..#..#.#.####.#...##..#....####.#..####...#.#...###...#..#..##...#....##...#.....#.#..#.#.#...# +...#.#.#.##..##.###..#.######....####.###...##...###.#...##.####..#.#..#.#..#.##.....#.#.#..##...... +.#.##.##.....##.#..###.###.##....#...###.#......#...##.###.#.##.##...###...###...#.######..#......#. +###..#...#......#..##...#....##.#..###.##.####..##..##....####.#...#.#....##..#.#######..#.#.#####.. +##...#####..####..##....#.#.###.##.#..#.#..#.....###...###.#####.....#..##.#......#...#.###.##.##... +...#.#.#..#.###..#.#.#....##.#.#..####.##.#.####.#.#.#...#....##....#.##.####..###.#.#...##.#..#..## +#.#.#..#.##..##.##.#...##.#....#...###..##..#.#######.#.###..##......##.#..###.########.#.##..#.#.## +######.###....##..#..#...####....#.#.#..#...#..######.#.#.##..##....##....##.##.##...#..#.####.#.#.. +#####.###..#..###......##...##.####.#.#.#.###.......##..##.####..##.####.#..#..####..#.####.#####... +##.#.#.###..##.#.##.#.#.#.##.#...##........###.#.##..####....###.#.####.####.#.......##.##.##...##.. +#.#..###...#..##.....##.#..#.#..##..######.#####...###.#.......###...#..##..#..#..##.#.#....#..#..#. +#.#..####.###..#...#...#...#.###..#.#.#.#.#.#.#..#....#.##.##.##..###..####.#..##..##.###.###....##. +#..#.##.#####........#..#.##.#..##.#...#....#..#.##..###..##..##.##..#..##.#.#...#.#.##.#.##....#.#. +.......##..#.....#..#.#.....#.##...####.###..####..#.#.#.#..#.....#....##...#..#.##..###.#.#....#... +#...###########.##.....##...###.#.##.##..####.##...#.####.#####.#####.####...###.##...##..#.#.###..# +....#.#.###.####.###...#...#.#..###.#.#.##...#..#.#.#..#.####..#..###.######.#.####.###...###.#.##.# +.....#..#..########...#.#.#.#.#.#.#.#..###.##..####...##.#.#.#...##..#####.##.#...#.####.#######.##. +.......#...#.#..#..#...#..#..##.....#.##....##.##...##..##.##...##...#.#..#.##.#.###.#.####.#.#..##. +.####...#...#.#.#....##..........##.##.###.##.#.#..#.#.#......########.#...#.####.##.###..##...####. +#.#.#...##.###..##..#..#.....####.#.....##.##.#..#.#.###.#..#######...##..#.#..#.#..############.### +.##..####.#..#.....###..#..#.#.....#.#.#...##.##.#....#..#..###.#...#....#.#...####..#.....###.####. +..#...#.###.###....##.#..#.##..####.##.#.##.##.##...###.####..#.#.#.##.#.#.#..###..##.##.##.##.#..## +#...............##.....######.#.#####.##.#....#.#..#.##...#.##....#........##.##...#.##.##.#..#.##.# +#..##..#.#.#.##.#..#.#.##.##...#...#..#.#.##..#.#...###...##...###..#####.#.#..#..#.#..#.#.##...##.# +.#######.#.....##...#.#.####.######.#..#......#....##.#.#..#..###.#...###...#....#.#..#.##.#...#.#.. +#.###......##.#.##..#.###.###..####..##....#..###......##..##..#####.####....#...###.....###.#..#... +###...#....###.#..#.###.##...###.##.......##.##.#.#.#....####....###..##.###...#..##....#.#.##..##.. +.##.......##.######.#.#..#..##....#####.###.#.##.....####....#......####....#.##.#.##..#.##...##.#.# +.#.###...#.#.#.##.###..###...##..#.##.##..##..#.....###.#..#.##.##.####........##.#####.#.#....#...# +##...##..#.##.#######.###.#.##.#####....##.....##.#.....#.#.##.#....#.##.#....##.#..#.###..#..#.#... +.#..#.#.#.#...#.##...###.##.#.#...###.##...#.#..###....###.#.###...##..###..#..##.##....###...###.## diff --git a/18/input1 b/18/input1 new file mode 100644 index 0000000..6ddce72 --- /dev/null +++ b/18/input1 @@ -0,0 +1,6 @@ +.#.#.# +...##. +#....# +..#... +#.#..# +####.. diff --git a/18/part1.py b/18/part1.py new file mode 100644 index 0000000..e0ef880 --- /dev/null +++ b/18/part1.py @@ -0,0 +1,71 @@ +#!/usr/bin/python + +import re + +RE_INGRADIENT_INFO = re.compile(r'^(.*): capacity (-?\d+), durability (-?\d+), flavor (-?\d+), texture (-?\d+), calories (-?\d+)$') + + +def read_file(filename): + file = open(filename, 'r') + while True: + line = file.readline() + if not line: + break + yield line + + +def get_neighbors(lights, posX, posY, minX, minY, maxX, maxY): + lightsOn = 0 + lightsOff = 0 + + for x in range(posX-1, posX+2): + if x < minX or x > maxX: + continue + for y in range(posY-1, posY+2): + if y < minY or y > maxY: + continue + if x == posX and y == posY: + continue + if lights[x][y] == '#': + lightsOn += 1 + if lights[x][y] == '.': + lightsOff += 1 + return (lightsOn, lightsOff) + + +def do_one_step(lights, sizeX, sizeY): + result = [] + for x in range(sizeX): + line = "" + for y in range(sizeY): + light = lights[x][y] + (on, off) = get_neighbors(lights, x, y, 0, 0, sizeX-1, sizeY-1) +# print "[%d, %d]: on:%d off: %d" % (x, y, on, off) + if lights[x][y] == '#': + if on != 2 and on != 3: + light = '.' + if lights[x][y] == '.': + if on == 3: + light = '#' + line = line + light + result.append(line) + + return result + + +def main(): + lights = [] + for line in read_file('input'): + lights.append(line.strip()) + + for i in range(100): + print "iteration: %d" % i + lights = do_one_step(lights, 100, 100) + + on = 0 + for line in lights: + on += line.count('#') + print on + +if __name__ == "__main__": + main() diff --git a/18/part2.py b/18/part2.py new file mode 100644 index 0000000..cd0fbcd --- /dev/null +++ b/18/part2.py @@ -0,0 +1,78 @@ +#!/usr/bin/python + +import re + +RE_INGRADIENT_INFO = re.compile(r'^(.*): capacity (-?\d+), durability (-?\d+), flavor (-?\d+), texture (-?\d+), calories (-?\d+)$') + + +def read_file(filename): + file = open(filename, 'r') + while True: + line = file.readline() + if not line: + break + yield line + + +def get_neighbors(lights, posX, posY, minX, minY, maxX, maxY): + lightsOn = 0 + lightsOff = 0 + + for x in range(posX-1, posX+2): + if x < minX or x > maxX: + continue + for y in range(posY-1, posY+2): + if y < minY or y > maxY: + continue + if x == posX and y == posY: + continue + if lights[x][y] == '#': + lightsOn += 1 + if lights[x][y] == '.': + lightsOff += 1 + return (lightsOn, lightsOff) + + +def do_one_step(lights, sizeX, sizeY): + result = [] + for x in range(sizeX): + line = "" + for y in range(sizeY): + light = lights[x][y] + (on, off) = get_neighbors(lights, x, y, 0, 0, sizeX-1, sizeY-1) +# print "[%d, %d]: on:%d off: %d" % (x, y, on, off) + if lights[x][y] == '#': + if on != 2 and on != 3: + light = '.' + if lights[x][y] == '.': + if on == 3: + light = '#' + if x == 0 and (y == 0 or y == sizeY-1): + light = '#' + if x == sizeX-1 and (y == 0 or y == sizeY-1): + light = '#' + line = line + light + result.append(line) + + return result + + +def main(): + lights = [] + for line in read_file('input'): + lights.append(line.strip()) + + lights[0] = "#" + lights[0][1:-1] + "#" + lights[99] = "#" + lights[99][1:-1] + "#" + + for i in range(100): + print "iteration: %d" % i + lights = do_one_step(lights, 100, 100) + + on = 0 + for line in lights: + on += line.count('#') + print on + +if __name__ == "__main__": + main() diff --git a/19/input b/19/input new file mode 100644 index 0000000..8bb6fa8 --- /dev/null +++ b/19/input @@ -0,0 +1,45 @@ +Al => ThF +Al => ThRnFAr +B => BCa +B => TiB +B => TiRnFAr +Ca => CaCa +Ca => PB +Ca => PRnFAr +Ca => SiRnFYFAr +Ca => SiRnMgAr +Ca => SiTh +F => CaF +F => PMg +F => SiAl +H => CRnAlAr +H => CRnFYFYFAr +H => CRnFYMgAr +H => CRnMgYFAr +H => HCa +H => NRnFYFAr +H => NRnMgAr +H => NTh +H => OB +H => ORnFAr +Mg => BF +Mg => TiMg +N => CRnFAr +N => HSi +O => CRnFYFAr +O => CRnMgAr +O => HP +O => NRnFAr +O => OTi +P => CaP +P => PTi +P => SiRnFAr +Si => CaSi +Th => ThCa +Ti => BP +Ti => TiTi +e => HF +e => NAl +e => OMg + +ORnPBPMgArCaCaCaSiThCaCaSiThCaCaPBSiRnFArRnFArCaCaSiThCaCaSiThCaCaCaCaCaCaSiRnFYFArSiRnMgArCaSiRnPTiTiBFYPBFArSiRnCaSiRnTiRnFArSiAlArPTiBPTiRnCaSiAlArCaPTiTiBPMgYFArPTiRnFArSiRnCaCaFArRnCaFArCaSiRnSiRnMgArFYCaSiRnMgArCaCaSiThPRnFArPBCaSiRnMgArCaCaSiThCaSiRnTiMgArFArSiThSiThCaCaSiRnMgArCaCaSiRnFArTiBPTiRnCaSiAlArCaPTiRnFArPBPBCaCaSiThCaPBSiThPRnFArSiThCaSiThCaSiThCaPTiBSiRnFYFArCaCaPRnFArPBCaCaPBSiRnTiRnFArCaPRnFArSiRnCaCaCaSiThCaRnCaFArYCaSiRnFArBCaCaCaSiThFArPBFArCaSiRnFArRnCaCaCaFArSiRnFArTiRnPMgArF