From c63736752ba504b097cd3549f008a827e55edd8d Mon Sep 17 00:00:00 2001 From: shockrah Date: Mon, 9 Dec 2024 11:27:22 -0800 Subject: [PATCH] Bringing some days up to speed after a short break --- 1/2.py | 2 +- 6/1.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 7/1.py | 48 +++++++++++++++++++++++++++++++++++++++++++ 7/sample.data | 9 ++++++++ 4 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 6/1.py create mode 100644 7/1.py create mode 100644 7/sample.data diff --git a/1/2.py b/1/2.py index 9ff4ae9..0d41064 100644 --- a/1/2.py +++ b/1/2.py @@ -18,7 +18,7 @@ if __name__ == '__main__': right.append(r) # how many times does each number apear in each list right = counts(right) - score = 0 + score = sum([0 if n in right else n * right[n] for n in left]) for n in left: if n not in right: continue score += n * right[n] diff --git a/6/1.py b/6/1.py new file mode 100644 index 0000000..b3fb61f --- /dev/null +++ b/6/1.py @@ -0,0 +1,57 @@ +def get_start(grid: list[str]) -> [int, int]: + x, y = 0, 0 + for row, line in enumerate(grid): + if '^' in line: + y = row + break + x = grid[y].find('^') + return [x, y] + +def peek(grid: list[str], x: int, y: int) -> str: + try: + if y < 0 or x < 0: + return '' + return grid[y][x] + except IndexError: + return '' + +def rotate(vel: [int, int]) -> [int, int]: + # going up? -> go right + if vel == [0,-1]: + return [1,0] + # going right? -> go down + elif vel == [1,0]: + return [0,1] + # going down? -> go left + elif vel == [0,1]: + return [-1,0] + # goign left? -> go up + elif vel == [-1,0]: + return [0,-1] + else: + raise ValueError('brother wtf') + +def crawl(grid: list[str], x: int, y: int) -> set[(int, int)]: + # non -unique set that we'll have to clean up + visited = set() + visited.add((x,y)) + vel = [0,-1] + while val := peek(grid, x+vel[0], y+vel[1]): + # Record where we go + print((x,y), 'vel ->', vel) + if val == '.' or val == '^': + x += vel[0] + y += vel[1] + visited.add((x,y)) + # Don't record the walls + if val == '#': + vel = rotate(vel) + return visited + +if __name__ == '__main__': + with open('input.data') as raw: + grid = [ line.strip() for line in raw ] + start_x, start_y = get_start(grid) + print(len(grid), len(grid[0])) + places = crawl(grid, start_x, start_y) + print(len(places)) diff --git a/7/1.py b/7/1.py new file mode 100644 index 0000000..dc4b5a4 --- /dev/null +++ b/7/1.py @@ -0,0 +1,48 @@ +import random + +def test(expression: list[str|int]) -> int: + while len(expression) >= 3: + left, op, right = expression[0], expression[1], expression[2] + if len(expression) > 3: + expression = expression[3:] + else: + expression = [] + if op == '*': + expression.insert(0, left * right) + else: + expression.insert(0, left + right) + return expression[0] + +def genrate_expression(vals: list[int]) -> list[str|int]: + ops = [random.choice('*+') for _ in range(len(vals) - 1)] + expr = [] + for item in zip(vals, ops): + expr.extend(item) + expr.append(vals[-1]) + return expr + + +with open('input.data') as file: + inputs = [] + for line in file: + vals = [int(val) for val in line.replace(':', '').split()] + inputs.append([vals[0], vals[1:]]) + s = 0 + for case in inputs: + answer, vals = case[0], case[1] + expr = genrate_expression(vals) + for i in range(20000): + expr = genrate_expression(vals) + result = test(expr) + if result == answer: + #print(expr) + s += answer + break + print(s) + + + + + + + \ No newline at end of file diff --git a/7/sample.data b/7/sample.data new file mode 100644 index 0000000..87b8b25 --- /dev/null +++ b/7/sample.data @@ -0,0 +1,9 @@ +190: 10 19 +3267: 81 40 27 +83: 17 5 +156: 15 6 +7290: 6 8 6 15 +161011: 16 10 13 +192: 17 8 14 +21037: 9 7 18 13 +292: 11 6 16 20 \ No newline at end of file