r/adventofcode • • Dec 06 '16

SOLUTION MEGATHREAD --- 2016 Day 6 Solutions ---

--- Day 6: Signals and Noise ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).


T_PAAMAYIM_NEKUDOTAYIM IS MANDATORY [?]

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

edit: Leaderboard capped, thread unlocked!

9 Upvotes

222 comments sorted by

•

u/topaz2078 (AoC creator) Dec 06 '16

I hope everyone has been enjoying Advent of Code! Today ends the warmup puzzles. It's uphill from here!

8

u/Quick_Question404 Dec 06 '16

Wait, those were the warmup puzzles?!

... Welp, I'm screwed.

→ More replies (5)

6

u/segfaultvicta Dec 06 '16

hype intensifies

1

u/daggerdragon Dec 06 '16

CODERS! TO THE LINE!

READY!

AIM!

WAIT FOR IT...

CODE!

→ More replies (1)

4

u/pedrosorio Dec 06 '16 edited Dec 06 '16

Today ends the warmup puzzles

P.S.: Thanks for today's reference. I have been laughing for a while reading this: https://philsturgeon.uk/php/2013/09/09/t-paamayim-nekudotayim-v-sanity/

2

u/[deleted] Dec 06 '16

:gasp:

→ More replies (5)

24

u/Mark-Simulacrum Dec 06 '16

Some simple bash scripting, where "t" is the file with the input.

Part 1:

for i in {1..8}; do cat t | cut -c$i | sort | uniq -c | sort | tail -n1 | awk '{print $2}' ; done | tr -d '\n'

Part 2:

for i in {1..8}; do cat t | cut -c$i | sort | uniq -c | sort | head -n1 | awk '{print $2}' ; done | tr -d '\n'

6

u/jwstone Dec 06 '16

now there is a solution i can read/understand!

2

u/topaz2078 (AoC creator) Dec 06 '16

Well done!

→ More replies (1)

16

u/John_Earnest Dec 06 '16 edited Dec 06 '16

Well damn, I guess I'm back on the leaderboard. K is exceedingly well-suited to this type of problem.

