Skip to content

Commit f86508d

Browse files
committed
Add day 100
1 parent 69012ce commit f86508d

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

BFS/day100/part1.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from collections import deque
2+
3+
def print_board(s,n):
4+
for i in s:
5+
l = ""
6+
for j in range(i-1):
7+
l += "."
8+
l += "♛"
9+
for j in range(n-i):
10+
l += "."
11+
print(l)
12+
13+
def safe(s):
14+
l = len(s)
15+
for i in range(l-1):
16+
qi = s[i]
17+
for j in range(1,l-i):
18+
qj = s[i+j]
19+
if (qj == qi) or (qj == qi + j) or (qj == qi - j):
20+
return False
21+
return True
22+
23+
def search(s, n):
24+
rs = []
25+
for i in range(1,n+1):
26+
if safe(s + tuple([i])):
27+
rs.append(s + tuple([i]))
28+
return rs
29+
30+
def solution(input):
31+
n = int(input)
32+
count = 0
33+
start = ()
34+
queue = deque([start])
35+
last = ""
36+
while len(queue) != 0:
37+
current = queue.popleft()
38+
for d in search(current, n):
39+
if len(d) == n:
40+
count += 1
41+
last = d
42+
else:
43+
queue.append(d)
44+
print_board(last, n)
45+
return count

BFS/day100/part2.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def safe(s):
2+
l = len(s)
3+
for i in range(l-1):
4+
qi = s[i]
5+
for j in range(1,l-i):
6+
qj = s[i+j]
7+
if (qj == qi) or (qj == qi + j) or (qj == qi - j):
8+
return False
9+
return True
10+
11+
def search(s, n):
12+
rs = 0
13+
for i in range(1,n+1):
14+
next = (s + tuple([i]))
15+
if safe(next):
16+
if len(next) == n:
17+
rs += 1
18+
else:
19+
rs += search(next, n)
20+
return rs
21+
22+
def solution(input):
23+
n = int(input)
24+
count = search(tuple(), n)
25+
return count

BFS/day100/part3.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
def safe(s):
2+
l = len(s)
3+
for i in range(l-1):
4+
qi = s[i]
5+
for j in range(1,l-i):
6+
qj = s[i+j]
7+
if (qj == qi) or (qj == qi + j) or (qj == qi - j):
8+
return False
9+
return True
10+
11+
def rollback(board):
12+
board.pop(-1)
13+
if len(board) == 0: return
14+
board[-1] += 1
15+
16+
def solution(input):
17+
n = int(input)
18+
c = 0
19+
board = [1]
20+
i = 0
21+
while len(board) != 0:
22+
if board[-1] > n:
23+
rollback(board)
24+
elif safe(board):
25+
if len(board) == n:
26+
rollback(board)
27+
c += 1
28+
else:
29+
board += [1]
30+
else:
31+
if board[-1] >= n:
32+
rollback(board)
33+
else:
34+
board[-1] += 1
35+
return c

BFS/day100/sample.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy