mirror of
https://github.com/Crocmagnon/advent-of-code.git
synced 2024-11-21 14:08:11 +01:00
Solve day3 part 1
This commit is contained in:
parent
0f1e1f7c3f
commit
3bf0fcec57
5 changed files with 1106 additions and 0 deletions
52
2021/day03_gamma.py
Normal file
52
2021/day03_gamma.py
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
|
||||||
|
def main(filename: str, expected_part_1: int = None, expected_part_2: int = None):
|
||||||
|
print(f"\n+ Running on {filename}")
|
||||||
|
with open(filename) as f:
|
||||||
|
data = f.read().strip().split("\n")
|
||||||
|
|
||||||
|
counter_part_1 = solve_part_1(data)
|
||||||
|
|
||||||
|
print(f"1. Found {counter_part_1}")
|
||||||
|
if expected_part_1:
|
||||||
|
assert expected_part_1 == counter_part_1
|
||||||
|
|
||||||
|
counter_part_2 = solve_part_2(data)
|
||||||
|
print(f"2. Found {counter_part_2}")
|
||||||
|
if expected_part_2:
|
||||||
|
assert expected_part_2 == counter_part_2
|
||||||
|
|
||||||
|
|
||||||
|
def solve_part_1(data):
|
||||||
|
count_ones = defaultdict(int)
|
||||||
|
total = len(data)
|
||||||
|
for binary in data:
|
||||||
|
for index, digit in enumerate(binary):
|
||||||
|
if digit == "1":
|
||||||
|
count_ones[index] += 1
|
||||||
|
gamma = ""
|
||||||
|
epsilon = ""
|
||||||
|
for index in range(len(data[0])):
|
||||||
|
most_common = _most_common(count_ones, total, index)
|
||||||
|
gamma += most_common
|
||||||
|
epsilon += "0" if most_common == "1" else "1"
|
||||||
|
return int(gamma, 2) * int(epsilon, 2)
|
||||||
|
|
||||||
|
|
||||||
|
def _most_common(count_ones, total, index):
|
||||||
|
"""
|
||||||
|
Return 1 if the most common value is 1, otherwise 0.
|
||||||
|
"""
|
||||||
|
if count_ones[index] >= total / 2:
|
||||||
|
return "1"
|
||||||
|
return "0"
|
||||||
|
|
||||||
|
|
||||||
|
def solve_part_2(data):
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main("inputs/day03-test1", expected_part_1=198)
|
||||||
|
main("inputs/day03")
|
1000
2021/inputs/day03
Normal file
1000
2021/inputs/day03
Normal file
File diff suppressed because it is too large
Load diff
12
2021/inputs/day03-test1
Normal file
12
2021/inputs/day03-test1
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
00100
|
||||||
|
11110
|
||||||
|
10110
|
||||||
|
10111
|
||||||
|
10101
|
||||||
|
01111
|
||||||
|
00111
|
||||||
|
11100
|
||||||
|
10000
|
||||||
|
11001
|
||||||
|
00010
|
||||||
|
01010
|
14
2021/new_day
Executable file
14
2021/new_day
Executable file
|
@ -0,0 +1,14 @@
|
||||||
|
#!/bin/bash
|
||||||
|
set -euxo pipefail
|
||||||
|
|
||||||
|
if [[ $# -ne 2 ]]; then
|
||||||
|
>&2 echo "Usage: $0 <number> <python_module_name>"
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
filename=day$1_$2.py
|
||||||
|
cp template.py $filename
|
||||||
|
sed -e "s/dayXX/day$1/g" -i "" ./$filename
|
||||||
|
mkdir -p inputs
|
||||||
|
touch inputs/day$1
|
||||||
|
touch inputs/day$1-test1
|
28
2021/template.py
Normal file
28
2021/template.py
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
def main(filename: str, expected_part_1: int = None, expected_part_2: int = None):
|
||||||
|
print(f"\n+ Running on {filename}")
|
||||||
|
with open(filename) as f:
|
||||||
|
data = f.read().strip().split("\n")
|
||||||
|
|
||||||
|
counter_part_1 = solve_part_1(data)
|
||||||
|
|
||||||
|
print(f"1. Found {counter_part_1}")
|
||||||
|
if expected_part_1:
|
||||||
|
assert expected_part_1 == counter_part_1
|
||||||
|
|
||||||
|
counter_part_2 = solve_part_2(data)
|
||||||
|
print(f"2. Found {counter_part_2}")
|
||||||
|
if expected_part_2:
|
||||||
|
assert expected_part_2 == counter_part_2
|
||||||
|
|
||||||
|
|
||||||
|
def solve_part_1(data):
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def solve_part_2(data):
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main("inputs/dayXX-test1")
|
||||||
|
main("inputs/dayXX")
|
Loading…
Reference in a new issue