Compare commits

...

4 Commits

Author SHA1 Message Date
Thom Dickson f3fc4fbf6b
Solve day03 2021-12-04 16:31:54 -05:00
Thom Dickson 1e0e651fea
Solve day02 part 2 2021-12-04 16:31:53 -05:00
Thom Dickson 9100b0f5d9
Finish day02 part 1 2021-12-04 16:31:52 -05:00
Thom Dickson 6ae040193e
Solve day one 2021-12-04 16:31:51 -05:00
8 changed files with 6109 additions and 0 deletions

2000
day01.in Normal file

File diff suppressed because it is too large Load Diff

22
day01.py Normal file
View File

@ -0,0 +1,22 @@
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")

1000
day02.in Normal file

File diff suppressed because it is too large Load Diff

1000
day02.in2 Normal file

File diff suppressed because it is too large Load Diff

29
day02.py Normal file
View File

@ -0,0 +1,29 @@
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])

1000
day03.in Normal file

File diff suppressed because it is too large Load Diff

1000
day03.in2 Normal file

File diff suppressed because it is too large Load Diff

58
day03.py Normal file
View File

@ -0,0 +1,58 @@
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()