15.1
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
input
|
||||||
input.txt
|
input.txt
|
||||||
# If you prefer the allow list template instead of the deny list, see community template:
|
# 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
|
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
||||||
|
|||||||
@@ -3,18 +3,20 @@ import re
|
|||||||
|
|
||||||
data = open("input.txt").read().strip()
|
data = open("input.txt").read().strip()
|
||||||
|
|
||||||
|
|
||||||
def mul(a, b):
|
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
|
f = True
|
||||||
s = 0
|
s = 0
|
||||||
for x in re.findall(q, data):
|
for x in re.findall(q, data):
|
||||||
if x=='do()':
|
if x == "do()":
|
||||||
f = True
|
f = True
|
||||||
elif x=='don\'t()':
|
elif x == "don't()":
|
||||||
f = False
|
f = False
|
||||||
elif f:
|
elif f:
|
||||||
s += eval(x)
|
s += eval(x)
|
||||||
|
|||||||
@@ -0,0 +1,74 @@
|
|||||||
|
test_input = """
|
||||||
|
########
|
||||||
|
#..O.O.#
|
||||||
|
##@.O..#
|
||||||
|
#...O..#
|
||||||
|
#.#.O..#
|
||||||
|
#...O..#
|
||||||
|
#......#
|
||||||
|
########
|
||||||
|
|
||||||
|
<^^>>>vv<v>>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())))
|
||||||
Reference in New Issue
Block a user