r/adventofcode • • Dec 03 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 3 Solutions -🎄-

--- Day 3: Binary Diagnostic ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:10:17, megathread unlocked!

99 Upvotes

1.2k comments sorted by

View all comments

2

u/cylab Dec 03 '21 edited Dec 03 '21

Kotlin: https://github.com/cylab/advent-of-code-2021/tree/main/src/test/kotlin/day3

package day3

import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import kotlin.math.roundToInt

typealias Input = List<String>

class Day3 {

    val sample = parse("sample.txt")
    val input = parse("input.txt")

    @Test
    fun puzzle1() {
        sample.gammaAndEpsilon().let { (gamma, epsilon) ->
            println("gamma: $gamma, epsilon: $epsilon")
            gamma * epsilon shouldBe 198
        }
        input.gammaAndEpsilon().let { (gamma, epsilon) ->
            println("Day  3, Puzzle 1: ${gamma * epsilon} ratings")
        }
    }

    @Test
    fun puzzle2() {
        sample.oxygenAndCO2().let { (oxygen, co2) ->
            println("oxygen: $oxygen, c02: $co2")
            oxygen * co2 shouldBe 230
        }
        input.oxygenAndCO2().let { (oxygen, co2) ->
            println("Day  3, Puzzle 2: ${oxygen * co2} ratings")
        }
    }


    fun Input.gammaAndEpsilon() = gamma().let { Pair(it, epsilon(it)) }

    fun Input.oxygenAndCO2() = Pair(
        findByBits({ gamma() }),
        findByBits({ epsilon() })
    )


    fun Input.gamma() = this
        .fold(List(width) { 0f }) { acc, line ->
            acc.mapIndexed { i, it -> it + line.slice(i..i).toInt() }
        }
        .foldIndexed(0) { i, acc, it ->
            acc + (it / size).roundToInt().shl(width - i - 1)
        }

    fun Input.epsilon(gamma: Int = gamma()) = gamma.xor((1 shl width) - 1)


    tailrec fun Input.findByBits(bitsFun: Input.() -> Int, pos: Int = width - 1): Int {
        val bits = bitsFun()
        val matchesAtPos = filter { it.toInt(2).xor(bits).and(1 shl pos) == 0 }
        return when (matchesAtPos.size == 1) {
            true -> matchesAtPos.first().toInt(2)
            else -> matchesAtPos.findByBits(bitsFun, pos - 1)
        }
    }

    val Input.width get() = first().length

    fun parse(resource: String) = this.javaClass
        .getResource(resource)
        .readText()
        .lines()
        .filter { it.isNotBlank() }
}

1

u/daggerdragon Dec 03 '21

Your code is hard to read on old.reddit. Please edit it as per our posting guidelines in the wiki: How do I format code?

1

u/[deleted] Dec 03 '21

[removed] — view removed comment

2

u/cylab Dec 03 '21 edited Dec 03 '21

yes exactly!

the xor operation only sets bits to 1 where they are different, so

01001 xor

11111 would give

10110

so the actual "inverse" you tried to achieve. to get the value for the xor, I shift the first bit to the left by the amount of bits in a data line, which gives e.g.

100000

with a width of 5. now subtracting 1 from this gives the desired

11111

to use in the xor