2022 Day 25 part 1 python

This commit is contained in:
Luke Hubmayer-Werner 2023-01-03 23:13:18 +10:30
parent 3711935b4f
commit faf78e46cf
2 changed files with 150 additions and 0 deletions

26
2022/day25.py Normal file
View File

@ -0,0 +1,26 @@
from helpers import *
lines = read_day(25).split('\n')
snafu_digits = {'=':-2, '-':-1, '0':0, '1':1, '2':2}
# minimal_snafus = [-2]
# for i in range(1, 50):
# minimal_snafus.append(minimal_snafus[-1] + (-2 * (5**i)))
# print(minimal_snafus)
def snafu_to_decimal(s: str) -> int:
return sum((snafu_digits[v] * (5**i) for i,v in enumerate(reversed(s))))
def decimal_to_snafu(i: int) -> str:
# We don't want to constantly flip addition/subtraction, so start with biggest number and subtract
digits = 0
maximal = 0
while i > maximal:
maximal += 2 * (5**digits)
digits += 1
base5 = np.base_repr(maximal-i, 5)
s = ['2'] * digits
for i,d in enumerate(reversed(base5)):
s[i] = {'0':'2', '1':'1', '2':'0', '3':'-', '4':'='}[d]
return ''.join(reversed(s))
# print(decimal_to_snafu(1747))
print(f'Part 1: {decimal_to_snafu(sum((snafu_to_decimal(l) for l in lines)))}')

124
2022/input/25 Normal file
View File

@ -0,0 +1,124 @@
21-0==1=-0
102211-220011-----
1=2=111=10=-
12-2=022=2
1=--=11
12-2211000=0011-0-
1=1-
2=0-10-=21-2
1210-
2=1-=
1=0=12=2112--11=
2--211=200
20-02-20
12=12=0=0=1
22===21=-0102-012=
1--=-121
1=02=2-=
12-0-20
2=---=0102--00=22
1=2==0=2=
11-1112
2--012
12=12=--212-0-==-02
1=220
20===1
2210-02=12-022-=1-
22--1-==1
211-1=1=0-==1
10
1-1-02
1=-2==-0=21
1=102
20121021
1=2=22=001200-==1020
10211-1-20--10-1=
1222==1--=1--11-=
11211211
1=101-20=
1=1-11=0=000
1=-=101
2
1000-2
1==0=210=1=21222-=2
1=1==2-
2--020=2=2===-
2-
2=0
222101=22=-21=11
2=
121201-0-20-2-=11=
10=
1212=20=2=-=-1--1
2-20-=212=01
1=1--00--0=12=-00
22--
22
22===1=-0-22-
111-=
2-111=0021=2
200=22-=12-2=
1==-=1=0
1--=02-0002=
1=-=2-2=2-
2=2222220=2==001
1=00==11=-20-=2-2
1---0=-=
1-0==10-=22001
1=02=02-1=0111
1-=010-21-=21==02
10200-2=-=20120-2
1=-
10=122-
210-1=-=2011-0--1
2==-1=20
1=-=101-20=221-1
210
1-=-==-0-=02=---
1-=1=-=12=0
1-=0---12-0=
210-==1021=010=1
1=-212
11=1=-=2211=101222-
2=-==--=2
1=11-200--=
1120-
11=-0=221=0101=
11-1=-
1-12-2=0
1012-22=1-
20-122=-1=
1-100=0=101-0
20-1=1=0
1102=
21
10=20=11
1-=2001
1-20=12-0221102-==
1-1--010100=-1=
221002==2102-2=1
2012=121-22
110222002=01
212102-1=1=-211
210-=12
1=-1-0101=1-12
12--21=1
10=-12
100-122211=--2=
101102001
11011=-
211-0=-=0
1-2=01-1=
1-022-=00000
2==-0==2=--00=
10=--2000==
1---=--1-110-02
11-012
2==-2210-0
212
22-=-12111
2--=
2=2=12-100
10-12===0-
1==0=0=22012201
211-1--22