day03
This commit is contained in:
parent
5904ced66b
commit
dde2715d2e
40
day03.py
Normal file
40
day03.py
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
from aoc import AOCDay
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
|
class Day(AOCDay):
|
||||||
|
test_solutions_p1 = [198]
|
||||||
|
test_solutions_p2 = [230]
|
||||||
|
|
||||||
|
def part1(self) -> Any:
|
||||||
|
report = self.getInput()
|
||||||
|
gamma = epsilon = ""
|
||||||
|
for c in range(len(report[0])):
|
||||||
|
count = 0
|
||||||
|
for b in report:
|
||||||
|
count += b[c] == "1"
|
||||||
|
|
||||||
|
gamma += str(int(count >= len(report) // 2))
|
||||||
|
epsilon += str(int(count < len(report) // 2))
|
||||||
|
|
||||||
|
return int(gamma, 2) * int(epsilon, 2)
|
||||||
|
|
||||||
|
def part2(self) -> Any:
|
||||||
|
report_ox = [a for a in self.getInput()]
|
||||||
|
report_co2 = [a for a in self.getInput()]
|
||||||
|
for i in range(len(report_ox[0])):
|
||||||
|
count_ox = 0
|
||||||
|
count_co2 = 0
|
||||||
|
for num in report_ox:
|
||||||
|
count_ox += num[i] == "1"
|
||||||
|
|
||||||
|
for num in report_co2:
|
||||||
|
count_co2 += num[i] == "1"
|
||||||
|
|
||||||
|
if len(report_ox) > 1:
|
||||||
|
report_ox = [a for a in report_ox if a[i] == str(int(count_ox >= len(report_ox) / 2))]
|
||||||
|
|
||||||
|
if len(report_co2) > 1:
|
||||||
|
report_co2 = [a for a in report_co2 if a[i] == str(int(count_co2 < len(report_co2) / 2))]
|
||||||
|
|
||||||
|
return int(report_ox[0], 2) * int(report_co2[0], 2)
|
||||||
1000
inputs/input03
Normal file
1000
inputs/input03
Normal file
File diff suppressed because it is too large
Load Diff
12
inputs/test_input03_1_0
Normal file
12
inputs/test_input03_1_0
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
00100
|
||||||
|
11110
|
||||||
|
10110
|
||||||
|
10111
|
||||||
|
10101
|
||||||
|
01111
|
||||||
|
00111
|
||||||
|
11100
|
||||||
|
10000
|
||||||
|
11001
|
||||||
|
00010
|
||||||
|
01010
|
||||||
12
inputs/test_input03_2_0
Normal file
12
inputs/test_input03_2_0
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
00100
|
||||||
|
11110
|
||||||
|
10110
|
||||||
|
10111
|
||||||
|
10101
|
||||||
|
01111
|
||||||
|
00111
|
||||||
|
11100
|
||||||
|
10000
|
||||||
|
11001
|
||||||
|
00010
|
||||||
|
01010
|
||||||
Loading…
Reference in New Issue
Block a user