Day 03
This commit is contained in:
1000
day03/input.txt
Normal file
1000
day03/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
13
day03/part1.py
Normal file
13
day03/part1.py
Normal 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
20
day03/part2.py
Normal 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)
|
||||||
@@ -26,4 +26,7 @@ echo "$txt" > "$f/part2.py"
|
|||||||
curl --request GET -sL \
|
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/*"
|
||||||
Reference in New Issue
Block a user