Skip to content

Commit

Permalink
Day 10 Part 1 Complete
Browse files Browse the repository at this point in the history
  • Loading branch information
SpawnTerror committed Dec 28, 2022
1 parent 0921a65 commit c2fd093
Show file tree
Hide file tree
Showing 17 changed files with 396 additions and 0 deletions.
140 changes: 140 additions & 0 deletions day10/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
addx 1
addx 4
noop
noop
noop
addx 5
addx 3
noop
addx 2
noop
noop
noop
noop
addx 3
addx 5
addx 2
addx 1
noop
addx 5
addx -1
addx 5
noop
addx 3
noop
addx -40
noop
addx 38
addx -31
addx 3
noop
addx 2
addx -7
addx 8
addx 2
addx 5
addx 2
addx 3
addx -2
noop
noop
noop
addx 5
addx 2
noop
addx 3
addx 2
noop
addx 3
addx -36
noop
noop
addx 5
noop
noop
addx 8
addx -5
addx 5
addx 2
addx -15
addx 16
addx 4
noop
addx 1
noop
noop
addx 4
addx 5
addx -30
addx 35
addx -1
addx 2
addx -36
addx 5
noop
noop
addx -2
addx 5
addx 2
addx 3
noop
addx 2
noop
noop
addx 5
noop
addx 14
addx -13
addx 5
addx -14
addx 18
addx 3
addx 2
addx -2
addx 5
addx -40
noop
addx 32
addx -25
addx 3
noop
addx 2
addx 3
addx -2
addx 2
addx 2
noop
addx 3
addx 5
addx 2
addx 9
addx -36
addx 30
addx 5
addx 2
addx -25
addx 26
addx -38
addx 10
addx -3
noop
noop
addx 22
addx -16
addx -1
addx 5
addx 3
noop
addx 2
addx -20
addx 21
addx 3
addx 2
addx -24
addx 28
noop
addx 5
addx 3
noop
addx -24
noop
30 changes: 30 additions & 0 deletions day10/part1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# SpawnTerror 2022
# Python 3.11.1
# AOC Day 10 Part 1

rewritedCode = [0,0]
cycleNumber = []
counter = 1

sumSignals = 0
lookUpLocations = [20,60,100,140,180,220]

with open('day10/input.txt', 'r') as f:
for line in f.read().split('\n'):

match line[0:4]:
case 'noop':
rewritedCode.append(0)
case 'addx':
rewritedCode.append(0)
rewritedCode.append(int(line[5:]))

for l in rewritedCode:
counter += l
cycleNumber.append(counter)

for location in lookUpLocations:
print(f'Cycle = ', location,'Register =', cycleNumber[location])
sumSignals += location * cycleNumber[location]
print(f'Total signal strengths = ', sumSignals)

69 changes: 69 additions & 0 deletions day10/part2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Code review

def check_signal_strength(cycle):
return (cycle == 20) or (cycle % 40 == 20 and 0 < cycle <= 220)


def process_sprite(cycle, row, x):
mid = cycle % 40
# Are we about to enter a new row?
if not mid:
row += 1
print()
if mid-1 <= x <= mid+1:
print("#", end='')
else:
print(".", end='')


def part_one():
with open('day10/input.txt', 'r') as f:
input = f.read().splitlines()

x = 1
cycle = 0
signal_strength = 0

for inst in input:
# For both noop and addx, we always need to increment at least one cycle.
cycle += 1
if check_signal_strength(cycle):
signal_strength += cycle * x
if inst.startswith("addx"):
# addx takes two cycles. We've already checked the cycle count after the first cycle, so add the second one here.
cycle += 1
if check_signal_strength(cycle):
signal_strength += cycle * x
x += int(inst.split()[1])
return signal_strength


def part_two():
with open('day10/input.txt', 'r') as f:
input = f.read().splitlines()

x = 1
cycle = 0
row = 0

for inst in input:
if inst == "noop":
# noop takes one cycle.
process_sprite(cycle, row, x)
cycle += 1
elif inst.startswith("addx"):
# addx takes two cycles.
for _ in range(2):
process_sprite(cycle, row, x)
cycle += 1

x += int(inst.split()[1])


def main():
print(part_one())
part_two()


if __name__ == '__main__':
main()
3 changes: 3 additions & 0 deletions day10/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
noop
addx 3
addx -5
146 changes: 146 additions & 0 deletions day10/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
addx 15
addx -11
addx 6
addx -3
addx 5
addx -1
addx -8
addx 13
addx 4
noop
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx -35
addx 1
addx 24
addx -19
addx 1
addx 16
addx -11
noop
noop
addx 21
addx -15
noop
noop
addx -3
addx 9
addx 1
addx -3
addx 8
addx 1
addx 5
noop
noop
noop
noop
noop
addx -36
noop
addx 1
addx 7
noop
noop
noop
addx 2
addx 6
noop
noop
noop
noop
noop
addx 1
noop
noop
addx 7
addx 1
noop
addx -13
addx 13
addx 7
noop
addx 1
addx -33
noop
noop
noop
addx 2
noop
noop
noop
addx 8
noop
addx -1
addx 2
addx 1
noop
addx 17
addx -9
addx 1
addx 1
addx -3
addx 11
noop
noop
addx 1
noop
addx 1
noop
noop
addx -13
addx -19
addx 1
addx 3
addx 26
addx -30
addx 12
addx -1
addx 3
addx 1
noop
noop
noop
addx -9
addx 18
addx 1
addx 2
noop
noop
addx 9
noop
noop
noop
addx -1
addx 2
addx -37
addx 1
addx 3
noop
addx 15
addx -21
addx 22
addx -6
addx 1
noop
addx 2
addx 1
noop
addx -10
noop
noop
addx 20
addx 1
addx 2
addx 2
addx -6
addx -11
noop
noop
noop
Empty file added day11/input.txt
Empty file.
Loading

0 comments on commit c2fd093

Please sign in to comment.