<jupyter_start><jupyter_text>Day 2, https://adventofcode.com/2021/day/2<jupyter_code>date = 2
dev = 0 # extra prints
part = 2 # 1,2, or 3 for both
# 0 or 1:
samp = 0<jupyter_output><empty_output><jupyter_text>Read the input data<jupyter_code>#time0 = time.time()

if samp == 1:
    filename = "/sample.txt"
else:
    filename = "/input.txt"
try:
    with open(str(date) + filename,"r") as f:
        t = f.readlines()
except FileNotFoundError:
    with open("." + filename,"r") as f:
        t = f.readlines()

t = [(x.strip().replace('  ',' ')) for x in t]
#t = [int(x) for x in t]
## Run the program
if 0:
    if part == 1:
        print("Part 1: ", day(t))
    elif part == 2:
        print("Part 2: ", day2(t))
    elif part == 3:
        pass
        #run both
        #print("Part 1: ", day(t))
        #print("Part 2: ", day2(t))

#tdif = time.time() - time0
#print("Elapsed time: {:.4f} s".format(tdif))<jupyter_output><empty_output><jupyter_text>Part one<jupyter_code>class Submarine:
    def __init__(self, name, depth, position):
        self.name = name
        self.depth = depth
        self.position = position
        self.aim = 0
    def getDepth(self):
        return self.depth
    def getPosition(self):
        return self.position
    def up(self, steps):
        self.depth -= steps
        if self.depth < 0:
            self.depth = 0
    def down(self, steps):
        self.depth += steps
    def forward(self, steps):
        self.position += steps
    def back(self, steps):
        self.position -= steps
def day(te):
    part = 1
    sub = Submarine("one", 0, 0)
    for t in te:
        row = t.split()
        task = row[0]
        number = int(row[1])
        if task == "forward":
            sub.forward(number)
        elif task == "back":
            sub.back(number)
        elif task == "up":
            sub.up(number)
        elif task == "down":
            sub.down(number)
        else:
            print("ERROR: ", task, number)
        #print(row, "---", sub.getDepth(), sub.getPosition())
    print(sub.getDepth(), sub.getPosition())
    return sub.getDepth() * sub.getPosition()
day(t) # 1250395<jupyter_output>655 1909<jupyter_text>Part two<jupyter_code>class Submarine:
    def __init__(self, name):
        self.name = name
        self.depth = 0
        self.position = 0
        self.aim = 0

    def getDepth(self):
        return self.depth
    def getPosition(self):
        return self.position
    def getAim(self):
        return self.aim

    def up(self, steps):
        self.aim -= steps
        if self.aim < 0:
            self.aim = 0
    def down(self, steps):
        self.aim += steps
    def forward(self, steps):
        self.position += steps
        self.depth += steps * self.aim
    def back(self, steps):
        self.position -= steps
    
def day2(te):
    part = 2
    sub = Submarine("one")
    for t in te:
        row = t.split()
        task = row[0]
        number = int(row[1])
        if task == "forward":
            sub.forward(number)
        elif task == "back":
            sub.back(number)
        elif task == "up":
            sub.up(number)
        elif task == "down":
            sub.down(number)
        else:
            print("ERROR: ", task, number)
        if samp:
            print(row, "---", sub.getDepth(), sub.getPosition(), sub.getAim())
    print(sub.getDepth(), sub.getPosition())
    return sub.getDepth() * sub.getPosition()
day2(t) # 1451210346<jupyter_output>760194 1909
{'fraction_non_alphanumeric': 0.1449067431850789, 'fraction_numerical': 0.023242467718794835, 'mean_word_length': 2.033942558746736, 'pattern_counts': {'":': 8, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '489171', 'license': 'MIT', 'n_tokens_mistral': 1222, 'n_tokens_neox': 1159, 'n_words': 297, 'path': '2/notebook.ipynb', 'repo_name': 'aapalo/aoc2021', 'size': '3485'}