Compare commits
No commits in common. "f3fc4fbf6b1ddfdf892afa6fb6de90963825271f" and "9e5c0a48de9e3a79191e9987b915b039ae8931f0" have entirely different histories.
f3fc4fbf6b
...
9e5c0a48de
22
day01.py
22
day01.py
|
@ -1,22 +0,0 @@
|
||||||
def calc_inc(x):
|
|
||||||
prev = 0
|
|
||||||
count = 0
|
|
||||||
for i in x:
|
|
||||||
if i > prev and prev != 0:
|
|
||||||
count += 1
|
|
||||||
prev = i
|
|
||||||
return count
|
|
||||||
|
|
||||||
|
|
||||||
with open("day01.in", "r") as f:
|
|
||||||
fa = [int(i.rstrip()) for i in f]
|
|
||||||
count_a = calc_inc(fa)
|
|
||||||
|
|
||||||
win = []
|
|
||||||
for i in range(len(fa)):
|
|
||||||
if i + 2 < len(fa):
|
|
||||||
win.append(sum(fa[i:i+3]))
|
|
||||||
count_b = calc_inc(win)
|
|
||||||
|
|
||||||
print("Increased", count_a, "times")
|
|
||||||
print("Increased", count_b, "times")
|
|
29
day02.py
29
day02.py
|
@ -1,29 +0,0 @@
|
||||||
pos = [0, 0]
|
|
||||||
direction_key = {"forward": (1, 0), "down": (0, 1), "up": (0, -1)}
|
|
||||||
with open("day02.in") as f:
|
|
||||||
# fa = [i for i in f]
|
|
||||||
for x in f:
|
|
||||||
cmd = x.split()[0]
|
|
||||||
val = int(x.split()[1])
|
|
||||||
pos[0] += direction_key[cmd][0] * val
|
|
||||||
pos[1] += direction_key[cmd][1] * val
|
|
||||||
|
|
||||||
print("Current position:", pos)
|
|
||||||
print("Product:", pos[0] * pos[1])
|
|
||||||
|
|
||||||
# Part 2
|
|
||||||
pos = [0, 0, 0]
|
|
||||||
with open("day02.in2") as f:
|
|
||||||
for x in f:
|
|
||||||
cmd = x.split()[0]
|
|
||||||
val = int(x.split()[1])
|
|
||||||
if cmd == "down":
|
|
||||||
pos[2] += val
|
|
||||||
elif cmd == "up":
|
|
||||||
pos[2] -= val
|
|
||||||
elif cmd == "forward":
|
|
||||||
pos[0] += val
|
|
||||||
pos[1] += pos[2] * val
|
|
||||||
|
|
||||||
print("Current position:", pos)
|
|
||||||
print("Product:", pos[0] * pos[1])
|
|
58
day03.py
58
day03.py
|
@ -1,58 +0,0 @@
|
||||||
def common_bit(list, bit, invert=False):
|
|
||||||
bit_count = [0, 0] # count of 0s and 1s respectively
|
|
||||||
for x in list:
|
|
||||||
if x[bit] == "0":
|
|
||||||
bit_count[0] += 1
|
|
||||||
elif x[bit] == "1":
|
|
||||||
bit_count[1] += 1
|
|
||||||
if bit_count[0] <= bit_count[1]:
|
|
||||||
return 0 if invert else 1
|
|
||||||
else:
|
|
||||||
return 1 if invert else 0
|
|
||||||
|
|
||||||
|
|
||||||
def part1():
|
|
||||||
with open("day03.in", "r") as f:
|
|
||||||
fa = [line.rstrip() for line in f]
|
|
||||||
gamma = ""
|
|
||||||
epsilon = ""
|
|
||||||
for bit in range(len(fa[0])):
|
|
||||||
gamma += str(common_bit(fa, bit))
|
|
||||||
epsilon += str(common_bit(fa, bit, invert=True))
|
|
||||||
gamma_i = int(gamma, 2)
|
|
||||||
epsilon_i = int(epsilon, 2)
|
|
||||||
print("Gamma:", gamma_i)
|
|
||||||
print("Epsilon:", epsilon_i)
|
|
||||||
print("Power consumption:", gamma_i * epsilon_i)
|
|
||||||
|
|
||||||
|
|
||||||
def part2():
|
|
||||||
with open("day03.in2", "r") as f:
|
|
||||||
fa = [line.rstrip() for line in f]
|
|
||||||
fb = fa.copy()
|
|
||||||
for bit in range(len(fa[0])):
|
|
||||||
fa = [x for x in fa if x[bit] == str(common_bit(fa, bit))]
|
|
||||||
# Break to stop if we're already down to one result
|
|
||||||
if len(fa) == 1:
|
|
||||||
break
|
|
||||||
for bit in range(len(fb[0])):
|
|
||||||
fb = [x for x in fb if x[bit] == str(common_bit(fb, bit,
|
|
||||||
invert=True))]
|
|
||||||
if len(fb) == 1:
|
|
||||||
break
|
|
||||||
oxygen = int(fa[0], 2)
|
|
||||||
co2 = int(fb[0], 2)
|
|
||||||
print("Oxygen:", oxygen)
|
|
||||||
print("CO2:", co2)
|
|
||||||
print("Life support rating:", oxygen * co2)
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
print("=== PART 1 ===")
|
|
||||||
part1()
|
|
||||||
print("=== PART 2 ===")
|
|
||||||
part2()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
Loading…
Reference in New Issue