DEV Community

Discussion on: Advent of Code 2020 Solution Megathread - Day 6: Custom Customs

Collapse
 
particleflux profile image
Stefan Linke

2 Solutions again.

Go:

package main

import (
    "bytes"
    "fmt"
    "io/ioutil"
    "os"
)

func countQuestions(group []byte) (int, int) {
    counts := map[byte]int{}
    numPeople := 1
    for _, question := range group {
        if question == '\n' {
            numPeople++
        } else {
            counts[question]++
        }
    }

    numAllYes := 0
    for _, c := range counts {
        if c == numPeople {
            numAllYes++
        }
    }

    return len(counts), numAllYes
}

func main() {
    input, _ := ioutil.ReadAll(os.Stdin)
    groups := bytes.Split(input, []byte("\n\n"))

    sum1, sum2 := 0, 0
    for _, group := range groups {
        a, b := countQuestions(group)
        sum1 += a
        sum2 += b
    }

    fmt.Println(sum1, sum2)
}
Enter fullscreen mode Exit fullscreen mode

And tweet-sized PHP:

<?for($i=0,$z=explode("

",file_get_contents('input'));$z[$i];){$a+=count($f=count_chars($z[$i++],1))-(($n=$f[10])>0);foreach($f as$k=>$v)$b+=$k!=10&$v==$n+1;}echo"$a $b";
Enter fullscreen mode Exit fullscreen mode