Browse Source

finished day 25

main
Luna Lailatova 2 years ago
parent
commit
1c688d1b96
  1. 73
      day25.py
  2. 19
      in2.txt
  3. 116
      input25.txt

73
day25.py

@ -0,0 +1,73 @@ @@ -0,0 +1,73 @@
converter = {
'2' : 2,
'1' : 1,
'0' : 0,
'-' : -1,
'=' : -2
}
def maximum(n):
maxi = 0
for x in range(n + 1):
maxi += 2*5**x
return maxi
def decimalToSnafu(num):
n = 0
while True:
if n == 0:
if num <= 2:
break
n += 1
continue
if num <= maximum(n):
break
n += 1
rest = num
subtract = False
if n > 0:
Snafu = ['0' for x in range(n+1)]
for digit in range(n, -1, -1):
if rest > 0:
count, rest = divmod(rest, 5**digit)
if count != 2 and rest > maximum(digit - 1):
count += 1
Snafu[-(digit+ 1)] = str(count)
elif rest < 0:
count, rest = divmod(abs(rest), 5**digit)
if count != 2 and rest > maximum(digit - 1):
count += 1
if count == 2:
Snafu[-(digit+ 1)] = '='
elif count == 1:
Snafu[-(digit+ 1)] = '-'
else:
Snafu[-(digit+ 1)] = '0'
rest = num - SnafuToDecimal(''.join(Snafu))
return(Snafu)
else:
return str(num)
def SnafuToDecimal(number):
number = list(reversed(number))
decimal = 0
for x in range(len(number)):
decimal += converter[number[x]] * 5 ** x
return decimal
total = 0
with open('input25.txt', 'r') as f:
numbers = f.read().splitlines(keepends=False)
for number in numbers:
total += SnafuToDecimal(number)
print(total)
print(''.join(decimalToSnafu(total)))

19
in2.txt

@ -1,6 +1,13 @@ @@ -1,6 +1,13 @@
#.######
#>>.<^<#
#.<..<<#
#>v.><>#
#<^v^^>#
######.#
1=-0-2
12111
2=0=
21
2=01
111
20012
112
1=-1=
1-12
12
1=
122

116
input25.txt

@ -0,0 +1,116 @@ @@ -0,0 +1,116 @@
1=-=2=-1000=0
11=001122
120-
1-0
202-0==-2-
2-1010=2-2=01
112=-2
2-0=
1-=2-=-1-0--0=1=0000
1=0=0--021-0
11000101=210
21-111=2112=2==-
111010-2
2=10=-101-=--0
2-=-2000012=0022=
1-=11-01--2100
1=2211-2-===
2=--2-==-1
1=12---02=2
1==0=--11=01-1-
121-21021
12=1001=2=-=22
1=2-212=2-0211
20----11=
10=100=0=0
111
1=--011==2--212121=
2=-10=210200-2-=0
1=-122=001-1
2-0=0121=-0=0
2-=20--1-=21-22=1=
20011
12=1--=-0--10-0=1-=
2002=100=-00-
202
1=2==21-
1=221--01=00-10=
1--0-1=
2-==11=222---
21-10-=22
1=--22-0-
2-0-
1-0=-2
20-02102
1=0=20----21=12=-
20=111-12=21101=
111=1=-2=0=--21
11-
10-1=01=-
1222-10
1=2-1
20
101==12-1-0
1-=22=--=22=1=22
1200-010-10=021
1=2-10=-02--=-
12-0=000=0
1==10=00===1--
2-1=-=1===1=-0=---
1=11=120
2===002010=-=0
1==00212=01-002=221
10020221===-
1--01=-=0-20-101-=-
112=101
10-
1220=1
2=
1=1120=--0
11-21200=
2==011-10-0==0=0=
12-=102
100-022
1=1
201-0--=-
2-02
21=11=00020
12=20-
1101=00-1
1=0=-11--=-010-==1
1-2-0=00=-=220=11-
2-2
12-2
12=10=2-=
20222110-10002
1002=0
12=11-=012
1==
1=00-01
110101222-020--111
2-0
1-12=1
222-10-12=00
11=1=1
10=2-00-121-2-=2
1---22-1-0=00-1220
2-=2210=-==2=010=
12201-111-220
2=122=022=22=12-1
211===2=020
121-2-0-=0==2=1
1-0221200-21100-
10
1-11
2===2=-2-
1-0-2021-21==
1==-00-=2-21===1200
22
1222=20=120=1-
11=120001012==01-1
2=-1--00
10==121--221
1=2100102=20012
2==1-
1-
10-2--0
Loading…
Cancel
Save