From 5f5cff83371cb1c4f213de55147eb9646c444b8d Mon Sep 17 00:00:00 2001 From: gergo Date: Sun, 25 Dec 2022 10:09:58 +0100 Subject: [PATCH] day 25 --- src/main/kotlin/me/gergo/Aoc25.kt | 40 +++++++++++ src/main/resources/input25.txt | 108 ++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 src/main/kotlin/me/gergo/Aoc25.kt create mode 100644 src/main/resources/input25.txt diff --git a/src/main/kotlin/me/gergo/Aoc25.kt b/src/main/kotlin/me/gergo/Aoc25.kt new file mode 100644 index 0000000..6a516bf --- /dev/null +++ b/src/main/kotlin/me/gergo/Aoc25.kt @@ -0,0 +1,40 @@ +package me.gergo + +import java.io.File +import kotlin.math.pow + +fun main() { + val snafuNumbers = File("src/main/resources/input25.txt").readLines() + + val result1 = decimalToSnafu(snafuNumbers.map(::snafuToDecimal).sum()) + + println("SNAFU number to supply to the console: $result1") +} + +private fun snafuToDecimal(value: String): Long { + var result = 0L + for (i in value.indices) { + val v = when (value[value.length - 1 - i]) { + '=' -> -2 + '-' -> -1 + '0' -> 0 + '1' -> 1 + '2' -> 2 + else -> throw NumberFormatException(value) + } + result += 5.0.pow(i).toLong() * v + } + return result +} + +private const val Digits = "=-012" +private fun decimalToSnafu(value: Long): String { + var result = "" + var rem = value + while (rem > 0) { + rem += 2 // Shift because of the negative values + result = "${Digits[(rem % 5).toInt()]}$result" + rem /= 5 + } + return result +} \ No newline at end of file diff --git a/src/main/resources/input25.txt b/src/main/resources/input25.txt new file mode 100644 index 0000000..b5d3df6 --- /dev/null +++ b/src/main/resources/input25.txt @@ -0,0 +1,108 @@ +2=11 +22=01-- +1=2=0-111-0 +1=121=--0 +222==1=2001=21121= +2-22221 +1==--1012=11- +10-==111--001 +2102=20-012-20 +11-==002 +1=1=-0= +21-1-=1111-- +10201==202=0-22=2-2 +2--10==11211111 +10==--0=2=220 +1-0210-0=2 +11--1=-=201-22== +210=102- +1=1121-=2222001 +110201=2-==22 +1=020=-2020111 +1-1-0-=022-01120 +2==01-2111210= +1=0---1=222111= +1=22=--20= +202--0--1=100 +12--2-01--2222=1 +1==-==2 +12111 +1-12 +22 +2-0-0=-=00=220--1= +1=-210=02-11= +21=2-01-11-02--02 +10-2-20=01-01-= +1=-11=2-1==-1=-11= +1211--22110 +1020 +1121==2002=== +2=1-02= +1- +2=11221-2101 +110-0--11 +1112---11- +1---=-2 +1022=0121==--02 +1=2-==2 +2-=010-02=2- +11-00-2=2=2202 +1=12-1-0==1-- +2-==0=2 +1-=-0=-0--= +201122--1220= +1=---002 +2=2=210012--12 +1==0=2= +201=1==-=1120122 +2=02000 +1=02==--=1100=- +1=-01=2-21-=-10 +1=211 +1-10=101-00=021=1 +2-0 +1--=01201200010== +1-1020--=-=11==1 +2=1= +1=- +1-10202=0=12-1 +100 +2--1=0-0-10 +2122=12 +1-0-=-=0=21202 +10111 +2200112-122=2=0-=1 +2--10-22=110==2-000 +1=1-010-1=11-000020- +1=2=---22 +1= +12-0== +1=0 +1==0==-=1=2-- +1--211-00-2= +1--0 +2=-0 +1-10=---=-=1= +2=1 +1=-0=021-20 +201=01= +1=0==1 +1---0212-= +1==21-=121= +21-=1020- +1002-102=2-212=0-0 +22-=--20 +22= +10-=-=1-2= +1--=-0 +102=000021 +10=== +100100-02 +2-=00=20=12=- +1=2=01=02 +1=101---0== +1-=1001--=22-=00 +11=21220-111011- +20 +2=2-=-22-=21-1210 +1=1110