|
1 | 1 | #Advent of code 2021
|
2 |
| -# 12/04/21 day 3b |
| 2 | +# 12/05/21 day 3b |
3 | 3 | # Joe McFarland
|
4 | 4 | # import sys
|
5 | 5 | # import re
|
|
14 | 14 | #print(a_list)
|
15 | 15 | maxcols = len(a_list[0])
|
16 | 16 |
|
17 |
| -ogen = None |
18 |
| -current_list = copy.deepcopy(a_list) |
19 |
| -for col in range(maxcols): |
20 |
| - zero_bits = one_bits = 0 |
21 |
| - for row in current_list: |
22 |
| - if row[col] == "0": |
23 |
| - zero_bits += 1 |
24 |
| - elif row[col] == "1": |
25 |
| - one_bits += 1 |
26 |
| - if one_bits >= zero_bits: |
27 |
| - common = 1 |
28 |
| - else: |
29 |
| - common = 0 |
30 |
| - new_list = [] |
31 |
| - for row in current_list: |
32 |
| - if row[col] == str(common): |
33 |
| - new_list.append(row) |
34 |
| - #print(f"new_list(col={col}):\n{new_list}") |
35 |
| - if len(new_list) == 1: |
36 |
| - print(f"found entry, {new_list}") |
37 |
| - #found_ogen = new_list[0] |
38 |
| - ogen = int(new_list[0],2) |
39 |
| - break |
40 |
| - current_list = copy.deepcopy(new_list) |
41 |
| -print(f"ogen = {ogen}") |
42 |
| - |
43 |
| -co2 = None |
44 |
| -current_list = copy.deepcopy(a_list) |
45 |
| -for col in range(maxcols): |
46 |
| - zero_bits = one_bits = 0 |
47 |
| - for row in current_list: |
48 |
| - if row[col] == "0": |
49 |
| - zero_bits += 1 |
50 |
| - elif row[col] == "1": |
51 |
| - one_bits += 1 |
52 |
| - if one_bits < zero_bits: #### TODO: only diff, refactor into one function |
53 |
| - common = 1 # least common.... |
54 |
| - else: |
55 |
| - common = 0 |
56 |
| - new_list = [] |
57 |
| - for row in current_list: |
58 |
| - if row[col] == str(common): |
59 |
| - new_list.append(row) |
60 |
| - #print(f"new_list(col={col}):\n{new_list}") |
61 |
| - if len(new_list) == 1: |
62 |
| - print(f"found entry, {new_list}") |
63 |
| - #found_co2 = new_list[0] |
64 |
| - co2 = int(new_list[0],2) |
65 |
| - break |
66 |
| - current_list = copy.deepcopy(new_list) |
67 |
| -print(f"co2 = {co2}") |
| 17 | +def findDiag( isOxygen ): |
| 18 | + diag = None |
| 19 | + current_list = copy.deepcopy(a_list) |
| 20 | + for col in range(maxcols): |
| 21 | + zero_bits = one_bits = 0 |
| 22 | + for row in current_list: |
| 23 | + if row[col] == "0": |
| 24 | + zero_bits += 1 |
| 25 | + elif row[col] == "1": |
| 26 | + one_bits += 1 |
| 27 | + if ( isOxygen ): |
| 28 | + if one_bits >= zero_bits: |
| 29 | + common = 1 # most common |
| 30 | + else: |
| 31 | + common = 0 |
| 32 | + else: |
| 33 | + if one_bits < zero_bits: |
| 34 | + common = 1 # least common |
| 35 | + else: |
| 36 | + common = 0 |
| 37 | + new_list = [] |
| 38 | + for row in current_list: |
| 39 | + if row[col] == str(common): |
| 40 | + new_list.append(row) |
| 41 | + #print(f"new_list(col={col}):\n{new_list}") |
| 42 | + if len(new_list) == 1: |
| 43 | + print(f"found entry, {new_list}") |
| 44 | + #found_val = new_list[0] |
| 45 | + diag = int(new_list[0],2) |
| 46 | + break |
| 47 | + current_list = copy.deepcopy(new_list) |
| 48 | + print(f"diag = {diag}") |
| 49 | + return diag |
68 | 50 |
|
| 51 | +ogen = findDiag( True ) |
| 52 | +co2 = findDiag( False ) |
69 | 53 | print(f"mul = {ogen*co2}")
|
0 commit comments