Compare commits
2 Commits
6f71ed1717
...
ef8a247ad6
Author | SHA1 | Date | |
---|---|---|---|
ef8a247ad6 | |||
af4507f785 |
70
2019/d8.py
Normal file
70
2019/d8.py
Normal file
@ -0,0 +1,70 @@
|
||||
from lib import get_data
|
||||
|
||||
|
||||
def part_1(data):
|
||||
wide = 25
|
||||
tall = 6
|
||||
xs = [int(c) for c in data.strip()]
|
||||
layers = []
|
||||
i = 0
|
||||
while i < len(xs):
|
||||
if i % (wide * tall) == 0:
|
||||
layers.append([])
|
||||
layers[-1].append(xs[i])
|
||||
i += 1
|
||||
|
||||
min_zero_layer = None
|
||||
min_zeros = 10**9
|
||||
for layer in layers:
|
||||
zeros = layer.count(0)
|
||||
if zeros < min_zeros:
|
||||
min_zeros = zeros
|
||||
min_zero_layer = layer
|
||||
assert min_zero_layer is not None
|
||||
ones = min_zero_layer.count(1)
|
||||
twos = min_zero_layer.count(2)
|
||||
print(ones * twos)
|
||||
|
||||
|
||||
def part_2(data):
|
||||
wide = 25
|
||||
tall = 6
|
||||
xs = [int(c) for c in data.strip()]
|
||||
layers = []
|
||||
i = 0
|
||||
while i < len(xs):
|
||||
if i % (wide * tall) == 0:
|
||||
layers.append([])
|
||||
layers[-1].append(xs[i])
|
||||
i += 1
|
||||
result = []
|
||||
for i in range(wide * tall):
|
||||
current = 2
|
||||
for layer in layers:
|
||||
pixel = layer[i]
|
||||
if current == 2:
|
||||
current = pixel
|
||||
result.append(current)
|
||||
|
||||
for row in range(tall):
|
||||
row_str = ""
|
||||
for col in range(wide):
|
||||
pixel_idx = row * wide + col
|
||||
pixel = result[pixel_idx]
|
||||
if pixel == 0:
|
||||
row_str += "⬛"
|
||||
elif pixel == 1:
|
||||
row_str += "⬜"
|
||||
else:
|
||||
assert False
|
||||
print(row_str)
|
||||
|
||||
|
||||
def main():
|
||||
data = get_data(__file__)
|
||||
part_1(data)
|
||||
part_2(data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
@ -117,7 +117,8 @@ Solutions and utility script for Advent of Code challenges in Python.
|
||||
- Day 5: 34:24 (that wasn't hard at all)
|
||||
- Day 6: 23:17 (so slow, no leaderboard)
|
||||
- Day 7: 72:17 (I found that one challenging, 30:33 would have been required for leaderboard)
|
||||
- Day 8:
|
||||
- Day 8: 08:55 (54th)
|
||||
- Day 9:
|
||||
|
||||
## AoC 2022
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user