DEV Community

DoctorLai
DoctorLai

Posted on

Avent of Code - Day 10 - Cathode-Ray Tube

Advent of Code occurs at Dec 01 to 25 where each day, you will need to solve a puzzle. It is Festival and the problem statement is mostly related to Christmas.

Day 10 - Cathode-Ray Tube

https://adventofcode.com/2022/day/10

Q1

import sys
from collections import defaultdict

file1 = open(sys.argv[1], "r")
score = 0

def f(c):
        return c in (20, 60, 100, 140, 180, 220)

data = defaultdict(int)

i = 0
while True:
        line = file1.readline()
        if not line:
                break
        i += 1
        a = line.strip().split()
        if a[0] != "noop":
                print(a)
                data[i + 1] += int(a[1])
                i += 1

x = 1
for j in range(i + 1):
        if f(j):
                score += j * x
                print(j, x)
        x += data[j]

print(score)
Enter fullscreen mode Exit fullscreen mode

Q2

import sys
from collections import defaultdict

file1 = open(sys.argv[1], "r")
score = 0

def f(c):
        return c in (20, 60, 100, 140, 180, 220)

row = list("........................................")

data = defaultdict(int)

i = 0
while True:
        line = file1.readline()
        if not line:
                break
        i += 1
        a = line.strip().split()
        if a[0] != "noop":
                data[i + 1] += int(a[1])
                i += 1

x = 1
cur = row[:]
print(i)
for j in range(1, i + 1):
        if j % 40 == 0:
                score += j * x
                print("".join(cur))
                cur = row[:]
        x += data[j]
        if abs((j % 40) - x) <= 1:
                cur[j%40] = '#'

print(score)
Enter fullscreen mode Exit fullscreen mode

5f359d5ca53d15dd4867f3acc10653e.jpg

cebb4ddb98e922d0085610f86c76f26.jpg

Getting interesting! Looking at Q1 - Huh? not much useful with only 2 instructions. Q2 - wow! Surprised to see how we make use of only 2 instructions to draw something meaningful using only ASCII characters.

I believe, similar questions have appeared in previous AOC (maybe 2020 or 2021).


Steem to the Moon!

Top comments (0)