diff --git a/.gitignore b/.gitignore index 8eac1ae..9243e6f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +input input.txt # If you prefer the allow list template instead of the deny list, see community template: # https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore diff --git a/03/03.py b/03/03.py index d5dfd8a..a6fce6c 100644 --- a/03/03.py +++ b/03/03.py @@ -3,18 +3,20 @@ import re data = open("input.txt").read().strip() + def mul(a, b): - return a*b + return a * b -print(sum(eval(x) for x in re.findall("mul\(\d{1,3},\d{1,3}\)", data))) -q = "mul\(\d{1,3},\d{1,3}\)|do\(\)|don't\(\)" +print(sum(eval(x) for x in re.findall(r"mul\(\d{1,3},\d{1,3}\)", data))) + +q = r"mul\(\d{1,3},\d{1,3}\)|do\(\)|don't\(\)" f = True s = 0 for x in re.findall(q, data): - if x=='do()': + if x == "do()": f = True - elif x=='don\'t()': + elif x == "don't()": f = False elif f: s += eval(x) diff --git a/15/15.py b/15/15.py new file mode 100644 index 0000000..1388053 --- /dev/null +++ b/15/15.py @@ -0,0 +1,74 @@ +test_input = """ +######## +#..O.O.# +##@.O..# +#...O..# +#.#.O..# +#...O..# +#......# +######## + +<^^>>>vv>v<< +""".strip() + + +def parse(input): + m, c = input.split("\n\n") + c = "".join(c.split("\n")) + m = list(map(list, m.split("\n"))) + return m, c + + +d = {"v": (1, 0), "^": (-1, 0), "<": (0, -1), ">": (0, 1)} + + +def gps(c): + i, j = c + return i * 100 + j + + +def start(m): + for i, row in enumerate(m): + for j, ch in enumerate(row): + if ch == "@": + return i, j + raise ValueError("no start position") + + +def boxes(m): + for i, row in enumerate(m): + for j, ch in enumerate(row): + if ch == "O": + yield i, j + + +def field(m): + return "\n".join("".join(y for y in x) for x in m) + + +def part1(m, c): + ri, rj = start(m) + # print("1", ri, rj) + for ch in c: + di, dj = d[ch] + # print("2", di, dj) + move = True + ci = ri + di + cj = rj + dj + while move and m[ci][cj] != ".": + if m[ci][cj] == "#": + move = False + ci += di + cj += dj + # print("3", move, ci, cj) + if move: + m[ci][cj] = m[ri + di][rj + dj] + m[ri + di][rj + dj] = "@" + m[ri][rj] = "." + ri = ri + di + rj = rj + dj + # print(field(m)) + return sum(map(gps, boxes(m))) + + +print(part1(*parse(open("input").read())))