Day25
This commit is contained in:
111
day25/input.txt
Normal file
111
day25/input.txt
Normal file
@@ -0,0 +1,111 @@
|
||||
1=021=20---11-1=1
|
||||
222==-=1-2=-11-11
|
||||
1010==0=0=1--=1
|
||||
2
|
||||
1122=
|
||||
2222==2222-2--10
|
||||
100=-2-002=
|
||||
12221-2-10-=1=
|
||||
1-=211000=221-1
|
||||
2=--12==11221-2-002
|
||||
1101==-01===01=
|
||||
220=0=-2-1-0===10=2
|
||||
2-0-
|
||||
1=-12=2==
|
||||
1-=-02==-1
|
||||
100-0-11--02--0=-
|
||||
1==0-1220011=-
|
||||
21
|
||||
1102-22==
|
||||
222
|
||||
1==0=22-0--011-0
|
||||
1020
|
||||
2==2-102
|
||||
1010-02==02=
|
||||
1=1
|
||||
2=010==12022=
|
||||
1=-22001-2=
|
||||
101=2-201122111
|
||||
1=-0021211-1-1-1202
|
||||
2-22-2
|
||||
1212002==1
|
||||
2=-0-
|
||||
1-1==1122110100
|
||||
1--020-2-=-0-21--
|
||||
2=22-21220022==1=
|
||||
1=-00-121-20-2122
|
||||
1-==-00
|
||||
20=21=0
|
||||
20-02-0-=-==-=22
|
||||
1120==-2
|
||||
2=--0-0-000201=101
|
||||
1---=02=21
|
||||
1-011
|
||||
211-2112=20--2
|
||||
1=-
|
||||
1=--=021
|
||||
1-1-122200==1-1111
|
||||
11001-
|
||||
200=200
|
||||
1===21=1-2
|
||||
1===10
|
||||
2=22
|
||||
10-110210=01112-
|
||||
2121=1=2=-201-
|
||||
1---2=-0=-
|
||||
211-120---000=-0-
|
||||
1-
|
||||
1-101-2=01020=-0=
|
||||
112-12--0=12=12--
|
||||
1-0-=2--=--12=20
|
||||
1=0=-121
|
||||
1000122-210--011200
|
||||
120==-1-=01
|
||||
1=2=1--
|
||||
101-1=--2-
|
||||
2=
|
||||
2--10
|
||||
2201020-=2-
|
||||
1===2001-1=0=
|
||||
222=-2---2=112
|
||||
2010=-22=012=202=
|
||||
111
|
||||
1-2112=0200=1
|
||||
2=02211=2
|
||||
2-121
|
||||
1----12=-2011
|
||||
2==0
|
||||
112
|
||||
22=0==1-1=
|
||||
1-20=0-
|
||||
1=110211-022201222
|
||||
20
|
||||
1-111
|
||||
1=-=022011=2=2=1
|
||||
1-02122
|
||||
2-1
|
||||
1-2-=2-0=0---
|
||||
1-20
|
||||
1=1=102
|
||||
1=1=0--1
|
||||
11222-==0101-
|
||||
2122100=0
|
||||
2011==12-10
|
||||
210--121-0=
|
||||
22121=01=0=-=-=-==
|
||||
10-0-=1-12
|
||||
2-2=21
|
||||
1=1-0-0-
|
||||
10020
|
||||
10-0-0120=211-2-
|
||||
1-==10-0-==001201-
|
||||
1-0
|
||||
21=00202002-0
|
||||
1-1=2021
|
||||
11=1==1=0
|
||||
1=102-1=-20-0
|
||||
2=100-12-0=
|
||||
1=0
|
||||
1--=
|
||||
12=212110200-
|
||||
222002100
|
||||
BIN
day25/snafu
Executable file
BIN
day25/snafu
Executable file
Binary file not shown.
102
day25/snafu.c
Normal file
102
day25/snafu.c
Normal file
@@ -0,0 +1,102 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define BUFFER_SIZE 64
|
||||
#define MAGIC_NUMBER 5
|
||||
|
||||
long powNum(long, long);
|
||||
long snafu_to_human(char*, int);
|
||||
void human_to_snafu(char*, long);
|
||||
|
||||
int main() {
|
||||
char buf[BUFFER_SIZE], *p, c;
|
||||
memset(buf, 0, BUFFER_SIZE);
|
||||
p = buf;
|
||||
long sum = 0;
|
||||
|
||||
while ((c = getchar()) != EOF) {
|
||||
*p++ = c;
|
||||
if (c == '\n') {
|
||||
sum += snafu_to_human(p-2, p-buf);
|
||||
memset(buf, 0, BUFFER_SIZE);
|
||||
p = buf;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Sum in decimal: %li\n", sum);
|
||||
char snafu[64];
|
||||
memset(snafu, 0, 64);
|
||||
human_to_snafu(snafu, sum);
|
||||
printf("Sum in SNAFU: %s\n", snafu);
|
||||
}
|
||||
|
||||
long powNum(long b, long e) {
|
||||
long result = 1;
|
||||
for (int i = 0; i < e; i++) {
|
||||
result *= b;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
long snafu_to_human(char* p, int len) {
|
||||
long result = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
switch(*(p-i)) {
|
||||
case '0':
|
||||
break;
|
||||
case '1':
|
||||
result += powNum(MAGIC_NUMBER, i);
|
||||
break;
|
||||
case '2':
|
||||
result += 2*powNum(MAGIC_NUMBER, i);
|
||||
break;
|
||||
case '-':
|
||||
result += -powNum(MAGIC_NUMBER, i);
|
||||
break;
|
||||
case '=':
|
||||
result += -2*powNum(MAGIC_NUMBER, i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void human_to_snafu(char* p, long decimal) {
|
||||
char buf[64], *q;
|
||||
memset(buf, 0, 64);
|
||||
|
||||
long num = decimal;
|
||||
int len = 0;
|
||||
int remainder = 0;
|
||||
|
||||
while (num != 0) {
|
||||
int numeric = num % MAGIC_NUMBER;
|
||||
num /= MAGIC_NUMBER;
|
||||
numeric += remainder;
|
||||
remainder = 0;
|
||||
if (numeric > 2) {
|
||||
remainder = 1;
|
||||
numeric = numeric - MAGIC_NUMBER;
|
||||
}
|
||||
if (numeric == -1) {
|
||||
buf[len] = '-';
|
||||
} else if (numeric == -2) {
|
||||
buf[len] = '=';
|
||||
} else {
|
||||
buf[len] = '0' + numeric;
|
||||
}
|
||||
len++;
|
||||
}
|
||||
|
||||
if (remainder) {
|
||||
buf[len] = '1';
|
||||
len++;
|
||||
}
|
||||
|
||||
for (int i = 0; i <= len; i++) {
|
||||
*(p+len-i-1) = buf[i];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user