Compare commits

...

3 Commits

@ -5,4 +5,6 @@ ToDo
- Day 5 Part 2
- Day 7 Part 2
- Day 7 Part 2
- Day 10 both parts

@ -1,6 +1,5 @@
lines = open('inputs/day1.txt').readlines()
digit_names = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
replaced_names = ['o1e', 't2o', 't3e', 'f4r', 'f5e', 's6x', 's7n', 'e8h', 'n9e']
lines = [x.strip("\n") for x in open('inputs/day1.txt').readlines()]
digit_names = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
def digits(lines: list[str]) -> list[int]:
dgts = []
@ -22,20 +21,23 @@ def transform_digits(lines: list[str]) -> list[str]:
for line in lines:
indices = {}
for idx, c in enumerate(digit_names):
c_loc = line.find(c)
if c_loc != -1:
indices[str(c_loc)] = str(idx+1)
c_loc = [i for i in range(len(line)) if line.startswith(c, i)]
while c_loc:
loc = c_loc.pop(0)
indices[loc] = str(idx+1)
if len(indices) == 0:
newlines.append(line)
else:
first_index = sorted(indices)[0]
first_number = replaced_names[int(indices[first_index])-1]
first_digit_name = digit_names[int(indices[first_index])-1]
first_number = int(indices[first_index])
second_index = sorted(indices)[-1]
second_number = replaced_names[int(indices[second_index])-1]
second_digit_name = digit_names[int(indices[second_index])-1]
newlines.append(line.replace(first_digit_name, first_number).replace(second_digit_name, second_number))
second_number = int(indices[second_index])
newline = line[:first_index+1] + str(first_number) + line[first_index+1:]
newline = newline[:second_index+2] + str(second_number) + newline[second_index+2:]
newlines.append(newline)
return newlines
print(f'Part 1 Solution = {sum(digits(lines))}')
print(f'Part 2 Solution = {sum(digits(transform_digits(lines)))}')
print(f'Part 2 Solution = {sum(digits(transform_digits(lines)))}')

@ -0,0 +1,50 @@
lines = open("inputs/day11.txt").readlines()
def mkmat(lines):
mat = [[0 if x == "." else 1 for x in line.strip("\n")] for line in lines]
return mat
def empty_spaces(mat):
empty_lines = []
for idx, line in enumerate(mat):
if sum(line) == 0:
empty_lines.append(idx)
empty_cols = []
for i in range(len(mat[0])):
col = [x[i] for x in mat]
if sum(col) == 0:
empty_cols.append(i)
return empty_lines, empty_cols
def galaxies(mat):
coords = []
for idx, line in enumerate(mat):
for jdx, p in enumerate(line):
if p == 1:
coords.append((idx, jdx))
return coords
def manhattendistances(coords, expansions, expansionrate=1000000-1):
(rows, cols) = expansion
dists = [[0 for x in range(len(coords))] for y in range(len(coords))]
for i, coord1 in enumerate(coords):
for j, coord2 in enumerate(coords):
emptyrows = len([x for x in range(coord1[0], coord2[0]+1) if x in rows]) + len([x for x in range(coord1[0], coord2[0]+1)[::-1] if x in rows])
emptycols = len([x for x in range(coord1[1], coord2[1]+1) if x in cols]) + len([x for x in range(coord1[1], coord2[1]+1)[::-1] if x in cols])
d = manhatten(coord1, coord2) + ((emptyrows+emptycols) * expansionrate)
dists[i][j] = d
return dists
def manhatten(x,d):
(x,y) = x
(dx, dy) = d
return abs(x-dx) + abs(y-dy)
mat = mkmat(lines)
expansion = empty_spaces(mat)
coords = galaxies(mat)
dists = manhattendistances(coords, expansion)
print(f"Solution for Part 1: {sum([sum(i) for i in zip(*manhattendistances(coords, expansion, 1))])//2}")
print(f"Solution for Part 1: {sum([sum(i) for i in zip(*manhattendistances(coords, expansion, 1000000-1))])//2}")

@ -0,0 +1,62 @@
import itertools
from functools import cache
lines = [line.strip("\n") for line in open("inputs/day12.txt").readlines()]
def encodings(lines: list[str]) -> tuple:
damaged = []
alt = []
for line in lines:
line = line.split(" ")
damaged.append(line[0])
alt.append([int(x) for x in line[1].split(",")])
return damaged, alt
def qm_idx(string: str) -> list[int]:
idx = [i for i, s in enumerate(string) if s == "?"]
return idx
def matches(string: str, enc: list[int]) -> bool:
groups = []
counter = 0
cont = False
for i, s in enumerate(string):
if s == "#":
counter += 1
cont = True
if i == len(string) - 1:
groups.append(counter)
else:
if cont:
cont = False
groups.append(counter)
counter = 0
return groups == enc
def combinations(string: str) -> list[str]:
ids = qm_idx(string)
combs = []
for comb in itertools.product("#.", repeat=len(ids)):
newstring = string
for i, c in zip(ids, comb):
newstring = newstring[:i] + c + newstring[i+1:]
combs.append(newstring)
return combs
@cache
def count_matches(string: str, alt: list[int]) -> int:
combs = combinations(string)
counter = 0
for comb in combs:
if matches(comb, alt):
counter += 1
return counter
def main():
dmgd, alts = encodings(lines)
counts = 0
for dmg, alt in zip(dmgd, alts):
counts += count_matches(dmg, alt)
print(f"Solution Part 1: {counts}")
# part 2 missing
main()

