OK, today seemed pretty straightforward, although it took me to reach task 2 to have the insight that it was union and then intersect... oh well it is a Sunday :-)
Anyway Haskell was a nice fit and simply used set operations on lists to keep with my somewhat weak goal of using lists.
Clever solution. I had an instinct that I could use binary to solve Day5, although couldn't quite flesh out a working solution (ended up solving it in a different way). It didn't even dawn on me that you could use binary for this challenge, although it seems obvious now. 👍
Coding since 11yo, that makes it over 30 years now ~~~
Have a PhD in Comp Sci ~~~
Love to go on bike tours ~~~
I try to stay as generalist as I can in this crazy wide place coding is at now.
IDENTIFICATION DIVISION.
PROGRAM-ID. AOC-2020-06-2.
AUTHOR. ANNA KOSIERADZKA.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INPUTFILE ASSIGN TO "d6.input"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD INPUTFILE
RECORD IS VARYING IN SIZE FROM 1 to 99
DEPENDING ON REC-LEN.
01 INPUTRECORD PIC X(99).
WORKING-STORAGE SECTION.
01 FILE-STATUS PIC 9 VALUE 0.
01 REC-LEN PIC 9(2) COMP.
01 WS-GROUP-ANSWERS PIC 9 OCCURS 26 TIMES.
01 WS-CHAR PIC X.
LOCAL-STORAGE SECTION.
01 I UNSIGNED-INT VALUE 1.
01 C UNSIGNED-INT VALUE 1.
01 X UNSIGNED-INT VALUE 1.
01 GROUP-SIZE UNSIGNED-INT VALUE 0.
01 GROUP-TOTAL UNSIGNED-INT VALUE 0.
01 TOTAL UNSIGNED-INT VALUE 0.
PROCEDURE DIVISION.
001-MAIN.
PERFORM 004-INIT-VARIABLES.
OPEN INPUT INPUTFILE.
PERFORM 002-READ UNTIL FILE-STATUS = 1.
CLOSE INPUTFILE.
PERFORM 004-NEXT-GROUP.
DISPLAY TOTAL.
STOP RUN.
002-READ.
READ INPUTFILE
AT END MOVE 1 TO FILE-STATUS
NOT AT END PERFORM 003-PROCESS-RECORD
END-READ.
003-PROCESS-RECORD.
IF REC-LEN = 0 THEN
PERFORM 004-NEXT-GROUP
ELSE
PERFORM 005-PROCESS-ROW
END-IF.
004-INIT-VARIABLES.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 26
MOVE 0 TO WS-GROUP-ANSWERS(I)
END-PERFORM.
MOVE 0 TO GROUP-SIZE.
MOVE 0 TO GROUP-TOTAL.
004-NEXT-GROUP.
IF GROUP-SIZE > 0 THEN
PERFORM 006-TALLY-GROUP-TOTAL
END-IF.
ADD GROUP-TOTAL TO TOTAL.
PERFORM 004-INIT-VARIABLES.
005-PROCESS-ROW.
ADD 1 TO GROUP-SIZE.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > REC-LEN
MOVE INPUTRECORD(I:1) TO WS-CHAR
COMPUTE C = FUNCTION ORD(WS-CHAR)
COMPUTE X = WS-GROUP-ANSWERS(C - 97) + 1
MOVE X TO WS-GROUP-ANSWERS(C - 97)
END-PERFORM.
006-TALLY-GROUP-TOTAL.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 26
IF WS-GROUP-ANSWERS(I) = GROUP-SIZE THEN
ADD 1 TO GROUP-TOTAL
END-IF
END-PERFORM.
I thought the most obvious way to do this would be to intersect a bunch of HashSets, but seemed to be more short & sweet to just check that every character in the first line was contained in every other line 🤷
(nsday6(:require[clojure.string:refer[split]][clojure.set:refer[intersection]]))(definput(->"./day6input.txt"(clojure.core/slurp)(split#"\n\n")))(defany-answers(->>input(map#(re-seq#"\w"%))(mapset)(reduce#(+%1(count%2))0))); Part 1(defall-answers(->>input(map#(split%#"\n"))(map#(map(fn[x](set(clojure.core/char-arrayx)))%))(map#(applyintersection%))(reduce#(+%1(count%2))0))); Part 2
Python. A bit less elegant than some of the other python versions already posted but the same idea using set theory
n=0checkset=set()withopen('input.txt','r')asin_file:forlinin_file:ifl.strip()=='':n+=len(checkset)checkset.clear()else:checkset.update((cforcinl.strip()))ifl.strip()!='':n+=len(checkset)print(f"Part 1: total is {n}")checkset=set()new_group=Truen=0withopen('input.txt','r')asin_file:forlinin_file:ifl.strip()=='':n+=len(checkset)checkset.clear()new_group=Trueelse:ifnew_group:checkset.update((cforcinl.strip()))new_group=Falseelse:# must be in every person: equivalent to set intersection
checkset.intersection_update((cforcinl.strip()))ifl.strip()!='':n+=len(checkset)print(f"Part 2: total is {n}")
I'm a Staff Software Engineer at Cisco. I specialize in Python and Go, building functional, practical, and maintainable web systems leveraging Kubernetes and the cloud. Blog opinions are my own.
A little late to the party, but I got it done before bed time 😁
Day6.h:
#ifndef AOC2020_DAY6_H
#define AOC2020_DAY6_H
/// Day 6: Custom Customs/// /// Search through lists of questionnaire answers./// Each questionnaire is 26 questions, so a yes to question 'b' would/// be represented by the presence of 'b' in the output line./// People are one to a line, and parties are separated by a blank line.#include <stdlib.h>
#include <stdio.h>
/// Return the number of letters that are present for any line in a groupinttally_anyone_yeses(FILE*fp);/// Return the number of letters that are present in every line in a groupinttally_everyone_yeses(FILE*fp);/// Run both intday6(void);#endif
Day6.c:
#include "Day6.h"
#include <stdio.h>
#include <string.h>
/// Number of letters in the alphabet#define LETTERS 26
/// Max number of chars per line (yes to all plus newline plus NULL)#define MAX_LINE_SIZE (LETTERS + 2)
inttally_anyone_yeses(FILE*fp){intcounts[LETTERS]={0};charline[MAX_LINE_SIZE]={0};// Log each occurrence of a character.while(!feof(fp)){fgets(line,MAX_LINE_SIZE,fp);if(line[0]=='\n')break;for(inti=0;line[i]!='\n'&&line[i];i++){intindex=line[i]-'a';counts[index]++;}memset(line,0,MAX_LINE_SIZE);}// Loop through and find the total count of those that appeared.inttotal=0;for(inti=0;i<LETTERS;i++){if(counts[i]>0)total++;}returntotal;}/// Run the part 1 code on the input file.staticintpart1(){FILE*fp;fp=fopen("data/day6.txt","r");if(fp==NULL){printf("Couldn't open input file.\n");exit(EXIT_FAILURE);}// Loop through and add up the total for each group.inttotal=0;while(!feof(fp)){total+=tally_anyone_yeses(fp);}fclose(fp);returntotal;}inttally_everyone_yeses(FILE*fp){intcounts[LETTERS]={0};charline[MAX_LINE_SIZE]={0};intpeople_count=0;// Loop through the group and add up everybody's answers.while(!feof(fp)){fgets(line,MAX_LINE_SIZE,fp);if(line[0]=='\n')break;people_count++;for(inti=0;line[i]!='\n'&&line[i];i++){intindex=line[i]-'a';counts[index]++;}memset(line,0,MAX_LINE_SIZE);}// Count the number of letters where everyone answered it yes// i.e. number of yeses == number of peopleinttotal=0;for(inti=0;i<LETTERS;i++){if(counts[i]==people_count)total++;}returntotal;}/// Run the part 2 code on the input file.intpart2(){FILE*fp;fp=fopen("data/day6.txt","r");if(fp==NULL){printf("Couldn't open input file.\n");exit(EXIT_FAILURE);}// Loop through and add up the total for each group.inttotal=0;while(!feof(fp)){total+=tally_everyone_yeses(fp);}fclose(fp);returntotal;}intday6(){printf("====== Day 6 ======\n");printf("Part 1: %d\n",part1());printf("Part 2: %d\n",part2());returnEXIT_SUCCESS;}
I've solved this using Clojure for the sake of. It seems to be the shortest one so far.
(nsday6(:require[clojure.string:refer[split]][clojure.set:refer[intersection]]))(definput(->"./day6input.txt"(clojure.core/slurp)(split#"\n\n")))(defany-answers(->>input(map#(re-seq#"\w"%))(mapset)(reduce#(+%1(count%2))0))); Part 1(defall-answers(->>input(map#(split%#"\n"))(map#(map(fn[x](set(clojure.core/char-arrayx)))%))(map#(applyintersection%))(reduce#(+%1(count%2))0))); Part 2
I'm a Staff Software Engineer at Cisco. I specialize in Python and Go, building functional, practical, and maintainable web systems leveraging Kubernetes and the cloud. Blog opinions are my own.
I'm a Staff Software Engineer at Cisco. I specialize in Python and Go, building functional, practical, and maintainable web systems leveraging Kubernetes and the cloud. Blog opinions are my own.
Haha! Yeah, I was thinking, "I'm pretty sure that's not Go, but maybe it's C#? Or C++?" I just thought of inspecting the HTML classes yesterday after running into a similar issue. 😂
OK, today seemed pretty straightforward, although it took me to reach task 2 to have the insight that it was union and then intersect... oh well it is a Sunday :-)
Anyway Haskell was a nice fit and simply used set operations on lists to keep with my somewhat weak goal of using lists.
I've created a step-by-step tutorial (TypeScript) again:
[Advent of Code 2020] Day 6 Step-by-Step Tutorial (TypeScript)
Kai ・ Dec 6 ・ 6 min read
and I did something with bits, just for fun:
Clever solution. I had an instinct that I could use binary to solve Day5, although couldn't quite flesh out a working solution (ended up solving it in a different way). It didn't even dawn on me that you could use binary for this challenge, although it seems obvious now. 👍
Python one-liners, thanks to set theory and list comprehension, and map
Part 1:
Part 2:
I try to explain this more fully at dev.to/meseta/advent-of-code-2020-...
2 Solutions again.
Go:
And tweet-sized PHP:
I decided to do this year in Haskell. Perhaps if it gets too crazy, I'll revert to what I know better, but until now it's been fun.
I might start a library with the
groupsandaddAllfunctions, seems I'm going to use them for every puzzle...Python impl today. Prob not great python but works.
Tomorrow will try something like prolog?!
Failing that, maybe Haskell.
COBOL
I thought the most obvious way to do this would be to intersect a bunch of HashSets, but seemed to be more short & sweet to just check that every character in the first line was contained in every other line 🤷
Part 1
Part 2
PlaneGroup
Rust!
Ugh, made it too late for this day (but I had it done in time for the problem, honest!).
Used HashSet to my benefit here, as I could apply set theory to the problem and get it done easily.
As always, in Github.
Ruby, part 2:
Python. A bit less elegant than some of the other python versions already posted but the same idea using set theory
Haskell:
Yesterday I had no time to post my solution in Elixir, but there is!
I spent a long time trying to get a regex to work for part 2, but I have given up for the time being. Got a short couple of answers for this:
Only took a few minutes today; here is what I got in Ruby:
JavaScript - I did it in COBOL first, but still wanted to practice reduce a bit.
A little late to the party, but I got it done before bed time 😁
Day6.h:
Day6.c:
I've solved this using Clojure for the sake of. It seems to be the shortest one so far.
Ruby, part 1:
Re yesterday: actually my solution was in D, but I don't see it listed in the table.
OK cool, I wasn't 100% sure. I'll update that.
Yeah, I should've mentioned that in the original. Those C-like languages look pretty much all the same if you don't know some particular details.
Haha! Yeah, I was thinking, "I'm pretty sure that's not Go, but maybe it's C#? Or C++?" I just thought of inspecting the HTML classes yesterday after running into a similar issue. 😂
Here is my walkthrough for day 6 in JavaScript:
Source code: github.com/tpatel/advent-of-code-2...