Skip to content

Commit 877bd36

Browse files
authored
Merge pull request kodsnack#464 from estomagordo/master
The golfing project is initialized
2 parents 2c15c1d + f48ede2 commit 877bd36

File tree

11 files changed

+152
-2
lines changed

11 files changed

+152
-2
lines changed

estomagordo-python3/9a.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ def solve(s):
2424

2525
return count
2626

27-
with open('input.txt', 'r') as f:
27+
with open('input_9.txt', 'r') as f:
2828
s = f.read().strip()
2929
print(solve(s))

estomagordo-python3/9b.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ def solve(s):
1919

2020
return count
2121

22-
with open('input.txt', 'r') as f:
22+
with open('input_9.txt', 'r') as f:
2323
s = f.read().strip()
2424
print(solve(s))

estomagordo-python3/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Hiya!
2+
3+
To run a particular solution to a challenge, just run the files using Python 3 from out of the `advent_of_code_2017` directory.
4+
5+
For instance: `python estomagordo-python3\4a.py` should run the solution to the first challenge from December 4th, using Windows slash conventions. This assumes the base directory has got a file called `input_4.txt` in the `advent_of_code_2017` directory.
6+
7+
To run the golfed solution if present (see [golf/README.md](golf/README.md) for more information), just do `python estomagordo-python3\golf\4a.py` instead.
8+
9+
Run `golf.py` for some quick statistics for the golf project. Note: this will execute every golfed solution, along with its regular counterpart.

estomagordo-python3/golf.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
from os import fsencode, fsdecode, listdir, system, name
2+
from io import StringIO
3+
from contextlib import redirect_stdout
4+
5+
path = 'estomagordo-python3\\'
6+
golf = 'golf\\'
7+
readme = 'README.md'
8+
9+
rm = []
10+
with open(path + golf + readme, 'r') as f:
11+
for line in f.readlines():
12+
rm.append(line)
13+
14+
system('cls' if name == 'nt' else 'clear')
15+
print("Welcome to the golfing experience part of estomagordo's 2017 Advent of Code submissions!\nThe following golfing solutions have been found.")
16+
17+
results = []
18+
19+
for f in listdir(path + golf):
20+
out = StringIO()
21+
22+
filename = fsdecode(f)
23+
if not filename.endswith('.py'):
24+
continue
25+
26+
result = [filename]
27+
28+
with open(path + golf + filename, 'r') as golfed:
29+
contents = golfed.read()
30+
31+
with redirect_stdout(out):
32+
exec(contents)
33+
34+
result.append(len(contents))
35+
result.append(out.getvalue())
36+
37+
out = StringIO()
38+
39+
with open(path + filename, 'r') as regular:
40+
contents = regular.read()
41+
42+
with redirect_stdout(out):
43+
exec(contents)
44+
45+
result.append(len(contents))
46+
result.append(out.getvalue())
47+
48+
results.append(result)
49+
50+
for result in results:
51+
name, golflen, golfsult, regulen, regusult = result
52+
eqstr = 'EQUALS' if golfsult == regusult else 'DOES NOT EQUAL'
53+
percentage = round(100.0 * float(golflen) / float(regulen), 2)
54+
55+
output = '\n{}\nGolfed result {} the regular result. The solution was shortened from {} to {} chars, or {}% of the original.'.format(
56+
name, eqstr, str(regulen), str(golflen), percentage)
57+
58+
namenl = name + '\n'
59+
60+
exists = namenl in rm
61+
62+
if exists:
63+
i = rm.index(namenl) + 1
64+
prevresult = rm[i]
65+
topos = prevresult.find(' to ')
66+
charspos = prevresult.find(' chars,')
67+
68+
resultstr = prevresult[topos + 4: charspos]
69+
oldlen = int(resultstr)
70+
71+
if golflen < oldlen:
72+
rm[i] = output
73+
else:
74+
rm.append(output + '\n')
75+
76+
print(output)
77+
78+
if rm[-1][-1] == '\n':
79+
rm[-1] = rm[-1][:-1]
80+
81+
with open(path + golf + readme, 'w') as f:
82+
for line in rm:
83+
f.write(line)

estomagordo-python3/golf/1a.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
s=open('input_1.txt').read()[:-1]
2+
print(sum(int(s[x])for x in range(len(s))if s[x]==(s+s)[x+1]))

estomagordo-python3/golf/1b.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
s=open('input_1.txt').read()[:-1]
2+
l=len(s)
3+
print(sum(int(s[x])for x in range(l)if s[x]==(s+s)[x+l//2]))

estomagordo-python3/golf/4a.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print(sum(len(p)==len(set(p))for p in[l.split()for l in open('input_4.txt')]))

estomagordo-python3/golf/4b.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print(sum(len(p)==len(set(tuple(sorted(w))for w in p))for p in[l.split()for l in open('input_4.txt')]))

estomagordo-python3/golf/9a.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
s=open('input_9.txt').read()
2+
o=g=x=n=0
3+
for c in s:
4+
if g:
5+
if x:x=0
6+
else:
7+
if c=='!':x=1
8+
if c=='>':g=0
9+
else:
10+
if c=='{':o+=1
11+
if c=='}':n+=o;o-=1
12+
if c=='<':g=1
13+
print(n)

estomagordo-python3/golf/9b.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
s=open('input_9.txt').read()
2+
o=g=x=n=0
3+
for c in s:
4+
if g:
5+
if x:x=0
6+
else:
7+
if c=='!':x=1
8+
elif c=='>':g=0
9+
else:n += 1
10+
elif c=='<':g=1
11+
print(n)

0 commit comments

Comments
 (0)