r/adventofcode • u/daggerdragon • Dec 06 '20
SOLUTION MEGATHREAD -π- 2020 Day 06 Solutions -π-
NEW AND NOTEWORTHY
/u/jeroenheijmans is back with the Unofficial AoC 2020 Participant Survey!
/u/maus80 is back with an interactive scatterplot of the global leaderboard!
Advent of Code 2020: Gettin' Crafty With It
UNLOCKED! Go forth and create, you beautiful people!
Full details and rules are in the Submissions Megathread
Make sure you use one of the two templates!
Or in the words of AoC 2016: USING A TEMPLATE IS MANDATORY
--- Day 06: Custom Customs ---
Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.
Reminder: Top-level posts in Solution Megathreads are for 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:04:35, megathread unlocked!
5
u/DFreiberg Dec 06 '20 edited Dec 06 '20
Mathematica, 1268 / 323
Two nice one-liners with Mathematica's convenient set operations...once I was able to get the input.
Part 1:
Total@Table[Length[DeleteCases[Union[Characters[line]], "\n"]], {line, input}]Part 2:
Total@Table[Length[Intersection @@ (Characters /@ StringSplit[line, "\n"])], {line, input}][POEM]: Quaint Customs & Curious Questions
The customs at this airport, people say,
Are short, and simpler than in other ports.
But all the questions asked, from Z to A,
Are never questions of the normal sorts:
Are mirrors really real if even our own eyes are not?
Buy anything abroad that really can't be sold or bought?
Do people call you on the phone you're trying to avoid?
Can the people on TV see me or am I just paranoid?
Ever shot a man in Reno, just to watch him die?
Forget your coat or luggage when you came on board to fly?
Got any chapstick you could spare? The air here's awfully cold.
Hot dogs: are they sandwiches with bread that has a fold?
I meant to cut this question out - the next one too, in fact.
Just fill them with a 'yes' or 'no', no need to be exact.
Kazoos and old harmonicas: can newer stuff compare?
Lose any luggage -- wait, that's question G, a ways up there.
Made any mashed potatoes lately? Did they turn out well?
Noticed any odd designs on anyone's lapel?
Of course, you might not know this, since it's pretty tricky stuff:
Part 2 from 2017's day sixteen: was it tough?
Question seventeen's a breeze: is this short sentence true?
Right, back to other matters: ever gotten a tattoo?
So since tomatoes are a fruit, can they go in a pie?
Then shouldn't pineapples on pizza work well? What's awry?
Until today, have coding puzzles kept you up at night?
Vegemite's a sandwich thing Down Under, is that right?
When you were younger, did you ever skip on school?
Xtreme Kool LetterZ - do they work to make you hip and cool?
You ever watched the movie "Fastest Bullet in the West?
Zymurgy's a real good Scrabble word - is it the best?
The questions never have made sense to me
(Aside from the song lyrics under A).
But tedious the nonsense form might be...
...this custom surely beats the TSA.
→ More replies (5)2
u/daggerdragon Dec 07 '20
Hot dogs: are they sandwiches with bread that has a fold?
Oh, don't you start
3
u/Attitude-Certain Dec 06 '20 edited Dec 07 '20
In a functional mood this Christmas. Really liking the toolz package for Python.
import operator
from toolz import compose, map, reduce
with open("input.txt") as f:
groups = f.read().split("\n\n")
print(
"Part 1:", sum(map(compose(len, set, list, lambda g: g.replace("\n", "")), groups))
)
print(
"Part 2:",
sum(
len(reduce(operator.and_, map(set, group)))
for group in map(lambda g: g.split("\n"), groups)
),
)→ More replies (2)1
u/daggerdragon Dec 06 '20
Please follow the posting guidelines and edit your post to add what language(s) you used. This makes it easier for folks who Ctrl-F the megathreads looking for a specific language.
→ More replies (1)
3
u/Jedimastert Dec 06 '20
Rust
And lo, a voice of distant days past, of practice interview questions and university data structure classes whispered to me...
haaaaaasssshhhhhseeeeeeettttsss
and I said
I'm sorry? I'm a little hard of hearing, can you say that a little clearer?
Oh, sorry. I really should have known. Hash sets.
Oh yeah. Cheers
2
u/daggerdragon Dec 06 '20
You sure this is Rust and not Python due to the Parseltongue voices in your head? >_>
3
u/bcgroom Dec 06 '20 edited Dec 07 '20
Elixir
This one ended up being pretty nice. I kind of panicked when reading the second part thinking I would have to stop using sets for some reason, instead all it required was combining sets of answers in different ways. Set theory FTW!
Here's the bulk of my solution:
def part_one do
@input
|> parse()
|> Enum.map(fn group -> declaration_for_group(group, &anyone_combinator/1) end)
|> Enum.map(&MapSet.size/1)
|> Enum.sum()
end
def part_two do
@input
|> parse()
|> Enum.map(fn group -> declaration_for_group(group, &everyone_combinator/1) end)
|> Enum.map(&MapSet.size/1)
|> Enum.sum()
end
def declaration_for_group(group, combinator) do
group
|> Enum.map(&MapSet.new/1)
|> combinator.()
end
def anyone_combinator(people) do
people
|> Enum.reduce(&MapSet.union/2)
end
def everyone_combinator(people) do
people
|> Enum.reduce(&MapSet.intersection/2)
endFull code here: https://github.com/ericgroom/advent2020/blob/master/lib/days/day_6.ex
→ More replies (4)1
u/daggerdragon Dec 06 '20
Please follow the posting guidelines and add the language used to your post to make it easier for folks who Ctrl-F the megathreads looking for a specific language.
(looks like Elixir?)
→ More replies (1)
3
u/StringFinal Dec 06 '20
python3
part 1
with open("adventofcode/2020/day6") as input:
lines = input.readlines()
answer_groups = []
answers = ""
for line in lines:
line = line.strip()
answers += f"{line} "
if not line:
answer_groups.append(len(set(answers.replace(" ",""))))
answers = ""
answer_groups.append(len(set(answers.replace(" ",""))))
print(sum(answer_groups))part 2
with open("adventofcode/2020/day6") as input:
lines = input.readlines()
answer_groups = []
answers = ""
for line in lines:
line = line.strip()
answers += f"{line} "
if not line:
split_answers = [set(answer) for answer in answers.strip().split(" ")]
answer_groups.append(set.intersection(*split_answers))
answers = ""
split_answers = [set(answer) for answer in answers.strip().split(" ")]
answer_groups.append(set.intersection(*split_answers))
print(sum([len(answer_set) for answer_set in answer_groups]))→ More replies (1)1
u/daggerdragon Dec 06 '20
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?
3
u/SeaworthinessOk1009 Dec 06 '20 edited Dec 07 '20
PASCAL part one:
program leer;
uses
sysutils;
type
f = file of char;
alf = array ['a'..'z'] of boolean;
procedure agregar(letter: char; var v: alf);
begin
if v[letter] = false then
v[letter]:= true;
end;
procedure inicializar(var v: alf);
var
i: char;
begin
for i:='a' to 'z' do
v[i]:= false;
end;
procedure contar( var v: alf; var c:longint);
var
i: char;
begin
for i:='a' to 'z' do
if v[i] = true then
begin
c:= c + 1;
end;
end;
procedure imprimirVector (v:alf);
var
i:char;
begin
for i:='a' to 'z' do
write(i,' : ',v[i],' | ' );
end;
var
file_name: f;
preg: char;
count: longint;
a: alf;
total: longint;
begin
count:=0;
total:=0;
inicializar(a);
assign(file_name, 'input6.txt');
reset(file_name);
while not(eof(file_name))do begin
read(file_name, preg);
agregar(preg, a);
if (preg = #10) and not(eof(file_name)) then
begin
read(file_name, preg);
agregar(preg, a);
if (preg = #10) and not(eof(file_name)) then
begin
imprimirVector(a);
contar(a,count);
total:= total + count;
count:= 0;
inicializar(a);
end
end;
end;
write('el total para todos los grupos es ',total);
close(file_name);
end.part two:
program leer;
uses
sysutils;
type
f = file of char;
alf = array ['a'..'z'] of integer;
procedure agregar(letter: char; var v: alf);
begin
v[letter]:= v[letter] + 1;
end;
procedure inicializar(var v: alf);
var
i: char;
begin
for i:='a' to 'z' do
v[i]:= 0;
end;
procedure contar( var v: alf; var c:longint; num: integer);
var
i: char;
begin
for i:='a' to 'z' do
if v[i] = num then
begin
c:= c+1;
end;
end;
procedure imprimirVector (v:alf);
var
i:char;
begin
for i:='a' to 'z' do
write(i,' : ',v[i],' | ' );
end;
var
file_name: f;
preg: char;
count: longint;
a: alf;
total: longint;
num: integer;
begin
count:= 0;
total:= 0;
num:= 0;
inicializar(a);
assign(file_name, 'input6.txt');
reset(file_name);
while not(eof(file_name))do begin
read(file_name, preg);
agregar(preg, a);
if (preg = #10) and not(eof(file_name)) then
begin
read(file_name, preg);
agregar(preg, a);
num:= num + 1;
if (preg = #10) and not(eof(file_name)) then
begin
write(num);
contar(a,count,num);
total:= total + count;
count:= 0;
num:= 0;
inicializar(a);
end
end;
end;
write('el total para todos los grupos es ',total);
close(file_name);
end.→ More replies (6)
3
u/vu47 Dec 07 '20
Kotlin:
package day06
import java.io.File
/**
* Count the number of yeses in a group, i.e. the size of the union of all the lines representing the group.
*/
private fun numYesInGroup(group: String): Int =
group.filter { it != '\n' }.toSet().size
/**
* Count the number of people who all answered yes to a question in a group, i.e. the size of the intersection of all
* the lines representing the group.
*/
private fun numAllYesInGroup(group: String): Int =
group.trim()
.split('\n')
.map(String::toSet)
.reduceRight(Set<Char>::intersect).size
fun main() {
val data = File("src/main/kotlin/day06/input.txt").readText().split("\n\n")
println(data.map { numYesInGroup(it) }.sum())
println(data.map { numAllYesInGroup(it) }.sum())
}→ More replies (3)
2
u/encse Dec 06 '20 edited Dec 06 '20
C#, turned out quite readable imho
int PartOne(string input) => Solve(input, (a,b) => a.Union(b));
int PartTwo(string input) => Solve(input, (a,b) => a.Intersect(b));
int Solve(string input, Func<ImmutableHashSet<char>, ImmutableHashSet<char>, ImmutableHashSet<char>> combine) {
return (
from grp in input.Split("\n\n")
let answers = from line in grp.Split("\n") select line.ToImmutableHashSet()
select answers.Aggregate(combine).Count
).Sum();
}https://github.com/encse/adventofcode/blob/master/2020/Day06/Solution.cs
→ More replies (1)2
u/daggerdragon Dec 06 '20
Not so readable on old.reddit, lol... As per our posting guidelines, would you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks?
Put four spaces before every code line. (If you're using new.reddit, click the button in the editor that says "Switch to Markdown" first.)
[space space space space]public static void main()[space space space space][more spaces for indenting]/* more code here*/turns into
public static void main() /* more code here */Alternatively, just link to your repo instead.
2
2
u/21ROCKY12 Dec 06 '20 edited Dec 07 '20
just solved both parts using java, got the main idea but needed some debugging to fix the little things, here is my code:) -(works)
part1&2
public static Scanner reader= new Scanner(System.in);
public static void part1() {/copy paste to main or run function
int cnt=0;
String str="";
for(String s:str.split("(\r?\\n){2}")) {
char[] check=new char[s.length()];
int gg=0;
for(String ss: s.split("\r\n")) {
for(int h=0;h<ss.length();h++) {//goes through each line
boolean flag=true;
for(int f=0;f<check.length;f++) {
if(check[f]==ss.charAt(h)) {flag=false;}
}
if(flag==true) {
check[gg]=ss.charAt(h);
cnt++;
gg++;
}
}
}
}
System.out.println(cnt);
}
public static part2() {//part2 here
int cnt = 0;
String str = "";//copy paste input here
for (String s : str.split("(\r?\\n){2}")) {
int cntgroups = 0;
HashMap<Character, Integer> top = new HashMap<Character, Integer>();
for (String ss : s.split("\r\n")) {
cntgroups++;
for (int h = 0; h < ss.length(); h++) {//goes through each line
if (top.containsKey(ss.charAt(h))) {
top.replace(ss.charAt(h), top.get(ss.charAt(h)) + 1);
} else {
top.put(ss.charAt(h), 1);
}
}
}
for (int value : top.values()) {
if (value == cntgroups) {
cnt++;
}
}
}
System.out.println(cnt);
}1
u/daggerdragon Dec 06 '20
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?
→ More replies (1)2
2
u/MartinDavenport Dec 06 '20
Thought I'd have a go at day 6 part 1 in one line. It ain't pretty, but it works.
print(sum([len(i) for i in [''.join(set(i)) for i in [i.replace('\n', '') for i in open('input.txt').read().split('\n\n')]]]))
I'd be interested to see if anyone can see any more ways of cutting it down besides renaming input.txt.
→ More replies (2)1
u/daggerdragon Dec 06 '20
Please follow the posting guidelines and add the language used to your post to make it easier for folks who Ctrl-F the megathreads looking for a specific language.
2
u/alexandreyc Dec 06 '20
My solutions with Rust using bitsets. I'm not totally happy with my two for_each for the second part...
Part 1
use std::error::Error;
use std::io::{self, Read};
fn count_yes(s: &str) -> u32 {
let mut set: u32 = 0;
s.split("\n")
.flat_map(|a| a.chars())
.for_each(|c| set |= 1 << ((c as u8) - 97));
set.count_ones()
}
fn main() -> Result<(), Box<dyn Error>> {
let mut input = String::new();
io::stdin().read_to_string(&mut input)?;
let sum: u32 = input
.split("\n\n")
.map(|s| s.trim())
.map(count_yes)
.sum();
println!("{}", sum);
Ok(())
}Part 2
use std::error::Error;
use std::io::{self, Read};
fn count_all_yes(s: &str) -> u32 {
let n = s.split("\n").count();
let mut sets: Vec<u32> = vec![0; n];
s.split("\n")
.map(|a| a.chars())
.enumerate()
.for_each(|(i, a)| a.for_each(|c| sets[i] |= 1 << ((c as u8) - 97)));
sets.iter()
.fold(0xffffffff, |acc, e| acc & e)
.count_ones()
}
fn main() -> Result<(), Box<dyn Error>> {
let mut input = String::new();
io::stdin().read_to_string(&mut input)?;
let sum: u32 = input
.split("\n\n")
.map(|s| s.trim())
.map(count_all_yes)
.sum();
println!("{}", sum);
Ok(())
}→ More replies (1)1
u/daggerdragon Dec 06 '20
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?
2
u/xalvador Dec 06 '20
Quite a tricky one today, lots of googling. If you have any feedback I would gladly hear it as I'm trying to improve my coding.
class Program
{
static List<string> input = File.ReadAllLines("input.csv").ToList();
static void Main(string[] args)
{
input.Add(" ");
Console.WriteLine(PartOne());
Console.WriteLine(PartTwo());
Console.ReadLine();
}
static int PartOne()
{
int people = 0;
string entry = "";
int yes = 0;
int result = 0;
for (int i = 0; i < input.Count; i++)
{
if (!String.IsNullOrWhiteSpace(input[i]))
{
people++;
entry = entry + input[i];
}
if (String.IsNullOrWhiteSpace(input[i]))
{
yes = entry.Distinct().Count();
entry = "";
}
result += yes;
yes = 0;
}
return result;
}
static int PartTwo()
{
int people = 0;
string entry = "";
int yes = 0;
int result = 0;
char a = ' ';
for (int i = 0; i < input.Count; i++)
{
if (!String.IsNullOrWhiteSpace(input[i]))
{
people++;
entry = entry + input[i];
}
if (String.IsNullOrWhiteSpace(input[i]))
{
for (int j = 97; j < 123; j++)
{
a = (char)j;
string b = a.ToString();
if (Regex.Matches(entry, b).Count == people)
yes++;
}
entry = "";
people = 0;
}
result += yes;
yes = 0;
}
return result;
}
}1
u/daggerdragon Dec 06 '20
Please follow the posting guidelines and add the language used to your post to make it easier for folks who Ctrl-F the megathreads looking for a specific language.
(looks like Java?)
2
u/frankbsad Dec 06 '20
c# Solution, trying my hand at some more functional type stuff
namespace AdventOfCode
{
class Day06 : BaseDay
{
private readonly List<List<string>> _input;
public Day06()
{
_input = new List<List<string>>();
var lineGroup = new List<string>();
foreach (string line in File.ReadAllLines(InputFilePath))
{
if(string.IsNullOrEmpty(line))
{
_input.Add(lineGroup);
lineGroup = new List<string>();
continue;
}
lineGroup.Add(line.Trim());
}
_input.Add(lineGroup);
}
public override string Solve_1()
{
var count = 0;
count = _input.Aggregate(0, (acc, group) =>
{
var s = new HashSet<char>();
group.ForEach(l => s.UnionWith(l.ToCharArray()));
return acc + s.Count;
});
return $"{count}";
}
public override string Solve_2()
{
var count = 0;
count = _input.Aggregate(0, (acc, group) =>
{
var m = new Dictionary<char, int>();
group.ForEach(line => line.ToCharArray().ToList().ForEach(c => m[c] = m.ContainsKey(c) ? m[c]+ 1 : 1));
return acc + m.Where(kvp => kvp.Value == group.Count).Count();
});
return $"{count}";
}
}
}→ More replies (2)
2
Dec 06 '20
Oneliner:
[print(sum([len(f(*[set(x) for x in l.split('\n')])) for l in open('input.txt').read().split('\n\n')])) for f in [set.union, set.intersection]]1
u/daggerdragon Dec 06 '20
Please follow the posting guidelines and add the language used to your post to make it easier for folks who Ctrl-F the megathreads looking for a specific language.
2
u/melefabrizio Dec 06 '20
It is my understanding that weekend puzzles should be more time consuming? π€
Php solution:
<?php
$file = file_get_contents('day6.txt');
$groups = explode("\n\n", $file);
//Part 1
$count = 0;
foreach ($groups as $group) {
$group = array_unique(str_split(str_replace("\n", '', $group))); //doppie quote per forza per rilevare il \n
$count += count($group);
}
echo "Part 1: $count\n";
//Part 2
$count = 0;
foreach ($groups as $group) {
$group = array_map('str_split', explode("\n", $group));
if(count($group)<2){
$count += count($group[0]);
continue;
}
$int = call_user_func_array('array_intersect', $group);
$count += count($int)."\n";
}
echo "Part 2: $count\n";
?>→ More replies (3)
2
u/joey_devivre Dec 06 '20 edited Dec 07 '20
#!/bin/perl
# D A Y 6 AdventOfCode 2020
#
use strict;
use warnings;
use integer;
$/ = "\n\n"; # Blank lines separate groups
my $count = 0;
while(<>){
my $people = () = m/\n/g; # one person for each line
$people--; # (but we seem to always get 1 extra)
foreach my $a ('a'..'z'){
my $yes = () = m/$a/g; # global match in list context
$count++ if $people == $yes;
}
}
print "count is $count\n";1
u/daggerdragon Dec 06 '20
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?
→ More replies (11)
2
u/HesletQuillan Dec 06 '20
DESCRIPTION: Fortran
SUBMISSION:
program AOC06
implicit none
integer :: answers,group,total,i,ios
character(80) :: line
open (unit=1,file='input.txt', form='formatted', status='old')
total = 0
ios = 0
outer: do while (ios == 0)
group = int(Z'03FFFFFF')
inner: do
read (1,'(A)', iostat=ios) line
if ((ios /= 0) .or. (line == "")) exit inner
answers = 0
do i=1,len_trim(line)
answers = ibset(answers, ichar(line(i:i)) - ichar('a'))
end do
group = iand(group,answers)
end do inner
total = total + popcnt(group)
end do outer
print *, total
end program AOC061
u/daggerdragon Dec 06 '20
Heh, this looks like you used part of the templates from Gettin' Crafty... You don't have to use a template here (unless you want to) :)
That being said, holy cow, FORTRAN.
2
u/Lispwizard Dec 06 '20 edited Dec 06 '20
Emacs lisp (elisp) in termux emacs 26.3 in Android (Galaxy Tab A 10")
(require'cl)
(defun day6-part1-responses (&optional input)
"return sum of number of unique answers in all groups"
(unless input
(setq input *aoc2020-day6-input*))
(loop for e in (split-string input "\n\n") ;; groups separated by blank line
sum (loop with set
for c across e
unless (eql 10 c) ;; skip newlines within group
do (pushnew c set) ;; set won't grow if answer already there
finally (return (length set)))))
;; Part 1 - type into m-: or c-x c-e
;; (day6-part1-responses)
(defun dedup-chars (str)
(loop with ans
for c across str
do (pushnew c ans)
finally (return (coerce ans 'string))))
(defun day6-part2-responses (&optional input)
"return sum of number of unanimous answers in all groups"
(unless input
(setq input *aoc2020-day6-input*))
(loop for e in (split-string input "\n\n") ;; groups separated by blank line
for indiv = (split-string e) ;; individual entries in current group
for l = (length indiv) ;; number of individual entries
sum (loop with set
for iv in indiv ;; for each individual answer string
do (loop for c across (dedup-chars iv) ;; ignore multiples of the same answer
for p = (assoc c set) ;; see if this answer in set
if p
do (incf (second p)) ;; yes, increment usage count
else
do (push (list c 1) set)) ;; no, create entry with usage of 1
finally (return (loop for (x n) in set ;; answers and counts
count (eql n l)))))) ;; only unanimous counts
;; Part 2 - type into m-: or c-x c-e
;; (day6-part2-responses)1
u/daggerdragon Dec 06 '20
Android (Galaxy Tab A 10")
When I said "play with your toys" in Gettin' Crafty this is not quite what I had in mind, but ya know what? If it works, it works!
2
u/shortpoet Dec 06 '20
2020 Day #6 (Parts 1 & 2) Typescript / Javascript
This is my first time doing AoC, so much fun!! π₯³
I have a part 2b which is a bit terser. Would love feedback on which people prefer and why.
part 1
const customsForms1: string[] = fs.readFileSync(
file,
"utf-8",
)
.split(/\n\n+/)
.map((g: string) => g.replace(/\n/gm, ''));
export const customCustoms =
(groups: string[]): number => {
return groups.reduce((final, group) => {
const distinct = group.split('').filter((letter, i, a) => a.indexOf(letter) == i);
return final += distinct.length;
}, 0 as number);
};part 2
const customsForms2: string[][][] = fs.readFileSync(
file,
"utf-8",
)
.split(/\n\n+/)
.map((group: string) => group
.split(/\n/)
.map(person => person.split(''))
)
export const customCustoms2 =
(groups: string[][][]): number => {
let count = 0;
for (const group of groups) {
const intersection = group.reduce((common, person, personIndex) => {
if (personIndex == 0) {
return [...common, ...person]
} else {
return common.filter(commonAnswer => person.includes(commonAnswer));
};
}, []);
count += intersection.length
};
return count;
};
export const customCustoms2b =
(groups: string[][][]): number => {
return groups
.map(group => group
.reduce((common, person) => common
.filter(commonAnswer => person
.includes(commonAnswer)
)
), [])
.reduce((total, current) => total += current.length, 0)
};2
2
u/kap89 Dec 06 '20 edited Dec 06 '20
TypeScript
const goA = (rawInput: string) =>
rawInput
.split("\n\n")
.reduce((acc, x) => acc + new Set([...x.replace(/[\s\n]/g, "")]).size, 0)
const goB = (rawInput: string) =>
rawInput.split("\n\n").reduce((acc, rawGroup) => {
const letters = Array.from(new Set([...rawGroup.replace(/\n/g, "")]))
const group = rawGroup.split("\n")
return (
acc +
letters.reduce(
(all, l) => all + (group.every((ans) => ans.includes(l)) ? 1 : 0),
0,
)
)
}, 0)Repo: github
→ More replies (1)1
u/daggerdragon Dec 06 '20
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?
→ More replies (2)
2
u/r00t4cc3ss Dec 08 '20 edited Dec 08 '20
→ More replies (1)1
u/daggerdragon Dec 08 '20
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?
57
u/tckmn Dec 06 '20
ruby 1/1 woo
p read.split("\n\n").map{|x|x.split.join.chars.uniq.size}.sum
p read.split("\n\n").map{|x|x.lines.map{|x|x.chomp.chars}.reduce(:&).size}.sum→ More replies (4)6
30
u/Arknave Dec 06 '20
Python / C
Lost a bit of time because of site issues and an off by one error, but I doubt I'm ever going to leaderboard on a string parsing problem when people are so fast these days.
First time I've done the art the night of instead of the next day! I'm starting to learn the tiny ones are the hardest to tinker with because there's just no wiggle room.
#include <stdio.h>
#include <stdlib.h>
// AOC DAY NUMBER //
int main(int c,char*
*v){int t,p,q;
char b[66]
,h[ 66],*k ,*l=
b,* e=h+26;for(p=q
=0 ;l; ){for(k=h
;k <e;++k
)* k=0;for (t=0
;( l=gets(b ))&&
*l ;++t)for( k=l;
*k; ++k)++h [-97
+*k]; for(k
=h;k<e; ++k){p+=
*k>0;q+=*k==t;}}--c;
printf("%d",c?q:p);}→ More replies (5)
25
u/sophiebits Dec 06 '20
38/27, Python. https://github.com/sophiebits/adventofcode/blob/main/2020/day06.py
Server problems during unlock again today? I had to refresh several times to get each page to load.
12
u/etotheipi1 Dec 06 '20
I got 504 and couldn't see the page for more than 2 minutes.
→ More replies (1)9
u/smrq Dec 06 '20
Same-- by the time my page loaded, 18 people had already finished part 1, judging from leaderboard times.
(115/38-- at least part 2 loaded right away!)
8
u/pred Dec 06 '20 edited Dec 06 '20
Took me a full five minutes before I could grab the inputs; 504s all along the way, and a bunch of people on IRC (names redacted) had trouble. Guess scores won't count today.
[06:00:17] <> Down again?
[06:00:22] <> rip
[06:00:29] <> yep for me.
[06:00:31] <> yeah just wondering if anyone else was having issues
[06:01:15] <> cmon
[06:01:35] <> lol
[06:01:51] <> NOw I know what day 1 felt like for everyone else.
Edit: Asked around the private leaderboards (North Europe); looks like ~4 minutes to get the site to load was normal.
7
6
u/Nathanfenner Dec 06 '20
I loaded the page fine for Part 1, but then I had to try several times (and got some 504s and page-hangs) to submit the answer. It was slow for Part 2 but a bit better.
6
u/morgoth1145 Dec 06 '20
The server wouldn't serve me either. I think the leaderboard for part 1 was done before the problem even loaded for me :(
(I also wasted a minute due to using
s.splitlines()instead ofs.split('\n\n')to break my input into groups, but that one's on me.)→ More replies (9)6
u/reesmichael1 Dec 06 '20
I submitted my answer for Part 1 at 12:03, but it took several minutes to go through. My official time under stats is 7:12.
10
u/pred Dec 06 '20 edited Dec 06 '20
Python; would have made some work of the leaderboards today if the servers didn't crash again, and it took five minutes to get past all the 504s (and all of a sudden being logged out).
groups = data.split('\n\n')
# Part one
sum(len(set.union(*map(set, g.split('\n')))) for g in groups)
# Part two
sum(len(set.intersection(*map(set, g.split('\n')))) for g in groups)→ More replies (11)
9
u/0rac1e Dec 06 '20 edited Dec 06 '20
Raku
my @answers = 'input'.IO.slurp.split("\n\n");
put [+] @answers.map: { .words.join.comb.Set }
put [+] @answers.map: { [β©] .words.map(*.comb.Set) }Part 1 could have been written [+] @answers.map: { .comb(/\S/).Set } but I have a habit of avoiding RegEx unless necessary.
Also, doing the plus reduction [+] is the same number of characters as sum... I guess I just felt a little reductionist today.
→ More replies (5)
8
u/Unihedron Dec 06 '20
Ruby 9/31
My input downloader failed so I had to manually open the input text in browser to copy into file lmao
a=$<.read.split("\n\n")
p a.sum{|x|
# x.split.join.chars.uniq.size <- part 1
x.lines.map{|x|x.chomp.chars}.reduce{|x,y|x&y}.size
}8
u/_jonah Dec 06 '20
J, both parts
echo ((1 #. #@~.@;;._2) , (1 #. ([: #@; ([-.-.)&.>/);._2)) <;._2 d→ More replies (6)
8
u/trollerskates1 Dec 06 '20
Raku, Parts 1 & 2
Trying to do all my solutions in functional Raku. I like the middle ground here between readability and conciseness.
sub part-one($group) {
(set $group.subst("\n", '', :g).comb).elems;
}
sub part-two($group) {
[β©] $group.split("\n").map(-> $entry { set $entry.comb });
}
sub MAIN($file, Bool :$p2 = False) {
say [+] $file.IO.lines(:nl-in("\n\n")).map($p2 ?? &part-two !! &part-one);
}→ More replies (2)
7
u/Smylers Dec 06 '20
Vim keystokes β the first line, combining the paragraphs, is copied from day 4 but with an added ! at the end:
:g/^/ ,/\v^$|%$/j!β¨Enterβ©
:%s/\v(.)(.*\1)@=//gβ¨Enterβ©
:%j!β¨Enterβ©
gβ¨Ctrl+Gβ©Your partΒ 1 answer is displayed as βCol 1 of <num>β.
Having got each group on to a single line, the :%s/// removes any character which also appears later in the line, so we have each question letter just once per group of passengers.
Then the total we want is the number of letters remaining in the file. :%j! joins them all into a single line, and gβ¨Ctrl+Gβ© tells us (among other things) how many columns are in that line.
Yes, there are ways of getting that count into the buffer, but it spoils the simplicity of this and I didn't see the need β we have the answer on the screen, available for typing it into the website.
→ More replies (3)
7
u/cggoebel Dec 06 '20
Raku
sub rv (&code) { 'input'.IO.slurp.split("\n\n", :skip-empty).map(&code).sum }
say "One: " ~ rv { .comb(/\S/).Set.elems };
say "Two: " ~ rv { .lines.map({ .comb(/\S/).Set }).reduce(&infix:<β©>).elems };→ More replies (1)
6
u/jonathan_paulson Dec 06 '20
Placed 112/69. Python. Video of me solving at https://youtu.be/e_66g1QcVlE. Code
→ More replies (1)
6
6
u/naclmolecule Dec 06 '20
Python
Exact same function for both parts, just changing union to intersection!
data = [list(map(set, group.splitlines())) for group in raw.split('\n\n')]
def combine_with(func):
return sum(len(reduce(func, group)) for group in data) # functools.reduce
def part_one():
return combine_with(set.union)
def part_two():
return combine_with(set.intersection)→ More replies (5)
6
u/mschaap Dec 06 '20
I love how easy Raku makes this:
sub MAIN(IO() $inputfile where *.f = 'aoc06.input', Bool :v(:$verbose) = False)
{
my @groups = $inputfile.slurp.split(/\n\s*\n/);
my $totalCount = @groups.map(*.comb(/<[a..z]>/).unique.elems).sum;
say $verbose ?? 'Part one: the sum of the counts is: ' !! '',
$totalCount;
my $totalCount2 = @groups.map(-> $g { $g.comb(/<[a..z]>/).Bag.grep(*.value == $g.lines).elems }).sum;
say $verbose ?? 'Part two: the sum of the counts is: ' !! '',
$totalCount2;
}6
u/ImHavik Dec 06 '20
Python
Another horrible (lovely) set of one line solutions
# Part 1
total = sum([len(set("".join(group.split("\n")))) for group in open("input.txt").read().split("\n\n")])
print(f"[P1] Sum of counts: {total}")
# Part 2
total = sum([len(set.intersection(*[set(sub) for sub in group.split("\n")])) for group in open("input.txt").read().split("\n\n")])
print(f"[P2] Sum of counts: {total}")→ More replies (3)
6
u/ianstalk Dec 06 '20 edited Dec 06 '20
My answer for both in Python:
text = open("/Users/ieaston/advent6_input.txt", "r").read().strip()
sets = [[set(member) for member in group.split('\n')] for group in text.split('\n\n')]
print(sum([len(set.union(*l)) for l in sets]))
print(sum([len(set.intersection(*l)) for l in sets]))→ More replies (3)
6
u/Chitinid Dec 06 '20 edited Dec 06 '20
Python 3 short oneliners
Part 1:
with open("input6.txt") as f:
l = f.read().split("\n\n")
sum(len(set.union(*(set(x) for x in group.splitlines()))) for group in l)Part 2:
sum(len(set.intersection(*(set(x) for x in group.splitlines()))) for group in l)→ More replies (8)
5
u/voidhawk42 Dec 06 '20
Dyalog APL, 47/68:
pβ(β’ββ¨(β'')β’Β¨β’)ββNGET'in\6.txt'1
+/β’ββͺββΒ¨p β part 1
+/(β’βββ©/)Β¨p β part 2→ More replies (1)
5
u/azzal07 Dec 06 '20
Awk; loving the simplicity of automatic record & field splitting
BEGIN {
RS=""
FS="\n"
}
{
for (i = 1; i <= NF; i++) {
for (j = split($i, line, ""); j; j--) {
part1 += !(line[j] in ans)
ans[line[j]]++
}
}
for (k in ans) part2 += ans[k] == NF
delete ans
}
END {
print part1
print part2
}5
u/MasterMedo Dec 06 '20
python
Couldn't submit part1, thought the server was dead. github
with open('../input/6.txt') as f:
data = f.read().strip().split('\n\n')
print(sum(len(set.union(*map(set, x.split('\n')))) for x in data))
print(sum(len(set.intersection(*map(set, x.split('\n')))) for x in data))→ More replies (1)
5
u/Arkoniak Dec 06 '20 edited Dec 06 '20
Julia
function part(data, f)
sum(split(data, "\n\n")) do s
split(s, "\n", keepempty = false) .|> Set |> x -> f(x...) |> length
end
end
let data = read("input.txt", String)
for (i, f) in enumerate((union, intersect))
println("Part ", i, ": ", part(data, f))
end
end5
u/deltux Dec 06 '20
Dyalog APL
pβββNGET'input'1
pβ(~pββ'')βp
+/{β’ββͺ/β΅}Β¨p β Part 1
+/{β’ββ©/β΅}Β¨p β Part 2→ More replies (1)
5
u/mount-cook Dec 06 '20
this problem was made for Haskell
import Data.List
import Data.List.Split
parse :: String -> [[String]]
parse = map lines . splitOn "\n\n"
solve1 :: [[String]] -> Int
solve1 = sum . map (length . foldl1 union)
solve2 :: [[String]] -> Int
solve2 = sum . map (length . foldl1 intersect)
day06a = show . solve1 . parse
day06b = show . solve2 . parse5
u/ka-splam Dec 06 '20
APL (Dyalog)
Quite pleased with Part 1:
linesβββNGET 'C:\advent\day6.txt' 1
+/ {β’ βͺβ ,/β΅}Β¨ lines ββ¨ (β'') β’Β¨ linesNot so pleased with Part 2:
+/{rowCountββ’β΅ β β’ ' '~β¨ β {rowCount=β’β΅:βΊ β ''}βΈ ββ΅}Β¨ linesββ¨ linesβ’Β¨β''→ More replies (2)
5
u/Ody55eu5_ Dec 06 '20 edited Dec 06 '20
Python 3
Didn't work on this one until this morning, so to make up for doing it so late I tried to refactor as small as I could. (Also, this is my first post here so let me know if it needs to be edited.)
#Day 6 refactored
allData = origData.split('\n\n')
print("Part 1:",sum([len(set(d.replace('\n',''))) for d in allData]))
print("Part 2:", sum([len(set.intersection(*[set(item) for item in groupData])) for groupData in [d.split('\n') for d in allData]]))`→ More replies (1)
5
u/wjholden Dec 08 '20
Pretty proud of this one. I haven't seen any other Python solutions that pass set.union and set.intersection as a method reference this way for a generalized solution. Maybe there was a reason why others didn't do this...is there a side effect I don't know of?
with open("input.txt") as f:
input = f.read().strip().split('\n\n')
def yes_answers(input, fcn):
for group in input:
yield len(fcn(*(set(s) for s in group)))
input = [line.split() for line in input]
print("Part 1:", sum(yes_answers(input, set.union)))
print("Part 2:", sum(yes_answers(input, set.intersection)))→ More replies (7)
4
u/muckenhoupt Dec 06 '20
Prolog. This one is pretty easy if you have good set functions, which Prolog does. After solving part 2, I reworked my solution to part 1 to show the symmetry.
:- use_module(library(pure_input)).
:- use_module(library(dcg/basics)).
person(Set) --> string_without("\n", Answers),
{
length(Answers, L),
L > 0,
list_to_set(Answers, Set)
}.
group([Answers]) --> person(Answers).
group([Answers|T]) --> person(Answers), "\n", group(T).
groups([Group]) --> group(Group).
groups([Group|T]) --> group(Group), "\n\n", groups(T).
read_input(Groups) --> groups(Groups), "\n", eos.
% Using raw foldl is just a little inconvenient when you're
% dealing with intersections.
reduce(Pred, [H|T], Result) :-
foldl(Pred, T, H, Result).
all_answer_count(Group, Count) :-
reduce(union, Group, Union),
length(Union, Count).
part1(Data, Answer) :-
maplist(all_answer_count, Data, Counts),
sum_list(Counts, Answer).
common_answer_count(Group, Count) :-
reduce(intersection, Group, Intersection),
length(Intersection, Count).
part2(Data, Answer) :-
maplist(common_answer_count, Data, Counts),
sum_list(Counts, Answer).
main :-
phrase_from_stream(read_input(Data), current_input),
part1(Data, Answer1), writeln(Answer1),
!,
part2(Data, Answer2), writeln(Answer2).4
u/CivicNincompoop Dec 06 '20 edited Dec 06 '20
Python
def day6():
with open('data/day6.txt', 'r') as f:
data = [list(map(set, group.splitlines())) for group in f.read().split('\n\n')]
part1 = 0
part2 = 0
for group in data:
part1 += len(set.union(*group))
part2 += len(set.intersection(*group))
print(f"Part1: {part1}")
print(f"Part2: {part2}")
day6()→ More replies (2)
4
4
u/maccosmo Dec 06 '20 edited Dec 07 '20
Ruby, Part 1 and 2 in one line (input filename is called "i")
i=IO.read(?i).split(?\n*2);p i.sum{|x|(x.chars.uniq-[?\n]).size},i.sum{|g|g.split.map(&:chars).inject(:&).size}Or separate:
i=IO.read(?i).split(?\n*2)Part 1
p i.sum{|g|(g.chars.uniq-[?\n]).size}Part 2
p i.sum{|g|g.split.map(&:chars).inject(:&).size}4
u/yarsiemanym Dec 06 '20 edited Dec 06 '20
Set-Based Solution in F#
open System
open System.IO
open System.Text.RegularExpressions
let processPassenger = Set.ofSeq
let processGroup text =
Regex.Split(text, @"\s+", RegexOptions.Singleline)
|> Array.toList
|> List.filter (not << String.IsNullOrWhiteSpace)
|> List.map processPassenger
//|> List.reduce (Set.union)
|> List.reduce (Set.intersect)
let processFlight text =
Regex.Split(text, @"(\s*\n){2,}", RegexOptions.Singleline)
|> Array.toList
|> List.filter (not << String.IsNullOrWhiteSpace)
|> List.map processGroup
let countYesAnswers =
List.map Set.count
>> List.sum
let printAnswer answer = printfn "The answer is '%d'." answer
let findAnswer =
File.ReadAllText
>> processFlight
>> countYesAnswers
>> printAnswer
[<EntryPoint>]
let main argv =
findAnswer argv.[0]
0→ More replies (2)
4
Dec 06 '20 edited Dec 06 '20
My unnecessarily compact Python solution:
groups = [[set(person) for person in group.splitlines()]
for group in open("day_06_input.txt").read().split("\n\n")]
# part 1
print(sum(len(set.union(*group)) for group in groups))
# part 2
print(sum(len(set.intersection(*group)) for group in groups))4
u/mingjim Dec 06 '20 edited Dec 06 '20
F#
let parseGroups =
split (Environment.NewLine + Environment.NewLine)
>> Seq.map (replace Environment.NewLine " ")
>> Seq.map (split " ")
let findAggregate = Seq.fold (+) "" >> Seq.distinct >> Seq.length
let findCommon = Seq.map Set.ofSeq >> Seq.reduce Set.intersect >> Set.count
[<Solution(2020, 6, 1)>]
let part1 fileName =
fileName
|> readText
|> parseGroups
|> Seq.map findAggregate
|> Seq.sum
[<Solution(2020, 6, 2)>]
let part2 fileName =
fileName
|> readText
|> parseGroups
|> Seq.map findCommon
|> Seq.sum→ More replies (3)
4
u/gammaanimal Dec 06 '20 edited Dec 06 '20
Pretty short Python code using sets
Part 1:
with open('6 input.txt') as f:
groups = [set(group.replace('\n', '')) for group in f.read().split('\n\n')]
print(sum([len(x) for x in groups]))Part 2:
with open('6 input.txt') as f:
groups = [[set(member) for member in group.split('\n')] for group in f.read().split('\n\n')]
print(sum([len(set.intersection(*member)) for member in groups]))The set.intersection() method was very helpful and I learned about the * operator to return the list entries today.
Can anyone explain to me why 'abc\ndef'.strip('\n') does not remove the newline?
4
u/Jean-Paul_van_Sartre Dec 06 '20
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INPUT "../input/06.txt"
int main() {
FILE * fp;
char * line = NULL;
size_t len = 0;
int part1 = 0;
int part2 = 0;
int answers[26] = {0};
int people = 0;
fp = fopen(INPUT, "r");
if (fp == NULL) {
perror(INPUT);
exit(EXIT_FAILURE);
}
while (getline(&line, &len, fp) != -1){
line[strcspn(line, "\r\n")] = 0; //remove line breaks.
if(line[0] == '\0') {
for(int i=0; i<26; i++) {
part1 += answers[i]>0;
part2 += answers[i]==people;
answers[i] = 0;
}
people = 0;
} else {
people++;
int length = strlen(line);
for(int i=0; i<length; i++) {
answers[line[i]-'a']++;
}
}
}
for(int i=0; i<26; i++) {
part1 += answers[i]>0;
part2 += answers[i]==people;
}
printf("part 1: %d\n", part1);
printf("part 2: %d\n", part2);
fclose(fp);
free(line);
exit(EXIT_SUCCESS);
}4
u/Dtd03 Dec 06 '20
Python
An easy day with sets :)
in_ = ' '.join(open('data/6.txt').read().splitlines()).split(' ')
print(sum(len(set(a.replace(' ', ''))) for a in in_))
print(sum(len(set.intersection(*[set(x) for x in a.split()])) for a in in_))4
u/thulyadalas Dec 06 '20
My rust solution. Minimal and functional as possible.
use crate::util::get_puzzle_input;
pub fn run() {
let input = get_puzzle_input(2020, 6);
let p1 = part1(&input);
let p2 = part2(&input);
println!("{}", p1);
println!("{}", p2);
}
fn part1(input: &str) -> usize {
input
.split("\n\n")
.map(|x| {
x.chars()
.filter(|c| c.is_ascii_alphabetic())
.collect::<HashSet<char>>()
.len()
})
.sum::<usize>()
}
fn full_answer() -> HashSet<char> {
('a'..='z').collect()
}
fn part2(input: &str) -> usize {
input
.split("\n\n")
.map(|x| {
x.lines()
.map(|e| e.chars().collect::<HashSet<char>>())
.fold(full_answer(), |acc, e| {
acc.intersection(&e).cloned().collect()
})
.len()
})
.sum::<usize>()
}On part2 intersections, I could have used acc.retain(|x| e.contains(x) with making it mut to avoid another allocation by cloning but they have similar performance and I assume compiler is able to optimize it out.
4
u/clouddjr Dec 06 '20
Python3: short one-liners for both parts
from functools import reduce
input = []
with open('input.txt') as f:
input = f.read().split('\n\n')
# part 1
print(sum([len(set("".join(group).replace('\n', ''))) for group in input]))
# #part 2
print(sum([len(reduce(lambda x, y: set(x) & set(y), group.split())) for group in input]))4
u/TweenageDream Dec 06 '20
Tried a few different implementations for this one, and used benchmarking to compare. Golang:
package day6
import (
"aoc/2020/util"
)
type bits int32
func set(b, flag bits) bits { return b | flag }
const (
a = 1 << iota
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
)
var lookup = map[rune]bits{
'a': a,
'b': b,
'c': c,
'd': d,
'e': e,
'f': f,
'g': g,
'h': h,
'i': i,
'j': j,
'k': k,
'l': l,
'm': m,
'n': n,
'o': o,
'p': p,
'q': q,
'r': r,
's': s,
't': t,
'u': u,
'v': v,
'w': w,
'x': x,
'y': y,
'z': z,
}
func Part1() int {
// goos: windows
// goarch: amd64
// pkg: aoc/2020/day6
// BenchmarkPart1MapSet-12 2445 487560 ns/op
// BenchmarkPart1SwitchLookup-12 6309 187363 ns/op
// BenchmarkPart1MapLookup-12 3746 311015 ns/op
// PASS
return part1SwitchLookup()
}
// Slowest, but simplest
func part1MapSet() int {
check := make(map[rune]bool)
var total int
for _, line := range util.QuickReadFull("2020/day6/input.txt") {
if line == "" {
total += len(check)
check = make(map[rune]bool)
continue
}
for _, r := range line {
check[r] = true
}
}
total += len(check)
return total
}
// Fastest but somewhat tedius setup (26 case switch statement)
func part1SwitchLookup() int {
var total int
var current, look bits
for _, line := range util.QuickReadFull("2020/day6/input.txt") {
if line == "" {
total += countSetBits(current)
current = 0
continue
}
for _, ru := range line {
look = switchLookup(ru)
current = set(current, look)
}
}
total += countSetBits(current)
return total
}
// Medium, relatively easy setup
func part1MapLookup() int {
var total int
var current, look bits
for _, line := range util.QuickReadFull("2020/day6/input.txt") {
if line == "" {
total += countSetBits(current)
continue
}
for _, ru := range line {
look = lookup[ru]
current = set(current, look)
}
}
total += countSetBits(current)
return total
}
func Part2() int {
var total int
var current, group, flag bits
group = -1 // A sentinel value
for _, line := range util.QuickReadFull("2020/day6/input.txt") {
if line == "" {
total += countSetBits(group)
group = -1
continue
}
current = 0
for _, ru := range line {
flag = switchLookup(ru)
current = set(current, flag)
}
if group == -1 {
group = current
}
group = current & group
}
total += countSetBits(group)
return total
}
func countSetBits(bitmask bits) int {
var count int
var mask bits = 1
for i := 0; i < 32; i++ {
if (bitmask & mask) == mask {
count++
}
mask = mask << 1
if mask > bitmask {
break // The rest will be zero, lets jet!
}
}
return count
}
func switchLookup(ru rune) bits {
switch ru {
case 'a':
return a
case 'b':
return b
case 'c':
return c
case 'd':
return d
case 'e':
return e
case 'f':
return f
case 'g':
return g
case 'h':
return h
case 'i':
return i
case 'j':
return j
case 'k':
return k
case 'l':
return l
case 'm':
return m
case 'n':
return n
case 'o':
return o
case 'p':
return p
case 'q':
return q
case 'r':
return r
case 's':
return s
case 't':
return t
case 'u':
return u
case 'v':
return v
case 'w':
return w
case 'x':
return x
case 'y':
return y
default:
return z
}
}
Time taken for Day 6 Part 1: 438.8Β΅s Answer: 7120
Time taken for Day 6 Part 2: 368.2Β΅s Answer: 3570→ More replies (2)
4
u/xelf Dec 06 '20
python as a one liner
print([(sum(len(set(g)-{'\n'}) for g in lines),sum(len(set.intersection(*(set(s) for s in g.split()))) for g in lines))for lines in [open(day_06_path).read().split('\n\n')]])Which seems silly as it's strictly longer than the original version in 3 lines. =)
lines = open(day_06_path).read().split('\n\n')
print(sum(len(set(s)-{'\n'}) for s in lines))
print(sum(len(set.intersection(*(set(s) for s in g.split()))) for g in lines))→ More replies (4)
4
u/HashWorks Dec 06 '20
Rust
Two solutions β slow HashSet and fast bitmagic.
https://github.com/hashworks/AoC/blob/master/2020/day6/src/main.rs
→ More replies (5)
4
u/Chitinid Dec 06 '20 edited Dec 06 '20
Python 3 Here's a short solution using collections.Counter
Part 1
with open("input6.txt") as f:
l = f.read()[:-1].split("\n\n")
sum(x != "\n" for c in map(Counter, l) for x in c)Part 2
sum(c[x] > c.get("\n", 0) for c in map(Counter, l) for x in c)4
u/1-more Dec 06 '20
Quick Javascript using Sets. Was in decent shape for a 3min part1 but I forgot to `trim()` and just could not see where the problem would be. So silly.
const groups = $0.innerText.split('\n\n');
const intersect = (l, r) => new Set(
[...l].filter(v => r.has(v))
);
Array.prototype.sumSizes = function() {
return this.reduce((s,v) => s + v.size, 0);
}
console.log({
part1: groups.map(
g => new Set(g.trim().split(/\s?/))
).sumSizes(),
part2: groups.map(
g => g.trim().split('\n').map(x => new Set(x)).reduce(intersect)
).sumSizes()
});4
u/Chris_Hemsworth Dec 06 '20
Every day I try to golf my solution down to as few lines as possible, while still being able to somewhat understand what's going on. Today, using Python 3, I got down to 4 lines:
groups, group = [], []
for line in [line.strip() for line in open('../inputs/day6.txt')] + ['']:
groups, group = (groups + [group], []) if line == '' else (groups, group + [set(list(line))])
print(f"Part 1 Answer: {sum([len(set.union(*g)) for g in groups])}\nPart 2 Answer: {sum([len(set.intersection(*g)) for g in groups])}")→ More replies (3)
4
u/YourVibe Dec 06 '20
Pure C# LINQ Solutions
Part 1:
public long Calculate(List<string> input)
{
return string.Join("\n", input)
.Split("\n\n")
.Select(x => x.Replace("\n", "").ToCharArray().Distinct().Count())
.Sum();
}Part 2:
public long Calculate(List<string> input)
{
return string.Join("\n", input)
.Split("\n\n")
.Select(x => x.Split("\n").Select(l => l.ToCharArray().Distinct()))
.Select(g => g.Aggregate((prev, next) => prev.Intersect(next).ToList()).Count())
.Sum();
}→ More replies (1)
4
u/detsood Dec 06 '20
Rust
#![feature(iterator_fold_self)]
use std::fs;
use std::collections::HashSet;
fn count_group(g: &str) -> usize {
g.lines()
.filter(|l| !l.is_empty())
.map(|l| l.chars().collect::<HashSet<char>>())
.fold_first(|tot, c| &tot & &c)
.unwrap()
.len()
}
fn count(i: &str) -> usize {
i.split("\n\n").fold(0, |tot, g| tot + count_group(g))
}
fn main() {
let i = fs::read_to_string("input/1.txt").expect("unable to read file");
let answer = count(&i);
println!("Answer: {}", answer);
}→ More replies (1)
4
u/4goettma Dec 07 '20
Part 1 / Python 3 (84 Bytes):
c=0
for p in open('i').read().split('\n\n'):c+=len(set(p.replace('\n','')))
print(c)Part 2 / Python 3 (144 Bytes):
c=0
for g in open('i').read().split('\n\n'):
f=g.replace('\n',' ').split()
k=set(f[0])
for i in f[1:]:k=k.intersection(i)
c+=len(k)
print(c)→ More replies (1)
4
u/volatilebit Dec 07 '20 edited Dec 07 '20
Working with sequences vs lists in Raku can be obnoxious at times.
Raku
use v6;
my @groups = $*IN.slurp.trim.split(/\v\v/).map(*.split(/\v/)Β».list);
# Part 1
say @groups.map(*.join.comb.unique).sum;
# Part 2
say @groupsΒ».combΒ».reduce(&infix:<β©>).sum;4
1
u/Flavorless_Quark Dec 06 '20 edited Dec 06 '20
Python 3 - Part 1
inp= open('input').read().rstrip()
alpha = ["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"]
s = 0
inpu = inp.split("\n\n")
for x in inpu:
tot = sum(1 for y in alpha if y in x)
s +=tot
print(s)Part 2
inp= open('input').read().rstrip()
alpha = ["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"]
s = 0
inpu = inp.split("\n\n")
for x in inpu:
line = x.split("\n")
for letter in alpha:
if all([letter in z for z in line ]):
s+=1
print(s)
I think it'd have done a lot better if I didn't keep misspelling my variables, also had to write the alphabet....→ More replies (5)
1
u/zdu863 Dec 06 '20
Python
part1
with open('input.txt','r') as f:
data=f.read().splitlines()
ans=0
cur=set()
for line in data:
if line:
cur|=set(line)
else:
ans+=len(cur)
cur.clear()
ans+=len(cur)
print(ans)part2
with open('input.txt','r') as f:
data=f.read().splitlines()
ans=0
cur=set('abcdefghijklmnopqrstuvwxyz')
for line in data:
if line:
cur&=set(line)
else:
ans+=len(cur)
cur=set('abcdefghijklmnopqrstuvwxyz')
ans+=len(cur)
print(ans)1
u/daggerdragon Dec 06 '20
Your code is hard to read on old.reddit. As per our posting guidelines, would you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks?
Put four spaces before every code line. (If you're using new.reddit, click the button in the editor that says "Switch to Markdown" first.)
[space space space space]public static void main()[space space space space][more spaces for indenting]/* more code here*/turns into
public static void main() /* more code here */Alternatively, stuff your code in /u/topaz2078's
pasteor an external repo instead and link to that instead.
1
1
u/captainAwesomePants Dec 06 '20 edited Dec 06 '20
Python
I missed the leaderboard by a wide margin, but I'm pretty happy with the solution I ended up with.
from collections import Counter
import functools
def main():
all_yes = 0
for group in [x.splitlines() for x
in (open('input.txt').read() + "\n").split("\n\n")]:
g = Counter(''.join(group))
all_yes += len([v for v in g.values() if v == len(group)])
print(all_yes)→ More replies (1)
1
u/raevnos Dec 06 '20 edited Dec 06 '20
Chicken scheme. Using SRFI-14 character sets made this really easy. The input parsing took more lines of code than the part actually calculating the answers.
#!/usr/local/bin/csi -s
(import
(chicken format)
(chicken io)
(srfi 1)
(srfi 14))
(define (read-group #!optional (port (current-input-port)))
(let loop ((answers '())
(line (read-line port)))
(if (or (eof-object? line) (string=? line ""))
answers
(loop (cons (string->char-set line) answers) (read-line port)))))
(define (read-groups #!optional (port (current-input-port)))
(let loop ((groups '()))
(let ((new-group (read-group port)))
(if (null? new-group)
groups
(loop (cons new-group groups))))))
(define (solve reduce-op groups)
(fold (lambda (group total)
(+ total (char-set-size (reduce reduce-op (char-set) group))))
0 groups))
(define (solve1 groups) (solve char-set-union groups))
(define (solve2 groups) (solve char-set-intersection groups))
(define input (read-groups))
(printf "Part 1: ~A~%" (solve1 input))
(printf "Part 2: ~A~%" (solve2 input))→ More replies (1)
1
u/Peuniak Dec 06 '20
Pyhton 3
# part 1
print(sum([len(set(line.replace("\n", ""))) for line in open("input.txt", 'r').read().split("\n\n")]))
# part 2
print(sum([sum([all([answer in other_person_answers for other_person_answers in group_answers]) for answer in group_answers[0]]) for group_answers in [list(line.split("\n")) for line in open("input.txt", 'r').read().split("\n\n")]]))→ More replies (1)
1
u/higgsmass2020 Dec 06 '20
from collections import Counter
fn='input.06' #today's input
# part 1
l = open(fn).read().split('\n\n')
print ( 'part 1:', sum ( [ len(Counter(i.replace('\n','')).keys()) for i in l ] ) )
# part 2
y = [ i.strip().split('\n') for i in l ]
print ('part 2:', sum ([ len([ j for j in Counter(''.join(i)).values() if j == len(i) ]) for i in y ]))→ More replies (1)
1
u/Pandabweer Dec 06 '20
PHP
<?php$firstvalidΒ =Β 0;$totalΒ =Β 0;foreachΒ (preg_split("#\n\s*\n#Uis",Β file_get_contents("input.txt"))Β asΒ $inp)Β {$dataΒ =Β "";foreachΒ (explode("\n",Β $inp)Β asΒ $exin2)Β {$data .= preg_replace("/\s+/",Β "",Β $exin2);
Β Β Β Β }foreachΒ (count_chars($data,Β 1)Β asΒ $charΒ =>Β $val)Β {$firstvalid++;
Β Β Β Β }
ifΒ (stristr($inp,Β "\n"))Β {$answΒ =Β array();foreach(explode("\n",Β $inp)Β asΒ $p)Β {for($iΒ =Β 0;Β $iΒ <Β strlen($p);Β $i++)Β {if(preg_match('/[a-z]/',Β $p[$i]))Β {if(@!$answ[$p[$i]])Β {$answ[$p[$i]]Β =Β 1;
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β }Β elseΒ {$answ[$p[$i]]++;
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β }
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β }
Β Β Β Β Β Β Β Β Β Β Β Β }
Β Β Β Β Β Β Β Β }foreachΒ ($answΒ asΒ $kΒ =>Β $v)Β {ifΒ ($vΒ ==Β count(explode("\n",Β $inp)))Β {$total++;
Β Β Β Β Β Β Β Β Β Β Β Β }
Β Β Β Β Β Β Β Β }
Β Β Β Β }Β elseΒ {preg_match('/[a-z]+/',Β $inp,Β $matches);$totalΒ +=Β strlen($matches[0]);
Β Β Β Β }}echo "PartΒ 1:<br>" . $firstvalid . "<br><br>PartΒ 2:<br>" . $total;?>
→ More replies (1)
1
u/ReplyNo7609 Dec 06 '20
hey there!
with AoC I am just learning python. I saw some of very amazing solutions, which i had to try step by step to even understand what is happening! :D
I would be glad if you could comment my code and help improving my skills. Thanks!
btw: with this code, you need two blank lines at the end of the input
inputfile = open('input.txt', 'r')
data = [x.strip() for x in inputfile.readlines()]
dictionary={}
alphabet=list(string.ascii_lowercase)
for each in alphabet:
dictionary[each]=0
#-----------------------------Part1-------------------------------
yescounts=[]
for line in data:
if line=='':
yescounts.append(sum(list(dictionary.values())))
for x in dictionary:
dictionary[x]=0
continue
else:
for letter in line:
if [letter in dictionary]:
dictionary[letter]=1
print('Answer Part 1: %d' % sum(yescounts))
#-----------------------------Part2-------------------------------
def checkyes(dic, n):
count=0
if n>0:
for each in (list(dic.values())):
#print(each)
if each==n:
count+=1
return count
else:
return 0
yescounts=[]
people=0
for each in data:
if each=='':
yescounts.append(checkyes(dictionary, people))
people=0
for x in dictionary:
dictionary[x]=0
continue
else:
people+=1
for every in each:
if [every in dictionary]:
dictionary[every]+=1
print('Answer Part 2: %d' % sum(yescounts))→ More replies (3)
1
u/bgclara Dec 06 '20
Readable Python solution:
with open('6.txt', 'r') as file: data = file.read().split('\n\n')
def part_A(data): newdata=[] count = 0 totalcount = 0 for i in data: newdata.append(i.replace('\n', ''))
for i in newdata: s=set(i) count = len(s) totalcount+=count return totalcount
def part_B(data): totalcountb = 0 for i in data: newlist = [] countb = 0 newlist.append(i.split('\n')) for j in newlist: count = len(set.intersection(*[set(k) for k in j])) totalcountb +=count return totalcountb
print('Part A: The sum of the counts is: ', part_A(data)) print('Part B: The sum of the counts is: ', part_B(data))
→ More replies (1)
1
u/TheTechWolf Dec 06 '20
fun task1(input: List<String>): Int {
if (input.isEmpty()) return 0
val answersOfAllPersonsInGroup: List<List<String>> = input.takeNextGroup()
.parseIndividualAnswers()
val uniqueAnswersThatAnyoneAnswered: Set<String> = answersOfAllPersonsInGroup
.flatten()
.toSet()
return uniqueAnswersThatAnyoneAnswered.count() + task1(input.drop(answersOfAllPersonsInGroup.size + 1))
}
fun task2(input: List<String>): Int {
if (input.isEmpty()) return 0
val answersOfAllPersonsInGroup: List<List<String>> = input.takeNextGroup()
.parseIndividualAnswers()
val uniqueAnswersThatEveryoneAnswered: Set<String> = answersOfAllPersonsInGroup
.fold(answersOfAllPersonsInGroup.first().toSet()) { currentAnswers, acc -> currentAnswers.intersect(acc) }
return uniqueAnswersThatEveryoneAnswered.count() + task2(input.drop(answersOfAllPersonsInGroup.size + 1))
}
private fun List<String>.takeNextGroup() = takeWhile { it.isNotEmpty() }
/**
* This operates on all answers from a single group
* Every person answer string is split and converted to a list of individual answers
* The function returns the list of answers of every person in the group
*/
fun List<String>.parseIndividualAnswers(): List<List<String>> = map { answers ->
answers.split("")
.filterNot { it.isBlank() }
}→ More replies (2)
1
u/kkent4 Dec 06 '20
Python Solution
from collections import Counter
unique_resp = 0
c = Counter()
num_people = -1
all_resp = 0
with open(r"Inputs/day_6.txt") as openfileobject:
for line in openfileobject:
if line == "\n":
unique_resp = unique_resp + len(c.keys())
all_resp = all_resp + sum([val == num_people for val in c.values()])
num_people = -1
c = Counter()
line = line.replace('\n','')
for char in line:
c[char]+=1
num_people+=1
# Part 1
print("Part 1:", unique_resp + len(c.keys())) #to get the last one
# Part 2
print("Part 2:", all_resp + sum([val == num_people for val in c.values()])) #to get the last one→ More replies (2)
1
u/rainbowDudeAlltheway Dec 06 '20 edited Dec 11 '20
static void Main(string[] args)
{
var lines = File.ReadAllLines("6.txt").ToList();
int count = 0;
int peopleCount = 0;
Dictionary<char, int> answers = new Dictionary<char, int>();
foreach (var line in lines)
{
if (line == "")
{
count += answers.Count(x => x.Value == peopleCount);
answers = new Dictionary<char, int>();
peopleCount = 0;
continue;
}
foreach (var letter in line)
{
if (answers.ContainsKey(letter))
{
answers[letter] += 1;
}
else
{
answers[letter] = 1;
}
}
++peopleCount;
}
Console.WriteLine(count);
}C# 9.0
C sharp
→ More replies (3)
1
u/Kuter5551 Dec 06 '20
My Python answer. Managed to make the reading from the file as many lines as the actual calculation
import collections
from functools import reduce
answers= []
person = []
with open("Day6Inputs.txt") as file:
for row in file:
person.append(row.strip().split(" "))
if len(row.strip()) == 0:
person.pop()
answers.append(reduce(lambda n, m: n + m, person))
person = []
answers.append(reduce(lambda n, m: n + m, person))
val = 0
val2 = 0
for item in answers:
_cur = ""
for thing in item:
_cur += thing
#get the amount of different items
val+=len(collections.Counter(_cur))
#check if the amount of each question is equal to the amount of people who asked it
for value in list(collections.Counter(_cur).items()):
val2 += 1 if value[1]/len(item) == 1 else 0
print(f"Part 1: {val}")
print(f"Part 2: {val2}")
→ More replies (1)
1
u/Zexion01 Dec 06 '20 edited Dec 06 '20
Python 3 solution:
from functools import reduce
INPUT_FILE = "input"
def part_1():
with open(INPUT_FILE, "r") as fp:
lines = fp.readlines()
counter = 0
current_answers = set()
for line in lines:
if line == "\n":
counter += len(current_answers)
current_answers.clear()
else:
current_answers.update(set(line[:-1]))
return counter
def part_2():
with open(INPUT_FILE, "r") as fp:
lines = fp.readlines()
counter = 0
current_answers = []
for line in lines:
if line == "\n":
counter += len(reduce(lambda x, y: x.intersection(y), current_answers))
current_answers.clear()
else:
current_answers.append(set(line[:-1]))
return counter
if __name__ == "__main__":
result = part_1()
print(result)
result = part_2()
print(result)https://github.com/AlinMH/advent-of-code/blob/master/2020/day06/main.py
→ More replies (1)
1
Dec 06 '20 edited Dec 07 '20
There has to be a better way... Any ideas on what could be done to improve the map() where I've left a comment -- it feels dirty.
https://github.com/zameericle/AdventofCode2020/blob/main/Day6/Part2.js
/* I hate this -- there must be a better way to do this */
var filteredResponse = []
Object.keys(val[1]).forEach((key) => {
if (val[1][key] === val[0]) {
filteredResponse[key] = val[1][key]
}
})
return Object.keys(filteredResponse).length→ More replies (3)
1
Dec 07 '20
Java Implementation both parts
package com.gubatron.aoc._2020;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import static com.gubatron.aoc._2020.Utils.readStringList;
import static com.gubatron.aoc._2020.Utils.readStringsBySeparator;
public class Day06 {
static class Group {
List<String> answers = new ArrayList<>();
Group(List<String> answers_p) {
answers.addAll(answers_p);
}
long countCommonAnswers() {
if (answers == null || answers.size() == 0) {
return 0;
}
if (answers.size() == 1) {
return answers.get(0).length();
}
Map<Integer, Integer> commonChars = new HashMap<>();
answers.forEach(s -> {
s.chars().boxed().forEach(c ->
commonChars.merge(c, 1, Integer::sum));
});
return commonChars.entrySet().stream().filter(entry -> entry.getValue() == answers.size()).count();
}
}
public static long part1(List<String> groupLines) {
return groupLines.stream().map(groupLine -> groupLine.chars().filter(c -> c != '\n').distinct().count()).reduce(Long::sum).get();
}
// Convert List<String> -> Stream<Group> -> Stream<Long> -> sum it
public static long part2(List<String> groupLines) {
return groupLines.stream().map(groupLine -> {
List<String> answers = new ArrayList<>();
Collections.addAll(answers, groupLine.split("\n"));
return new Group(answers);
}
).map(Group::countCommonAnswers).reduce(Long::sum).get();
}
public static void main(String[] args) throws IOException {
List<String> splitLines = readStringsBySeparator(new File("resources/input_day_06.txt"), "\n\n");
System.out.println("DAY 06");
System.out.println("Part 1: " + part1(splitLines) + " (Expected: 6297)");
System.out.println("==============================");
System.out.println("Part 2: " + part2(splitLines) + " (Expected: 3158)");
}
}→ More replies (2)1
u/daggerdragon Dec 07 '20
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/dedolent Dec 07 '20 edited Dec 07 '20
Python
My first 2-line solution (for part 1) though I consider it pretty ugly. Considered condensing down part 2 as well but honestly I'd rather just have a few defined helper variables for readability's sake, not that that's important here.
2
u/daggerdragon Dec 07 '20
Your Markdown is showing. Did you switch to "Markdown mode" in your editor?
→ More replies (1)
1
u/MatSkapski Dec 07 '20 edited Dec 07 '20
C# using Linq
static void Main(string[] args)
{
FileParser parser = new FileParser(Path.Combine(PathHelper.ProjectRootFolder(), "Input.txt"), $"{Environment.NewLine}{Environment.NewLine}");
var groups = parser.ToStringList();
int yesAllQuestionsCount = CountAllYesAnswers(groups);
Console.WriteLine($"(Yes) all answers: {yesAllQuestionsCount}");
int sameQuestionAnswersCount = CountSameQuestionAnswers(groups);
Console.WriteLine($"(Yes) same questions all persons: {sameQuestionAnswersCount}");
}
private static int CountAllYesAnswers(List<string> groups)
{
int answersCount = 0;
groups.ForEach(group => answersCount += group.Replace(Environment.NewLine, string.Empty).Select(x => x).Distinct().Count());
return answersCount;
}
private static int CountSameQuestionAnswers(List<string> groups)
{
int sameQuestionAnswersCount = 0;
groups.ForEach(group => sameQuestionAnswersCount += group.Replace(Environment.NewLine, string.Empty)
.GroupBy(x => x)
.Select(x => new { Letter = x.Key, Count = x.Count() })
.Where(x => x.Count == group.Split(Environment.NewLine).Length)
.OrderBy(x => x.Letter)
.Select(x => x)
.Count());
return sameQuestionAnswersCount;
}→ More replies (2)1
u/daggerdragon Dec 07 '20
Please follow the posting guidelines and edit your post to add what language(s) you used. This makes it easier for folks who Ctrl-F the megathreads looking for a specific language.
3
u/hugh_tc Dec 06 '20 edited Dec 09 '20
Python 3, 103/102.
Man that was terrifying; submitting these random-looking answers to the server. And one second too late for the leaderboard, too!
def solve(responses):
a1, a2 = 0, 0
for response in responses:
people = [set(p) for p in response.split("\n")]
a1 = a1 + len(set.union(*people))
a2 = a2 + len(set.intersection(*people))
return (a1, a2)edit: probably should use set.union for Part 1, too.
→ More replies (4)
3
u/spohngellert_o Dec 06 '20
Thought today was pretty easy :). Scala solutions:
Part 1
import scala.io.Source
object Main {
def main(args: Array[String]): Unit = {
val file = Source.fromFile("input")
val groups = file.mkString.split("\n\n")
println(groups.map(g => g.filter(c => "^[a-z]$".r.matches(c.toString)).toSet.size).sum)
file.close()
}
}Part 2
import scala.io.Source
object Main {
def main(args: Array[String]): Unit = {
val file = Source.fromFile("input")
val groups = file.mkString.split("\n\n")
println(groups.map(g => {
val subgs = g.split("\n")
subgs.head.map(c => subgs.forall(ans => ans.contains(c))).filter(v => v).size
}).sum)
file.close()
}
}3
u/jayfoad Dec 06 '20
Dyalog APL 576/126
pβ'\w+'βS'&'Β¨'(\n?[^\n])+'βS'&'β 'Mode' 'D'ββNGET'p6.txt'
β’ββͺ/Β¨p β part 1
β’ββ©/Β¨p β part 2The hardest part was splitting the input.
→ More replies (2)
3
u/daftmaple Dec 06 '20 edited Dec 06 '20
Perl
This is my code for question 2. Question 1 pretty much uses the same code
#!/usr/bin/perl -w
@array = ();
@count = ();
$index = 0;
open F, "q06_input.txt" or die;
while (<F>) {
chomp $_;
if (length($_) == 0) {
$index++;
} else {
$array[$index] .= $_;
$count[$index]++;
}
}
close F;
$count = 0;
for (my $idx = 0; $idx < scalar(@array); $idx++) {
my $str = $array[$idx];
my %hash;
for my $i (0..length($str)-1) {
my $char = substr($str, $i, 1);
$hash{$char}++;
}
my $ct = 0;
for my $i ("a".."z") {
# For question 1:
# $ct++ if defined $hash{$i};
# For question 2:
$ct++ if defined $hash{$i} and $hash{$i} == $count[$idx];
}
$count += $ct;
}
print "$count\n";I'm also fairly sure that there's a more efficient way to do this in Perl. I can think of an efficient way for Q1 with regex.
Edit: nevermind. The regex is probably harder...
→ More replies (2)
3
u/chrispsn_ok Dec 06 '20
k9
input: 0:`6.txt
+/(#?,/)'a:(~#')_'(@[;0;1b]@~#')^input
+/(##/)'a→ More replies (1)
3
u/LtHummus Dec 06 '20
Scala
Sets are cool and good
import util.ReadResourceString
object CustomsConfusion extends App {
ReadResourceString("input2020/day06") { input =>
val groups = input.split("\n\n")
println(groups.map { currGroup =>
currGroup
.split("\n")
.map(_.toSet)
.fold(Set.empty[Char])(_.union(_))
.size
}.sum)
println(groups.map { currGroup =>
val responseSets = currGroup
.split("\n")
.map(_.toSet)
responseSets
.fold(responseSets.head)(_.intersect(_))
.size
}.sum)
}
}3
u/JIghtuse Dec 06 '20
Racket
One more nice thing about AoC: you dig into many aspects of the tool you use to solve problems. Never worked with sets in Racket before.
#lang racket
(define DATA-FILE "/path/to/input.txt")
(define INPUT-LINE (file->string DATA-FILE))
(define GROUPS-DATA (string-split INPUT-LINE "\n\n"))
(define (string->set s)
(list->set (string->list s)))
(for/sum
([unique-group-answers
(for/list ([group GROUPS-DATA])
(for/fold ([answers (set)])
([man-answers (string-split group)])
(set-union (string->set man-answers) answers)))])
(set-count unique-group-answers))
(for/sum
([common-group-answers
(for/list ([group GROUPS-DATA])
(let ([first-man-answers (string->set (car (string-split group)))])
(for/fold ([s first-man-answers])
([man-answers (string-split group)])
(set-intersect s (string->set man-answers)))))])
(set-count common-group-answers))3
u/IridianSmaster Dec 06 '20
Part 2 in OCaml:
open Core
open Stdio
let tally_group group =
let collect = String.fold ~f:Set.add ~init:(Set.empty (module Char)) in
let responses =
let people = String.split_lines group in
List.map people ~f:collect
in
List.fold responses ~init:(List.hd_exn responses) ~f:Set.inter
|> Set.count ~f:(Fn.const true)
let custom_customs input =
Str.split_delim (Str.regexp "\n\n") input
|> List.map ~f:tally_group
|> List.sum (module Int) ~f:Fn.id3
u/__Juris__ Dec 06 '20 edited Dec 06 '20
Scala 3
import scala.io.Source
object Advent06 extends App:
opaque type Answer = Char
opaque type Form = Set[Answer]
opaque type Group = Set[Form]
val groups: List[Group] = Source
.fromResource("06.txt")
.getLines()
.mkString("\n")
.split("\n\n")
.map(_.split("\n").map(_.toSet).toSet)
.toList
type MergeFunction[T] = (Set[T], Set[T]) => Set[T]
val results = List[MergeFunction[Answer]](
_.union(_),
_.intersect(_),
).map { f => groups.map(_.reduce(f).size).sum }
results foreach println→ More replies (2)
3
u/s3aker Dec 06 '20
Raku
my @a = 'input.txt'.IO.slurp.split(/\n\n/);
put 'part 1: ', @aΒ».comb(/<[a..z]>/)Β».uniqueΒ».elems.sum;
put 'part 2: ', @aΒ».&{ [(&)] $_.split(/\n/,:skip-empty)Β».combΒ».Set }Β».elems.sum;3
u/ianonavy Dec 06 '20 edited Dec 06 '20
Python golf 149 bytes
c=open(0).read().split("\n\n")
print(sum(len(set(d)-{"\n"})for d in c))
print(sum(len(set.intersection(*map(set,d.strip().split("\n"))))for d in c))Edit: My editor stripped the extra newline at the end of the input, so you can actually remove the .strip() and get down to 141 bytes.
→ More replies (1)
3
u/masterarms Dec 06 '20
Tcl
proc parts input {
set result1 0
set result2 0
set data [ split [string map [list \n\n \f] [string trim $input]] \f]
set answers {}
foreach group $data {
set answers [lmap x [split $group \n] {split $x {}}]
#puts $answers
incr result1 [llength [struct::set union {*}$answers]]
incr result2 [llength [struct::set intersect {*}$answers]]
}
return [list $result1 $result2]
}
aoc::results→ More replies (2)
3
u/michaelgallagher Dec 06 '20
Python
One liners for both parts
with open('06.txt', 'r') as file:
data = file.read().split('\n\n')
def part_one(data):
return sum(len(set.union(*[set(s) for s in group.split()])) for group in data)
def part_two(data):
return sum(len(set.intersection(*[set(s) for s in group.split()])) for group in data)
print(f'Part 1: {part_one(data)}') # 6775
print(f'Part 2: {part_two(data)}') # 33563
u/zertillon Dec 06 '20
Ada
with Ada.Strings.Fixed, Ada.Text_IO;
procedure AoC_2020_06_Full_Ada is
total : Integer;
new_group : Boolean;
subtype Answer_Range is Character range 'a' .. 'z';
type Yes_Answer is array (Answer_Range) of Boolean;
r, rg : Yes_Answer;
--
procedure Collect_Group_Total is
g : Natural := 0;
begin
for c in Answer_Range loop if rg (c) then g := g + 1; end if; end loop;
total := total + g;
new_group := True;
end Collect_Group_Total;
--
use Ada.Strings.Fixed, Ada.Text_IO;
f : File_Type;
begin
for part in 1 .. 2 loop
Open (f, In_File, "aoc_2020_06.txt");
total := 0;
new_group := True;
while not End_Of_File (f) loop
declare
s : constant String := Get_Line (f);
begin
if s = "" then
Collect_Group_Total;
else
for c in Answer_Range loop
r (c) := Index (s, (1 => c)) > 0;
end loop;
if new_group then
rg := r;
new_group := False;
elsif part = 1 then
rg := rg or r;
else
rg := rg and r;
end if;
end if;
end;
end loop;
Collect_Group_Total;
Put_Line ("Part" & Integer'Image (part) & ' ' & Integer'Image (total));
Close (f);
end loop;
end AoC_2020_06_Full_Ada;3
u/Smylers Dec 06 '20 edited Dec 06 '20
Perl for part 1 is pretty readable as a one-liner:
perl -MList::Util=uniq -n00 -E '$a += uniq /(\w)/g; END { say $a }' inputFor part 2, still process each βparagaraphβ at a time, splitting into individual characters, counting the number of each, and counting those where the number of a letter equals the number of new-line characters in that paragraph:
use v5.14; use warnings; no warnings qw<uninitialized>;
$/ = '';
my $total;
while (<>) {
chomp;
my %q_count;
$q_count{$_}++ foreach split //;
my $passengers = (delete $q_count{"\n"}) + 1;
$total += grep { $_ == $passengers } values %q_count;
}
say $total;The slight awkwardness is the chomp and the +1: without the chomp, the paragraph includes as many trailing new-line characters as there are (2 after most of them, but just 1 at the end of the final para). chomp removes all of those, leaving the final line in the para without a \n, so the total number of passengers is 1 more than the number of \ns counted.
Edit: Removed sort from the one-liner; Perl's uniq isn't like Unix's uniq(1).
Edit 2: Removed backslashes from the first edit, where I apparently typed Markdown syntax in Fancy Pants mode.
3
u/shandley256 Dec 06 '20 edited Dec 06 '20
Day 6 in Ruby
Golfed into chained calls. This is an easy one to solve with Ruby built-in methods.
input.
split("\n\n").
map { |e| e.split.map { |f| f.chars } }.
tap { |r| p r.sum { |g| g.reduce(:|).count } }.
tap { |r| p r.sum { |g| g.reduce(:&).count } }This outputs answers for part 1 and part 2.
The keys to this solution are the set operations | (union) and & (intersection). Applying these via reduce has the effect of checking each passenger's answer's within their group to find the total number of unique answers in the group, and then the total number of answers common to each passenger in the group.
See: https://ruby-doc.org/core-2.7.2/Enumerable.html#method-i-reduce
See: https://ruby-doc.org/stdlib-2.7.2/libdoc/set/rdoc/Set.html#method-i-26
See: https://ruby-doc.org/stdlib-2.7.2/libdoc/set/rdoc/Set.html#method-i-7C
→ More replies (3)
3
Dec 06 '20
F#:
As normal it's a bit over engineered, but a lot shorter than yesterday at least the code on github
→ More replies (6)
3
u/qzyki Dec 06 '20
Dyalog APL (19 characters for each main function)
Part 1:
d6p1β{
β Given a file path as β΅, read in the text (newlines as empty elements).
dβββNGET β΅ 1
β Get the union of elements in each empty-separated group,
β and total all remaining.
fβ{+/β’¨¨βͺ/Β¨β΅ββ¨~β΅ββ''}
β Apply the function to the data.
f d
} Part 2:
d6p2β{
β Given a file path as β΅, read in the text (newlines as empty elements).
dβββNGET β΅ 1
β Get the intersection of elements in each empty-separated group,
β and total all remaining.
fβ{+/β’¨¨β©/Β¨β΅ββ¨~β΅ββ''}
β Apply the function to the data.
f d
}3
u/exploding_cat_wizard Dec 06 '20 edited Dec 06 '20
Mathematica
I wonder if I'm doing redundant work - I generally just have to fool around in Mathematica until something works, it's not like I understand what's going on, Also, I should really stop mixing shorthand /@ and full expressions for Map...
Total[Length /@
Union /@ (
StringSplit[#, ""] & /@
StringReplace[
StringSplit[
Import[NotebookDirectory[] <> "input", "String"],
"\n\n"],
"\n" -> ""]
)
]
Total[Length /@
Apply[Intersection,
Apply[StringSplit[#, ""] &,
Map[List, #] & /@ (
StringSplit[#] & /@
StringSplit[Import[NotebookDirectory[] <> "input", "String"], "\n\n"]
),
{2}],
{1}]
]3
u/100721 Dec 06 '20 edited Dec 06 '20
Python 3 - one liner
def part2():
print(sum([len(set.intersection(*[set(t) for t in i.split("\n")])) for i in file.read().split("\n\n")]))
def part1():
print(sum([len(set.union(*[set(t) for t in i.split("\n")])) for i in file.read().split("\n\n")]))→ More replies (1)
3
u/diddle-dingus Dec 06 '20
Thank god for sets lol.
Elixir
defmodule AdventOfCode.Day06 do
def set_from_input(input) do
String.split(input, ~r/\n\n/)
|> Enum.map(fn x -> String.split(x)
|> Enum.map(&(String.to_charlist(&1)
|> MapSet.new)) end)
end
def part1(args) do
set_from_input(args)
|> Enum.map(fn x -> Enum.reduce(x, &MapSet.union(&1, &2)) |> Enum.count end)
|> Enum.sum
end
def part2(args) do
set_from_input(args)
|> Enum.map(fn x -> Enum.reduce(x, &MapSet.intersection(&1, &2)) |> Enum.count end)
|> Enum.sum
end
endClojure
(ns aoc-clojure-2020.day-06
(:require [aoc-clojure-2020.helpers :refer [get-input]]
[clojure.string :as s]
[clojure.set :refer [union intersection]]))
(def input (as-> (get-input 6) i
(s/split i #"\n\n")
(map #(->> (s/split-lines %) (map set)) i)))
(def part-1 (reduce #(+ %1 (count (apply union %2))) 0 input))
(def part-2 (reduce #(+ %1 (count (apply intersection %2))) 0 input))3
u/Zweedeend Dec 06 '20
Python
from functools import reduce
groups = open("day6.txt").read().split("\n\n")
def count(group):
return reduce(set.union, map(set, group.split()))
questions = map(count, groups)
print(sum(map(len, questions)))And for part two, change union to intersection
3
u/nutki2 Dec 06 '20
Perl (golf) for both parts
#!perl -ln0aF\n\n
for$l(a..z){
$y+=map/$l/,@F;
$z+=grep{(split)==map/$l/,split}@F
}
print"$y $z";→ More replies (2)
3
u/troelsbjerre Dec 06 '20
Python3 one liner for both parts, without map, reduce, or lambda:
print(*(sum(len(op(*(set(l) for l in g.split()))) for g in data) for data in (sys.stdin.read().split('\n\n'),) for op in (set.union, set.intersection)),sep='\n')3
u/Vultureosa Dec 06 '20 edited Dec 06 '20
Python
Again, a short python script for Day6.
groups = forms.split("\n\n")
print("Part1 answer: {}\nPart2 answer: {}".format(sum([len(set(answ.replace("\n", ""))) for answ in groups]),
sum([len(group_ans.pop().intersection(*group_ans)) for group_ans in [[set(answ) for answ in group.split("\n")] for group in groups]])))→ More replies (3)
3
u/nemetroid Dec 06 '20 edited Dec 06 '20
C++, using bitsets and std::popcount():
#include <bit>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char **argv)
{
if (argc != 2)
return 1;
ifstream input_stream{argv[1]};
if (!input_stream)
return 1;
string line;
int sum_any{};
int sum_all{};
unsigned int seen_in_group{};
// perhaps rather "not not seen by anyone in group".
// set all bits to 1 when starting a new group (even the bits above the
// 26th, they will be zeroed out by the algorithm as long as the group
// has at least one member).
unsigned int seen_by_all_in_group = -1;
while (getline(input_stream, line)) {
if (line.empty()) {
sum_any += popcount(seen_in_group);
sum_all += popcount(seen_by_all_in_group);
seen_in_group = 0;
seen_by_all_in_group = -1;
continue;
}
unsigned int seen_by_person{};
for (const auto ch : line) {
seen_by_person |= 1 << (ch - 'a');
}
seen_in_group |= seen_by_person;
seen_by_all_in_group &= seen_by_person;
}
sum_any += popcount(seen_in_group);
sum_all += popcount(seen_by_all_in_group);
printf("part 1: %d\n", sum_any);
printf("part 2: %d\n", sum_all);
return 0;
}→ More replies (1)
3
3
u/ilikecodiing Dec 06 '20
F#
I need to figure out a better way to process the incoming data. I seem to spend more time doing that than doing the calculations.
// Advent of Code 2020 - Day 06
open System
open System.IO
let allQuestions = seq [ 'a' .. 'z' ] |> List.ofSeq
let interect a b =
Set.intersect (Set.ofList a) (Set.ofList b)
let lines =
File.ReadAllText(@".\my-input.txt")
let input =
lines.Split([| "\013\010\013\010" |], StringSplitOptions.None)
|> Seq.ofArray
|> Seq.map Seq.sort
|> Seq.map List.ofSeq
|> List.ofSeq
|> List.map (fun cl -> interect allQuestions cl)
|> List.map (fun a -> Set.toList a)
|> List.map (fun a -> a.Length)
|> List.sum
printfn "The answer to part 1 is %i" input
// Part 2
let count (c: char) (s: string) =
s |> Seq.filter (fun a -> a = c) |> Seq.length
let allQuestionChecker (sl: string list) =
let peopleInGroup = sl.Length
let questionTotal (s: string) =
allQuestions
|> List.map (fun c -> count c s)
|> List.filter (fun i -> i = peopleInGroup)
|> List.length
sl
|> List.reduce (fun acc s -> acc + s)
|> questionTotal
let input2 =
lines.Split([| "\013\010\013\010" |], StringSplitOptions.None)
|> List.ofArray
|> List.map (fun s -> s.Split([| "\013\010" |], StringSplitOptions.None))
|> List.map (fun a -> List.ofArray a)
|> List.map (fun s -> allQuestionChecker s)
|> List.sum
printfn "The answer to part 2 is %i" input23
u/Jessseee Dec 06 '20
I know it's not the most pythonic solution but I think it is pretty readable and straightforward. Github | Day 6
# Day 6 Advent of Code
# Check customs declaration forms
file_name = "input/input_day_6.txt"
def count_group_unique_answers(group):
group = group.replace('\n', '')
return len(set(group))
def count_group_matching_answers(group):
forms = group.split('\n')
matching_answers = set(forms[0])
for form in forms:
matching_answers = matching_answers.intersection(set(form))
return len(matching_answers)
if __name__ == '__main__':
with open(file_name) as f:
groups = f.read().split('\n\n')
print(f'Sum of unique answers of every group:
{sum(map(count_group_unique_answers, groups))}')
print(f'Sum of same answers of every group:
{sum(map(count_group_matching_answers, groups))}')
f.close()3
u/i_have_no_biscuits Dec 06 '20
Microsoft QBasic (well, QB64).
Yes, I have just spent Sunday morning relearning BASIC...
If I'm feeling particularly masochistic later I might downgrade it to GWBasic...
OPEN "data06.txt" FOR INPUT AS 1
MAX_GROUPSIZE = 20
DIM Group(MAX_GROUPSIZE) AS STRING
DIM GroupSize AS INTEGER
DIM UnionTotal AS INTEGER
DIM IntersectionTotal AS INTEGER
DO UNTIL EOF(1)
ReadGroup
UnionTotal% = UnionTotal% + GroupUnion%
IntersectionTotal% = IntersectionTotal% + GroupIntersection%
LOOP
PRINT "Part 1 total: "; UnionTotal%
PRINT "Part 2 total: "; IntersectionTotal%
CLOSE 1
END
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Reads in the next group's data from the file.
SUB ReadGroup ()
SHARED Group() AS STRING, GroupSize%
DIM DataLine$, i%
FOR i% = 0 TO MAX_GROUPSIZE
Group$(i%) = ""
NEXT
GroupSize% = 0
DO
LINE INPUT #1, DataLine$
IF DataLine$ = "" THEN EXIT DO
Group$(GroupSize%) = DataLine$
GroupSize% = GroupSize% + 1
LOOP UNTIL EOF(1)
END SUB
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Calculates the number of letters used in a group (the union)
FUNCTION GroupUnion%
SHARED Group() AS STRING, GroupSize%
DIM Characters(25) AS INTEGER
DIM i%, j%, Count%
FOR i% = 0 TO 25
Characters%(i%) = 0
NEXT
FOR i% = 0 TO GroupSize% - 1
FOR j% = 1 TO LEN(Group$(i%))
index% = ASC(MID$(Group$(i%), j%, 1)) - ASC("a")
Characters%(index%) = -1
NEXT
NEXT
FOR i% = 0 TO 25
IF Characters%(i%) = -1 THEN Count% = Count% + 1
NEXT
GroupUnion% = Count%
END FUNCTION
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Calculates the number of letters used by all the group (the intersection)
FUNCTION GroupIntersection%
SHARED Group() AS STRING, GroupSize%
DIM Characters(25) AS INTEGER
DIM i%, j%, Count%
FOR i% = 0 TO 25
Characters%(i%) = -1
NEXT
FOR i% = 0 TO GroupSize% - 1
FOR j% = 0 TO 25
IF INSTR(Group$(i%), CHR$(j% + ASC("a"))) = 0 THEN
Characters%(j%) = 0
END IF
NEXT
NEXT
FOR i% = 0 TO 25
IF Characters%(i%) = -1 THEN Count% = Count% + 1
NEXT
GroupIntersection% = Count%
END FUNCTION→ More replies (3)
3
u/Breezing Dec 06 '20
Python 3, both parts
Made use of sets again today. Any tips or obvious bad practices?
group_answers = [list(x.split()) for x in open('6.in').read().split('\n\n')]
part1_answers = 0
part2_answers = 0
for group in group_answers:
valid_answers = []
for person in group:
valid_answers += list(person)
totality = [1 for x in set(valid_answers) if valid_answers.count(x) == len(group)]
part1_answers += len(set(valid_answers))
part2_answers += (sum(totality))
print('Day 6: Part 1 answer is', part1_answers)
print('Day 6: Part 2 answer is', part2_answers)→ More replies (2)
3
u/snurre Dec 06 '20
Kotlin
I convert the answers from each person into binary then simply use OR and AND to count:
val groups = File("resources/06.txt").readText()
.split("\n\n")
.map {
it.trimEnd().lines().map { p ->
('a'..'z').map { c -> if (c in p) 1 else 0 }.joinToString("").toInt(2)
}
}
println("Part 1: ${groups.sumBy { it.reduce { acc, i -> acc or i }.countOneBits() }})
println("Part 2: ${groups.sumBy { it.reduce { acc, i -> acc and i }.countOneBits() }})3
u/xrgbit Dec 06 '20
Common Lisp
COUNT-ANSWERS could have be written like COUNT-ANSWERS* using UNION instead of INTERSECTION. The functions could have been combined to take an optional argument for which function to use.
(defparameter *input* (utils:read-file "6.dat"))
(defun count-answers (group)
(length (remove-duplicates (apply #'concatenate 'string group))))
(defun count-answers* (group)
(length (reduce #'intersection (mapcar (lambda (x)
(coerce x 'list))
group))))
(defun part-1 ()
(loop :for group :in (utils:group-into-sublists
(utils:map-line #'utils:string-not-empty-p *input*))
:sum (count-answers group)))
(defun part-2 ()
(loop :for group :in (utils:group-into-sublists
(utils:map-line #'utils:string-not-empty-p *input*))
:sum (count-answers* group)))3
u/Sopef86 Dec 06 '20
Java, functional approach
Integer problem1(String input) {
return Arrays
.stream(splitOnEmptyLine(input))
.map(removeNewline)
.map(stringToIntSet)
.map(Set::size)
.reduce(0, Integer::sum);
}
Integer problem2(String input) {
return Arrays
.stream(splitOnEmptyLine(input))
.map(splitOnNewline)
.map(countCommonChars)
.reduce(0, Integer::sum);
}
String[] splitOnEmptyLine(String input) {
return input.split("\n\n");
}
Function<String, Set<Integer>> stringToIntSet = s -> s.chars().boxed().collect(Collectors.toSet());
Function<String, String> removeNewline = s -> s.replace("\n", "");
Function<String, String[]> splitOnNewline = s -> s.split("\n");
Set<Integer> aToZ = IntStream.range(97, 97 + 26).boxed().collect(Collectors.toSet());
BinaryOperator<Set<Integer>> intersection = (a, b) -> {
b.retainAll(a);
return b;
};
Function<String[], Integer> countCommonChars = s ->
Arrays.stream(s).map(stringToIntSet).reduce(aToZ, intersection).size();→ More replies (1)
3
u/lib20 Dec 06 '20 edited Dec 06 '20
TCL
#!/usr/bin/env tclsh
#
package require struct::set
set t0 [clock microseconds]
set fd [open "input.txt"]
set input [read $fd]
close $fd
set data [split $input "\n"]
set questions {}
set each {}
set questions2 {}
set first 0
foreach d $data {
if {$d eq {}} {
lappend questions [llength [lsort -unique $each]]
set each {}
lappend questions2 [llength $every]
set every {}
set first 0
} else {
# --- part 1
lappend each {*}[split $d ""]
# --- part 2
if {$first == 0} {
lappend every {*}[split $d ""]
set first 1
} else {
set common [struct::set intersect $every [split $d ""]]
set every $common
}
}
}
set t1 [clock microseconds]
puts "day 06 part 1: [tcl::mathop::+ {*}$questions]"
puts "day 06 part 2: [tcl::mathop::+ {*}$questions2]"
puts "time (microseconds): [expr {$t1 - $t0}]"time (microseconds): 18708
3
Dec 06 '20 edited Dec 06 '20
Python 3 - Simple solution
with open('day6_input.txt', 'r') as f:
groups_answers = [list(group.split()) for group in f.read().split('\n\n')]
part_1, part_2 = 0, 0
for group_answers in groups_answers:
answer_sets = [set(answer) for answer in group_answers]
all_answers = set(answer_sets[0])
shared_answers = set(answer_sets[0])
for s in answer_sets[1:]:
all_answers |= s
shared_answers &= s
part_1 += len(all_answers)
part_2 += len(shared_answers)
print('Part 1: ' + str(part_1))
print('Part 2: ' + str(part_2))→ More replies (4)
3
u/hrunt Dec 06 '20
Python 3
#!/usr/bin/env python3
import os
import pathlib
import string
import sys
sys.path.append(str(pathlib.Path(__file__).resolve().parent.parent / 'lib'))
import aoc
def run() -> None:
input_file = aoc.inputfile('input.txt')
groups = open(input_file).read().split("\n\n")
answers = set(string.ascii_lowercase)
count_any = sum(len(set(x for x in group if x in answers)) for group in groups)
print(f'Sum count of any: {count_any}')
count_all = 0
for group in groups:
yes = set() | answers
for passenger in group.split("\n"):
yes &= set(x for x in passenger)
count_all += len(yes)
print(f'Sum count of all: {count_all}')
if __name__ == '__main__':
run()
sys.exit(0)→ More replies (5)
3
u/_hyhy Dec 06 '20
Python3 oneliner:
Part1:
with open("input.txt",Β "r")Β asΒ f:Β print(sum(map((lambda group:Β len(set(''.join(group)))),Β [[personΒ forΒ personΒ inΒ group.split('\n')]Β forΒ groupΒ in f.read().split('\n\n')])))
Part2:
with open("input.txt",Β "r")Β asΒ f:Β print(sum(map((lambda group:Β len(set.intersection(*[set(person)Β forΒ personΒ inΒ group]))),Β [[personΒ forΒ personΒ inΒ group.split('\n')]Β forΒ groupΒ inΒ f.read().split('\n\n')])))
→ More replies (1)
3
u/kawzeg Dec 06 '20
J
raw =: freads 'input'
NB. Add LF before and after input
NB. Two LF mark the end of a record
NB. The first LF is used as a fret for intervals in part 2
q =: (<;._2~((2#LF)&E.)) LF,raw,LF
abc =: 'abcdefghijklmnopqrstuvwxyz'
NB. Part 1
NB. abc&e."1 Make a list that's 1 for each letter that appears, 0 otherwise
NB. +/+/ Sum all lines & columns
echo +/+/abc&e."1(>q)
NB. Part 2
NB. ];.1 Cut into intervals using the first LF as a fret
NB. +/ Find how often each letter appeared
NB. #=(u) A fork finding out which characters appeared in every line
NB. At the end, sum all columns and rows
echo +/+/([: (#=([:+/abc&e."1)) ];.1)@>q3
u/remysharp Dec 06 '20
In jq (repo)
def sum:
reduce .[] as $i (0; . + $i)
;
def mapper:
reduce .[] as $i (
{};
. + { ($i): (.[$i] + 1) }
)
;
def part1:
reduce (.[] | to_entries[]) as $root ({}; . +
{ ($root.key): 1 }
) | length
;
def part2:
length as $count |
reduce (.[] | to_entries) as $root (
{};
. + (
. as $_ |
$root | reduce .[] as $item (
$_;
. + { ($item.key): ($item.value + .[$item.key]) }
)
)
) |
map(select(. == $count)) |
if length > 0 then
length
else
empty
end
;
rtrimstr("\n") | split("\n\n") |
map(
split("\n") | map(
split("") | mapper
)
) | [(map(part1) | sum), (map(part2) | sum)]3
u/el-guish Dec 06 '20
Python
Part 1:
groups = open('input.txt').read().split('\n\n')
def count_answers(group):
return len(set(group.replace('\n','')))
print(sum(count_answers(g) for g in groups))Part 2:
groups = open('input.txt').read().split('\n\n')
def count_answers(group):
questions = set(group.replace('\n',''))
answers = group.split()
return sum(all(q in a for a in answers) for q in questions)
print(sum(count_answers(g) for g in groups))→ More replies (3)
3
u/inokichi Dec 06 '20
Solution in D (dlang) - frustrated with part 2 since i couldnt figure out something along the lines of group.fold!setIntersection and had to go for a raw loop instead.
import std;
void solve() {
auto input = "in6.txt".readText.stripRight.split("\r\n\r\n");
input.map!(a => a.replace("\r\n", "").array.redBlackTree.length).sum.writeln(" (part 1)");
int total;
foreach (ref group; input) {
auto lines = group.split("\r\n").map!"a.array.sort.to!string";
auto curr = lines[0];
foreach (ref line; lines) {
curr = curr.setIntersection(line).to!string;
}
total += curr.length;
}
writeln(total);
}→ More replies (2)
3
u/A-UNDERSCORE-D Dec 06 '20
Golang, after completing decided to implement a set and see if I could make the code either cleaner or faster. I think its a bit cleaner at least, but its not faster by any means
https://github.com/A-UNDERSCORE-D/aoc2020/blob/main/2020/06/solution.go
3
u/AidGli Dec 06 '20
Python
Another relatively simple solution. Threw in a list expansion so I would have more to talk about in today's video (and because it makes the code way simpler.) Github link
def readGroups(inpath="input.txt"):
with open(inpath, "r") as infile:
return infile.read().split('\n\n')
def part1(groups):
count = 0
for group in groups:
unique = set("".join(group.split()))
count += len(unique)
return count
def part2(groups):
count = 0
for group in groups:
people = list(map(set, group.split("\n")))
count += len(people[0].intersection(*people[1:]))
return count
def main():
groups = readGroups()
print(f"Part 1: {part1(groups)}\nPart 2: {part2(groups)}")
main()3
u/busdriverbuddha2 Dec 06 '20
Python one-liners
# Part 1
part1answer = sum(len(set(line.replace("\n", ""))) for line in open("input").read().split("\n\n"))
# Part 2
from functools import reduce
part2answer = sum(len(reduce(lambda x, y: set(x).intersection(set(y)), line.strip("\n").split("\n"))) for line in open("input").read().split("\n\n"))→ More replies (4)
3
u/mathsaey Dec 06 '20
Elixir
Pipelining (|>) and support for sets in the standard library made today's solution very clean in Elixir:
import AOC
aoc 2020, 6 do
def p1, do: solve(&MapSet.union/2)
def p2, do: solve(&MapSet.intersection/2)
def solve(joiner) do
input_string()
|> String.split("\n\n")
|> Enum.map(&group(&1, joiner))
|> Enum.map(&Enum.count/1)
|> Enum.sum()
end
def group(str, joiner) do
str
|> String.trim()
|> String.split("\n")
|> Enum.map(&String.graphemes/1)
|> Enum.map(&MapSet.new/1)
|> Enum.reduce(joiner)
end
end→ More replies (2)
3
u/MaxDeviant Dec 06 '20
PureScript
module Main where
import Prelude
import Data.Array (concatMap, uncons, (:))
import Data.Array as Array
import Data.Either (Either(..))
import Data.Foldable (foldl, sum)
import Data.Maybe (Maybe(..))
import Data.Set (Set)
import Data.Set as Set
import Data.String.CodeUnits (toCharArray)
import Data.String.Utils (lines)
import Effect (Effect)
import Effect.Console (log, logShow)
import Node.Encoding (Encoding(..))
import Node.FS.Sync (readTextFile)
type Answers
= Set Char
type Person
= { answers :: Answers }
parsePerson :: String -> Person
parsePerson = toCharArray >>> Set.fromFoldable >>> { answers: _ }
type Group
= Array Person
groupAnswers :: Group -> Answers
groupAnswers =
foldl (flip Set.insert) Set.empty
<<< concatMap (Array.fromFoldable <<< _.answers)
parseGroups :: String -> Array Group
parseGroups = parseGroups' [] [] <<< lines
where
parseGroups' groups currentGroup lines = case uncons lines of
Just { head: "", tail } -> parseGroups' (currentGroup : groups) [] tail
Just { head, tail } ->
let
person = parsePerson head
in
parseGroups' groups (person : currentGroup) tail
Nothing -> Array.reverse (currentGroup : groups)
partOne :: String -> Either String Int
partOne =
parseGroups
>>> map groupAnswers
>>> map Set.size
>>> sum
>>> pure
groupAnswers' :: Group -> Answers
groupAnswers' group = case uncons group of
Just { head, tail } -> anyYeses head.answers tail
Nothing -> Set.empty
where
anyYeses acc members = case uncons members of
Just { head: { answers }, tail } -> anyYeses (Set.intersection acc answers) tail
Nothing -> acc
partTwo :: String -> Either String Int
partTwo =
parseGroups
>>> map groupAnswers'
>>> map Set.size
>>> sum
>>> pure
main :: Effect Unit
main = do
input <- readTextFile UTF8 "input.txt"
log "Part One"
case partOne input of
Right answer -> logShow answer
Left error -> log $ "Failed with: " <> error
log "Part Two"
case partTwo input of
Right answer -> logShow answer
Left error -> log $ "Failed with: " <> error3
u/tsqd Dec 06 '20
Postgresql
CREATE TEMP TABLE raw_input (
line TEXT,
line_id SERIAL
);
\COPY raw_input (line) FROM ~/Downloads/input6.txt
-- Question 1
WITH
parsed_by_group AS (
SELECT line,
COALESCE(sum(1)
FILTER (WHERE line = '')
OVER (rows between unbounded preceding and current row),
0) + 1 AS group_id
FROM raw_input),
counts_by_group AS (
SELECT DISTINCT
UNNEST(string_to_array(string_agg(line, ''), NULL)) AS yes_answer,
group_id
FROM parsed_by_group
GROUP BY 2
)
SELECT COUNT(*) FROM counts_by_group WHERE yes_answer IS NOT NULL AND yes_answer != '';3
u/hyperTrashPanda Dec 06 '20
Day 6 in learning Elixir, pretty straightforward once I read about the MapSet module. I also finally got the chance to use recursion.
groupedAnswers1 = input |> String.split("\n\n", trim: true) |> Enum.map(fn l -> String.replace(l, "\n", "", trim: true) end)
groupedAnswers2 = input |> String.split("\n\n", trim: true)
defmodule Day6 do
def countYesses1(l) do
# l |> String.graphemes() |> Enum.frequencies() |> Map.keys() |> length()
l |> String.graphemes() |> MapSet.new() |> MapSet.size()
end
def intersect(r, [head | tail]) do
intersect(MapSet.intersection(r, head |> String.graphemes |> MapSet.new()), tail)
end
def intersect(r, []) do
r
end
end
res1 = Enum.map(groupedAnswers1, fn l -> Day6.countYesses1(l) end) |> Enum.sum()
IO.inspect(res1)
r = "abcdefghijklmnopqrstuvwxyz" |> String.graphemes |> MapSet.new()
res2 = Enum.map(groupedAnswers2, fn l -> Day6.intersect(r, l |> String.split("\n", trim: true)) |> MapSet.size() end) |> Enum.sum()
IO.inspect(res2)Any feedback or suggestions would be invaluable; off to study other people's solutions!
https://github.com/tpaschalis/aoc-2020/blob/main/day06/day06.exs
→ More replies (2)
3
u/Braxo Dec 06 '20
Coffeescript Javascript
fs = require 'fs'
input = fs.readFileSync('input.txt').toString().split('\n\n')
count = 0
for group in input
uniques = []
for answers in group.split '\n'
for single in answers.split ''
uniques.push single unless uniques.includes single
count += uniques.length
console.log "Part 1:", count
count = 0
for group in input
intersection = undefined
for answers in group.split '\n'
split = answers.split ''
intersection = intersection or split
intersection = intersection.filter (v) -> split.includes v
count += intersection.length
console.log "Part 2:", count3
u/chicagocode Dec 06 '20
Kotlin - [Blog/Commentary] | [GitHub Repo]
I'm pretty satisfied with this, once I figured out the newline issues. :) I got to use groupingBy/eachCount, which is a nice abstraction over a common problem built right into the standard library!
class Day06(input: String) {
private val answers: List<List<String>> = input
.split("\n\n")
.map { it.lines().filter { line -> line.isNotBlank() } }
fun solvePart1(): Int =
answers.sumBy { it.joinToString("").toSet().size }
fun solvePart2(): Int =
answers.sumBy { group ->
group
.joinToString("")
.groupingBy { it }
.eachCount()
.count { it.value == group.size }
}
}→ More replies (4)
3
u/goeyj Dec 06 '20
Since today was a bit easier, I did a solution in JavaScript after finishing up C++.
const fs = require('fs');
const filename = 'input.txt';
const groups = fs.readFileSync(filename)
.toString()
.trim()
.split('\n\n')
.map(group => group.split('\n'));
const frequenciesMatchingGroupSize = groups.map(group => {
const groupSize = group.length;
const frequencies = new Uint8Array(26).fill(0);
for (const member of group) {
for (const answer of member) {
frequencies[answer.charCodeAt()-97]++;
}
}
return frequencies.filter(freq => freq === groupSize).length;
});
const sumOfUnanimousAnswers = frequenciesMatchingGroupSize.reduce((acc, cur) => acc + cur, 0);
console.log(sumOfUnanimousAnswers);→ More replies (2)
3
u/emmanuel_erc Dec 06 '20
My short Haskell solution. For my solutions, I have tried to only use the libraries that are immediately available from base (this comes with Haskell by default).
import Data.Char
import Data.List
import Data.Monoid
import Data.Set (Set)
import qualified Data.Set as S
import Text.ParserCombinators.ReadP
main :: IO ()
main = do
file <- readFile "day6.txt"
case find (null . snd) $ readP_to_S parseGroups1 (stripSpaces file) of
Nothing -> error "parse unsuccesful"
Just (ss,_) -> mapM_ print $ foldMap (Sum . S.size) ss
case find (null . snd) $ readP_to_S parseGroups2 (stripSpaces file) of
Nothing -> error "parse unsuccesful"
Just (ss,_) -> mapM_ print $ foldMap (Sum . S.size) ss
simple :: [String] -> Set Char
simple = S.fromList . concat
complex :: [String] -> Set Char
complex = foldr (S.intersection . S.fromList) (S.fromList ['a'..'z'])
parseGroup :: ([String] -> Set Char) -> ReadP (Set Char)
parseGroup collapseGroups = collapseGroups <$> sepBy (many1 $ satisfy isLetter) (satisfy isSpace)
parseGroups1 :: ReadP [Set Char]
parseGroups1 = sepBy (parseGroup simple) (string "\n\n")
parseGroups2 :: ReadP [Set Char]
parseGroups2 = sepBy (parseGroup complex) (string "\n\n")
stripSpaces :: String -> String
stripSpaces = dropWhile isSpace . dropWhileEnd isSpace3
u/thedjotaku Dec 06 '20
Python
Usually either it's easy for me to figure it out and implement or it's really hard to do both. Today was one that when I read the problem before breakfast, I knew how I'd solve it - pretty easy. Then when I tried to actually get it to work, the fact that even though Python is awesome in having lists (rather than arrays where you have to know the size ahead of time), it's still not possible to easily create phantom sets and lists for a moment. You have to really think about it. Then I got screwed on list = list making a ref, not a copy. Eventually got it all.
Here's my code:
https://github.com/djotaku/adventofcode/tree/main/2020/Day_6
I tried to be Pythonic (ie list comprehension) where I could, but when I had nested lists and it wasn't quite working right, I gave up in favor of getting the answer and getting on with the rest of my day. Once again, Pytest is a lifesaver. (Although corner cases tripped me up again)
3
u/jitwit Dec 06 '20 edited Dec 09 '20
J Programming Language
Late to the party but here's day 6:
az=: a.{~97+i.26
in=: LF,(aoc 2020 6),LF
+/"1 +/ ((+./,:*./)@:(az&e.;._1);._2~ (2#LF)&E.) in→ More replies (4)
3
u/i_have_no_biscuits Dec 06 '20
Microsoft GWBASIC
Tested on PCBASIC: http://robhagemans.github.io/pcbasic/index.html
After the QBasic solution earlier, I felt like going back in time to the early 80s. It should be possible to port this over to most of the 8-bit BASICs quite easily.
10 DIM U(25), N(25)
20 OPEN "i", 1, "data06.txt"
30 IF EOF(1) GOTO 190
40 FOR I=0 TO 25: U(I)=0: N(I)=-1: NEXT
50 LINE INPUT #1, S$
60 IF S$ = "" GOTO 140
70 FOR I=1 TO LEN(S$)
80 U(ASC(MID$(S$, I, 1)) - ASC("a")) = -1
90 NEXT I
100 FOR I = 0 TO 25
110 IF INSTR(S$, CHR$(I + ASC("a"))) = 0 THEN N(I) = 0
120 NEXT I
130 IF NOT EOF(1) GOTO 50
140 FOR I = 0 TO 25
150 IF U(I) = -1 THEN UC = UC+1
160 IF N(I) = -1 THEN NC = NC+1
170 NEXT I
180 IF NOT EOF(1) GOTO 40
190 PRINT "Union count:";UC
200 PRINT "Intersection count:";NC3
u/Lakret Dec 06 '20
Rust
Solution via HashSet and set operations. Live Stream of the solution.
→ More replies (2)
3
u/xMufasaa Dec 06 '20
PoSH
Could probably improve both, especially Part 2, but they work.
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Green
Write-Host "+ Advent of Code 2020; Day 6 +" -ForegroundColor Green
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Green
Set-Location $PSScriptRoot
$input = "day6input.txt"
Write-Host "++++++ Part 1 ++++++" -ForegroundColor Yellow
$total = 0
(Get-Content $input -Raw) -split '(?:\r?\n){2}' | ForEach-Object {
$uniq = ($_.ToCharArray() | Where-Object {![string]::IsNullOrWhiteSpace($_)} | Sort-Object -Unique).Count
$total += $uniq
}
Write-Host "Total: $total" -ForegroundColor Green
Write-Host "++++++ Part 2 ++++++" -ForegroundColor Yellow
$total = 0
(Get-Content $input -Raw) -split '(?:\r?\n){2}' | ForEach-Object {
$people = ($_ | Measure-Object -Line).Lines
$uniq = ($_.ToCharArray() | Where-Object {![string]::IsNullOrWhiteSpace($_)} | Group-Object)
foreach ($u in $uniq) {
if ($u.Count -eq $people) {
$total++
}
}
}
Write-Host "Total: $total" -ForegroundColor Green3
u/Icy-Sky-902 Dec 06 '20 edited Dec 06 '20
JavaScript
My solution for Part 1
const groups = input.split("\n\n").map((group) => {
group = group.split(/\s+/).map((answer) => {
return answer.split("");
});
return [...new Set([...[].concat.apply([], group)])].length;
})
console.log(groups.reduce((sum, current) => {return sum + current}, 0));And Part 2
const groups = input.split("\n\n").map((group) => {
return group.split(/\s+/).map((answer) => {
return answer.split("");
}).reduce((a, b) => a.filter(c => b.includes(c))).length;
})
console.log(groups.reduce((sum, current) => {return sum + current}, 0));3
u/ssnoyes Dec 06 '20 edited Dec 07 '20
MySQL
https://github.com/snoyes/AoC/blob/main/2020/day06.sql
A lovely mixture of deprecated features (because MySQL isn't great at parsing files and string manipulation, and there seems to be a bug which interferes with a more SQLish approach) and new features (requiring 8.0+).
3
u/chemicalwill Dec 06 '20 edited Dec 06 '20
Continuing my "not the shortest/most efficient, but maybe the most straightforwards?" streak. If anyone has some nifty ways to optimize this, I'd love to learn them.
#! python3
with open('day_6_2020.txt', 'r') as infile:
questions = infile.read().split('\n\n')
any_yes = 0
for group in questions:
any_yes += len(set(group.replace('\n', '')))
print(any_yes)
all_yes = 0
for group in questions:
passengers = group.split('\n')
for answer in passengers[0]:
yesses = [answer for passenger in passengers if answer in passenger]
if len(yesses) == len(passengers):
all_yes += 1
print(all_yes)Edit: refactoring
→ More replies (9)
3
u/jschulenklopper Dec 06 '20
A puzzle that fits well with Ruby:
groups = ARGF.read.split("\n\n").map(&:split)
puts "part 1"
puts groups.map { |group| group.join.chars.uniq.length }.sum
puts "part 2"
puts groups.map { |group| group.map(&:chars).reduce(&:&).length }.sum→ More replies (2)
3
u/killermelga Dec 06 '20 edited Dec 06 '20
Kotlin, part 2, 99 chars 88 by using lines(): Please let me know if you can reduce it further!
File(f).readText().split("\n\n").sumBy{it.lines().reduce{a,s->a.filter{it in s}}.length}You can try it here
→ More replies (3)
3
u/Legitimate_Estate_75 Dec 06 '20 edited Dec 06 '20
R, RStudio solution:
#========================#
# ==== Load Packages ====
#========================#
# load packages
library(data.table)
library(stringr)
library(stringi)
#====================#
# ==== Load Data ====
#====================#
# load the file
puzzle_6 <- fread("puzzle_input1_day_6.txt", header = F)
#=================#
# ==== Part 1 ====
#=================#
#===========================#
# ==== assign groupings ====
#===========================#
# first, assign group number to parse information, starting with 0
group_num_stored <- 0
# every row in the puzzle data
for(i in 1:nrow(puzzle_6)) {
# if the row is empty/NA (and therefore indicating a separation from group 1 to group 2)
if(all(puzzle_6[i] == "" | is.na(puzzle_6[i]))){
# just set that group number to 999999999
puzzle_6[i, group_num := 999999999]
# because we don't want to store the 9999, just get the latest stored number
group_num_stored <- stored_num
}
# if the row value is NOT empty or NA
else {
# subset to that row value and assign the stored group num and add 1
puzzle_6[i, group_num := group_num_stored + 1]
# store the number
stored_num <- puzzle_6[i]$group_num
}
# end for loop
}
# just remove the 999999999 group b/c not needed anymore
puzzle_6 <- subset(puzzle_6, group_num != 999999999)
#==========================#
# ==== create function ====
#==========================#
# start function
get_q_num_func <- function(in_data, in_group_num){
# get vector
vector <- paste(in_data[group_num == in_group_num]$V1, collapse = "")
# split the string
split_vector <- str_split(vector, "")
# return only unique values
unique_values <- stri_unique(split_vector[[1]])
# get number
length <- length(unique_values)
# add questions to the table
in_data[group_num == in_group_num, n_questions := length]
# end function
}
#======================#
# ==== run function ====
#======================#
# create xwalk
in_xwalk <- data.table(group_num = 1:max(puzzle_6$group_num))
# store data and run function
purrr::walk(1:nrow(in_xwalk), ~get_q_num_func(in_data = puzzle_6,
in_group_num = in_xwalk[.x]$group_num))
#=======================#
# ==== final checks ====
#=======================#
# deduplicate by group number
dedupe <- puzzle_6[!duplicated(puzzle_6$group_num)]
# get the sum of all the questions and solved!
sum(dedupe$n_questions)3
u/hello_friendssss Dec 06 '20
PYTHON 3
Baby programmer so any tips fire away - I think I should have split the function rather than make it multitask :P
def build_groups(data, part1_or_2_int):
groups=[]
group=[]
for line in range(len(data)):
#if data is not an enpty line and is not the last line of the txt
if data[line] != '\n' and line!=len(data)-1:
#add each line within a group to the group string. For part one, remove all special characters. For part 2, keep \n as a counter for people number.
clean_line=data[line]
if part1_or_2_int==1:
clean_line=data[line].replace('\n','')
group+=clean_line
#if it is the last line of txt
elif line==len(data)-1:
#for part one, add current line to group as there is no \n flag. Then append set to list to get unique values in group
if part1_or_2_int==1:
group+=data[line].replace('\n','')
groups.append(set(group))
#for part two, add current line to group after adding '\n' for se in people counting. Then append list to list to get total values in group
elif part1_or_2_int==2:
group+=data[line]+'\n'
groups.append(group)
else:
#if its an empty line then group is complete, so append to list of groups as set (part 1) or list (part 1). Don't add \n flag for part 2, as it is in original data set. Reinitialise group for next group.
if part1_or_2_int==1:
groups.append(set(group))
if part1_or_2_int==2:
groups.append(group)
group=[]
return groups
##setup##
with open ('day6.txt','r') as file:
data=file.readlines()
##part 1##
groups=build_groups(data, 1)
part1=0
for i in groups:
part1+=len(i)
##part 2##
groups=build_groups(data, 2)
part2=0
for i in groups:
shared=[]
done=[]
#make string of group
group_answer=''.join(i)
#count people
num_people=i.count('\n')
#remove special chars
joined_answer=group_answer.replace('\n','')
#if number of letters == number of people and it hasnt been found earlier in the string (NB - they will all be present miultiple times in groups >1!) then add to string of shared letters
for letter in joined_answer:
if joined_answer.count(letter) == num_people and letter not in done:
shared+=letter
done.append(letter)
#sum len of all shared strings
part2+=len(shared)→ More replies (3)
3
u/gerikson Dec 06 '20
Perl 5
This solution is essentially my part 1, which I ripped out and rewrote to get part 2. After some reflection I rewrote it again to handle both parts
#! /usr/bin/env perl
use Modern::Perl '2015';
use Test::More tests => 2;
#### INIT - load input data from file into array
my $testing = 0;
my @file_contents;
my $file = $testing ? 'test.txt' : 'input.txt';
open( my $fh, '<', "$file" );
{
# set the local IFS to an empty string to treat the input as paragraphs
local $/ = "";
while (<$fh>) {
chomp;
push @file_contents, $_;
}
}
### CODE
my $part1;
my $part2;
foreach (@file_contents) {
my $respondents = 0;
my %h;
foreach ( split( "\n", $_ ) ) {
foreach ( split( //, $_ ) ) {
$h{$_}++;
}
$respondents++;
}
foreach my $k ( keys %h ) {
$part1++;
$part2++ if $h{$k} == $respondents;
}
}
say $part1;
say $part2;3
u/Snazzy_Redditor Dec 06 '20
JavaScript
My goal was to keep things clean and functional, rather than focusing on speed. Also if anyone knows a way to avoid the problem where the last split results in an empty string let me know. That's the only reason for the filter after the split in part 2.
Part 1
const fs = require("fs");
fs.readFile("inputs/input6.txt", "utf8", (err, data) => {
const answers = data.split("\n\n")
.map(group =>
group.split("\n")
.map(person => Array.from(person)))
.map(combineAnswers);
const numAnswers = answers.reduce((acc, groupAnswers) =>
acc + groupAnswers.size, 0);
console.log(numAnswers);
});
function combineAnswers(group) {
return group.reduce((acc, person) => {
person.map(answer => acc.add(answer));
return acc;
}, new Set());
}Part 2
const fs = require("fs");
fs.readFile("inputs/input6.txt", "utf8", (err, data) => {
const answers = data.split("\n\n")
.map(group =>
group.split("\n")
.filter(person => person.length > 0)
.map(person => Array.from(person)))
.map(combineAnswers);
const numAnswers = answers.reduce((acc, groupAnswers) =>
acc + groupAnswers.length, 0);
console.log(numAnswers);
});
function combineAnswers(group) {
return group.reduce((acc, person) =>
acc.filter(answer => person.includes(answer)));
}→ More replies (2)
3
u/GrbavaCigla Dec 06 '20 edited Dec 06 '20
Python 1st part:
with open("input", "r") as file:
text = file.read().split("\n\n")
text = [i.replace("\n", "") for i in text]
text = [list(set(i)) for i in text]
text = [len(i) for i in text]
print(sum(text))2nd part:
with open("input", "r") as file:
text = file.read().split("\n\n")
text = [i.splitlines() for i in text]
res = 0
for group in text:
main_set = set(group[0])
for people in group[1:]:
main_set = main_set.intersection(set(people))
res += len(main_set)
print(res)→ More replies (1)
3
u/scott-mcc-1 Dec 06 '20 edited Dec 06 '20
Kotlin
Trying for concise code...
class Day06 {
private val groups =
File("""data\y2020\day06.txt""").readText()
.replace("\r","") // windows!
.split("\n\n")
.map { group -> group.split("\n").map { it.toSet() } }
fun part1() = groups.sumBy { it.reduce { a, b -> a.union(b) }.size }
fun part2() = groups.sumBy { it.reduce { a, b -> a.intersect(b) }.size }
}3
u/Trazko Dec 06 '20
Dart
Tried to make "oneliners" as check functions.
import 'dart:io';
main() {
var data = new File('input.txt').readAsLinesSync();
task1(data);
task2(data);
}
void task1(List<String> data) {
var questions = questionParser(data);
int counter = 0;
questions.forEach((element) {
counter += checkAmountOfAnswers(element);
});
print("Task1: Amount of answers: $counter");
}
void task2(List<String> data) {
var questions = questionParser(data);
int counter = 0;
questions.forEach((element) {
counter += checkCommonAnswers(element);
});
print("Task2: Amount of answers: $counter");
}
int checkAmountOfAnswers(Question question) {
return question.answers.join().split("").toSet().length;
}
int checkCommonAnswers(Question question) {
var joinedAnswersSet = question.answers.join().split("").toSet();
var joinedAnswersList = question.answers.join().split("");
int counter = 0;
joinedAnswersSet.forEach((setElement) {
if (joinedAnswersList.where((element) => setElement == element).length ==
question.answers.length) counter++;
});
return counter;
}
List<Question> questionParser(List<String> data) {
List<Question> questions = new List();
List<String> answers = new List();
var dataIt = data.iterator;
while (dataIt.moveNext()) {
if (dataIt.current != "")
answers.add(dataIt.current);
else {
questions.add(new Question(answers));
answers = new List();
}
}
// in case the input is not proper terminated..
if (answers.isNotEmpty) questions.add(new Question(answers));
return questions;
}
class Question {
Question(List<String> list) {
this.answers = list;
}
List<String> answers;
List<String> get getAnswers => answers;
set setAnswers(List<String> answers) => this.answers = answers;
}→ More replies (1)
3
u/friedrich_aurelius Dec 06 '20
Elixir
For Part 1, I simply removed the newlines and counted unique characters within each group.
For Part 2, I made a dictionary for each group to store how many times each character occurs. I saw that group size was always (newlines) + 1, and filtered each dictionary to only keep characters where the quantity is equal to the group size.
The only unexpected issue I encountered was the newline at end of file making the group size larger than it should be for that final group, therefore inaccurately counting it as having zero common letters. Easy fix by just dropping the last character from the input.
3
u/e_blake Dec 06 '20 edited Dec 06 '20
golfed C
210 bytes, relying on gcc or clang for __builtin_popcount and hardcoding ASCII encoding, and assuming you are okay ignoring the compiler warning about read() being used without declaration (I had to include stdio.h, since printf is varargs which does not play as nicely with implicit declarations)
#include<stdio.h>
#define C __builtin_popcount
int main(){int r,p,P=-1,c,s,S=r=p=s=0;while(read(0,&c,1))if(c-10)r|=1<<(c-97);else if(r)p|=r,P&=r,r=0;else s+=C(p),S+=C(P),p=0,P=-1;printf("%d %d",s+C(p),S+C(P));}That solves both parts at once; the program would be even shorter if it only had to solve part 1 or part 2 in isolation.
→ More replies (4)
3
u/improviseallday Dec 06 '20
(Cleaned up)
with open('input.txt') as f:
groups = ''.join(f.readlines()).rstrip().split('\n\n')
print(sum([len(set(group.replace('\n', ''))) for group in groups]))
print(sum([len(set.intersection(*[set(line) for line in group.split('\n')])) for group in groups]))Explanation:
(1) Group input by splitting by double newline.
(2) Within each group, strip remaining newlines. Get length of unique questions per group. Sum.
(3) Within each group, split into lines. Turn each line into a set. Within each group find length of intersection of sets. Sum.
→ More replies (2)
3
u/petrovmartin Dec 06 '20
C#:
using System;
using System.Collections.Generic;
using System.Linq;
namespace AdventOfCode
{
class Program
{
static void Main(string[] args)
{
var input = GetInput();
var groups = input
.Split("\n\n", StringSplitOptions.RemoveEmptyEntries).Select(x => x.Split("\n", StringSplitOptions.RemoveEmptyEntries).Select(x => x.ToCharArray().ToList()).ToList()).ToList();
var totalPart1 = 0;
var totalPart2 = 0;
foreach (var group in groups)
{
//Part 1:
var allPeopleAnswers = new List<char>();
foreach (var person in group)
{
person.Distinct().ToList().ForEach(x => allPeopleAnswers.Add(x));
}
var allPeopleAnswersDistinct = allPeopleAnswers.Distinct().ToList();
totalPart1 += allPeopleAnswers.Count();
//Part 2:
var sameAnswers = new List<char>();
foreach (var answer in allPeopleAnswersDistinct)
{
if (group.All(person => person.Contains(answer))) sameAnswers.Add(answer);
}
totalPart2 += sameAnswers.Count();
}
Console.WriteLine($"Total is: {totalPart1}");
Console.WriteLine($"Total2 is: {totalPart2}");
}
static string GetInput()
{
return System.IO.File.ReadAllText("C:\\Users\\*\\Desktop\\day-6.txt");
}
}
}3
u/kakaroto_BR Dec 06 '20
Python:
# part 1
sum([len(set(x.replace("\n", ""))) for x in test.split("\n\n")])
# part 2
def freq(l):
return {c:l.count(c) for c in l.replace('\n', '')}
def npersons(s):
return len(s.strip('\n').split('\n'))
def count_yes(g):
f = freq(g)
p = npersons(g)
return sum(1 for k in f if f[k] == p)
sum(count_yes(g) for g in test.split('\n\n'))→ More replies (1)
3
u/TheElTea Dec 06 '20 edited Dec 13 '20
C# Solution for 2020 Day 6 Parts 1 and 2
Done inside of Unity in case I felt like doing visualization; class TextAsset is just the text file as hooked up in the editor; replace however you like.
And yes, the code doesn't follow DRY; for Advent of Code I'm finding I prefer having standalone solutions to aid in understanding the core problem.
public class CustomsDeclarationHelper : MonoBehaviour
{
[SerializeField] TextAsset declarations = null; //Hooked up in the input text in the Unity editor.
void Start()
{
SolvePartOne();
SolvePartTwo();
}
void SolvePartOne()
{
//Group the entries into strings.
string[] stringSeparator = { "\r\n\r\n" }; //Find two new lines for breaks. Windows encoding has both carriage return and line feed.
string[] allDeclarations = declarations.text.Split(stringSeparator, System.StringSplitOptions.RemoveEmptyEntries); //One set of declarations, with line breaks, per string.
Dictionary<char, bool> groupDeclarationYes = new Dictionary<char, bool>(); //Track presence of a yes from any member of the group.
int totalOfAllYesResponse = 0;
foreach (string d in allDeclarations)
{
//Fill the dictionary with declarations.
//When multiples of the same character are encountered they will overwrite the entry already there.
foreach (char c in d)
{
groupDeclarationYes[c] = true; //This will add line breaks too but that's fine; we don't need to look them up.
}
int numberQuestionsYes = 0;
//Count the entire group's declaration for all questions they responded yes to.
for (int i = 0; i < 26; i++)
{
char c = (char)(i + 'a'); //Generate a character from a-z.
if (groupDeclarationYes.ContainsKey(c))
{
numberQuestionsYes++;
}
}
totalOfAllYesResponse += numberQuestionsYes;
groupDeclarationYes.Clear(); //Reset tracker for next group.
}
Debug.Log($"Total of all yes responses: {totalOfAllYesResponse}");
}
void SolvePartTwo()
{
//Group the entries into strings.
string[] stringSeparator = { "\r\n\r\n" }; //Find two new lines for breaks. Windows encoding has both carriage return and line feed.
string[] allDeclarations = declarations.text.Split(stringSeparator, System.StringSplitOptions.RemoveEmptyEntries); //One set of declarations per group, with line breaks, per string.
Dictionary<char, int> groupDeclarationCounts = new Dictionary<char, int>(); //Track a count of how many yes reponses there were for each question.
int totalOfAllYesResponses = 0;
foreach (string groupDeclaration in allDeclarations)
{
string[] individualDeclarationsForGroup = groupDeclaration.Split('\n'); //Break a group's declarations into individual ones just to count how many are in the group.
int numberInGroup = individualDeclarationsForGroup.Length;
//We can still iterate across all characters in the group declaration for part 2 as we only need to count the total number of yes responses to each question.
//There's no need to count them for each individual. If there are 4 in the group, and 4 yes responses to 'g', then it's a yes for the group as a whole!
foreach (char c in groupDeclaration)
{
if (groupDeclarationCounts.ContainsKey(c))
{
groupDeclarationCounts[c]++;
}
else
{
groupDeclarationCounts[c] = 1;
}
}
//Declarations to each question for one group have been summed, so iterate
//across and count all entries where the number of yes responses is equal to
//the group size.
int numberOfYesResponsesForEntireGroup = 0;
for (int i = 0; i < 26; i++)
{
char c = (char)(i + 'a'); //Generate a character from a-z.
if (groupDeclarationCounts.ContainsKey(c))
{
if (groupDeclarationCounts[c] == numberInGroup)
{
numberOfYesResponsesForEntireGroup++;
}
}
}
totalOfAllYesResponses += numberOfYesResponsesForEntireGroup;
groupDeclarationCounts.Clear();
}
Debug.Log($"Total of all yes responses for part 2: {totalOfAllYesResponses}");
}
}3
u/betaveros Dec 07 '20
Belatedly posting my golfed Paradoc solutions. You only need to change one character to go between the parts!
Part 1:
iNΓ/Ξ³Ε‘W|rL(try it in-browser, explanation on GitHub)Part 2:
iNΓ/Ξ³Ε‘W&rL(try it in-browser, explanation on GitHub)
3
u/ViliamPucik Dec 07 '20
Python 3 - Minimal readable solution for both parts [GitHub]
import sys
s1 = s2 = 0
for group in sys.stdin.read().split("\n\n"):
s1 += len(set(group.replace("\n", "")))
s2 += len(set.intersection(
*map(set, group.split())
))
print(s1)
print(s2)→ More replies (1)
3
u/belibebond Dec 10 '20
PowerShell
Might be complete garbage code, but it works. Btw, its sad to see no powershell solutions after day 4.
Clear-Host
$data = Get-Content .\6input.txt #| Select-Object -First 10
$List = [System.Collections.Generic.List[PSObject]]::new()
$listTemp = @()
foreach ($line in $data) {
if ($line) {
$ListTemp += $line
}
else {
$List.Add($ListTemp -join ";")
$listTemp = @()
}
}
$List.Add($ListTemp -join ";")
$finalCount = 0
foreach ($group in $List) {
#Write-Host $group -ForegroundColor Yellow
$PeopleInGroup = ($group -split ";").Count
$NumberOfCommonYes = ($group.ToCharArray() | Group-Object -NoElement | Where-Object { $_.count -eq $PeopleInGroup } | Measure-Object).count
#Write-Host "People = $PeopleInGroup ; Count = $NumberOfCommonYes"
$finalCount += $NumberOfCommonYes
}
Write-Host "Answer is : $finalCount"→ More replies (2)
β’
u/topaz2078 (AoC creator) Dec 06 '20
We're aware of some issues during unlock today; we'll let you know when we have more information.