diff --git a/day25/day25.go b/day25/day25.go new file mode 100644 index 0000000..781a388 --- /dev/null +++ b/day25/day25.go @@ -0,0 +1,79 @@ +package main + +import ( + "AOC2022/helper" + "fmt" + "math" + "os" + "strconv" +) + +func main() { + args := os.Args[1:] + lines := helper.ReadTextFile(args[0]) + sum := 0 + for _, line := range lines { + sum += toNormal(line) + } + fmt.Println(sum) + fmt.Println(toSNAFU(sum)) +} + +func toNormal(SNAFU string) int { + returnValue := float64(0) + for i := 0; i < len(SNAFU); i++ { + currentChar := SNAFU[len(SNAFU)-1-i] + amount := float64(0) + switch currentChar { + case '2': + amount = 2 + case '1': + amount = 1 + case '-': + amount = -1 + case '=': + amount = -2 + } + returnValue += amount * math.Pow(float64(5), float64(i)) + } + return int(returnValue) +} + +func toSNAFU(input int) string { + returnValue := "" + highestFitting := 99 + i := 10 + for highestFitting == 99 { + i-- + amount := int(math.Pow(5, float64(i))) + if amount < input { + highestFitting = i + 1 + } + } + fitIns := 0 + for input > 0 { + remains := input % 5 + remains += fitIns + fitIns = 0 + input = input / 5 + if remains > 2 { + remainsRemains := remains % 5 + fitIns = remains / 5 + if remainsRemains > 2 { + fitIns++ + switch remainsRemains { + case 3: + returnValue = "=" + returnValue + case 4: + returnValue = "-" + returnValue + + } + } else { + returnValue = strconv.Itoa(remainsRemains) + returnValue + } + } else { + returnValue = strconv.Itoa(remains) + returnValue + } + } + return returnValue +} diff --git a/day25/input b/day25/input new file mode 100644 index 0000000..ec0ae43 --- /dev/null +++ b/day25/input @@ -0,0 +1,141 @@ +121=10120=1022=2=0 +10==--=-=2 +2=11 +120221-1- +2002011 +1-00-11=0 +1==2=-200101=== +2122=-10 +1=1=2=-1 +11--=-10-0-0 +2=-210= +2-==1221011=021==- +2 +1-2==212 +1-2=0=12--0111010=0 +1220-20=1=1 +1211=1 +10-22=-212--00 +1=---021 +1-010-21-2- +10-==00122-=0-- +10=- +1=20=-=---1=1 +1-0-0 +2===11=12200=2 +1===1 +1002 +10=-==-02 +12---2-==-020122 +11=-=2 +1-==2-=--2 +1-1112 +210-212-=20220-1 +1=-0111=0== +100=20==- +200==22-2= +2- +21-=1012=2=01= +1=-=0=0-01=0 +1-2 +1121==0= +2--21 +212111=00=-1-=1= +10-021- +2= +2=0=12- +1-0-- +2-0=21012==112=-1= +1222==01=100121=0-= +1011=01 +12102- +10012=0-11021 +1-=01- +12=010=12-0=02=11 +1-22=2--1- +100=010-1 +210 +1- +22--011--1212101 +11201 +1=2=-122=---1 +1===2122=-01=0=2 +1011--0--=20 +202-2-1=2 +2=000=10=-0 +110-=00-1211 +2==0=--00-2=== +110 +2=-02-1220012- +1=21----=-= +1=1 +12=--2=012-21 +1=0020-1--=0-= +1== +11-01101221=-02 +1=0201-2==0--01 +1=120120-=1 +1-121=====2-0=- +201=1=1101-=02== +1-00--0- +10-202 +22- +1==1=-2==-0-110 +1==21=1=20-1210=12 +20112-1=0--0= +1=-=2021--0===21 +1-==-2 +1==120-0- +1=0111=21-00-0=2 +1-110-=11-1021 +22 +1=1212=20= +212=1=-12---122 +2=2 +2220-1=2==022=--0= +2-02222 +1--0=0-22=-1 +120==2100 +10==201121==2-1221-0 +12210--==2--01022 +211120-21202 +22021011 +21---0=2-10=22 +1-22=-0 +12201=-2121-=-1 +11122-=202- +1-2---= +1-22=01=10-10 +1-0=0=21211011-0- +2--- +1==12100 +112=1121 +120--1 +1-11=-2=-0 +1--=02-02 +2000-22==0221=2= +1=1-10-1-==100=1 +202-011011 +20=10=-0002=00= +2=1-2 +220 +11==-0=120--0-10= +1=112-22202=2 +10=-0 +1=1=2=0 +2222--11---011-1 +1=-1=2=1-2122--10-1 +11021 +101 +11=2101222== +100112-2011 +1=1= +2-021-- +12211-1122=2- +2--2=-1=- +21012=2---2=2120=1 +1-010-==--2 +12=-22000- +2-===1-1 +22=-=122=--1= +2-2=-0-021 \ No newline at end of file diff --git a/day25/testinput b/day25/testinput new file mode 100644 index 0000000..237ef0c --- /dev/null +++ b/day25/testinput @@ -0,0 +1,13 @@ +1=-0-2 +12111 +2=0= +21 +2=01 +111 +20012 +112 +1=-1= +1-12 +12 +1= +122 \ No newline at end of file