-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
278 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
110=11=10-=202=2-10 | ||
200 | ||
110120221-1021=0- | ||
10=1 | ||
2=2-0=0 | ||
2-2= | ||
1=2== | ||
10=2==0= | ||
2==1=200==-010= | ||
11=00 | ||
1-=2 | ||
1212 | ||
2=- | ||
211-=02-2 | ||
112 | ||
1===-1=21 | ||
1=212-0=12 | ||
1=0=1 | ||
2---0-=1=00 | ||
1011=112--1-= | ||
1--02-1- | ||
21=21 | ||
221 | ||
1-2=-1=11----1=-=- | ||
212001101= | ||
211=2=02102-11-0 | ||
1-210 | ||
201-00 | ||
2=0==000 | ||
11011=1=-21= | ||
1=11= | ||
1==222=11 | ||
100 | ||
1== | ||
120- | ||
2=1=0 | ||
1-101-2121----2 | ||
1020--=2 | ||
2=0==0-110 | ||
2-=0-=-1== | ||
22=201-21020=20-1 | ||
1-1220----=210-1= | ||
210=01 | ||
20=0==2220=22=-022 | ||
11=102 | ||
1-1-=01-10--- | ||
1=1 | ||
1=2=1-122-0-=01020 | ||
11=12 | ||
2- | ||
1=2==1-= | ||
1221=211101-=010 | ||
11=21-2--122-2 | ||
11--1- | ||
1=0-2 | ||
10=11=0 | ||
1-2=02-2--0- | ||
2==-20--11-1 | ||
1=1021=220112-= | ||
1--=1=202-022000 | ||
1-=02--=-1=-12-- | ||
11--00==0 | ||
1=1022-1=-2 | ||
1=11-22- | ||
1011= | ||
2= | ||
2-2-0-2-2=- | ||
12002=1=-110 | ||
2 | ||
11=1=2-1-0= | ||
2022201-100-2=02=2 | ||
1111=0101210 | ||
2001100-00=2- | ||
2=2202=0==20 | ||
1=0 | ||
1=1=0022-02-111-2-2 | ||
21==10 | ||
221==11=1-2-=221=2 | ||
20=0-02=-01=-1101 | ||
2====2-0=-==2 | ||
1===0110=- | ||
2-20-101-01 | ||
101-021=1112102 | ||
2=-=1-=2=0101-- | ||
1=20211= | ||
1=202=-=---1121= | ||
1==0200-= | ||
2=1=022=21--122=02 | ||
1-1010-=-0222=1==1-2 | ||
112=2=001- | ||
2121-=00=110= | ||
1--01-202 | ||
1-== | ||
1-0011- | ||
1-001===0 | ||
1=20-110- | ||
12010--==2-11-1=12 | ||
1--0-1 | ||
2=1-0=2=1-20 | ||
111=11 | ||
2-==--1=1201 | ||
1=- | ||
100-22-0=-10021 | ||
100==1-02111111= | ||
12---1== | ||
101==1 | ||
202111-1120122=2 | ||
2-- | ||
212- | ||
1===02-1-=102-00 | ||
111-10-=-0-== | ||
1-002101=0= | ||
220=1-01-=201-0 | ||
1-2--112=01 | ||
1-00 | ||
11==10212=1 | ||
1=10-==-- | ||
2110020002 | ||
1-==-==1--002 | ||
2=011=-1=2=10=== | ||
1=21101=20=2- | ||
21=02-00=21-02= | ||
10=0 | ||
1--221120=2-0=-001 | ||
111-0=-2010100=2 | ||
11==1-11-011-1- | ||
2=220=1= | ||
11-0121==2=2=0-20 | ||
1-001-1-02=-1=100 | ||
1=11-210-02 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
1=-0-2 | ||
12111 | ||
2=0= | ||
21 | ||
2=01 | ||
111 | ||
20012 | ||
112 | ||
1=-1= | ||
1-12 | ||
12 | ||
1= | ||
122 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
const { assert } = require('console'); | ||
const fs = require('fs'); | ||
|
||
const smallTest = [ | ||
[1, '1'], | ||
[2, '2'], | ||
[3, '1='], | ||
[4, '1-'], | ||
[5, '10'], | ||
[6, '11'], | ||
[7, '12'], | ||
[8, '2='], | ||
[9, '2-'], | ||
[10, '20'], | ||
[15, '1=0'], | ||
[20, '1-0'], | ||
[2022, '1=11-2'], | ||
[12345, '1-0---0'], | ||
[314159265, '1121-1110-1=0'], | ||
[1747, '1=-0-2'], | ||
[906, '12111'], | ||
[198, '2=0='], | ||
[11, '21'], | ||
[201, '2=01'], | ||
[31, '111'], | ||
[1257, '20012'], | ||
[32, '112'], | ||
[353, '1=-1='], | ||
[107, '1-12'], | ||
[7, '12'], | ||
[3, '1='], | ||
[37, '122'], | ||
]; | ||
|
||
day25a = () => { | ||
// const filename = './data/day25test.txt'; // 4890 => 2=-1=0 | ||
const filename = './data/day25.txt'; // 33658310202841 2--1=0=-210-1=00=-=1 | ||
let arr = fs.readFileSync(filename, { encoding: 'utf8', flag: 'r' }).split('\n'); | ||
|
||
// test decode | ||
for ([decimal, snafu] of smallTest) { | ||
assert(decode(snafu) === decimal); | ||
} | ||
|
||
// test encode | ||
for ([decimal, snafu] of smallTest) { | ||
assert(encode(decimal) === snafu); | ||
} | ||
|
||
let snafuDecimal = 0; | ||
for (let snafu of arr) { | ||
snafuDecimal += decode(snafu); | ||
} | ||
|
||
let answer = encode(snafuDecimal); | ||
|
||
console.log('day25a = ', snafuDecimal, answer); | ||
}; | ||
|
||
day25b = () => { | ||
|
||
} | ||
|
||
const decode = (snafuStr) => { | ||
const snafu = snafuStr.split(''); | ||
let power = snafu.length - 1; | ||
let result = 0; | ||
while (snafu.length > 0) { | ||
const item = snafu.shift(); | ||
switch (item) { | ||
case '-': | ||
result -= 1 * Math.pow(5, power); | ||
break; | ||
case '=': | ||
result -= 2 * Math.pow(5, power); | ||
break; | ||
default: | ||
result += +item * Math.pow(5, power); | ||
break; | ||
} | ||
power -= 1; | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
const encode = (decimalStr) => { | ||
let decimal = +decimalStr; | ||
let intVal = -1; | ||
let remainder = 0; | ||
|
||
// step 1. generate decimal equilavent. | ||
let snafuArr = []; | ||
while (decimal > 0) { | ||
intVal = Math.floor(decimal / 5); | ||
remainder = decimal % 5; | ||
decimal = intVal; | ||
snafuArr.unshift(remainder); | ||
} | ||
// pad an extra 0 at the end. | ||
snafuArr.unshift(0); | ||
|
||
// step 2. encode 3 and 4. | ||
let pos = snafuArr.length - 1; | ||
while (pos >= 0) { | ||
const digit = snafuArr[pos]; | ||
switch (digit) { | ||
case 3: | ||
snafuArr[pos] = '='; | ||
snafuArr[pos - 1] = snafuArr[pos - 1] + 1; | ||
break; | ||
case 4: | ||
snafuArr[pos] = '-'; | ||
snafuArr[pos - 1] = snafuArr[pos - 1] + 1; | ||
break; | ||
case 5: | ||
snafuArr[pos] = 0; | ||
snafuArr[pos - 1] = snafuArr[pos - 1] + 1; | ||
break; | ||
default: | ||
break; | ||
} | ||
pos -= 1; | ||
} | ||
|
||
if (snafuArr[0] === 0) snafuArr = snafuArr.slice(1); | ||
return snafuArr.join(''); | ||
}; | ||
|
||
module.exports = { day25a, day25b }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters