「🎉」 init(2025): day 1
This commit is contained in:
39
2024/day09/part1.py
Normal file
39
2024/day09/part1.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import os
|
||||
import os.path
|
||||
|
||||
def checksum(hdd):
|
||||
res = 0
|
||||
for i in range(len(hdd)):
|
||||
if hdd[i] != '.':
|
||||
res += i * int(hdd[i])
|
||||
return (res)
|
||||
|
||||
def main():
|
||||
file_path = "./input.txt"
|
||||
if (os.path.isfile(file_path)):
|
||||
content = open(file_path, "r").read()
|
||||
else:
|
||||
content = "2333133121414131402"
|
||||
|
||||
hdd = []
|
||||
|
||||
for i in range(len(content)):
|
||||
if i % 2 == 0:
|
||||
hdd.extend([i // 2 for x in range(int(content[i]))])
|
||||
else:
|
||||
hdd.extend(['.' for x in range(int(content[i]))])
|
||||
|
||||
print(hdd)
|
||||
|
||||
for i in range(len(hdd) - 1, -1, -1):
|
||||
for j in range(len(hdd[:i])):
|
||||
if (hdd[j] == '.'):
|
||||
hdd[i], hdd[j] = hdd[j], hdd[i]
|
||||
break
|
||||
|
||||
print(checksum(hdd))
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user