This repository has been archived by the owner on Jan 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP04.py
117 lines (104 loc) · 4.17 KB
/
P04.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# MIT License
# Copyright (c) [2020] [Olivier Delbeke]
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
__author__ = "Olivier Delbeke"
__year__ = 2019
zero = set( ['a','b','c','e','g','f'] )
one = set( ['c','g'] )
two = set( ['a','c','d','e','f'] )
three = set( ['a','c','d','g','f'] )
four = set( ['b','c','d','g'] )
five = set( ['a','b','d','g','f'] )
six = set( ['a','b','d','g','f','e'] )
seven = set( ['a','c','g'] )
eight = set( ['a','b','c','d','e','f','g'] )
nine = set( ['a','b','c','d','g','f'] )
codes = [ zero, one, two, three, four, five, six, seven, eight, nine ]
# Get possibilities ( chiffre1, max changements, alumettes dispo )
# -> [ (4,2), (5,3) ] # 4 en deux mouvements,
# manque le solde en alumettes
def get_changes( a, b ):
common = len(codes[a].intersection(codes[b]))
missing = len(codes[b]-codes[a])
excess = len(codes[a]-codes[b])
if excess >= missing:
movable = missing
external = 0
else:
movable = excess
external = missing - movable
# Count as movement :number of internal movements + number of additional matches needed
movements = movable + external
saldo = excess - missing # Correct
#print("From %d to %d : %d moved internally, %d saldo, %d movements" % (a,b,movable,saldo,movements))
return (saldo, movements)
#get_changes(2,1)
#get_changes(3,9)
#get_changes(5,7)
#get_changes(2,2)
#exit()
for sign in [0, -1]:
if sign==0:
current_saldo, movements_left = 0, 4
else:
current_saldo, movements_left = 1, 4
for c1 in range(10):
saldo1, movements1 = get_changes(2 , c1)
if movements1 > movements_left:
continue
current_saldo += saldo1
movements_left -= movements1
for c2 in range(10):
saldo2, movements2 = get_changes(3 , c2)
if movements2 > movements_left:
continue
current_saldo += saldo2
movements_left -= movements2
for c3 in range(10):
saldo3, movements3 = get_changes(5 , c3)
if movements3 > movements_left:
continue
current_saldo += saldo3
movements_left -= movements3
for c4 in range(10):
saldo4, movements4 = get_changes(2 , c4)
# Exactly 4 movements
if movements4 != movements_left:
continue
# Saldo must be 0
if current_saldo + saldo4 != 0:
continue
# Calculus must match
if sign==0 and 10*c1+1 + c2 == 10*c3 + c4:
print("%d + %d = %d" % (10*c1+1,c2,10*c3+c4))
if sign!=0 and 10*c1+1 - c2 == 10*c3 + c4:
print("%d - %d = %d" % (10*c1+1,c2,10*c3+c4))
current_saldo -= saldo3
movements_left += movements3
current_saldo -= saldo2
movements_left += movements2
current_saldo -= saldo1
movements_left += movements1
# 21 + 2 = 23
# 21 + 7 = 28
# 31 + 2 = 33
# 31 + 7 = 38
# 51 + 2 = 53
# 51 + 7 = 58
# 31 - 9 = 22
# 71 - 3 = 68
# 81 - 9 = 72 --- solution