day4 part2, and some cleanup
This commit is contained in:
parent
2bcab64eff
commit
be3fdad22b
@ -3,12 +3,15 @@ import shared
|
|||||||
import itertools
|
import itertools
|
||||||
import functools
|
import functools
|
||||||
|
|
||||||
|
|
||||||
def is_increasing(sequence):
|
def is_increasing(sequence):
|
||||||
return all(sequence[i] < sequence[i + 1] for i in range(len(sequence) - 1))
|
return all(sequence[i] < sequence[i + 1] for i in range(len(sequence) - 1))
|
||||||
|
|
||||||
|
|
||||||
def is_decreasing(sequence):
|
def is_decreasing(sequence):
|
||||||
return all(sequence[i] > sequence[i + 1] for i in range(len(sequence) - 1))
|
return all(sequence[i] > sequence[i + 1] for i in range(len(sequence) - 1))
|
||||||
|
|
||||||
|
|
||||||
def parse_line(row):
|
def parse_line(row):
|
||||||
for previous, current in zip(row, row[1:]):
|
for previous, current in zip(row, row[1:]):
|
||||||
diff = abs(current - previous)
|
diff = abs(current - previous)
|
||||||
@ -41,7 +44,6 @@ def brute_force(row):
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# @shared.profile
|
# @shared.profile
|
||||||
def part2(rows):
|
def part2(rows):
|
||||||
rows = [list(map(int, row.split())) for row in rows]
|
rows = [list(map(int, row.split())) for row in rows]
|
||||||
|
@ -2,10 +2,11 @@ import shared
|
|||||||
import re
|
import re
|
||||||
from scanf import scanf
|
from scanf import scanf
|
||||||
|
|
||||||
|
|
||||||
# @shared.profile
|
# @shared.profile
|
||||||
def part1(rows):
|
def part1(rows):
|
||||||
total = 0
|
total = 0
|
||||||
r = re.compile(r'mul\(\d+,\d+\)')
|
r = re.compile(r"mul\(\d+,\d+\)")
|
||||||
for row in rows:
|
for row in rows:
|
||||||
muls = r.findall(row)
|
muls = r.findall(row)
|
||||||
for m in muls:
|
for m in muls:
|
||||||
@ -14,11 +15,10 @@ def part1(rows):
|
|||||||
print(total)
|
print(total)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# @shared.profile
|
# @shared.profile
|
||||||
def part2(rows):
|
def part2(rows):
|
||||||
total = 0
|
total = 0
|
||||||
r = re.compile(r'(mul\(\d+,\d+\)|do\(\)|don\'t\(\))')
|
r = re.compile(r"(mul\(\d+,\d+\)|do\(\)|don\'t\(\))")
|
||||||
enabled = True
|
enabled = True
|
||||||
for row in rows:
|
for row in rows:
|
||||||
muls = r.findall(row)
|
muls = r.findall(row)
|
||||||
|
@ -19,9 +19,34 @@ def part1(mx):
|
|||||||
|
|
||||||
|
|
||||||
# @shared.profile
|
# @shared.profile
|
||||||
def part2(rows):
|
def part2(mx):
|
||||||
pass
|
count = 0
|
||||||
|
for idr, row in enumerate(mx):
|
||||||
|
for idc, c in enumerate(row):
|
||||||
|
if c != "A":
|
||||||
|
continue
|
||||||
|
|
||||||
|
nb = matrix.get_neighbors_cardinal(mx, idc, idr, diagonals=True)
|
||||||
|
|
||||||
|
eastdown= []
|
||||||
|
for d in ("NW","SE"):
|
||||||
|
eastdown.append(nb.get(d, {}).get("value"))
|
||||||
|
if None in eastdown:
|
||||||
|
continue
|
||||||
|
eastdown = sorted(eastdown)
|
||||||
|
|
||||||
|
westdown = []
|
||||||
|
for d in ("NE", "SW"):
|
||||||
|
westdown.append(nb.get(d, {}).get("value"))
|
||||||
|
if None in westdown:
|
||||||
|
continue
|
||||||
|
westdown = sorted(westdown)
|
||||||
|
|
||||||
|
if eastdown == ['M','S'] and westdown == ['M', 'S']:
|
||||||
|
count += 1
|
||||||
|
|
||||||
|
|
||||||
|
print(count)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
mx = matrix.load_matrix_file(shared.get_fname(4), str)
|
mx = matrix.load_matrix_file(shared.get_fname(4), str)
|
||||||
@ -29,9 +54,9 @@ def main():
|
|||||||
part1(mx)
|
part1(mx)
|
||||||
print("🕒", elapsed())
|
print("🕒", elapsed())
|
||||||
|
|
||||||
rows = [row for row in shared.load_rows(4, True)]
|
mx = matrix.load_matrix_file(shared.get_fname(4), str)
|
||||||
with shared.elapsed_timer() as elapsed:
|
with shared.elapsed_timer() as elapsed:
|
||||||
part2(rows)
|
part2(mx)
|
||||||
print("🕒", elapsed())
|
print("🕒", elapsed())
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import functools
|
|||||||
import pstats
|
import pstats
|
||||||
from itertools import groupby
|
from itertools import groupby
|
||||||
|
|
||||||
|
|
||||||
def all_equal(iterable):
|
def all_equal(iterable):
|
||||||
g = groupby(iterable)
|
g = groupby(iterable)
|
||||||
return next(g, True) and not next(g, False)
|
return next(g, True) and not next(g, False)
|
||||||
@ -27,9 +28,11 @@ def profile(func):
|
|||||||
|
|
||||||
return inner
|
return inner
|
||||||
|
|
||||||
|
|
||||||
def spl(y):
|
def spl(y):
|
||||||
return [int(w) for w in y]
|
return [int(w) for w in y]
|
||||||
|
|
||||||
|
|
||||||
def minmax(l):
|
def minmax(l):
|
||||||
return min(l), max(l)
|
return min(l), max(l)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user