Skip to content

Commit ac5c056

Browse files
committed
2015/04: Faster md5sum using python
md5sum in the shell/awk is painfully slow. Python solution finishes in seconds. Version-agnostic code. Tested on Python 2.7.17 and Python 3.6.9. PUREBASH version will test all numbers from 1, and takes +7 hours.
1 parent f6f8739 commit ac5c056

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

2015/04.sh

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
#!/usr/bin/env bash
22
A=$(<"${1:-4.txt}")
3+
if [[ -n ${2:-$PUREBASH} ]]; then
4+
trap 'echo "Giving up at number $i after $SECONDS seconds"; exit 1' INT TERM
5+
echo "Part 2 could take 7-8 hours. Ctrl-C or 'kill $$' to exit"
6+
i=1
7+
#i=100000 # Saves over 10 minutes
8+
until echo -n '$A$i' | md5sum | grep -q ^00000; do ((++i)); done
9+
#until [[ $(echo -n '$A$i' | md5sum) == 00000* ]]; do ((++i)); done
10+
echo '4A: $i'
11+
#i=3900000 # Saves over 7 hours
12+
until echo -n '$A$i' | md5sum | grep -q ^000000; do ((++i)); done
13+
echo '4B: $i'
14+
else # awk has no builtin md5sum. Use whatever python is in $PATH
15+
python -c "import hashlib
316
i=1
4-
i=100000 # Saves over 10 minutes
5-
#until echo -n "$A$i" | md5sum | grep -q ^00000; do ((++i)); done
6-
until [[ $(echo -n "$A$i" | md5sum) == 00000* ]]; do ((++i)); done;
7-
#until [[ $(md5sum < <(echo -n "$A$i")) == 00000* ]]; do ((++i)); done;
8-
echo "4A: $i"
9-
i=3900000 # Saves over 7 hours
10-
until echo -n "$A$i" | md5sum | grep -q ^000000; do ((++i)); done
11-
#until [[ $(md5sum < <(echo -n "$A$i")) == 000000* ]]; do ((++i)); done;
12-
echo "4B: $i"
17+
for part,target in {'A': '00000', 'B': '000000'}.items():
18+
while not hashlib.md5(b'$A%d' % i).hexdigest().startswith(target):
19+
i+=1
20+
print('4%s(py): %d' % (part, i))"
21+
fi

2015/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ https://adventofcode.com/2015/
44

55
Input can be given on the command line.
66
Defaults to *number*.txt in the same folder (no leading 0).
7-
Setting env variable PUREBASH to any value (or giving a second argument) will use slow bash instead of using faster awk in days 6 and 25.
7+
Setting env variable PUREBASH to any value (or giving a second argument) will use slow bash instead of using faster awk in days 6 and 25 and python2/3 in day 4.
88

99
### 01.sh
1010
1. Convert brackets to ±1 and add them up.
@@ -19,7 +19,8 @@ Sort the dimensions by size. Add up the dimensions. Using a bash "bubble sort" i
1919

2020
### 04.sh
2121
Oneliners using md5sum. Calling md5sum in a subshell forever to run (~150 hashes/sec). Running it from 1 could take 7-8 hours.
22-
Speed up by setting the number to nearest 100000 below my target, skipping over 98% of the md5sum calculations.
22+
I'm breaking all my self-imposed rules here and using python. Python version finishes both parts in 2-3 seconds (py2 is about a second slower than py3).
23+
Optional speed up for bash by setting the number to nearest 100000 below my target, skipping over 98% of the md5sum calculations.
2324

2425
### 05.sh
2526
Oneliners using grep -E.

0 commit comments

Comments
 (0)