Skip to content

Commit

Permalink
[Python/2019] Restructure solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
Defelo committed Oct 29, 2023
1 parent 40e7670 commit fb42493
Show file tree
Hide file tree
Showing 63 changed files with 1,390 additions and 1,736 deletions.
17 changes: 17 additions & 0 deletions Python/2019/01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from lib import *

input = read_input(2019, 1)

out = 0
for line in input.splitlines():
out += int(line) // 3 - 2
print(out)


out = 0
for line in input.splitlines():
fuel = int(line) // 3 - 2
while fuel > 0:
out += fuel
fuel = fuel // 3 - 2
print(out)
4 changes: 0 additions & 4 deletions Python/2019/01/puzzle1.py

This file was deleted.

7 changes: 0 additions & 7 deletions Python/2019/01/puzzle2.py

This file was deleted.

36 changes: 36 additions & 0 deletions Python/2019/02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from lib import *

input = read_input(2019, 2)

(*mem,) = map(int, input.strip().split(","))
i = 0
mem[1] = 12
mem[2] = 2
while i < len(mem):
if mem[i] == 99:
break
elif mem[i] == 1:
mem[mem[i + 3]] = mem[mem[i + 1]] + mem[mem[i + 2]]
elif mem[i] == 2:
mem[mem[i + 3]] = mem[mem[i + 1]] * mem[mem[i + 2]]
i += 4
print(mem[0])
(*mem,) = map(int, input.strip().split(","))


def simulate(a, b, mem):
i = 0
mem[1] = a
mem[2] = b
while i < len(mem):
if mem[i] == 99:
break
elif mem[i] == 1:
mem[mem[i + 3]] = mem[mem[i + 1]] + mem[mem[i + 2]]
elif mem[i] == 2:
mem[mem[i + 3]] = mem[mem[i + 1]] * mem[mem[i + 2]]
i += 4
return mem[0]


print(*[a * 100 + b for a in range(100) for b in range(100) if simulate(a, b, mem[:]) == 19690720])
13 changes: 0 additions & 13 deletions Python/2019/02/puzzle1.py

This file was deleted.

17 changes: 0 additions & 17 deletions Python/2019/02/puzzle2.py

This file was deleted.

40 changes: 40 additions & 0 deletions Python/2019/03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from lib import *

input = read_input(2019, 3)


def parse_wire(wire):
x = 0
y = 0
out = set()
for e in wire.split(","):
dx, dy = {"U": (0, -1), "D": (0, 1), "L": (-1, 0), "R": (1, 0)}[e[0]]
for _ in range(int(e[1:])):
x += dx
y += dy
out.add((x, y))
return out


wire1, wire2 = map(parse_wire, input.splitlines())
print(min(abs(x) + abs(y) for x, y in (wire1 & wire2)))


def parse_wire(wire):
x = 0
y = 0
out = {}
count = 0
for e in wire.split(","):
dx, dy = {"U": (0, -1), "D": (0, 1), "L": (-1, 0), "R": (1, 0)}[e[0]]
for _ in range(int(e[1:])):
x += dx
y += dy
count += 1
if (x, y) not in out:
out[(x, y)] = count
return out


wire1, wire2 = map(parse_wire, input.splitlines())
print(min(wire1[intersection] + wire2[intersection] for intersection in (wire1.keys() & wire2.keys())))
14 changes: 0 additions & 14 deletions Python/2019/03/puzzle1.py

This file was deleted.

17 changes: 0 additions & 17 deletions Python/2019/03/puzzle2.py

This file was deleted.

24 changes: 24 additions & 0 deletions Python/2019/04.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from lib import *

input = read_input(2019, 4)

a, b = map(int, input.split("-"))
count = 0
for i in range(a, b + 1):
string = str(i)
if list(string) != sorted(string):
continue
if any(x == y for x, y in zip(string[1:], string)):
count += 1
print(count)


