Browse Source

Day 6 update

master
Noëlle 2 years ago
parent
commit
bc56944ca3
No known key found for this signature in database
6 changed files with 15 additions and 17 deletions
  1. 0
    4
      aoc4-1.py
  2. 1
    5
      aoc4-2.py
  3. 1
    2
      aoc5-1.py
  4. 11
    4
      aoc5-2.py
  5. 0
    1
      aoc6-1.py
  6. 2
    1
      aoc6-2.py

+ 0
- 4
aoc4-1.py View File

@@ -8,9 +8,6 @@ class Board:
self.marks.append([0]*self.boardsize)

def mark(self, digit):
import pdb
# if self.boardnum == 1:
# pdb.set_trace()
for i, line in enumerate(self.lines):
if digit in line:
y = i
@@ -51,7 +48,6 @@ class Board:
outstr += f"{' '.join([str(el).rjust(2) for el in self.lines[i]])} {' '.join([str(el).rjust(2) for el in self.marks[i]])}\n"
return outstr


def main():
with open("aoc4-1.txt", "r") as file:
lines = [line.strip() for line in file.readlines()]

+ 1
- 5
aoc4-2.py View File

@@ -8,9 +8,6 @@ class Board:
self.marks.append([0]*self.boardsize)

def mark(self, digit):
import pdb
# if self.boardnum == 1:
# pdb.set_trace()
for i, line in enumerate(self.lines):
if digit in line:
y = i
@@ -51,7 +48,6 @@ class Board:
outstr += f"{' '.join([str(el).rjust(2) for el in self.lines[i]])} {' '.join([str(el).rjust(2) for el in self.marks[i]])}\n"
return outstr


def main():
with open("aoc4-1.txt", "r") as file:
lines = [line.strip() for line in file.readlines()]
@@ -66,7 +62,7 @@ def main():
i += 6
for el in firstline:
print(el)
is_to_pop = []
is_to_pop = [] # that's the plural of "i"
for i, board in enumerate(boards):
board.mark(el)
if board.check():

+ 1
- 2
aoc5-1.py View File

@@ -42,7 +42,7 @@ def main():
# For 5-1, only care about the ones where, of x1,y1 -> x2,y2, x1 == x2 or y1 == y2
# i.e. only horizontal or vertical
lines = [line for line in lines if line[0][0] == line[1][0] or line[0][1] == line[1][1]]
# god, this feels goofy
# god, this feels goofy, but it works
size_x = max([max(line[0][0], line[1][0]) for line in lines])
size_y = max([max(line[0][1], line[1][1]) for line in lines])
board_map = FloorMap(size_x, size_y)
@@ -50,6 +50,5 @@ def main():
board_map.add_line(*line)
print(board_map.count_inters())


if __name__ == "__main__":
main()

+ 11
- 4
aoc5-2.py View File

@@ -5,21 +5,28 @@ class FloorMap:
self.board.append([0]*(size_x+1))

def add_line(self, start_pos, end_pos):
if start_pos[0] == end_pos[0]:
if start_pos[0] == end_pos[0]: # Vertical line: X stays constant, Y changes
if start_pos[1] > end_pos[1]:
start_pos, end_pos = end_pos, start_pos
for i in range(start_pos[1], end_pos[1]+1):
self.board[i][start_pos[0]] = self.board[i][start_pos[0]] + 1
elif start_pos[1] == end_pos[1]:
elif start_pos[1] == end_pos[1]: # Horizontal line: Y stays constant, X changes
if start_pos[0] > end_pos[0]:
start_pos, end_pos = end_pos, start_pos
for i in range(start_pos[0], end_pos[0]+1):
self.board[start_pos[1]][i] = self.board[start_pos[1]][i] + 1
else:
else: # Diagonal line: X and Y both change
# These are only ever 45° angles, so each changes by 1 per iteration.
if start_pos[0] > end_pos[0]:
start_pos, end_pos = end_pos, start_pos
i, j = start_pos
end_i, end_j = end_pos[0], end_pos[1]
# I've arranged this so X always increases. Y can either increase or decrease.
# I don't like that so much repeats.
# What I WANT to do here is be able to set a toggle variable so that
# if Y decreases, the second condition flips from <= to >=,
# and then j += dir, which is either 1 or -1.
# But I can't find a way to do it without eval() bullshit.
if j <= end_j: # there should be no cases where they're equal, but...
while i <= end_i and j <= end_j:
self.board[j][i] = self.board[j][i] + 1
@@ -52,7 +59,7 @@ def main():
lines = [line.strip() for line in file.readlines()]
# Each line is a pair of x,y coordinates...es separated by " -> "
lines = [[list(map(int, el.split(","))) for el in line.split(" -> ")] for line in lines]
# god, this feels goofy
# god, this feels goofy, but it works
size_x = max([max(line[0][0], line[1][0]) for line in lines])
size_y = max([max(line[0][1], line[1][1]) for line in lines])
board_map = FloorMap(size_x, size_y)

+ 0
- 1
aoc6-1.py View File

@@ -31,6 +31,5 @@ def main():
print(len(fishes))
return len(fishes)


if __name__ == "__main__":
main()

+ 2
- 1
aoc6-2.py View File

@@ -16,6 +16,7 @@ class LanternfishSchool:
self.time_left["ttr"] = 0
if self.school_size() >= self.size_base:
print(f"School size now {self.school_size()}.")
self.size_base += self.size_inc

def school_size(self):
total = 0
@@ -24,7 +25,7 @@ class LanternfishSchool:
return total

def main():
# The old version gets untenable above about 100 days.
# The 6-1 version gets untenable above about 100 days.
# Trying a different approach.
num_days = 256
with open("aoc6-1.txt", "r") as file:

Loading…
Cancel
Save