IDENTIFICATION DIVISION.
PROGRAM-ID. AOC-2020-02-1.
AUTHOR. ANNA KOSIERADZKA.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INPUTFILE ASSIGN TO "d2.input"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD INPUTFILE
RECORD IS VARYING IN SIZE FROM 8 to 50
DEPENDING ON REC-LEN.
01 INPUTRECORD PIC X(50).
WORKING-STORAGE SECTION.
01 FILE-STATUS PIC 9 VALUE 0.
01 REC-LEN PIC 9(2) COMP.
01 WS-MIN PIC 9(4).
01 WS-MAX PIC 9(4).
01 WS-CHAR PIC A.
01 WS-STRING-EMPTY PIC X.
01 WS-PASSWORD PIC A(50).
01 WS-SUBSTR-1 PIC X(5).
01 WS-CHAR-COUNT PIC 9(2).
LOCAL-STORAGE SECTION.
01 CORRECT-ROWS UNSIGNED-INT VALUE 0.
PROCEDURE DIVISION.
001-MAIN.
OPEN INPUT INPUTFILE.
PERFORM 002-READ UNTIL FILE-STATUS = 1.
CLOSE INPUTFILE.
DISPLAY CORRECT-ROWS.
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.
MOVE 0 TO WS-CHAR-COUNT.
UNSTRING INPUTRECORD DELIMITED BY SPACE OR "-" OR ":" INTO
WS-MIN
WS-MAX
WS-CHAR
WS-STRING-EMPTY
WS-PASSWORD.
INSPECT WS-PASSWORD TALLYING WS-CHAR-COUNT FOR ALL WS-CHAR.
IF WS-CHAR-COUNT >= WS-MIN AND WS-CHAR-COUNT <= WS-MAX THEN
ADD 1 TO CORRECT-ROWS
END-IF.
I started in the spring and wrote a couple things like FizzBuzz: github.com/GaloisGirl/Coding/tree/...
Then I had a bit of a life, and now I'm having a second wave of COBOL.
Hi guys!
The second day was awesome. I've really enjoyed the task. :) I'm still learning Elixir, but I think my solution was not that bad. :) Of course, I did it in Elixir.
This time I've split the day into two modules - one per part. The full solution is on my GitHub repository.
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.
Ooh boy it's a little early in the season to already be stumped by an off-by-one bug. 1-BASED INDICES RYAN! 1-BASED! IT'S RIGHT THERE IN THE PROMPT!
Also, I started a little parsing module to help me with some of the repetitive tedia.
Day2.h:
/// Day 2: Password Philosophy /// /// Find the passwords that aren't compliant.#include <stdlib.h>
#include <stdint.h>
/// A Policied Password is a password that is accompanied by a policy/// consisting of two positive integers and a letter. These components/// can be used to validate the password.typedefstruct{inta;intb;charletter;charpassword[30];}PoliciedPassword;/// Part 1 calculates how many valid passwords there are./// A password is valid if the # of occurrences of 'letter' is between/// 'a' and 'b,' inclusive.////// passes: the list of policied passwords to check/// count: the number of passwords to checkintpart1(PoliciedPassword**passes,size_tcount);/// Part 2 calculates how many valid passwords there are./// A password is valid of either the 'a' index or 'b' index character /// (1-based!) is equal to 'letter,' but not both.intpart2(PoliciedPassword**passes,size_tcount);/// day2 runs both parts in sequence and outputs their results.intday2();
Day2.c:
/// Day 2: Password Philosophy /// /// Find the passwords that aren't compliant.#include "Day2.h"
#include "parsing.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
/// Parse the input file,staticPoliciedPassword**parse(size_t*lines){FILE*fp;fp=fopen("data/day2.txt","r");if(fp==NULL){printf("Couldn't open input file.\n");exit(EXIT_FAILURE);}*lines=count_lines(fp);PoliciedPassword**passes=malloc(sizeof(PoliciedPassword*)**lines);for(size_ti=0;i<*lines;i++){PoliciedPassword*p=malloc(sizeof(PoliciedPassword));fscanf(fp,"%d-%d %c: %s\n",&p->a,&p->b,&p->letter,p->password);passes[i]=p;}fclose(fp);returnpasses;}staticvoidfreePoliciedPasswordList(PoliciedPassword**passes,size_tcount){for(size_ti=0;i<count;i++){free(passes[i]);passes[i]=NULL;}free(passes);}intpart1(PoliciedPassword**passes,size_tcount){size_tvalid=0;for(size_ti=0;i<count;i++){PoliciedPassword*p=passes[i];uint8_tmatches=0;for(size_tj=0;p->password[j];j++){if(p->password[j]==p->letter)matches++;}if(p->a<=matches&&matches<=p->b)valid++;}returnvalid;}intpart2(PoliciedPassword**passes,size_tcount){size_tvalid=0;for(size_ti=0;i<count;i++){PoliciedPassword*p=passes[i];intmatches=0;if(p->password[p->a-1]==p->letter)matches++;if(p->password[p->b-1]==p->letter)matches++;if(matches==1)valid++;}returnvalid;}intday2(){size_tcount;PoliciedPassword**passes=parse(&count);printf("====== Day 2 ======\n");printf("Part 1: %d\n",part1(passes,count));printf("Part 2: %d\n",part2(passes,count));freePoliciedPasswordList(passes,count);returnEXIT_SUCCESS;}
parsing.h:
#ifndef AOC2020_PARSING_H
#define AOC2020_PARSING_H
#include <stdio.h>
#include <stdlib.h>
/// Counts the number of newline characters in a text file./// Assumes no newline at the end of the last line (so adds +1 more)size_tcount_lines(FILE*fp);#endif
Also a little tooling to help generate the daily files:
bin/newday:
#!/usr/bin/env bash# newday: Creates a new day of files for the Advent of Code Challenge## Date: 12/2/2020# Author: Ryan Palofunction usage(){echo"usage: newday NUMBER"echo
echo" NUMBER: The number of the day to create. Will be inserted"echo" into the templates and used for filenames."}function help(){echo"newday: Creates a new day of files for the Advent of Code challenge."echo
usage
echo}function make_day(){day="$1"echo"Creating Day ${day}.">&2
sed"s/{X}/$day/g""templates/DayX.c">"src/Day${day}.c"sed"s/{X}/$day/g""templates/DayX.h">"src/Day${day}.h"sed"s/{X}/$day/g""templates/TestDayX.c">"test/TestDay${day}.c"sed"s/{X}/$day/g"'templates/main.c'>'src/main.c'touch"data/day${day}.txt"echo"Complete.">&2
}function main(){if[["$#"-ne 1 ]];then
usage
exit 1
fi
if[["$1"=='-h']];then
help
exit 0
fi
if![["$1"=~ [:digit:]+ ]];then
echo"Input must be a number"exit 1
fi
make_day "$1"}
main "$@"
Hey Y'all!! This time, I actually brought the input.txt file in as it was, and again, made a ruby solution that im sure could be a bit more refactored, but i got to play with regex! so that was fun... Here's my full code, and here's a snippet of what i did to solve it:
classPasswordCheckerattr_reader:parse_passwords,:check_passwordsdefinitialize(passwords)@passwords=passwords@parsed_passwords=parse_passwordsenddefparse_passwordsparsed=@passwords.mapdo|item|item_split=item.match(/(^\d*)-(\d*) (\w): (\w*)/){min: item_split[1].to_i,max: item_split[2].to_i,letter: item_split[3],password: item_split[4]}endparsedenddefvalidate_password_frequency(log)# puts "rules are minimum #{log[:min]} and max #{log[:max]}"# puts "there are #{log[:password].count(log[:letter])} letter #{log[:letter]}'s"log[:password].count(log[:letter]).to_i.between?(log[:min],log[:max])enddefvalidate_password_accuracy(log)returnfalseunlesslog[:password].include?(log[:letter])returnfalseiflog[:password][log[:min]-1]==log[:letter]&&log[:password][log[:max]-1]==log[:letter]# puts "rules are letter #{log[:letter]} must be at position #{log[:min]} and then at position #{log[:max]}"# puts "there's a #{log[:password][log[:min] - 1]} at position #{log[:min]} and a #{log[:password][log[:max] - 1]} at position #{log[:max]}"log[:password][log[:min]-1]==log[:letter]||log[:password][log[:max]-1]==log[:letter]enddefcheck_passwords(way="frequency")@parsed_passwords.mapdo|log|ifway=="accuracy"validate_password_accuracy(log)elsevalidate_password_frequency(log)endend.count(true)endend
COBOL (second one is on my GitHub):
IMO you’re a real hipster using Cobol! Great job! :)
Thanks! Gotta stay busy in the pandemic somehow. :)
May I ask you - have you used Cobol before? Or it's just your pandemic-advent challenge? :D
I started in the spring and wrote a couple things like FizzBuzz: github.com/GaloisGirl/Coding/tree/...
Then I had a bit of a life, and now I'm having a second wave of COBOL.
Rust solution for Day 2. Felt happy and clever at the inclusion of the XOR for part 2.
Full repo on Github.
Rust looks intriguing! I hope that one day I will find time to learn this language. :D
Hi guys!
The second day was awesome. I've really enjoyed the task. :) I'm still learning Elixir, but I think my solution was not that bad. :) Of course, I did it in Elixir.
This time I've split the day into two modules - one per part. The full solution is on my GitHub repository.
The first part of the day looks like:
The second part of the day looks like:
Ooh boy it's a little early in the season to already be stumped by an off-by-one bug. 1-BASED INDICES RYAN! 1-BASED! IT'S RIGHT THERE IN THE PROMPT!
Also, I started a little parsing module to help me with some of the repetitive tedia.
Day2.h:
Day2.c:
parsing.h:
parsing.c:
Also a little tooling to help generate the daily files:
bin/newday:
Hey Y'all!! This time, I actually brought the input.txt file in as it was, and again, made a ruby solution that im sure could be a bit more refactored, but i got to play with regex! so that was fun...
Here's my full code, and here's a snippet of what i did to solve it:
SQL (0 lines of code huehue)
Get this table naming 'day2'
2.1. Solution
2.2. Solution
My Rust solution for Day 2. Also using an XOR :). Great minds think alike.
Using AoC to brush up my Rust this year.
I adapted Bodil's parser combinators into a little parser module, as I suspect there'll be much more parsing to come.
And then the solution is:
What I came up with in Ruby:
And for comparison, here is the inlined version:
Mine!
Javascript solution:
Short and hopefully non-cryptic python. Ideas to improve?
I can't remember the last time I had a genuine use for XOR in my day job!
Hi,
For this 2nd day, I want to play a little with regexp :) . Code In PHP
Full size here : Advent of Code - Day 2
Here's my solution in Python
My JavaScript walkthrough:
Late for the party, as always, but here's a few words about yesterday's challenge:
Solving AoC#2 puzzle with Parslet
Paweł Świątkowski ・ Dec 3 ・ 4 min read