l: 0: "../../Desktop/Advent/06.in"
(*>#:'=:)'+l  / part 1
(*<#:'=:)'+l  / part 2

The input is a list of strings, which can also be thought of as a matrix of characters. Each column of the input simply needs to be processed in isolation, so we'll apply an expression to each of the transpose of this matrix: (...)'+l.

The rest of the solution is a common(ish) idiom for calculating the most or least frequent item of a vector. Right to left, group items (=), take the count of each set of indices (#:'), grade up or down (< or >- grading a dictionary sorts keys by their values) and then take the first result (*).

 x
"cdeabdccdceaabbbebad"

 =x
"cdeab"!(0 6 7 9
 1 5 8 19
 2 10 16
 3 11 12 18
 4 13 14 15 17)

 #:'=x
"cdeab"!4 4 3 4 5

 <#:'=x
"ecdab"

 >#:'=x
"bcdae"

 *>#:'=x
"b"

Read left to right, the first solution is "The greatest of the count of each group of each of the transpose of l":

(*>#:'=:)'+l

The main thing to notice is that while K doesn't have any magic builtins which trivialize this specific problem, you can compose its primitive operators nicely in all sorts of useful ways to arrive at concise solutions.

3

u/jwstone Dec 06 '16

to a person who can't read K, that looks really neat, and i am grateful for the explanation =)

3

u/John_Earnest Dec 06 '16

I'm more than happy to share what I know about K. I have an enormous amount of fun programming with it. Here is a nice short primer that outlines some of the major features if I've piqued your curiosity to learn more. I also have a browser based interpreter available for immediate tinkering. It's a bit slow and buggy compared to the real thing, but good enough for solving AoC puzzles!

→ More replies (1)
→ More replies (3)

2

u/Godspiral Dec 06 '16

gz, arthur witney must have slept in :P

→ More replies (1)

2

u/[deleted] Dec 06 '16

Thank you for the explanation, I've always found J and K so interesting, but I haven't been able to wrap my head around it thus far :)

→ More replies (1)

14

u/[deleted] Dec 06 '16

Python:

from collections import Counter

with open('input.txt') as f:
    s = f.read().strip()
# Part 1
print(''.join(Counter(x).most_common()[0][0] for x in zip(*s.split('\n'))))
# Part 2
print(''.join(Counter(x).most_common()[-1][0] for x in zip(*s.split('\n'))))

3

u/kryptn Dec 06 '16

We had pretty much the same solution.

from collections import Counter

with open('input.txt') as fd:
        data = fd.read()

data_counted = [Counter(x).most_common() for x in zip(*data.splitlines())]

print('first star: {}'.format(''.join(x[0][0] for x in data_counted)))
print('second star: {}'.format(''.join(x[-1][0] for x in data_counted)))
→ More replies (1)

2

u/BumpitySnook Dec 06 '16

TIL about collections. I just open-coded mine.

2

u/h0razon Dec 06 '16

Damn, didn't know collections.Counter... :(

→ More replies (8)

7

u/Godspiral Dec 06 '16 edited Dec 06 '16

finally a high-ish spot (29-30). Secret, think/verify less submit faster. in J,

 a =. > cutLF wdclippaste ''
 {."1 (~. \: #/.~)"1 |: a  NB. p1
 {."1 (~. /: #/.~)"1 |: a  NB. p2

would have been faster if I hadn't second guessed "I wonder what the tie breaker rule might be?"

A bit similar to u/John_Earnest 's K solution, J is same family of languages.

a is a matrix of input.
|: transposes it.
"1 operates by rows
#/.~ frequency/"keyed" count of each char
\: grade down. often used to sort itself (with \:~) but can sort any other list too.
~. nub. unique list of chars in order of first appearance.
{."1 head by row.

(~. /: #/.~) is a fork. 3 verb phrases where the outer 2 access the argument(s if there is a left one as well), and the middle uses the other 2 results as arguments.

So: use keyed count to grade the nub of each row of the transposed input.

→ More replies (5)

6

u/jwstone Dec 06 '16

2

u/sowpods Dec 06 '16

more "more SQL" because why not

drop table if exists santa;
with puzzle_input as (select regexp_split_to_table('eedadn
drvtee
eandsr
raavrd
atevrs
tsrnev
sdttsa
rasrtv
nssdts
ntnada
svetve
tesnvt
vntsnd
vrdear
dvrsen
enarar', E'\n') row_n)



select generate_series, letter, count(*) as letter_count
into temp santa
from (select *, substring(row_n from generate_series for 1) as letter

from puzzle_input
cross join generate_series(1, (select length(row_n) from puzzle_input limit 1)))a
group by 1, 2
order by 1
;


select string_agg(s1.letter, '')
from santa s1
left join santa s2 on s2.generate_series = s1.generate_series
	and s2.letter_count < s1.letter_count
where s2.letter is null
→ More replies (3)

6

u/tterrag1098 Dec 06 '16

I'm that crazy guy doing this in Java:

public static void main(String[] args) throws IOException {
    List<String> lines = Files.readAllLines(Paths.get("day6.txt"));
    Map<Integer, Map<Character, Integer>> counts = new HashMap<>();

    for (String string : lines) {
        char[] chars = string.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            counts.putIfAbsent(i, new HashMap<>());
            counts.get(i).compute(chars[i], (c, val) -> val == null ? 1 : val + 1); 
        }
    }
    
    char[] maxchars = new char[8], minchars = new char[8];
    for (int i = 0; i < maxchars.length; i++) {
        List<Character> sorted = counts.get(i).entrySet().stream().sorted(Entry.comparingByValue()).map(Entry::getKey).collect(Collectors.toList());
        minchars[i] = sorted.get(sorted.size() - 1);
        maxchars[i] = sorted.get(0);
    }
    
    System.out.println("Part 1: " + new String(maxchars));
    System.out.println("Part 2: " + new String(minchars));
}

I must say this year has inspired me to learn a language like K or J, the speed with which they tackle these problems is impressive. Bravo to those who can figure them out.

4

u/Quick_Question404 Dec 06 '16

At least Java is good with tokenizing and handling strings relatively quickly. I'm one of the crazies tackling this in C. Low-Level is best level!

Nice code though. Simple to read, and easy to understand.

→ More replies (5)
→ More replies (7)

5

u/fatpollo Dec 06 '16
import collections

ans1, ans2 = '', ''
with open('06.txt') as fp:
    for stuff in zip(*fp.read().strip().split('\n')):
        counter = collections.Counter(stuff).most_common()
        ans1 += counter[0][0]
        ans2 += counter[-1][0]
print(ans1)
print(ans2)

6

u/fatpollo Dec 06 '16

one could even get cute and do something like

    (h, x), *body, (t, y) = collections.Counter(stuff).most_common()
    ans1 += h
    ans2 += t
→ More replies (1)

5

u/malisper Dec 06 '16

One line of Scala:

io.Source.fromFile(inputFile).getLines.toArray.map(_.toArray).transpose.map(_.groupBy(identity).mapValues(_.length).minBy(_._2)._1).mkString
→ More replies (2)

5

u/porphyro Dec 06 '16

Mathematica/Wolfram Language

input = StringSplit[Import[NotebookDirectory[] <> "input6.txt"],"\n"]

Commonest /@ Transpose@Characters@input

MinimalBy[#, #[[2]] &] & /@ (Tally/@Transpose@Characters@input)

4

u/Quick_Question404 Dec 06 '16 edited Dec 06 '16

Did anyone get solutions that made any sort of sense? Mine were along the line of batwpask and cyxeoccr, which cost me a higher score cause I thought I went wrong somewhere.

EDIT: So it seems like the meaning behind this challenge is: Repetition Code sucks? Or Santa has a secret code on an entirely different level.

10

u/topaz2078 (AoC creator) Dec 06 '16

As an extra challenge, build a script that takes two equal-length words and produces an input which returns one word for part 1 and the other for part 2. (with a sufficiently long input, with making it hard to tell what the word is without decoding it, with making every letter in the alphabet appear in every column at least once, etc etc)

4

u/bildzeitung Dec 06 '16

Same -- nothing that seems to make sense. But I was like "test data passes... damn the torpedoes, full speed ahead!" & dropped the sol' s into the box.

No leaderboard for me -- too old, too slow, but still fun!

3

u/artesea Dec 06 '16

This is what threw me, code passed the example but the result wasn't clean. Only after throwing out some debug text was I confident to submit an answer. Then when I saw part 2 I assumed "that's why part 1 was weird", only to get part 2 to also come back odd.

2

u/pedrosorio Dec 06 '16

Same here, specially in the second part.

→ More replies (1)

3

u/tdecker91 Dec 06 '16 edited Dec 06 '16

Scala Solution

import scala.collection.mutable.ListBuffer

object Day6 {
  def main(args: Array[String]): Unit = {
    val input = scala.io.Source.fromFile("input6.txt").mkString

    var mostCommon = new ListBuffer[Char]()
    var leastCommon = new ListBuffer[Char]()

    input.split('\n').map(_.toCharArray).transpose.map((column) => {
      column.groupBy(identity).mapValues(_.size)
    }).map((c) => {
      leastCommon +=  c.minBy(_._2)._1
      mostCommon += c.maxBy(_._2)._1
    })

    println(mostCommon.mkString(""), leastCommon.mkString(""))
  }
}

3

u/xkufix Dec 06 '16

Nice. I think all Scala solutions look quite similar today.

Just one thing. Instead of doing mkString on the file and then splitting on \n, just use getLines, which returns an Iterator[String].

→ More replies (2)

3

u/[deleted] Dec 06 '16

Haskell:

import Control.Arrow ((&&&))
import Data.List (group, sort, transpose)

count :: String -> [(Int, Char)]
count = sort . map (length &&& head) . group . sort

part1 :: String -> String
part1 = map (snd . last . count) . transpose . lines

part2 :: String -> String
part2 = map (snd . head . count) . transpose . lines

main = do
  input <- readFile "input.txt"
  putStrLn $ part1 input
  putStrLn $ part2 input

3

u/hodeer Dec 06 '16 edited Dec 06 '16

in python just use min or max depending on the part 1/2

def main():
    text_file = open(destination, "r")
    codeList = [x for x in text_file.read().split()]
    text_file.close()
    codeListB = [x for x in list(zip(*codeList))]
    for x in codeListB:
        decodedList = sorted(list(([y for y in x])))
        common = min(set(decodedList), key=decodedList.count)
        print(chr(common))
main()

EDIT: refined from the work in progress version to the final

→ More replies (2)

2

u/haoformayor Dec 06 '16

~~haskell~~

Fairly simple composition of transpose and maximumBy today. As always, it was fast to use Emacs/vim/anything-with-regular-expressions to convert the input into code. Should've gone with a shell one-liner, though, darn.

#!/usr/bin/env stack
-- stack --resolver lts-6.26 --install-ghc runghc --package base-prelude    
{-# LANGUAGE NoImplicitPrelude #-}
module D6 where
import BasePrelude
import D6Input

main =
  print ( solution1 example
        , solution1 input
        , solution2 example
        , solution2 input)
  where
    solution1 input = map most (transpose input)
    solution2 input = map least (transpose input)
    most xs         = argmax (count xs) xs
    least xs        = argmax (negate . count xs) xs
    count xs x      = length . filter (== x) $ xs
    argmax f xs     = maximumBy (comparing f) xs

4

u/bdtddt Dec 06 '16

Would input <- lines <$> readFile "input.txt" not be even faster than transforming the input into code?

→ More replies (2)
→ More replies (4)

2

u/Deckard666 Dec 06 '16 edited Dec 08 '16

In Rust: Link

This challenge was pretty straightforward.

2

u/_Le1_ Dec 06 '16 edited Dec 06 '16

My C# code:

 static void Main(string[] args)
    {
        part1_2();
        Console.ReadLine();
    }

    static void part1_2()
    {
        string text1="";
        string text2 = "";
        string[] input = File.ReadAllLines(@"input.txt");

        for (int i = 0; i < input[0].Length; i++)
        {
            List<string> lst = new List<string>();
            foreach (var line in input)
            {
                lst.Add(line[i].ToString());
            }
            
            text1 += lst.GroupBy(c => c).Select(g => new { g.Key, Count = g.Count() }).OrderByDescending(x => x.Count).ThenBy(x => x.Key).Take(1).Select(x => x.Key).ToArray()[0];
            text2 += lst.GroupBy(c => c).Select(g => new { g.Key, Count = g.Count() }).OrderBy(x => x.Count).ThenBy(x => x.Key).Take(1).Select(x => x.Key).ToArray()[0];
        }

        Console.WriteLine("Part1: {0}", text1);
        Console.WriteLine("Part2: {0}", text2);
    }

2

u/PangolinRex Dec 06 '16

Hah, whoops. Lost some time there trying to figure out how I'd screwed up because I expected the answer(s) to be comprehensible

2

u/Quick_Question404 Dec 06 '16

Hey everyone! This is my take at the problem in C. Placed around ~450s on both parts. Personally I blame it on the answers. My answers didn't resemble any words, so I thought my code was messing up somewhere. What do you guys think?

https://github.com/HighTide1/adventofcode2016/tree/master/06

→ More replies (1)

2

u/Trolly-bus Dec 06 '16

It's so goddamn hard to get on the leaderboard. I thought I was really fast for this one. Here's my code in Python. Change max to min for part 2.

import operator

def part1(puzzle_input):
input_list = puzzle_input.split("\n")
position_list = []
for i in range(8):
    position_list.append({"a": 0, "b": 0, "c": 0, "d": 0, "e": 0, "f": 0, "g": 0, "h": 0, "i": 0, "j": 0, "k": 0,
                          "l": 0, "m": 0, "n": 0, "o": 0, "p": 0, "q": 0, "r": 0, "s": 0, "t": 0, "u": 0,
                          "v": 0, "w": 0, "x": 0, "y": 0, "z": 0})
for input_line in input_list:
    for character_index, character in enumerate(input_line):
        position_list[character_index][character] += 1
for i in position_list:
    print(max(i.items(), key=operator.itemgetter(1)))

4

u/gerikson Dec 06 '16

I wouldn't say this was trivially easy but it was among the easiest problem so far.

3

u/Aneurysm9 Dec 06 '16

If it's like last year, this is the calm before the storm.

→ More replies (3)

2

u/catcint0s Dec 06 '16

Could have used defaultdict(int) instead to avoid lots of typing.

2

u/fixed_carbon Dec 06 '16

Ruby. chunk seems to be getting a lot of exercise in these problems.

INPUTFILE = 'input.txt'

inp = File.readlines(INPUTFILE).map{|s| s.strip.chars}.transpose

# Part One
puts inp.map{|line| line.sort.chunk{|b| b}.sort_by{|el| el.last.size}.last.first}.join

# Part Two
puts inp.map{|line| line.sort.chunk{|b| b}.sort_by{|el| el.last.size}.first.first}.join

2

u/h0razon Dec 06 '16 edited Dec 06 '16

One line Python2:

print(', '.join([''.join([(func)([{'l': position.count(x), 'c': x} for x in position], key=lambda y: y['l'])['c'] for position in zip(*open('input.txt', 'r').read().rstrip().split('\n'))]) for func in [max, min]]))

*Edit: removed the "part" parameter

2

u/omnster Dec 06 '16

Mathematica

input = Import[ NotebookDirectory[] <> "input/input_06.txt"] // 
   StringSplit ;

(sorted = 
    Characters@input // Transpose // 
     Map[ SortBy[ Tally@#, { - # [[2 ]] , # [[1 ]]} &] &, #, 
       1] &  ) // # [[All, 1, 1 ]] & // StringJoin
StringJoin[ sorted [[All, -1, 1 ]]]

2

u/NeilNjae Dec 06 '16

Another Haskell solution (https://git.njae.me.uk/?p=advent-of-code-16.git;a=blob;f=advent06.hs). Spent a bit of time trying to make it more idiomatic.

module Main(main) where

import Data.List (transpose, maximum, minimum, sort, group)
import Data.Tuple (swap)

main :: IO ()
main = do 
    text <- readFile "advent06.txt" 
    let message = lines text
    part1 message
    part2 message

part1 :: [String] -> IO ()
part1 message = do 
    putStrLn $ map (snd . maximum . counts) $ transpose message

part2 :: [String] -> IO ()
part2 message = do 
    putStrLn $ map (snd . minimum . counts) $ transpose message

counts :: (Eq a, Ord a) => [a] -> [(Int, a)]
counts = map (\g -> (length g, head g)) . group . sort

2

u/pyow_pyow Dec 06 '16

Nice. TIL about the Ord instance for (,) that enables maximum and minimum to work on tuples.

I was originally using (head &&& length) but flipped it around after I saw your solution. Resulting code: http://lpaste.net/5219590922089529344

2

u/NeilNjae Dec 06 '16

Thanks! Control.Arrow is something I need to learn too!

2

u/Gummoz Dec 06 '16

Ugly Powershell!

Part one:

[array]$tempArray = "","","","","","","","","","",""
"asdasd" -split '\n' | % {for ($i = 0; $i -lt $_.ToChararray().length; $i++) {[array]$tempArray[$i] += [string]$_.ToCharArray()[$i]}}

$answer = $null
$tempArray | %{ $_ | Sort-Object | Group-Object | Sort-Object -Property Count -Descending | Select-Object name -First 1 | % {[string]$answer += [string]$_.name} }
$answer

Part two:

[array]$tempArray = "","","","","","","","","","",""
"asdasd" -split '\n' | % {for ($i = 0; $i -lt $_.ToChararray().length; $i++) {[array]$tempArray[$i] += [string]$_.ToCharArray()[$i]}}

$answer = $null
$tempArray | %{ $_ | Sort-Object | Group-Object | Sort-Object -Property Count -Descending | Select-Object name -Last 2 | % {[string]$answer += [string]$_.name} }
$answer

2

u/tg-9000 Dec 06 '16

Here is my solution in Kotlin. I could probably combine the bulk of parts one and two and just change the min/max part, but I need to get other work done so I'll try that later.

Solutions and tests for all days so far can be found in my GitHub repo. I'm just learning Kotlin, so I welcome all feedback!

class Day06(val input: List<String>) {

    fun solvePart1(): String =
        (0 until input[0].length)
            .map { i -> input.map { it[i] } }
            .map { it.groupBy { it }.maxBy { it.value.size }?.key ?: ' ' }
            .joinToString(separator = "")

    fun solvePart2(): String =
        (0 until input[0].length)
            .map { i -> input.map { it[i] } }
            .map { it.groupBy { it }.minBy { it.value.size }?.key ?: ' ' }
            .joinToString(separator = "")
}

2

u/Borkdude Dec 06 '16

Clojure!

(ns day6
  (:require [clojure.string :as str]
            [clojure.java.io :as io]))

(def input (-> "day6.txt"
               io/resource
               slurp
               str/trim
               str/split-lines))

(def columns (apply map vector input))
    
;; first answer
(map #(->> %
           frequencies
           (sort-by val >)
           ffirst) columns) ;;=> (\u \m \e \j \z \g \d \w)

;; second answer
(map #(->> %
           frequencies
           (sort-by val)
           ffirst) columns) ;;=> (\a \o \v \u \e \a \k \v)

Code on Github: https://github.com/borkdude/aoc2016

1

u/amalloy Dec 06 '16

You keep declining to use line-seq. Also, try min-key and max-key instead of sorting the whole list.

→ More replies (6)

2

u/Jiggins_ Dec 06 '16

A Haskell solution without sorting the input:

module Main where

import Data.Ord (comparing)
import Data.List (maximumBy, minimumBy)
import Data.Map (Map)
import qualified Data.Map as Map

buildMap :: [String] -> [Map Char Int]
buildMap = foldr insertWord $ replicate 8 Map.empty

insertWord :: Ord k => [k] -> [Map k Int] -> [Map k Int]
insertWord = zipWith (\char map -> Map.insertWith (const (+1)) char 1 map)

maxPair, minPair :: (Ord b, Foldable t) => t (c, b) -> c
maxPair = fst . maximumBy (comparing snd)
minPair = fst . minimumBy (comparing snd)

-- Part 1
main = getContents >>= mapM_ (putChar . maxPair . Map.toList) . buildMap . lines >> putChar '\n'

-- Part 2
main = getContents >>= mapM_ (putChar . minPair . Map.toList) . buildMap . lines >> putChar '\n'

1

u/TheMuffinMan616 Dec 06 '16

Python 3:

from collections import Counter

def frequencies(words):
    return (Counter(x).most_common() for x in zip(*words))

def part1(input):
    return "".join(x[0][0] for x in frequencies(input))

def part2(input):
    return "".join(x[-1][0] for x in frequencies(input))

def day6(input):
    return part1(input), part2(input)

input = open("../input.txt").read()
input = [x.strip() for x in input.split("\n")]
print(day6(input))

1

u/andars_ Dec 06 '16

Ruby.

positions = []
10.times do |i|
  positions << Hash.new(0)
end

File.open(filename).each_line do |line|
  chars = line.split('')
  chars.each_with_index do |c, i|
    positions[i][c] += 1
  end
end

puts "Part 1:"
puts positions.map { |pos|
  k,_ = pos.max_by{|k,v| v}
  k
}.join

puts "Part 2:"
puts positions.map { |pos|
  k,_ = pos.min_by{|k,v| v}
  k
}.join

1

u/1wheel Dec 06 '16
var d3 = require('d3')
var _ = require('lodash')

var lines = require('fs').readFileSync('06-input', 'utf-8').split('\n')

var out = d3.range(8).map(function(i){
  var chars = lines.map(d => d[i])
  var byChar = d3.nest().key(d => d).entries(chars)

  return _.sortBy(byChar, d => d.values.length)[0].key
})

console.log(out.join(''))

d3 is a bit of overkill, quicker than writing a reducer though...

1

u/Tokebluff Dec 06 '16

C# solution. I knew from the start that I couldn't get on the leaderboard with this language /sad

https://github.com/Tokebluff/AdventOfCode2016/blob/master/Advent%20of%20code%202016/Advent%20of%20code%202016/Day6.cs

1

u/BafTac Dec 06 '16

I thought the same with C++. Still, I got rank 105 and I definitely wasted a few seconds here and there, so it would have been possible for me to end on the leaderboard..

1

u/IcyHammer Dec 06 '16

While I absolutely love C#, it's just not the right tool for those tasks. I also chose C# because I have lots of exp with it and from day one I realized I will never be able to compete with python, perl, bash, etc... Instead I'm giving myself other challenges like minimizing time or space complexity, simplifying the rules like here, where I didn't want to use matrices and so on. C# comes in play in bigger projects where you need to create complex and yet readable structures (for example games) and still utilize the power of garbage collection and to avoid time consuming pointer debugging. Performance wise it beats any scripting language and produces way less garbage. For performance critical parts you can still use pointers or run c++.

→ More replies (3)

1

u/dragonnards Dec 06 '16

in R

data = readLines("inputs/day06.txt")

commonChar <- function(data, col) {
  chars = unname(sapply(data, function(line) {
    strsplit(line, "")[[1]][col]
  }))
  counts = table(chars)
  max = max(counts)
  names(counts)[max == counts]
}

sapply(1:8, function(i) commonChar(data, i))

1

u/wickerman999 Dec 06 '16

A bit more straight-forward solution of mine:

input1 <- read.fwf("day06_input_1.txt", widths = rep(1, 8), stringsAsFactors = F)
input_count <- apply(input1, 2, table)
paste0(rownames(input_count[apply(input_count, 2, which.max), ]), collapse = "")

You can even put it in one line (even though its messy to understand it that way):

paste0(rownames(input_count[apply(apply(read.fwf("day06_input_1.txt", widths = rep(1, 8), stringsAsFactors = F), 2, table), 2, which.max), ]), collapse = "")

For the second part use which.min() instead which.max()

→ More replies (1)

1

u/snorkl-the-dolphine Dec 06 '16

JavaScript / Node.js

const input = 'INPUT';
const a = input.split('\n').map(line => line.split(''));
let part1 = '';
let part2 = '';
for (let col = 0; col < a[0].length; col++) {
  const f = {};
  for (let row = 0; row < a.length; row++) {
    if (!f[a[row][col]]) f[a[row][col]] = 0;
    f[a[row][col]]++;
  }
  part1 += Object.keys(f).reduce((a, b) => f[a] > f[b] ? a : b); // Most common
  part2 += Object.keys(f).reduce((a, b) => f[a] < f[b] ? a : b); // Least common
}
console.log('Part 1:', part1);
console.log('Part 2:', part2);

2

u/AndrewGreenh Dec 06 '16

Lodash makes this one kinda cool :D

const _ = require('lodash');
const lines = require('../getInput')(6, 2016).trim().split('\n');

const getResult = funcName =>
  _(_.range(8)).map(i => _(lines).map(i).countBy().toPairs()[funcName](1)).map(0).join('');

console.log(['maxBy', 'minBy'].map(getResult));

1

u/doublehyphen Dec 06 '16

Ruby solution. I think it is pretty short and clean.

count = Hash.new { |h, k| h[k] = Hash.new(0) }
$stdin.each do |line|
  line.scan(/./).each_with_index { |c, i| count[i][c] += 1 }
end
puts count.values.map { |x| x.max_by { |_, n| n }[0] }.join

1

u/Ulyssesp Dec 06 '16

It's Haskell time! This was a similar transpose situation, then just some manipulation of list properties.

result = map (fst . maximumBy (\a b -> compare (snd b) (snd a)) . map (\as -> (head as, length as)) . group . sort) . transpose . splitOn "|"

1

u/cobbpg Dec 06 '16

I did pretty much the same thing, just using prelude combinators to make it a bit shorter:

solution6 = map (snd . maximumBy (comparing fst) . map (length &&& head) . group . sort) . transpose

1

u/_WhiteBoyWonder Dec 06 '16

Trying to use this as an opportunity to learn Go. Yesterday I learned about the ellipses operator, and today I learned more about slices and how to have an array of them.

It's not the best code, but I was trying to get something working out there. I can't believe how fast people finished today. This was by far my fastest day, so I am proud of myself.

Any critique is very much welcome! I am mostly winging it with go at the moment, trying to learn by example.

package main

import (
  "bufio"
  "fmt"
  "os"
)

func main() {
  fmt.Println("Day 6 of Advent of Code 2016")
  f, _ := os.Open("input")

  m := [8]map[byte]int{}
  for i := 0; i < 8; i++ { // init each index of m
    m[i] = make(map[byte]int)
  }

  scanner := bufio.NewScanner(f)
  for scanner.Scan() {
    line := scanner.Text()
    for i := 0; i < len(line); i++ {
      m[i][line[i]]++
    }
  }

  var text []byte
  var pt2_text []byte
  for i := 0; i < 8; i++ {
    text = append(text, get_extreme_key(m[i], func(x, y int) bool { return x > y }, -1))
    pt2_text = append(pt2_text, get_extreme_key(m[i], func(x, y int) bool { return x < y }, 1))
  }

  fmt.Printf("Part 1: %s\n", text)
  fmt.Printf("Part 2: %s\n", pt2_text)
}

func get_extreme_key(m map[byte]int, f func(x, y int) bool, parity int) byte {
  extreme := parity * 1000
  var key byte
  for k, v := range m {
    if f(v, extreme) {
      extreme = v
      key = k
    }
  }
  return key
}

1

u/_jonah Dec 06 '16

ramda:

pipe(
  transpose,
  map(pipe(sort(Array.sort), groupWith(equals), sortBy(length), head, head)),
  join(''),
  console.log
)(input().split("\n").map(split('')));

1

u/Philboyd_Studge Dec 06 '16

Java. This one was pretty similar to day 4, just frequency tables.

https://gist.github.com/anonymous/cad958d85c3363ca70db7bcd97f04d9b

1

u/JeffJankowski Dec 06 '16

Doing these in F# since I haven't touched this language since last Advent :P

let correct (grps : seq<seq<char>>) func = 
	grps
	|> Seq.map (fun grp ->
		grp
		|> Seq.groupBy id
		|> Seq.sortBy func
		|> Seq.head
		|> fst )
	|> String.Concat

let main argv = 
    let input = File.ReadLines("..\..\input.txt")
    let grps = 
        input
        |> Seq.map (fun s -> s.ToCharArray())
        |> Seq.concat
        |> Seq.mapi (fun i c -> i,c)
        |> Seq.groupBy (fun (i,_) -> i % 8)
        |> Seq.sortBy fst
        |> Seq.map (fun (_,chs) -> Seq.map snd chs)

    let corrected = correct grps (fun (_,chs) -> -Seq.length chs)
    Console.WriteLine("Corrected: " + corrected)
    let decoded = correct grps (fun (_,chs) -> Seq.length chs)
    Console.WriteLine("Decoded:   " + decoded)

1

u/WildCardJoker Dec 06 '16

C# part 1 and 2.

I was able to finish this one quicker than usual because a) I postponed my lunch break so I could work on it, and b) I remembered the major failure I had with Day 3 part 2 and wasn't going to make that mistake again.

Of course, I made different mistakes this time, but that's what makes programming fun!

1

u/Pyrobolser Dec 06 '16

I was really too long to produce my C# solution, but still, here it is :)

1

u/[deleted] Dec 06 '16

common code to build the lookup:

css=[];document.body.innerText.trim().split("\n").forEach((ss,i)=>{while(ss.length>css.length){css.push({})}ss.split("").forEach((s,j)=>{css[j][s]=(css[j][s]+1)||1})});

part1

ans="";css.forEach(cs=>{max_k=null,max_v=null;for(var c in cs){if(max_k===null||cs[c]>max_v){max_k=c,max_v=cs[c]}}ans+=max_k});ans;

part2

ans="";css.forEach(cs=>{min_k=null,min_v=null;for(var c in cs){if(min_k===null||cs[c]<min_v){min_k=c,min_v=cs[c]}}ans+=min_k});ans;

1

u/brantyr Dec 06 '16

Trying to learn ruby... apparently there are no do - while loops...

file = File.new("input06.txt","r")
frequencies = Array.new
line = file.gets
line.each_char do
	frequencies << Hash.new(0)
end

loop do
	for i in 0..(line.length-1) do
		frequencies[i][line[i]] += 1
	end
	break if !(line=file.gets)
end

frequencies.each do | current |
	freqArr = current.sort { |a,b| b[1]<=>a[1]}
	#print freqArr[0][0] # Part 1
	print freqArr.last[0] # Part 2
end

1

u/TheNiXXeD Dec 06 '16 edited Dec 06 '16

My JavaScript solution. Lodash is win. Change max to min for part 2.

i => _.unzip(i.map(_.values)).map(v => _.maxBy(v, c => v.join``.split(c).length)).join``

1

u/miran1 Dec 06 '16

python 3

all solutions here

day6 solution:

from collections import Counter

with open('./06 - Signals and Noise.txt', 'r') as infile:
    noise = infile.read().split('\n')

columns = (''.join(column) for column in zip(*noise))

first_solution = ''
second_solution = ''

for column in columns:
    (most, _), *others, (least, _) = Counter(column).most_common()
    first_solution += most
    second_solution += least

print("The message usually consists of the most frequent letters....")
print("Then it must be:", first_solution)
print("....")
print("Or is it the least frequent letters? I never know....")
print("It might be then", second_solution)

1

u/QshelTier Dec 06 '16

My solution in Kotlin:

package y2016

fun main(args: Array<String>) {
  println(first())
  println(second())
}

private fun first() = recombineLetters { it.maxBy { it.value }!! }
private fun second() = recombineLetters { it.minBy { it.value }!! }

private fun recombineLetters(sorter: (Map<Char, Int>) -> Map.Entry<Char, Int>) = getInput()
    .fold(mapOf<Int, List<Char>>()) { map, current ->
      map + current.toCharArray().mapIndexed { index, char ->
        index to map.getOrElse(index, { emptyList() }) + char
      }
    }
    .toSortedMap()
    .map {
      it.value.groupBy { it }
          .mapValues { it.value.size }
          .let(sorter).key
    }
    .joinToString("")

private fun getInput(day: Int = 6) = AllDays().javaClass.getResourceAsStream("day$day.txt")
    .reader()
    .readLines()

1

u/indienick Dec 06 '16

Solutions in Go!

1

u/d3adbeef123 Dec 06 '16

Fairly easy this one. My attempt in Clojure

(ns advent-of-code-2016.day6
  (:require [clojure.java.io :as io]
            [clojure.string :as str]))

(def input 
  (-> (slurp (io/resource "day6-input.txt"))
      (str/split #"\n")))

(defn get-max-freq [compare-fn xs]
  (->> (frequencies xs) (into []) 
       (sort-by second compare-fn) (take 1) (first)))

(defn solve [compare-fn]
  (->> (range 0 8)
       (map (fn [i] (->> (map #(nth % i) input)
                         (get-max-freq compare-fn)
                         (first))))
       (apply str)))

; part 1, part 2
(println (solve >) (solve <))

1

u/_8836 Dec 06 '16
(println (apply str (->> (slurp "input.txt")
                      (clojure.string/split-lines)
                      (apply mapv vector)
                      (map frequencies)
                      (map #(sort-by val %))
;                      (map reverse)
                      (map first)
                      (map first))))

clojure

1

u/splurke Dec 06 '16

Maybe your way is more readable, but here's an alternative without repeating map:

(map (comp first first reverse #(sort-by val %) frequencies))

1

u/[deleted] Dec 06 '16

1

u/stuque Dec 06 '16

A Python 2 solution:

def solve(part):
    pos_freq = [{}, {}, {}, {}, {}, {}, {}, {}]
    for line in open('day6_input.txt'):
        for i, c in enumerate(line.strip()):
            if c in pos_freq[i]:
                pos_freq[i][c] += 1
            else:
                pos_freq[i][c] = 1

    for d in pos_freq:
        lst = [(d[c], c) for c in d]
        lst.sort()
        if part == 1: lst.reverse()
        print lst[0][1],

def part1():
    solve(part=1)

def part2():
    solve(part=2)

if __name__ == '__main__':
    part1()
    print
    part2()

1

u/wzkx Dec 06 '16 edited Dec 06 '16

Aha, finally found solution like mine. List of dictionaries, then lists of pairs...

t = open('06.dat','rt').read().strip().split('\n')
n = len(t[0]); d = [{} for i in range(n)]
for l in t: # can't have two for's in one line
  for i,c in enumerate(l): d[i][c] = d[i].get(c,0)+1
sd = [sorted([(d[i][c],c) for c in d[i]]) for i in range(n)]
print( ''.join( sd[i][-1][1] for i in range(n) ) )
print( ''.join( sd[i][0][1] for i in range(n) ) )

1

u/sinokawori Dec 06 '16

I had fun for this, I spawn one thread per column and solve for both min and max in a single go.

All done in c++ :)

#include <iostream>
#include <future>
#include <string>
#include <fstream>
#include <array>
#include <iterator>

struct ColumnInfo {
	char most_common = '\0';
	char least_common = '\0';
};

ColumnInfo column_process(std::array<int, 26>::iterator begin, std::array<int, 26>::const_iterator end) {	
	int max_value = 0, min_value = 26;
	ColumnInfo info;
	for (char i = 0; begin != end; ++begin) {		
		if (*begin > max_value) {
			max_value = *begin;
			info.most_common = i;
		}
		else if (*begin < min_value) {
			min_value = *begin;
			info.least_common = i;
		}
		++i;
	}
	info.most_common += 'a';
	info.least_common += 'a';
	return info;
}

int main() {
	std::ifstream file("input.txt");
	std::string line;	
	std::array<std::array<int, 26>, 8> columns{};

	while (std::getline(file, line)) {
		for (int i = 0; i < 8; ++i) {
			size_t yo = line.at(i) - 'a';
			++columns.at(i).at(yo);
		}
	}
	file.close();

	std::vector<std::future<ColumnInfo>> futures{};

	for (int i = 0; i < 8; ++i) {
		futures.emplace_back(std::async(std::launch::async, column_process, columns.at(i).begin(), columns.at(i).cend()));
	}

	char password_1[9]{};
	char password_2[9]{};
	
	for (size_t i = 0; i < futures.size(); ++i) {
		ColumnInfo info = futures.at(i).get();
		password_1[i] = info.most_common;
		password_2[i] = info.least_common;
	}

	std::cout << "part 1: " << password_1 << std::endl; 
	std::cout << "part 2: " << password_2 << std::endl;

	return 0;
}

1

u/Twisol Dec 06 '16

Using Javascript/Node.js, optimizing for legibility as usual:

const File = require("fs");

function transpose(rows) {
  const cols = [];
  for (let row of rows) {
    let i = 0;
    for (let x of row) {
      cols[i] = (cols[i] || []);
      cols[i].push(x);
      i += 1;
    }
  }

  return cols;
}

function to_frequencies(list) {
  const freqs = {};
  for (let x of list) {
    freqs[x] = (freqs[x] || 0);
    freqs[x] += 1;
  }

  return freqs;
}

function best_key(obj, metric) {
  const comparator = (x, y) => {
    const score_x = metric(obj[x]);
    const score_y = metric(obj[y]);

    if (score_x < score_y) return 1;
    else if (score_y < score_x) return -1;
    else return 0;
  }

  return Object.keys(obj).sort(comparator)[0];
}

const lines = File.readFileSync("input.txt", "utf-8").trim().split("\n");
const columns = transpose(lines);
const frequencies = columns.map(to_frequencies);

const message1 = frequencies.map(ch => best_key(ch, x =>  x)).join("");
const message2 = frequencies.map(ch => best_key(ch, x => -x)).join("");

console.log("Part One: " + message1);
console.log("Part Two: " + message2);

1

u/aoc-fan Dec 06 '16

JavaScript-ES6

const createContenderList = (contendersList, characters, lineIndex) => {
    characters.forEach((character, placeIndex) => {
        if (!contendersList[placeIndex]) {
            contendersList[placeIndex] = {};
        }
        const contenders = contendersList[placeIndex];
        contenders[character] = (contenders[character] | 0) + 1;
    });
    return contendersList;
};
const leastCommon = o => Object.keys(o).reduce((a, b) => (o[a] < o[b] ? a : b));
const mostCommon = o => Object.keys(o).reduce((a, b) => (o[a] >  o[b] ? a : b));
const correctError = (input, selectionStrategy) => input.split("\n")
    .map((line) => line.split(""))
    .reduce(createContenderList, [])
    .map(selectionStrategy)
    .join("");
correctError("your-input", mostCommon);
correctError("your-input", leastCommon);

1

u/Iain_M_Norman Dec 06 '16

Some C#

var table = input.Split('\n').Select(x => x.ToCharArray()).ToArray();

var part1 = "";
var part2 = "";

for (int col = 0; col < table[0].Length; col++)
{
	var frequencies = new Dictionary<char, int>();
	for (int row = 0; row < table.Length; row++)
	{
		if (!frequencies.ContainsKey(table[row][col]))
		{
			frequencies.Add(table[row][col], 0);
		}
		frequencies[table[row][col]]++;
	}
	part1 += frequencies.Aggregate((a, b) => a.Value > b.Value ? a : b).Key;
	part2 += frequencies.Aggregate((a, b) => a.Value < b.Value ? a : b).Key;
}

Console.WriteLine(part1);
Console.WriteLine(part2);

1

u/johanw123 Dec 06 '16

My C# solution Part 1 and 2 is just a matter of changing the "Last" to "First" after the ordering.

`

    static void Main(string[] args)
    {
      string input = @"cmezkqgn...";

      var messages = input.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

      string message = "";

      for (int i = 0; i < messages.First().Length; i++)
      {
        string s = messages.Aggregate("", (current, t) => current + t.Trim()[i]);

        message += s.GroupBy(x => x).OrderByDescending(x => x.Count()).Last().Key;
      }
  
      Console.WriteLine(message);
      Console.ReadKey();
    }

`

1

u/[deleted] Dec 06 '16

Python, Parts 1 & 2

If I'd only gotten up at midnight, I might have had a shot at the leaderboard, this took me well short of 10 minutes -- although I'm not sure I could have beat 6 minutes and change.

import sys  # make sure you have the same version as me
assert sys.version_info >= (3,4)

# read data into memory as a list
data = open('input06.txt').read().splitlines()

# luckily the standard library has the very handy and appropriate
# collections.Counter class we can use
from collections import Counter
cntrs = []  # collect the counters in a list
result = []
for i in range(len(data[0])):  # i.e. i goes from [0, 8)
    cntrs.append(Counter())
    for token in data:  # calling it a token after NLP practice
        cntrs[i].update(token[i])
    result.append(cntrs[i].most_common()[0][0])
print(''.join(result))

# PART TWO - only one line needs to be changed!

cntrs = [] 
result = []
for i in range(len(data[0])): 
    cntrs.append(Counter())
    for token in data:
        cntrs[i].update(token[i])
    result.append(cntrs[i].most_common()[-1][0])  # This is the only line I changed
print(''.join(result))

All my solutions (I'm catching up still) in Jupyter Notebooks and autogenerated .py files in my GitHub repo

1

u/Jean-Paul_van_Sartre Dec 06 '16

Solution in C:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define INPUT "../input/06.txt"

int** getFrequency(char* word);

int main() {
	FILE* fp;
	char* line  = NULL;
	size_t len  = 0;
	int** freq  = NULL;
	int wordlen = 0;

	fp = fopen(INPUT, "r");
	if(fp == NULL) {
		perror(INPUT);
		exit(EXIT_FAILURE);
	}

	for(int i = 0;getline(&line, &len, fp) != -1; i++) {
		int** tmp = getFrequency(line);
		if(i == 0) {
			wordlen = strlen(line)-1;
			freq = tmp;
		} else {
			for(int j=0; j<wordlen; j++) {
				for(int k=0; k<26; k++) {
					freq[j][k] += tmp[j][k];
				}
				free(tmp[j]);
			}
			free(tmp);
		}
	}
	char* part1 = malloc(wordlen+1);
	char* part2 = malloc(wordlen+1);
	for(int i=0; i<wordlen; i++) {
		int most  = 0;
		int least = 0;
		for(int j=0; j<26; j++) {
			if(freq[i][j]!=0) {
				most = freq[i][most]>freq[i][j]?most:j;
				least = freq[i][least]<freq[i][j]?least:j;
			}
		}
		part1[i] = most+'a';
		part2[i] = least+'a';
		free(freq[i]);
	}
	part1[wordlen] = '\0';
	part2[wordlen] = '\0';
	printf("part1 %s\n", part1);
	printf("part2 %s\n", part2);

	free(part1);
	free(part2);
	fclose(fp);
	free(line);
	free(freq);
	exit(EXIT_SUCCESS);
}

int** getFrequency(char* word) {
	int** freq;
	int len;

	len = strlen(word)-1;
	freq = malloc(sizeof(int*) * len);
	for(int i=0; i<len; i++) {
		freq[i] = malloc(sizeof(int) * 26);
		for(int j=0; j<26; j++) {
			freq[i][j] = 0;
		}
		freq[i][word[i]-'a']++;
	}

	return freq;
}

1

u/_AceLewis Dec 06 '16 edited Dec 06 '16

Python 3 solutions to both parts, done in https://repl.it so input is a string as no files can be used.

Day 6 part 1: https://repl.it/EhYa/3

from statistics import mode

print(''.join(map(mode, zip(*strings.split()))))

Day 6 part 2: https://repl.it/EhYa/5

Either:

from collections import Counter

print(''.join(map(lambda x: Counter(x).most_common()[-1][0], zip(*strings.split()))))

or

from collections import Counter

print(''.join(Counter(x).most_common()[-1][0] for x in zip(*strings.split())))

All the zip(*strings.split()) does is transpose the string (in array form)

1

u/beefamaka Dec 06 '16

Feeling slightly jealous of Python's zip function which would have been ideal for this problem, but was still fairly straightforward to solve in F#.

let decodePos (messages:seq<string>) selector n =
    messages |> Seq.map (fun msg -> msg.[n]) |> Seq.countBy id |> selector snd |> fst
let decodeMessages (messages:string[]) selector =
     [|0..messages.[0].Length-1|] |> Array.map (decodePos messages selector) |> System.String 

let input = System.IO.File.ReadAllLines (__SOURCE_DIRECTORY__ + "\\input.txt")
decodeMessages input Seq.maxBy |> printfn "Part a: %s" 
decodeMessages input Seq.minBy |> printfn "Part b: %s"

1

u/beefamaka Dec 06 '16

created myself a poor man's zip for F# for an alternative solution

let zip (a:string[]) =
    [| for x in 0..a.[0].Length-1 -> [| for y in a -> y.[x] |] |]
let decodeMessages selector =
    zip >> (Array.map (Seq.countBy id >> selector snd >> fst)) >> System.String

let input = System.IO.File.ReadAllLines (__SOURCE_DIRECTORY__ + "\\input.txt")
decodeMessages Seq.maxBy input |> printfn "Part a: %s"
decodeMessages Seq.minBy input |> printfn "Part b: %s"

1

u/JakDrako Dec 06 '16 edited Dec 06 '16

VB.Net, LinqPad.

Just a simple 2D array (26 letters x 8 positions) to count the occurrences of each letter.

Sub Main

    Dim arr(25, 7) As Integer

    For Each line In input.Split(vbLf)
        For pos = 0 To line.Trim.Length - 1
            Dim ltr = AscW(line(pos)) - 97
            arr(ltr, pos) += 1
        Next
    Next

    Dim sMax = "", sMin = ""
    For pos = 0 To 7
        Dim vMax = 0, nMax = -1, vMin = Integer.MaxValue, nMin = -1
        For ltr = 0 To 25
            If arr(ltr, pos) > vMax Then vMax = arr(ltr, pos) : nMax = ltr
            If arr(ltr, pos) > 0 AndAlso arr(ltr, pos) < vMin Then vMin = arr(ltr, pos) : nMin = ltr
        Next
        If nMax >= 0 Then sMax &=ChrW(nMax+97)
        If nMin >= 0 Then sMin &=ChrW(nMin+97)
    Next
    
    sMax.Dump("Most common")
    sMin.Dump("Least common")
    
End Sub

1

u/wzkx Dec 06 '16

That was made for J indeed :)

m =: {.@([:\:+/"1@=){~.
n =: {.@([:/:+/"1@=){~.
echo (m"1 ; n"1) |:>cutLF CR-.~fread '06.txt'

1

u/[deleted] Dec 06 '16

Clojure.

(ns aoc2016.day06
  (:require [clojure.string :as s]))

(defn load-input []
  (s/split (slurp "./data/day06.txt") #"\n"))

(defn freq-by-index [data]
  (->> data
       (map #(s/split % #""))
       (mapcat #(map-indexed (fn [i x] [i x]) %))
       (frequencies)
       (sort-by val)
       (reverse)))

(defn solve [data]
  (->> data 
       (take 8)
       (sort-by first)
       (flatten)
       (filter string?)
       (s/join)))

(defn part-1 []
  (solve (freq-by-index (load-input))))

(defn part-2 []
  (solve (reverse (freq-by-index (load-input)))))

1

u/gruftfrosch Dec 06 '16

Using Advent of Code to try out some new languages, probably ending up in rather inconvenient solutions...Anyway, here is my attempt in Julia:

alphabet = "abcdefhgijklmnopqrstuvwxyz"
message1 = []
message2 = []
open("input.txt") do file
    global data = hcat([collect(strip(line)) for line in readlines(file)]...)
end
for i in 1:size(data,1)
    d = Dict(c => 0 for c in alphabet)
    for c in data[i,:]
        d[c] +=1
    end
    push!(message1, collect(keys(d))[indmax(collect(values(d)))])
    push!(message2, collect(keys(d))[indmin(collect(values(d)))])
end

println(join(message1))
println(join(message2))

1

u/Reibello Dec 06 '16

Here's mine in Python. I thought it was pretty good until I woke up and saw leaderboard times. Y'all better buckle up for the rest of these :D http://pastebin.com/7ZuEPV86

1

u/Scroph Dec 06 '16 edited Dec 06 '16

Edit : with array_column in PHP5.5 I was able to solve the first part in just a few lines :

<?php
$lines = array_map('str_split', file('input', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
for($i = 0; $i < sizeof($lines[0]); $i++)
{
	$values = array_count_values(array_column($lines, $i));
	echo array_search(max($values), $values);
}

I initially solved this in C but since T_PAAMAYIM_NEKUDOTAYIM is mandatory, I went ahead and rewrote the solution in PHP :

<?php
$count = [];
for($i = 0; $i < 6; $i++)
	$count[$i] = array_combine(range('a', 'z'), array_fill(0, 26, 0));

foreach(file('input6', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $msg)
{
	for($i = 0, $l = strlen($msg); $i < $l; $i++)
	{
		if($i >= sizeof($count))
			$count[] = array_combine(range('a', 'z'), array_fill(0, 26, 0));
		$count[$i][$msg[$i]]++;
	}
}

foreach($count as $slot)
	echo least_common($slot);

function least_common($slot)
{
	$min = 'a';
	foreach($slot as $letter => $count)
		if($count != 0 && $count < $slot[$min])
			$min = $letter;
	return $min;
}

1

u/topCyder Dec 06 '16

Anyone want a simple, readable Python3 answer? No? Too bad!

with open('input') as f:
inp = f.readlines()

msg1 = "1: "
msg2 = "2: "

i = 0


while (i<8):
	b = {}
	rl = []
	for s in inp:
		rl.append(s[i])
	for r in rl:
		b[r] = b.get(r, 0) + 1
	f = max(b, key=b.get)
	d = min(b, key=b.get)
	msg1 = msg1 + f
	msg2 = msg2 + d
	i += 1
print(msg1)
print(msg2)

1

u/TheQuinnFTW Dec 06 '16

Haskell Solution:

import qualified Data.Map as Map
import Data.Ord (comparing)
import Data.List (sortBy, transpose)

histogram :: (Ord a) => [a] -> [(a, Int)]
histogram seq = Map.toList $ foldr (flip (Map.insertWith (+)) 1) Map.empty seq

mode :: (Ord a) => [a] -> a
mode seq = head $ map fst $ sortBy (flip (comparing snd)) $ histogram seq

antiMode :: (Ord a) => [a] -> a
antiMode seq = head $ map fst $ sortBy (comparing snd) $ histogram seq

main = do
  codes <- lines <$> getContents
  print $ map mode (transpose codes)
  print $ map antiMode (transpose codes)

1

u/splurke Dec 06 '16

Today was… Surprisingly easy. I might even had a chance to be on the leaderboard, weren't for timezone issues

Haskell, both parts:

module Day6 where

import           Data.List (group, sort, sortOn, transpose)

-- Main
main :: IO ()
main = do
  input <- readFile "input/6"
  putStr "1. "
  putStrLn $ map (mapfn reverse) $ transpose $ lines input
  putStr "2. "
  putStrLn $ map (mapfn id) $ transpose $ lines input
  where
    mapfn f = (head . head . f . (sortOn length) . group . sort)

1

u/drewolson Dec 06 '16 edited Dec 06 '16

Part 2, elixir (switch min_by to max_by for part 1):

"./input.txt"
|> File.stream!
|> Enum.map(&String.strip/1)
|> Enum.map(&String.split(&1, "", trim: true))
|> List.zip
|> Enum.map(&Tuple.to_list/1)
|> Enum.map(fn chars ->
  Enum.reduce(chars, %{}, fn char, counts ->
    Map.update(counts, char, 1, &(&1 + 1))
  end)
end)
|> Enum.map(&Enum.min_by(&1, fn {_, count} -> count end))
|> Enum.map(&elem(&1, 0))
|> Enum.join
|> IO.puts

1

u/rubiconjosh Dec 06 '16

I got just over number 300 for part 1 and 2 in python

https://github.com/rubiconjosh/AoC-2016/blob/master/day6puzzle1.py

https://github.com/rubiconjosh/AoC-2016/blob/master/day6puzzle2.py

Switching between the two puzzles was just a matter of most_common(1)[0] being changed to most_common()[-1]

1

u/qwertyuiop924 Dec 06 '16

I'm surprised nobody else thought of AWK.

I mean, there weren't even any Perl solutions, and Perl is usually quite popular, and, like AWK, is ideally suited to the task.

Anyways, I got it in two lines, and I'm shocked by how long most of these solutions were: there was a one-line haskell solution, and JohnEarnest and the rest of the APL/J/K crowd were about as short as expected. There was a python one-liner, and, and a bit of bash. But many solutions were >10 lines!

Anyways, my solution:

Part 1:

function max(a){x=0;c="";for(i in a){if(a[i]>x {x=a[i];c=i;}}return c;}
BEGIN{FS=""}{for(i=1;i<=NF;i++) w[i][$i]+=1;}END{for(i in w) print max(w[i]);}

Part 2:

function max(a){x=1000;c="";for(i in a){if(a[i]<x){x=a[i];c=i;}}return c;}
BEGIN{FS=""}{for(i=1;i<=NF;i++) w[i][$i]+=1;}END{for(i in w) print max(w[i]);}

1

u/gerikson Dec 06 '16 edited Dec 06 '16

Perl solution.

I used a compact map to create the hashref and felt I had to to it for for the output side too!

Edit added a nice switch to enable part 1 or 2 from the same code.

→ More replies (5)

1

u/schlocke Dec 06 '16 edited Dec 06 '16

PHP both solutions:

<?php
	//rows
	$a = file("day6.txt");
	//columns
	$b = array("","","","","","","","");
	//answers
	$p1=$p2="";
	//transform rows into columns
	foreach($a as $c) {
		$b[0] .= substr($c, 0, 1);
		$b[1] .= substr($c, 1, 1);
		$b[2] .= substr($c, 2, 1);
		$b[3] .= substr($c, 3, 1);
		$b[4] .= substr($c, 4, 1);
		$b[5] .= substr($c, 5, 1);
		$b[6] .= substr($c, 6, 1);
		$b[7] .= substr($c, 7, 1);
	}
	//loop through the columns
	foreach($b as $d) {
		//split the string into an array for sorting
		$e = str_split($d);
		//sort alphabetically
		sort($e);
		//rejoin to string for preg matching
		$f = implode($e);
		$g = array();
		//turn the string into an array of letters e.g. [aaaaaaa][bbbbbbb][cccccccc]..... etc the matches are stored into $g
		preg_match_all("/[a]+|[b]+|[c]+|[d]+|[e]+|[f]+|[g]+|[h]+|[i]+|[j]+|[k]+|[l]+|[m]+|[n]+|[o]+|[p]+|[q]+|[r]+|[s]+|[t]+|[u]+|[v]+|[w]+|[x]+|[y]+|[z]+|
/", $f, $g);
		//create an array of lengths where the index correlates with the index of the letter
		$h = array_map('strlen', $g[0]);
		//find the index of the most occurring letter
		$i = array_search(max($h), $h);
		//find the index of the least occuring letter
		$j = array_search(min($h), $h);
		//add most occurring letter to part1
		$p1 .= substr($g[0][$i], 0, 1);
		//add least occuring letter to part2
		$p2 .= substr($g[0][$j], 0, 1);
	}
	//echo answer
	echo "$p1<br>$p2";

EDIT: I think this was the quickest time between part 1 and part 2 for me since i just had to add

$j = array_search(min($h), $h);

to get the second part.

EDIT2: added comments to code

1

u/LainIwakura Dec 06 '16

Day 6 solutions in erlang, I found this to be the easiest day yet. https://github.com/LainIwakura/AdventOfCode2016/tree/master/Day6

1

u/ShroudedEUW Dec 06 '16

C#

Wish I woke up in time for the leaderboards, cuz I did this one quite quickly.

https://github.com/KVooys/AdventOfCode/blob/master/AdventOfCode/Day6.cs

With a dictionary (letter -> frequency) approach, the 2nd part of the challenge was trivial, changing max to min.

1

u/Kwpolska Dec 06 '16

collections.Counter to the rescue!

#!/usr/bin/env python3
import collections
PART = 2
IDX = 0 if PART == 1 else -1

with open("input/06.txt") as fh:
    file_data = fh.read()


def solve(data):
    words = [l for l in data.split('\n') if l]
    length = len(words[0])
    counters = []
    for i in range(length):
        counters.append(collections.Counter())
    for word in words:
        for i, c in enumerate(word):
            counters[i].update(c)
    output = ""
    for counter in counters:
        output += counter.most_common()[IDX][0]
    print(counters)
    return output


with open("input/test_06.txt") as tf:
    test_data = tf.read()
test_output = solve(test_data)
test_expected = "advent"
print(test_output, test_expected)
assert test_output == test_expected
print(solve(file_data))

1

u/rmitchum Dec 06 '16 edited Dec 06 '16

Perl 6 worked pretty well today. Part 1:

say [~] ([Z] lines».comb)».Bag».invert».sort»[*-1]».value

Part 2:

say [~] ([Z] lines».comb)».Bag».invert».sort»[0]».value

1

u/volatilebit Dec 07 '16 edited Dec 07 '16

I always hope someone else posted a Perl 6 solution so I can see what tricks I missed. Well done.

Mine with a little less functional programming.

my @corrupted_messages = 'input'.IO.lines.list;
say [~] (0..^@corrupted_messages[0].chars).map({
    ([~] @corrupted_messages.map: *.substr($_, 1)).comb.Bag.sort(-*.value)[0].key;
});
say [~] (0..^@corrupted_messages[0].chars).map({
    ([~] @corrupted_messages.map: *.substr($_, 1)).comb.Bag.sort(*.value)[0].key;
});

Yours can be shortened by 1 character by not inverting but instead sorting by value. For example, part 1:

say [~] ([Z] lines».comb)».Bag».sort(-*.value)».[0]».key;

1

u/rmitchum Dec 08 '16

Nice! That actually shortens it by 2 characters for part 1 and 1 for part 2!

1

u/Fotomik Dec 06 '16

My pyhton solution. Feedback is welcome.

# -*- coding: utf-8 -*-
import itertools as it
import operator as op

code_p1,code_p2='',''

alphabet = [chr(x) for x in range(ord('a'), ord('z') + 1)]
corrupted_codes = []

for line in open(r'../inputs/day06.txt'):
    corrupted_codes.append(line.strip())

for i in range(0,len(corrupted_codes[0])):
    occurrences = dict(zip(alphabet,it.repeat(0)))
    
    for code in corrupted_codes: occurrences[code[i]] += 1
    
    order = sorted(occurrences.items(), key=op.itemgetter(1), reverse=True)
    code_p1 += order[0][0]
    code_p2 += order[-1][0]

print('Part 1:',code_p1)
print('Part 2:',code_p2)

1

u/[deleted] Dec 06 '16

Mathematica

input = Import[NotebookDirectory[] <> "day6.txt"];
msg = Transpose@Characters@StringSplit[input, Whitespace];

StringJoin[Commonest[#, 1] & /@ msg]

StringJoin@Map[MinimalBy[Tally[#], Last] &, msg][[All, 1, 1]]

1

u/[deleted] Dec 06 '16

How bad is my Python solution (it solves it at least!)

https://github.com/JosephBywater/AdventAnswers2016/blob/master/day6/day6a.py

Thanks :)

1

u/vesche Dec 07 '16
d = [{}, {}, {}, {}, {}, {}, {}, {}]

with open("day06_input.txt") as f:
    for word in f.read().splitlines():
        for i in range(len(word)):
            letter = word[i]
            if letter in d[i]:
                d[i][letter] += 1
            else:
                d[i][letter] = 1

most_common, least_common = '', ''
for i in range(len(d)):
    most_common += max(d[i], key=d[i].get)
    least_common += min(d[i], key=d[i].get)

print "Part One: {}, Part Two: {}".format(most_common, least_common)

1

u/tehjimmeh Dec 07 '16 edited Dec 08 '16

PowerShell, parts 1 and 2:

$p = cat day6input.txt
echo (-1,0) -pv x | %{ -join (echo (0..8) -pv i | %{ @($p | %{ $_[$i] } | group | sort Count)[$x].Name }) }

1

u/schling-dong Dec 07 '16

F#:

let path = System.IO.Path.Combine(__SOURCE_DIRECTORY__,"input.txt")
let input = System.IO.File.ReadAllLines path

let message = for i in [0..(input.[0].Length-1)] do
                input
                |> Array.map (fun s -> s.Substring(i, 1))
                |> Seq.groupBy id
                |> Seq.map (fun (letter, seq) -> letter, Seq.length seq)
                |> List.ofSeq
                |> List.sortBy (fun (fst, snd) -> -snd)
                |> List.item(0)
                |> (fun (fst, snd) -> printf "%s" fst)

Just had to remove the minus sign in front of snd for the second part.

1

u/jwnewman12 Dec 13 '16

surely there is a better way, but at least avoiding a .sort()

public class Day6 {

	static final int CHARS_PER_LINE = 8;

	public static void main(String[] args) {
		String input = args[0].replaceAll("\\n", "");
		int l = input.length() / CHARS_PER_LINE;
		for (int i = 0; i < CHARS_PER_LINE; ++i) {
			int[] b = new int[26];
			int m = 0;
			for (int j = 0; j < l; ++j) {
				int c = input.charAt(j * CHARS_PER_LINE + i) - 'a';
				m = ++b[c] > b[m] ? c : m;
			}
			System.out.print((char) (m + 'a'));
		}
	}
}

0

u/taliriktug Dec 06 '16 edited Dec 06 '16

Damn, I love Python.

def solve_first(fname):
    data = read_data(fname)
    return ''.join((Counter(d[i] for d in data).most_common()[0][0])
	               for i in range(len(data[0])))


def solve_second(fname):
    data = read_data(fname)
    return ''.join((Counter(d[i] for d in data).most_common()[-1][0])
	               for i in range(len(data[0])))

EDIT: split long line on two

2

u/miran1 Dec 06 '16
return ''.join([str(Counter(d[i] for d in data).most_common(1)[0][0]) for i in range(len(data[0]))])

I also love Python:

return ''.join(Counter(d).most_common()[0][0] for d in zip(*data))

;)

2

u/taliriktug Dec 06 '16

I always forget about zip! Done my homework with it and no Counter:

''.join([max(((column.count(c), c) for c in column))[1] for column in zip(*open('input.txt').readlines())])

''.join([min(((column.count(c), c) for c in column))[1] for column in zip(*open('input.txt').readlines())])

0

u/[deleted] Dec 06 '16

[deleted] — view deleted comment

1

u/PontifexVorticis Dec 06 '16

Didn't know max(..., key=...), thanks! I used sorted([ (col.count(x), x) for x in set(col) ]) and could then reference the first and last element You can also just iterate over zip(*rows) in your code, see my solution

0

u/[deleted] Dec 06 '16

[deleted] — view deleted comment

1

u/gerikson Dec 06 '16

I made a mistake when using the testing data and got the second part answer when working on part 1, so I was not surprised :D

0

u/[deleted] Dec 06 '16

[deleted] — view deleted comment

1

u/miran1 Dec 06 '16 edited Dec 06 '16
Counter(x).most_common()[::-1][0][0]

or just Counter(x).most_common()[-1][0] ;)

1

u/yacfOaky Dec 06 '16

I did it pretty much exactly the same way.

I then went back and did it using pandas:-

import pandas as pd
df = pd.read_table('input.txt', header=None).apply(lambda x: pd.Series(list(x[0])), axis=1)
part1 = ''.join(df.apply(lambda x: x.value_counts().index[0]))
part2 = ''.join(df.apply(lambda x: x.value_counts().index[-1]))

print 'Message1 is: {}\nMessage2 is: {}'.format(part1, part2)