a, b = map(int, input.split("-"))
count = 0
for i in range(a, b + 1):
string = str(i)
if list(string) != sorted(string):
continue
if any(string.count(c) == 2 for c in string):
count += 1
print(count)
9 changes: 0 additions & 9 deletions Python/2019/04/puzzle1.py

This file was deleted.

9 changes: 0 additions & 9 deletions Python/2019/04/puzzle2.py

This file was deleted.

83 changes: 83 additions & 0 deletions Python/2019/05.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from lib import *

input = read_input(2019, 5)

(*mem,) = map(int, input.split(","))
pc = 0
while pc < len(mem):
opcode = mem[pc] % 100
mode1 = mem[pc] // 100 % 10
mode2 = mem[pc] // 1000 % 10

if opcode == 1:
arg1 = mem[pc + 1] if mode1 else mem[mem[pc + 1]]
arg2 = mem[pc + 2] if mode2 else mem[mem[pc + 2]]
mem[mem[pc + 3]] = arg1 + arg2
pc += 4
elif opcode == 2:
arg1 = mem[pc + 1] if mode1 else mem[mem[pc + 1]]
arg2 = mem[pc + 2] if mode2 else mem[mem[pc + 2]]
mem[mem[pc + 3]] = arg1 * arg2
pc += 4
elif opcode == 3:
mem[mem[pc + 1]] = 1
pc += 2
elif opcode == 4:
arg1 = mem[pc + 1] if mode1 else mem[mem[pc + 1]]
if arg1:
print(arg1)
pc += 2
elif opcode == 99:
break


(*mem,) = map(int, input.split(","))
pc = 0
while pc < len(mem):
opcode = mem[pc] % 100
mode1 = mem[pc] // 100 % 10
mode2 = mem[pc] // 1000 % 10

if opcode == 1:
arg1 = mem[pc + 1] if mode1 else mem[mem[pc + 1]]
arg2 = mem[pc + 2] if mode2 else mem[mem[pc + 2]]
mem[mem[pc + 3]] = arg1 + arg2
pc += 4
elif opcode == 2:
arg1 = mem[pc + 1] if mode1 else mem[mem[pc + 1]]
arg2 = mem[pc + 2] if mode2 else mem[mem[pc + 2]]
mem[mem[pc + 3]] = arg1 * arg2
pc += 4
elif opcode == 3:
mem[mem[pc + 1]] = 5
pc += 2
elif opcode == 4:
arg1 = mem[pc + 1] if mode1 else mem[mem[pc + 1]]
print(arg1)
pc += 2
elif opcode == 5:
arg1 = mem[pc + 1] if mode1 else mem[mem[pc + 1]]
arg2 = mem[pc + 2] if mode2 else mem[mem[pc + 2]]
if arg1:
pc = arg2
else:
pc += 3
elif opcode == 6:
arg1 = mem[pc + 1] if mode1 else mem[mem[pc + 1]]
arg2 = mem[pc + 2] if mode2 else mem[mem[pc + 2]]
if not arg1:
pc = arg2
else:
pc += 3
elif opcode == 7:
arg1 = mem[pc + 1] if mode1 else mem[mem[pc + 1]]
arg2 = mem[pc + 2] if mode2 else mem[mem[pc + 2]]
mem[mem[pc + 3]] = int(arg1 < arg2)
pc += 4
elif opcode == 8:
arg1 = mem[pc + 1] if mode1 else mem[mem[pc + 1]]
arg2 = mem[pc + 2] if mode2 else mem[mem[pc + 2]]
mem[mem[pc + 3]] = int(arg1 == arg2)
pc += 4
elif opcode == 99:
break
26 changes: 0 additions & 26 deletions Python/2019/05/puzzle1.py

This file was deleted.

50 changes: 0 additions & 50 deletions Python/2019/05/puzzle2.py

This file was deleted.

Loading

0 comments on commit fb42493

Please sign in to comment.