added rest of the code
This commit is contained in:
parent
a9ceb0715a
commit
1226b1e448
53
19/part1.py
Normal file
53
19/part1.py
Normal file
@ -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()
|
55
19/part2.py
Normal file
55
19/part2.py
Normal file
@ -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()
|
5
19/readme
Normal file
5
19/readme
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
## part1
|
||||||
|
|
||||||
|
python part1.py | sort -u | wc -l
|
||||||
|
|
||||||
|
## part2
|
56
README.md
56
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**:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user