From 1226b1e448498126a6ca562827759a6659872cc8 Mon Sep 17 00:00:00 2001 From: Peter Hudec Date: Mon, 8 Aug 2016 07:34:33 +0200 Subject: [PATCH] added rest of the code --- 19/input1 | 5 +++++ 19/input2 | 7 +++++++ 19/part1.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 19/part2.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 19/readme | 5 +++++ README.md | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 6 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 19/input1 create mode 100644 19/input2 create mode 100644 19/part1.py create mode 100644 19/part2.py create mode 100644 19/readme diff --git a/19/input1 b/19/input1 new file mode 100644 index 0000000..1798c27 --- /dev/null +++ b/19/input1 @@ -0,0 +1,5 @@ +H => HO +H => OH +O => HH + +HOH diff --git a/19/input2 b/19/input2 new file mode 100644 index 0000000..70905c4 --- /dev/null +++ b/19/input2 @@ -0,0 +1,7 @@ +e => H +e => O +H => HO +H => OH +O => HH + +HOHOHO diff --git a/19/part1.py b/19/part1.py new file mode 100644 index 0000000..460ecb1 --- /dev/null +++ b/19/part1.py @@ -0,0 +1,53 @@ +#!/usr/bin/python + +import re + +RE_RULE = re.compile(r'^(.*) => (.*)$') + + +def read_file(filename): + file = open(filename, 'r') + while True: + line = file.readline() + if not line: + break + yield line.strip() + + +def apply_rule(formula, key, value): + keyLen = len(key) + formulaPos = 0 + +# print "formula: %s, key: %s, value: %s" % (formula, key, value) + while True: + pos = formula.find(key, formulaPos) +# print " - pos: %d" % (pos) + if pos < 0: + break + yield formula[0:pos] + value + formula[pos+keyLen:] + formulaPos = pos + keyLen + + +def main(): + formula = "" + rules = dict() + for line in read_file('input'): + # skip empty lines + if not line: + continue + match = re.match(RE_RULE, line) + if match: + if match.group(1) not in rules: + rules[match.group(1)] = [] + rules[match.group(1)].append(match.group(2)) + else: + formula = line + + for k1, v1 in rules.iteritems(): + for v2 in v1: + for r in apply_rule(formula, k1, v2): + print r + + +if __name__ == "__main__": + main() diff --git a/19/part2.py b/19/part2.py new file mode 100644 index 0000000..cab1621 --- /dev/null +++ b/19/part2.py @@ -0,0 +1,55 @@ +#!/usr/bin/python + +# solution borrowd from +# https://www.reddit.com/r/adventofcode/comments/3xflz8/day_19_solutions/?sort=new + +import re +from random import shuffle + +RE_RULE = re.compile(r'^(.*) => (.*)$') + + +def read_file(filename): + file = open(filename, 'r') + while True: + line = file.readline() + if not line: + break + yield line.strip() + +def find_formula(formula, rules): + steps = 0 + f = formula + while formula != 'e': + start = formula + for k,v in rules: + if v in formula: + steps += 1 + formula = formula.replace(v, k, 1) + + if formula == start: + shuffle(rules) + steps = 0 + formula = f + + return steps + +def main(): + medicine = "" + rules = [] + for line in read_file('input'): + # skip empty lines + if not line: + continue + match = re.match(RE_RULE, line) + if match: + rules.append((match.group(1), match.group(2))) + else: + medicine = line + + print rules + print medicine + print find_formula(medicine, rules) + +if __name__ == "__main__": + main() diff --git a/19/readme b/19/readme new file mode 100644 index 0000000..b9e82b8 --- /dev/null +++ b/19/readme @@ -0,0 +1,5 @@ +## part1 + + python part1.py | sort -u | wc -l + +## part2 diff --git a/README.md b/README.md index b64dd95..82c9a2d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,56 @@ -http://adventofcode.com/ +# Advent of Code +These are my solition for the problem descriptions on the http://adventofcode.com/ + +Of course, there could be better one + +- better code +- less memory space used +- faster + +## General notes + +In each directory teher are mostly 2 scripts `part1.py` and `part2.py`. Teh second one is the copy +of the first one with modifications to fit the chnaged problem description. + +### Input +There is in most cases file with name `input`. Yu can find tere the input data for the problem. +Any other file prefixed with `input` is my testing data. + +For some solutions, the input is hardcoded in variable `input` in the code. + +### Output +There are several output types + +- **integer** - paste this into the input box, it's a result +- **bunch of lines* - you need to do sort/uniq/tail/head on this output + +See README in each directory for more informations. + + +## Advent Days + +- **Day 1**: Not Quite Lisp +- **Day 2**: I Was Told There Would Be No Math +- **Day 3**: Perfectly Spherical Houses in a Vacuum +- **Day 4**: The Ideal Stocking Stuffer +- **Day 5**: Doesn't He Have Intern-Elves For This? +- **Day 6**: Probably a Fire Hazard +- **Day 7**: Some Assembly Required +- **Day 8**: Matchsticks +- **Day 9**: All in a Single Night +- **Day 10**: Elves Look, Elves Say +- **Day 11**: Corporate Policy +- **Day 12**: JSAbacusFramework.io +- **Day 13**: Knights of the Dinner Table +- **Day 14**: Reindeer Olympics +- **Day 15**: Science for Hungry People +- **Day 16**: Aunt Sue +- **Day 17**: No Such Thing as Too Much +- **Day 18**: Like a GIF For Your Yard +- **Day 19**: Medicine for Rudolph +- **Day 20**: Infinite Elves and Infinite Houses +- **Day 21**: RPG Simulator 20XX +- **Day 22**: Wizard Simulator 20XX +- **Day 23**: Opening the Turing Lock +- **Day 24**: