This commit is contained in:
Sebastian Seedorf
2021-12-03 09:48:57 +01:00
parent 646e139a19
commit 36c63a510c
4 changed files with 1037 additions and 1 deletions

1000
day03/input.txt Normal file

File diff suppressed because it is too large Load Diff

13
day03/part1.py Normal file
View File

@@ -0,0 +1,13 @@
#!/usr/bin/env python3
lines = (x.strip() for x in open("input.txt"))
cnt = [0] * 12
for line in lines:
cnt = [x+(int(char)*2-1) for x, char in zip(cnt, line)]
gamma = int(''.join(['1' if x > 0 else '0' for x in cnt]), 2)
epsilon = int(''.join(['1' if x < 0 else '0' for x in cnt]), 2)
print(gamma * epsilon)

20
day03/part2.py Normal file
View File

@@ -0,0 +1,20 @@
#!/usr/bin/env python3
def find(is_lowest):
lines = (x.strip() for x in open("input.txt"))
for bit in range(12):
zeros = []
ones = []
for line in lines:
zeros.append(line) if line[bit] == '0' else ones.append(line)
lines = zeros if (len(zeros) > len(ones)) != is_lowest else ones
if len(lines) <= 1:
break
return lines[0]
gamma = int(''.join(find(True)), 2)
epsilon = int(''.join(find(False)), 2)
print(gamma * epsilon)

View File

@@ -27,3 +27,6 @@ curl --request GET -sL \
--cookie "session=$AOC_SESSION" \ --cookie "session=$AOC_SESSION" \
--url "https://adventofcode.com/2021/day/$(echo $1 | sed 's/^0*//')/input"\ --url "https://adventofcode.com/2021/day/$(echo $1 | sed 's/^0*//')/input"\
--output "$f/input.txt" --output "$f/input.txt"
# git
git add "$f/*"