solution day 10
This commit is contained in:
parent
bf91a3a5a5
commit
76e8997ecc
2 changed files with 192 additions and 0 deletions
|
|
@ -0,0 +1,52 @@
|
|||
def getSprite(currentX: int) -> str:
|
||||
if currentX == 0:
|
||||
middle = "##."
|
||||
elif currentX == -1:
|
||||
middle = "#.."
|
||||
elif currentX < -1:
|
||||
middle = "..."
|
||||
else:
|
||||
middle = "###"
|
||||
|
||||
return "." * (currentX - 1) + middle + "." * (40 - currentX - 2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
x = 1
|
||||
cycle = 1
|
||||
|
||||
savedCycleValues = {}
|
||||
|
||||
toSave = [20, 60, 100, 140, 180, 220]
|
||||
output = ""
|
||||
with open("input.txt", "r") as f:
|
||||
operations = [line.replace("\n", "") for line in f.readlines()]
|
||||
|
||||
for operation in operations:
|
||||
if operation == "noop":
|
||||
output += getSprite(x)[(cycle - 1) % 40]
|
||||
cycle += 1
|
||||
if cycle % 40 == 1 and cycle != 1:
|
||||
output += "\n"
|
||||
else:
|
||||
output += getSprite(x)[(cycle - 1) % 40]
|
||||
value = int(operation.split(" ")[1])
|
||||
cycle += 1
|
||||
if cycle in toSave:
|
||||
savedCycleValues[cycle] = x
|
||||
|
||||
if cycle % 40 == 1 and cycle != 1:
|
||||
output += "\n"
|
||||
|
||||
output += getSprite(x)[(cycle - 1) % 40]
|
||||
cycle += 1
|
||||
x += value
|
||||
if cycle in toSave:
|
||||
savedCycleValues[cycle] = x
|
||||
|
||||
if cycle % 40 == 1 and cycle != 1:
|
||||
output += "\n"
|
||||
|
||||
print(f"Solution 1: {sum([key * value for key, value in savedCycleValues.items()])}")
|
||||
print("Solution 2:")
|
||||
print(" " + " ".join(output)) # with join its readability is increased by a lot
|
||||
|
|
@ -0,0 +1,140 @@
|
|||
noop
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
addx 4
|
||||
addx 25
|
||||
addx -20
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx 3
|
||||
noop
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
addx -1
|
||||
addx 6
|
||||
addx 1
|
||||
noop
|
||||
addx 4
|
||||
noop
|
||||
addx -37
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
addx 32
|
||||
addx -25
|
||||
addx 2
|
||||
addx 3
|
||||
noop
|
||||
addx 2
|
||||
addx 3
|
||||
noop
|
||||
addx 2
|
||||
addx 2
|
||||
addx -24
|
||||
addx 25
|
||||
addx 5
|
||||
addx 2
|
||||
addx 8
|
||||
addx -23
|
||||
addx 18
|
||||
addx 5
|
||||
addx -39
|
||||
addx 11
|
||||
addx -9
|
||||
addx 6
|
||||
addx -2
|
||||
addx 5
|
||||
addx 4
|
||||
addx -4
|
||||
addx 3
|
||||
addx 5
|
||||
addx 2
|
||||
noop
|
||||
addx -1
|
||||
addx 6
|
||||
addx -21
|
||||
addx 22
|
||||
addx 3
|
||||
addx 1
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
addx -35
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 37
|
||||
addx -33
|
||||
noop
|
||||
addx 6
|
||||
addx 2
|
||||
addx -1
|
||||
addx 3
|
||||
addx 1
|
||||
addx 5
|
||||
addx 2
|
||||
addx -19
|
||||
addx 21
|
||||
addx 1
|
||||
addx 5
|
||||
addx -31
|
||||
addx 36
|
||||
noop
|
||||
addx 3
|
||||
addx -2
|
||||
addx -38
|
||||
noop
|
||||
noop
|
||||
addx 7
|
||||
addx 14
|
||||
addx -4
|
||||
addx -7
|
||||
addx 5
|
||||
addx 2
|
||||
addx 12
|
||||
addx -15
|
||||
addx 6
|
||||
addx 2
|
||||
addx 5
|
||||
addx -27
|
||||
addx 25
|
||||
addx 5
|
||||
noop
|
||||
addx 7
|
||||
addx -2
|
||||
addx 5
|
||||
addx -40
|
||||
noop
|
||||
addx 7
|
||||
noop
|
||||
addx -1
|
||||
addx 2
|
||||
addx 5
|
||||
addx -1
|
||||
addx 1
|
||||
addx 2
|
||||
addx 7
|
||||
noop
|
||||
addx -2
|
||||
noop
|
||||
addx 3
|
||||
addx 2
|
||||
addx 7
|
||||
noop
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
Loading…
Add table
Add a link
Reference in a new issue