@ -0,0 +1,140 @@
................................................#.........#............................................#....................................
..#.............#......#.............................................#...........................................................#..........
..............................................................#..............................#..............................................
.................................................................................................................#..........................
...........#...........................#...............................................#..................................#.............#...
............................#..................#.....#..........................#...........................................................
..................................#................................#........................................................................
...........................................................................#................................................................
.....#...................................#......................................................#.......................#...........#.....#.
.....................#........#....................#....................................................#...................................
......................................................................#.....................................................................
...........................................................#.................................#..............................................
........#..................#....................#....................................................#......................................
#.............#....................................................#..............#.............................#...........................
...................#.............#...................................................................................#...............#......
.............................................................................#.............................................................#
.........................................................................................................#..................................
....#...........................................................................................#...........................#.....#.........
.....................#.......................#.........#....................................................................................
#............#.........................................................................#....................#...............................
................................#..................#...........#......#............................#...................#.......#............
.......................................................................................................................................#....
...................................................................................#........................................................
.........................#.............#...................................................................................#................
............................................................................................................................................
..........#..............................................................#......#.........#..........#......................................
............................................................................................................................................
.................................................................................................#............................#.............
................#...............................#............#.....................#........................................................
.#........................#..........................#......................................................#.......................#.......
.......#....................................#................................#..............................................................
.................................#.....................................#..................................................................#.
........................................................#.............................#...............................#........#............
.....................#...................#.....#...............#............................................................................
.............#...................................................................................................#..........................
................................................................................................#.....#...............................#.....
.................#................#................#........................................................................................
.#..........................#.............................#.......#.......#................................................................#
.............................................#.............................................................#................................
...........#.................................................................................#...............................#..............
.....................#.................................#.......#..................................................................#.....#...
.......#..............................#..............................................................#......................................
#...............................#.....................................#...............#........................#.......#....................
...........................................................................#.....#.........................................................#
...........................................................................................#................................................
........................................................#................................................#.......................#..........
.........#............#............................................#.................................................#.....#................
............................................................................................................................................
......................................#......#........................................................#.........#...........................
.............#.....#......#..........................#......................................................................................
......#................................................................................................................................#....
.............................................................................................................#..............................
..#.......#.............................#.....................#..................#........#.................................................
............................................................................................................................................
......................................................................................#...............................#.....................
..................#...........................#...........................#.........................#.........................#.............
............................................................................................................................................
......................................#..................................................#.........................................#........
.......................#.....#................................................................#.............#............#..................
.....#.............................................#.............#..........................................................................
.............#...........................................................#.......#..................................#...........#...........
.................................#..........................................................................................................
.............................................#...........#..................................................................................
.......#......................................................#............................#.........#........#.............................
.....................#................................................#...........................................................#.........
...............#...................#...............#.......................................................................#................
#..........................#......................................................................#.........................................
.............................................................................#..............................................................
...............................#......#.................#..............................................#..........#.........................
...........................................................................................................................................#
.................#......#.......................#....................................#.....#..............................#.................
.........#.............................................................#..................................#.................................
....................................................#............#............#.........................................................#...
..#.........................................................#........................................................#..........#...........
............................................................................................................................................
............................................................................................................................................
.......#...........................#..............................................................#.........................................
....................#...................................#................................................................#..................
..............................#.............................................................................................................
..#......................#.............................................#........#..........#................................................
..........#.............................#......................#................................................................#...........
.....................................................................................................#......................................
......................#.............................................................................................#......................#
#................#..................................#........................................#.................#............................
...................................#......#.......................#...........#..........................................#.............#....
............................................................................................................................................
..........#..............................................#..................................................................................
............................................................................................................................................
....................#......#..........#.......................#......#....................#..........#.......#..................#...........
...#.........................................................................#..............................................................
..................................................................................#..............#..........................................
........#.......................#..........#................................................................................................
..............#...........................................................................................#.................................
................................................................#.......................................................................#...
.......................#................#.....................................................................#.............................
....#............#............#.................................................................#.....#.....................................
...................................................................................................................#........................
...........................................#..............................................................................#.................
.......#.........................................#.......#.......#.....#...............................................................#....
................................................................................#...........#.....................................#.........
...........#.....................................................................................#..........................................
..............................................................#.............................................................................
...............#.........#..........#........................................................................#..............................
...........................................#......................................................................#......#..................
....#...............#.......................................................................................................................
......................................................................................#..............................................#......
...............................................#...............#..............#.................#...........................................
.#.........................................................................................#.........................#......................
................#......#................#................................#..........................#...........#...........................
......#......................#.....................#.......#................................................................................
.............................................#...........................................................#...............#.....#........#...
...........#.........................................................................#......................................................
............................................................................#...............................................................
.........................................................................................#............................#.....................
...................#...................#.....................#....................................#.............#...........................
.................................#..........................................................................................................
.......#......#...........#..................................................................................................#.....#........
.......................................................................#..............#........#.............#..............................
......................#..................#......#..............#..............#.....................................#..................#....
..........................................................................................#.............#...................................
.........#..........................................#..............#.......................................................#................
.............................#..............................................................................................................
.............................................#...................................#..........................................................
................#..................#........................................#......................................#........................
....#.................#.................#.............................................#.................................#..................#
............................................................#.........#..................................#..................................
......................................................#...................................#.................................................
.................................#..................................................................#.......................................
.................................................#...................................................................................#......
............................................................................................................................................
..................#.............................................#.............#.........................#................#..................
.........#.................................................#...........#.............#........#..............#............................#.
...................................#.........................................................................................#..............
............................................................................................................................................
#.....................#...................................................#.......#...............................#.........................
..............................#.................#........#..........................................................................#.......
..............#........................................................................#....................................................
...........................................#.............................................................#..............................#...
.................................#...................................................................................#...........#..........
...........#.......#.......#...........................#.....#................#......................#........#.............................

@ -0,0 +1,10 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....

File diff suppressed because it is too large Load Diff

@ -0,0 +1,6 @@
???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1
Loading…
Cancel
Save