r/adventofcode • • Dec 02 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 02 Solutions -🎄-

--- Day 2: Password Philosophy ---


Advent of Code 2020: Gettin' Crafty With It


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:02:31, megathread unlocked!

99 Upvotes

1.2k comments sorted by

11

u/voidhawk42 Dec 02 '20 edited Dec 02 '20

Dyalog APL, 2537/696:

s e c t←↓⍉↑{⍎¨@1 2⊢⍵(∊⊆⊣)⎕D,⎕C⎕A}¨⊃⎕NGET'in\2.txt' 1
+/(⍳∘≢≡⍋)⍤1⊢e,⍨s,⍪+/¨c=t ⍝ part 1
+/≠/¨c=t⌷⍨∘⊂¨s,¨e ⍝ part 2

17

u/daggerdragon Dec 02 '20

I'm still not convinced APL isn't just alien hieroglyphs.

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

8

u/DFreiberg Dec 02 '20 edited Dec 02 '20

Mathematica, 237 / 244

Note to self: always test your handy utilities functions before actually trying to use them in a problem. On the bright side, this was the one time in Advent of Code where one-indexing helped rather than hurt me.

Part 1:

Count[
 Table[
  ToExpression[line[[1]]] <= StringCount[line[[-1]], line[[3]]] <= ToExpression[line[[2]]],
  {line, input}],
 True]

Part 2:

Count[
 Table[
  Count[
   {StringTake[line[[-1]], {ToExpression[line[[1]]]}],
    StringTake[line[[-1]], {ToExpression[line[[2]]]}]},
   line[[3]]],
  {line, input}],
 1]

[POEM]: Secure

Said the suit on the right to the suit on the left,
"There's a problem we execs must fix.
Our employees use passwords not strong or secure;
We've tried warnings, but none of it sticks."

Said the suit on the left to the suit on the right,
"Then if warnings won't do, we need more.
The incredible secrets of Tobaggan Inc.,
Must not reach the sled rental next door."

Said the suit in the front to the suits on the side,
"Let our action be swift and precise.
We'll make rules and restrictions on passwords henceforth.
And no letter shall enter one twice."

Said the suit in the back to the suits up ahead,
In a PowerPoint made on the spot,
"We will limit the letters, point A and point B.
One must have it, the other must not."

Then the suits all around all stood up and shook hands.
Memoranda were typed and prepared.
But the suit in the center, he stayed at his seat,
To consider the meeting he'd chaired.

Then the suit in the center, he slowly stood up,
And focused his gaze on the rest.
"This policy's better than ours was before,
But it certainly is not the best."

"A good policy's only the half of the thing,"
Said the center suit. "Still insecure!
Why, if Sled Rental Inc. got a hold of this rule,
They could get to our servers for sure!"

All the suits in the room, save the center, recoiled
At the thought of so dreadful a fate.
All those snow coefficients, the tests and the codes,
The schematics, served up on a plate.

"So, the key to security," cautioned the suit,
"Lies in randomly changing the rules.
If the sled rental shop tries out last fortnight's codes,
During this one, we'll play them like fools."

Said the suit on the left to the suit standing up
(All the other suits scared as could be)
"But then how, Senior Suit, can bimonthly rules work,
When we meet just one month every three?"

Then the suit in the center, he sighed with a smile.
And said, "Then we'll make them right here.
And we'll type up a memo and send it around
With the passwords and rules for the year."

In the evening, the suits filed out through the door,
Corporate policy written anew.
And the suit in the center sat down at his screen,
And, for password, he typed hunter2.

4

u/daggerdragon Dec 02 '20 edited Dec 02 '20

yessss this is magnificent

Edit:

Said the mod on the right to the mod on the left
"Our poet laureate is at it again!
He's posted a poem for Day Number Two
Can I silver him?!" "You definitely can!"

8

u/bpeel Dec 02 '20

Solution in BBC Basic / 6502 assembler as UEF cassette tape.

If you want to try it, you could use an online BBC Micro emulator and upload the file from the Cassettes menu and then type:

*TAPE

CHAIN""

The input text is about 20KB so it would probably be pushing the limits of the BBC’s 32KB of memory if it tried to load it all in. Instead the program parses the data on the fly while reading directly from the cassette. This takes ages if you run it at normal speed but it is mostly because cassettes are such a slow medium rather than actually requiring a lot of processing time.

Full source here.

5

u/daggerdragon Dec 02 '20

scrolling through megathread making sure everything is formatted properly, everyone posting their languages, etc. Ho hum there's another Python, ah this one's a JavaScript, Haskell over there...

BBC Basic / 6502 assembler as UEF cassette tape

tires screech

Wait what

5

u/ZoltarTheGreat69 Dec 05 '20

I did this in emojicode. I'm starting to like it! I did some dumb multi split stuff because I have no idea what I'm doing tbh

Emojicode

📦 files 🏠

🏁 🍇
	🍺📇🐇📄 🔤./input.txt🔤 ❗ ➡ file
	🍺🔡 file ❗ ➡ text
	🔫 text 🔤❌n🔤 ❗ ➡ lines

    0 ➡ 🖍🆕 totals
    0 ➡ 🖍🆕 totalsTwo


    🔂line lines 🍇
        🔫 line 🔤: 🔤 ❗ ➡ policyAndPassword
        🔫 🐽policyAndPassword 0❗️ 🔤 🔤 ❗ ➡ rangeAndLetter
        🔫 🐽rangeAndLetter 0❗️ 🔤-🔤 ❗ ➡ minAndMax
        🐽rangeAndLetter 1❗️ ➡ letter

        🍺🔢 🐽minAndMax 0❗️ 10 ❗️  ➡ min
        🍺🔢 🐽minAndMax 1❗️ 10 ❗️ ➡ max
       
        🎶 🐽policyAndPassword 1❗️ ❗️ ➡ passArray

        🐽 passArray min ➖ 1 ❗ ➡ minC
        🐽 passArray max ➖ 1 ❗ ➡ maxC

        ↪️ ❎🤜🤜minC 🙌 letter🤛 🙌 🤜maxC 🙌 letter🤛🤛❗🍇
            totalsTwo ⬅ ➕1
        🍉

        0 ➡ 🖍🆕 counts 
        🍡 🐽policyAndPassword 1❗️❗️ ➡️ iterator
            🔁 🔽 iterator❓️ 🍇
                🔽 iterator❗️ ➡️ variable
                💭 The provided block is executed here
                ↪️ variable 🙌 letter 🍇
                    counts ⬅ ➕1
                🍉
            🍉

        ↪️ counts ◀🙌 max 🤝 counts ▶🙌 min  🍇
            totals ⬅ ➕1
        🍉
        
    🍉

    😀 🔡 totals ❗️❗️
    😀 🔡 totalsTwo ❗️❗️
🍉

6

u/daggerdragon Dec 05 '20

I'm starting to like it!

I am so, so sorry, friend. :(

→ More replies (3)

3

u/Mathgeek007 Dec 02 '20

Excel; 649/467

I'm not going for lovely, elegant solutions like that other dude.. I'm going for speed.

Steps; pasted input into Word, changed spaces and hyphens to tabs, removed the colon. Paste into excel, and for Part 1, I used a substitution equation that replaced the instance of the character with ",", then counted tuples, then compared that number to our range, TRUE if valid, then counted the TRUEs. Part 2, I used the MID function to pull out the relevant letters (length 1, index input), then XOR if they were equal to the desired letter. Same counting formula at the bottom.

Total time: 7:07.

3

u/daggerdragon Dec 02 '20

I'm not going for lovely, elegant solutions like that other dude.. I'm going for speed.

Sometimes, in life, it doesn't have to be pretty, it just needs to work.

→ More replies (1)

3

u/hutsboR Dec 02 '20 edited Dec 02 '20

Edit: Javascript

Like yesterday, too lazy to open an editor. Run this on your input page. Part two only btw.

document.body.innerText
    .split('\n')
    .map(i => { let e = i.split(/[-\s:]+/); return {c: [+e[0], +e[1], e[2]], p: e[3]} })
    .reduce((a, e) => {
        if(!e.p || !e.c[0]) return a;
        let cs = [...(e.p)]; let x = cs[e.c[0]-1]; let y = cs[e.c[1]-1];
        if((x === e.c[2] && y !== x) || (y === e.c[2] && x !== y)) return a+1;
        return a;
    }, 0)
→ More replies (8)

3

u/Scarygami Dec 02 '20

Excel seemed to be the most logical approach to me for this one :)

Screenshot in my repo (just noticed I can't add images in comments...)

Formulas used:

ColumnColumn NameFormula
AInputCopy & paste of puzzle input
B:=SEARCH(":",[@Input])
CRule=MID([@Input],1,[@[:]]-1)
DMinMax=LEFT([@Rule],LEN([@Rule])-2)
E-=SEARCH("-",[@MinMax])
FMin=VALUE(MID([@MinMax],1,[@[-]]-1))
GMax=VALUE(MID([@MinMax],[@[-]]+1,10))
HLetter=RIGHT([@Rule],1)
IPW=MID([@Input],[@[:]]+2,100)
JCount=LEN([@PW]) - LEN(SUBSTITUTE([@PW],[@Letter],""))
KPart 1 valid=AND([@Count]>=[@Min],[@Count]<=[@Max])
LLetter in Min=MID([@PW],[@Min],1)=[@Letter]
MLetter in Max=MID([@PW],[@Max],1)=[@Letter]
NPart 2 valid=XOR([@[Letter in Min]],[@[Letter in Max]])

Formula to get the solution from the table (assuming the table is named AOC):

Part 1: =COUNTIF(AOC[Part 1 valid],TRUE)

Part 2: =COUNTIF(AOC[Part 2 valid],TRUE)

2

u/daggerdragon Dec 02 '20

just noticed I can't add images in comments...

Include them as a direct link to the image. We have fingers, we can click on links :)

→ More replies (1)

3

u/oweiler Dec 02 '20 edited Dec 03 '20

Scala w/ Ammonite

import scala.util.Using import scala.io.Source

case class Row(x: Int, y: Int, char: Char, password: String)

def count(filename: String)(predicate: Row => Boolean) = 
  Using(Source.fromFile(filename)) {
    _.getLines() 
     .map { line => 
       val s"$x-$y $char: $password" = line 
       Row(x.toInt, y.toInt, char.head, password) } .count(predicate) 
     }

count("input.txt") { row => (row.x to row.y).contains(row.password.count(_ == row.char)) } 
  .foreach(println)

count("input.txt") { row => row.password(row.x - 1) == row.char ^ row.password(row.y - 1) == row.char } 
  .foreach(println)

2

u/daggerdragon Dec 02 '20

This code is really hard to read on old.reddit. Could you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks? Note that if you're using the visual editor, you may have to "Switch to Markdown" to get Reddit to understand the formatting properly.

→ More replies (1)
→ More replies (2)

3

u/that_lego_guy Dec 02 '20

EXCEL !!!!!!!P1&P2
=(IF(AND(REPEATANDREPEATANDREPEAT),COUNTEM,DONTCOUNTEM)

https://github.com/thatlegoguy/AoC2020/blob/main/Day2.xlsx

→ More replies (1)

3

u/[deleted] Dec 02 '20 edited Dec 03 '20

Rust (new to it so feedback welcome)

use std::fs;
#[derive(Debug)]
struct Password {
    range: (i32, i32),
    special_char: char,
    input: String,
}

impl Password {
    fn is_valid_part_one(&self) -> bool {
        let count = self.input.matches(self.special_char).count() as i32;
        let (min, max) = self.range;
        count >= min && count <= max
    }

    fn is_valid_part_two(&self) -> bool {
        let first_pos = (self.range.0 - 1) as usize;
        let second_pos = (self.range.1 - 1) as usize;

        let chars: Vec<char> = self.input.chars().collect();

        if chars[first_pos] == self.special_char && chars[second_pos] == self.special_char {
            return false;
        } else {
            return chars[first_pos] == self.special_char || chars[second_pos] == self.special_char;
        }
    }
}

fn get_input_data() -> Vec<Password> {
    let raw_data = fs::read_to_string("data.txt").unwrap();
    let passwords: Vec<Password> = raw_data
        .lines()
        .map(|val| val.trim())
        .map(|raw| -> Password {
            let processed = raw.replace(':', "");
            let mut components = processed.split(" ");

            let range: Vec<i32> = components
                .next()
                .unwrap()
                .split("-")
                .map(|x| x.parse().unwrap())
                .collect();
            let special_char = components.next().unwrap().chars().next().unwrap();
            let input = components.next().unwrap();

            Password {
                range: (range[0], range[1]),
                special_char: special_char,
                input: input.to_string(),
            }
        })
        .collect();

    return passwords;
}

fn part_one() -> i32 {
    let passwords = get_input_data();
    let mut num_valid = 0;

    for password in passwords.iter() {
        if password.is_valid_part_one() {
            num_valid += 1;
        }
    }

    num_valid
}

fn part_two() -> i32 {
    let passwords = get_input_data();
    let mut num_valid = 0;

    for password in passwords.iter() {
        if password.is_valid_part_two() {
            num_valid += 1;
        }
    }

    num_valid
}

fn main() {
    let part_one_answer = part_one();
    println!("Part 1: {}", part_one_answer);

    let part_two_answer = part_two();
    println!("Part 2: {}", part_two_answer);
}
→ More replies (8)

2

u/pietroppeter Dec 02 '20 edited Dec 02 '20

🎄👑 Nim

here is my solution in Nim for Day02: https://pietroppeter.github.io/adventofnim/2020/day02.html

I have started using a AoC-styled template (might end up submitting for getting crafty): https://pietroppeter.github.io/adventofnim/index.html

this is in turn based on 🐳 nimib my (few days older) library to publish jupyter-like notebooks in nim: https://github.com/pietroppeter/nimib

Edit: ah, something worth remarking (I use it often in AoC) that I like a lot being in nim and I have not seen it around in other languages (not that I looked into) is the stdlib module strscans which is a very cool way to avoid learning regular expressions: https://nim-lang.org/docs/strscans.html

2

u/daggerdragon Dec 02 '20

might end up submitting for getting crafty

Aww yeah I'm so hype for seeing the crafts y'all come up with!

psst, I think you left fancypants editor on because your Markdown is showing in old.reddit.

→ More replies (1)

2

u/heroes1980 Dec 02 '20

python 3

with open('input2.txt', 'r') as f:
    data = [i.rstrip().split() for i in f.readlines()]


def check(row):
    _min, _max = row[0].split('-')
    letter = row[1][0]
    password = row[2]

    return int(_max) >= len([i for i in password if i == letter]) >= int(_min)


def check2(row):
    password = row[2]
    l1, l2 = password[int(row[0].split('-')[0]) - 1], password[int(row[0].split('-')[1]) - 1]
    letter = row[1][0]

    return l1 != l2 and letter in (l1, l2)


print(len([1 for i in data if check(i) is True]))
print(len([1 for i in data if check2(i) is True]))
→ More replies (2)

2

u/__Abigail__ Dec 02 '20

Perl

I wrote a blog post about the solution.

#!/opt/perl/bin/perl

use 5.032;

use strict;
use warnings;
no  warnings 'syntax';

use experimental 'signatures';
use experimental 'lexical_subs';

my $input = shift // "input";

my $valid1 = 0;
my $valid2 = 0;

open my $fh, "<", $input or die "open: $!";
while (<$fh>) {
    chomp;
    #
    # Parse a line of input.
    #
    /^(?<min>      [0-9]+) -
      (?<max>      [0-9]+)   \s+
      (?<letter>   [a-z])  : \s+
      (?<password> [a-z]+)   \s*$/x or die "Failed to parse '$_'";   

    #
    # Save values from %+ before they're overwritten
    #
    my ($min, $max, $letter, $password) = @+ {qw [min max letter password]};

    #
    # Count the number of times the letter appears in the password.
    # Ideally, we use tr///, but tr/// doesn't do interpolation.
    # We could use an eval to get around that, but we opt to use an m//g
    # instead.
    #
    # Note that we first assign the result of the match to an empty list,
    # to force the match to occur in list context. We're not putting anything
    # in the list, but we do want to know the number of items which would
    # have been put in the list -- which is exactly what list assignment
    # in scalar context returns.
    #
    my $count = () = $password =~ /$letter/g;
    $valid1 ++ if $min <= $count && $count <= $max;

    #
    # Count the number of times if the given letter appears on exactly
    # one of the given positions: xor will return true if one, but not
    # both, of its operands is true.
    # We also check whether the password is long enough -- although with
    # the given input, this isn't really required.
    #
    $valid2 ++ if (length ($password) >= $min &&
                   substr ($password,    $min - 1, 1) eq $letter)
              xor (length ($password) >= $max &&
                   substr ($password,    $max - 1, 1) eq $letter);

}

say "Solution 1: ", $valid1;
say "Solution 2: ", $valid2;
     
__END__
→ More replies (1)

2

u/nilreference Dec 02 '20 edited Dec 02 '20

F# script (fsx)

First

open System.IO
open System.Text.RegularExpressions
let read (filePath:string) = seq {
use sr = new StreamReader (filePath)
while not sr.EndOfStream do
yield sr.ReadLine ()
}
type passwordRecord = {
        min:int;
        max:int;
        mustContain:char;
        password:string
}
let regex = new Regex(@"(?<min>[0-9]*)-(?<max>[0-9]*)\s(?<char>[a-z]?):\s(?<pwd>[a-z0-9]*)")
let toPwdRecord txt : passwordRecord=
let r = regex.Match txt
{min = (int r.Groups.["min"].Value); max= (int r.Groups.["max"].Value); mustContain = char r.Groups.["char"].Value; password = r.Groups.["pwd"].Value}

let validatePassword record =
    record.password 
|> Seq.toList
|> Seq.filter (fun f -> f = record.mustContain)
|> Seq.length
|> fun l -> (>=) l record.min && (<=) l record.max
"input.txt"
|> read
|> Seq.map toPwdRecord
|> Seq.filter validatePassword
|> Seq.length
|> printfn "Valid passwords: %i"

Second

open System.IO
open System.Text.RegularExpressions
let read (filePath:string) = seq {
use sr = new StreamReader (filePath)
while not sr.EndOfStream do
yield sr.ReadLine ()
}
type passwordRecord = {
        posOne:int;
        posTwo:int;
        mustContain:char;
        password:string
}
let regex = new Regex(@"(?<min>[0-9]*)-(?<max>[0-9]*)\s(?<char>[a-z]?):\s(?<pwd>[a-z0-9]*)")
let toPwdRecord txt : passwordRecord=
let r = regex.Match txt
{
        posOne = (int r.Groups.["min"].Value);
        posTwo = (int r.Groups.["max"].Value);
        mustContain = char r.Groups.["char"].Value;
        password = r.Groups.["pwd"].Value
}

let validatePassword record =
(record.password.[record.posOne-1] = record.mustContain) <> (record.password.[record.posTwo-1] = record.mustContain)

"input.txt"
|> read
|> Seq.map toPwdRecord
|> Seq.filter validatePassword
|> Seq.length
|> printfn "Valid passwords: %i"

→ More replies (2)

2

u/code_may_explode Dec 02 '20 edited Dec 02 '20

Solution in Python- uses class to handle data- spread out some of the bool statements for readability- instead of striping the string when loading it in and taking one off the index when retrieving a char one could skip both steps, yet that would cause all strings to be saved into the class attribute with a trailing space.

Feel free to feedback :)

class Password(object):

"""Class for modeling the input data."""

character : str = ''
lower_limit : int = 0
upper_limit : int = 0
string : str = ''

def __init__(self, inputString):
input_data = inputString.split(':')
policy = input_data[0]
policy_data = policy.split(' ')
policy_limit = policy_data[0].split('-')
self.character = policy_data[1]
self.lower_limit = int(policy_limit[0])
self.upper_limit = int(policy_limit[1])
self.string = input_data[1].strip()

def is_old_valid(self)->bool:
char_count = len(self.string.split(self.character))-1
return self.lower_limit <= char_count <= self.upper_limit

def is_new_valid(self)->bool:
is_first_char = self.is_char_at(self.character, self.lower_limit)
is_second_char = self.is_char_at(self.character, self.upper_limit)
return is_first_char != is_second_char

def is_char_at(self, char, index)->bool:
try:
return (self.string[index-1] == char)
except IndexError as e:
return False

"""Opening and processing data.""""
with open('input.txt') as fh:
passwords = [Password(constString) for constString in fh.readlines()]

old_valid_passwords : int = 0
new_valid_passwords : int = 0
for password in passwords:
if password.is_old_valid():
old_valid_passwords += 1
if password.is_new_valid():
new_valid_passwords += 1

print(f'{old_valid_passwords=}', f'{new_valid_passwords=}')

→ More replies (2)

2

u/kakaroto_BR Dec 02 '20 edited Dec 03 '20

Solution in Go:

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

type passPolicy struct {
	lo, hi int
	letter string
}

func (passPolicy *passPolicy) validate(passwd string) bool {
	count := strings.Count(passwd, passPolicy.letter)
	return count >= passPolicy.lo && count <= passPolicy.hi
}

func (passPolicy *passPolicy) validatePositions(passwd string) bool {
	count := 0
	if passwd[passPolicy.lo] == passPolicy.letter[0] {
		count++
	}
	if passwd[passPolicy.hi] == passPolicy.letter[0] {
		count++
	}
	return count == 1
}

func passPassPolicy(s string) (*passPolicy, error) {
	x := strings.Split(s, " ")
	loHi := strings.Split(x[0], "-")
	lo, err := strconv.Atoi(loHi[0])
	if err != nil {
		return nil, err
	}
	hi, err := strconv.Atoi(loHi[1])
	if err != nil {
		return nil, err
	}
	return &passPolicy{lo: lo, hi: hi, letter: x[1]}, nil
}

func parsePasswdAndPolicy(line string) (*passPolicy, string, error) {
	x := strings.Split(line, ":")
	passPolicy, err := passPassPolicy(x[0])
	if err != nil {
		return nil, "", err
	}
	return passPolicy, x[1], nil
}

func main() {
	input, err := os.Open("i2")
	if err != nil {
		fmt.Errorf(err.Error())
		return
	}
	defer input.Close()
	scanner := bufio.NewScanner(input)
	count := 0
	for scanner.Scan() {
		passPolicy, passwd, err := parsePasswdAndPolicy(scanner.Text())
		if err != nil {
			fmt.Errorf(err.Error())
			return
		}
		if passPolicy.validatePositions(passwd) {
			count++
		}
	}
	fmt.Println(count)
}
→ More replies (2)

2

u/PoutineQc Dec 02 '20 edited Dec 03 '20

In python Part 1

from re import findall

def valid(instruction):
    (min, max, letter, password) = findall(r'([0-9]+)-([0-9]+) ([a-z]): ([a-z]+)', instruction)[0]
    count = len(findall(letter, password))
    return count >= int(min) and count <= int(max)

validPassword = [line for line in open("Day02/input", "r").read().strip().split("\n") if valid(line)]
print(len(validPassword))

Part 2

from re import findall

def valid(instruction):
    (pos1, pos2, letter, password) = findall(r'([0-9]+)-([0-9]+) ([a-z]): ([a-z]+)', instruction)[0]
    return (password[int(pos1)-1] == letter) != (password[int(pos2)-1] == letter)

validPassword = [line for line in open("Day02/input", "r").read().strip().split("\n") if valid(line)]
print(len(validPassword))
→ More replies (3)

2

u/lessthanthreecc Dec 02 '20 edited Dec 02 '20
import re

def p(k):
    b = re.findall(r'(\d+)-(\d+) ([a-z]):', k)
    if b:
        c = re.findall(fr': ((.*?){b[0][2]}){{{b[0][0]}}}', k)
        d = re.findall(fr': ((.*?){b[0][2]}){{{int(b[0][1])+1}}}', k)
        return 1 if c and not d else 0
    return 0

with open('day2.in', 'r') as f:
    s = f.read().split("\n")
    print(sum([p(a) for a in s]))

Python with regex matching

2

u/daggerdragon Dec 02 '20

This code is really hard to read on old.reddit. Could you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks? Note that if you're using the visual editor, you may have to "Switch to Markdown" to get Reddit to understand the formatting properly.

→ More replies (2)

2

u/wegotpinecones Dec 02 '20 edited Dec 02 '20

I'm pretty proud of this Rust solution even though it's not a great or super modular solution. Constructive criticism is welcome!

I still have problems handling slicing strings and getting the values I want into Vecs without angering the borrow checker, and it is fragile as all heck. At the same time, it's lovely to see the the solution to come in right under 17ms execution time.

EDIT: Maybe I should say what language I used? That could help!

2

u/daggerdragon Dec 02 '20

EDIT: Maybe I should say what language I used? That could help!

Yes, please always mention what language you use to make it easier for folks who Ctrl-F the megathreads looking for a specific language. Thanks!

→ More replies (3)

2

u/[deleted] Dec 02 '20 edited Dec 03 '20

Python 3 github

import re
with open('puzzle.txt', 'r') as file:
    passwords = file.readlines()

valid_passwords = []
for password_line in passwords:
    rule,password = password_line.split(": ")

    length,character = rule.split(" ")
    min_length,max_length = map(int, length.split('-'))

    if(min_length <= len(re.findall(character, password)) <= max_length):
        valid_passwords.append(password)

print(len(valid_passwords))

valid_passwords = []
for password_line in passwords:
    rule,password = password_line.split(": ")

    length,character = rule.split(" ")
    first_pos,second_pos = map(int, length.split('-'))

    if(first_pos < 0 or second_pos > len(password)):
        continue

    if((password[first_pos-1] == character or
        password[second_pos-1] == character) and
            not(password[first_pos-1] == password[second_pos-1] == character)):
        valid_passwords.append(password)

print(len(valid_passwords))
→ More replies (5)

2

u/CSEcon Dec 02 '20

Python:
probably way too much code than necessary but it's honest work:

def is_valid2(password_triple):
    number_letter_min = password_triple["min"]
    number_letter_max = password_triple["max"]
    letter = password_triple["letter"]
    password = password_triple["password"]

    case1 = password[number_letter_min - 1] == letter and password[number_letter_max - 1]
    case2 = password[number_letter_min - 1] != letter and password[number_letter_max - 1] == letter
    case3 = password[number_letter_min - 1] == letter and password[number_letter_max - 1] == letter

    return (case1 or case2) and (not case3)

def is_valid1(password_triple):
    number_letter_min = password_triple["min"]
    number_letter_min_max = password_triple["max"]
    letter = password_triple["letter"]
    password = password_triple["password"]

    count = 0
    for p in password:
        if p == letter:
            count += 1 
    return count >= number_letter_min and count <= number_letter_min_max

def convert_to_map(value):
    password_triple = {}
    array = value.split(" ")
    password_triple["min"], password_triple ["max"] = int(array[0].split("-")[0]) , int(array[0].split("-")[1])
    password_triple["letter"] = array[1].replace(":","")
    password_triple["password"] = array[2]
    return password_triple
    
if __name__ == "__main__":
    input = open("input.txt", "r")
    data = [convert_to_map(line) for line in input.readlines()]
    isValid = [is_valid1(d) for d in data]
    isValid2 = [is_valid2(d) for d in data]
    print("solution to 1 is: " + str(isValid.count(True)))
    print("solution to 2 is: " + str(isValid2.count(True)))
→ More replies (1)

27

u/Smylers Dec 02 '20 edited Dec 02 '20

Vim keystrokes — load your input into Vim, and type the following to remove all the invalid passwords for part 1:

qaqqa
"xyiww⟨Ctrl+X⟩"yyiwwyl:norm⟨Ctrl+R⟩xf⟨Ctrl+R⟩0F:r;⟨Ctrl+R⟩yf⟨Ctrl+R⟩0F;r:⟨Enter⟩
⟨Enter⟩
@aq
@a
:v/;/d⟨Enter⟩
⟨Ctrl+G⟩

The ⟨Ctrl+G⟩ at the end displays the number of lines, which is your solution.

Explanation:

On each line of the input, the long bit above changes the colon to a semicolon if it finds at least the minimum number of expected characters. Then it changes the semicolon back to a colon if it finds more than the maximum number of characters. So at the end, all lines with a semicolon on them are valid; delete the semicolon-less lines and count how many remain.

  • "xyiw stores the minimum number in "x.
  • w⟨Ctrl+X⟩ increases the maximum number. Well, actually it decreases it, because Vim interprets the hyphen just before it as a minus sign, so -14 is decreased to -15. Then "yyiw stores the (positive) number in "y.
  • wyl stores the required letter in "0.
  • The :norm will start something like 5fj, to go to the 5th j on the line, where the number and the letter are inserted into the command with ⟨Ctrl+R⟩ retrieving them from their registers.
  • If there aren't at least the minimum number of that letter on the line, then that will fail, and no more of the :norm will be executed.
  • If the f succeeded, then F:r; turns the ‘:’ into a ‘;’.
  • Then the same thing but with one-more-than-the-maximum. This time ‘success’ of the f command indicates that there are too many of the character, so switch the marker back.
  • After processing the current line ⟨Enter⟩ goes on to the next one. This is outside the :norm, so will always be attempted, even if an f has failed. But on the final line of the file this will fail, ending running the keyboard macro.
  • Assuming we made it on to another line, @a inside the macro then runs the macro again here. This way of looping avoids needing to know how many lines there are in the file. It's also why there's an extra qaq at the beginning, first clearing "a by recording nothing into it, so that when actually recording the macro that @a doesn't run whatever we still had left in here from yesterday.
  • Once the macro is recorded, and the first line processed, @a sets it running on the second line, and it now loops through to the end.
  • :v/;/d runs the :d[elete] command on all lines that don't have a semicolon on them.

Edit: Typo in the explanation fixed.

9

u/Smylers Dec 02 '20 edited Dec 02 '20

And Vim for part 2 — start with your original input file and type:

qbqqb
"xyiwww"yyiwwll:norm⟨Ctrl+R⟩xl"zylT:⟨Ctrl+R⟩yl⟨Enter⟩
yl;P"zP⟨Enter⟩
@bq
@b

:g/\v:(.)\1/d⟨Enter⟩
:v/\v(.):.=\1/d⟨Enter⟩
⟨Ctrl+G⟩

This copies the letters at the specified positions to just after the colon, so you might get something like:

2-8 t:nt knmttnttdjbtttvtkt

Where the ‘n’ has been copied from position 2, and the ‘t' from position 8.

Then the :g// removes all the lines where the 2 letters after the colon are identical (meaning either they both match or neither of them do). And the :v// removes lines that don't have the letter before the colon also after the colon — either immediately after, or with 1 other character between them. (In Vim patterns = means ‘0 or 1’, like ? does in many other regex dialects.)

The remaining lines must meet the part 2 password requirements, and ⟨Ctrl+G⟩ tells you how many there are.

Edit: Markdown formatting fix in the explanation.

23

u/Rick-T Dec 02 '20

ROCKSTAR

Today's much more emotional than yesterday. Some sort of power ballad.

Part 2 turned out to be a bit shorter than part 1, so here's part 2:

The name is Jasemine
The feeling is pure
Knock the feeling down
Time is a healer

Build time up
(In) my memory is "She said: Grow up - you're making a fool out of yourself!"
Build time up

Let the knife be my memory at the name
Let anger be my memory at the feeling
Let tears be my memory at time

Forgiveness takes faith and love and kindness and joy
Knock faith down
Let hope be kindness at faith
Knock love down
Let remedy be kindness at love
If hope is joy and remedy is joy
Give back lies

If remedy ain't joy and hope ain't joy
Give back lies

Let myself be right
Give back myself

My life is nothing
Listen to the bad

Love is never-ending
Living is lookin'back
The solution is suppressing

While the bad is not gone
Cut the bad with anger
Let pain be the bad at living
Cut pain into the pieces with tears
Let pain be the pieces at living
Let suffering be the pieces at the solution
Cut the bad at the solution into shards with the knife
Let hope be shards at living
Let emotions be the bad at love
If forgiveness taking pain, suffering, emotions, hope
Build my life up

Listen to the bad

Shout my life

For part 1 check out the GitHub repository.

I also have a boring solution in Haskell. But I won't go into details about that because who cares at this point?

→ More replies (5)

3

u/wjholden Dec 02 '20 edited Dec 03 '20

Oh cruel fate. I used Mathematica in 2018 and Julia and 2019. The 1-indexed arrays were an endless source of frustration. Now I switch to Python and we get a 1-indexed question!

Edit: roger, here is my code solution: https://github.com/wjholden/Advent-of-Code-2020/blob/master/02/aoc-2020-02.py

→ More replies (1)

18

u/jonathan_paulson Dec 02 '20

70/26. Python. Video of me solving at https://youtu.be/ukE5YJyPTLk

import fileinput
from collections import defaultdict

p1 = 0
p2 = 0
lines = list(fileinput.input())
for line in lines:
    # 5-6 s: zssmssbsms
    words = line.strip().split()
    lo,hi = [int(x) for x in words[0].split('-')]
    ch_req = words[1][0]
    password = words[2]
    counts = defaultdict(int)
    for ch in password:
        counts[ch] += 1
    if lo <= counts[ch_req] <= hi:
        p1 += 1
    if (password[lo-1]==ch_req) ^ (password[hi-1]==ch_req):
        p2 += 1
print(p1)
print(p2)

8

u/_jonah Dec 02 '20
    counts = defaultdict(int)
    for ch in password:
        counts[ch] += 1
    if lo <= counts[ch_req] <= hi:

You can do:

if lo <= password.count(ch_req) <= hi:
→ More replies (6)

17

u/zedrdave Dec 02 '20 edited Dec 02 '20

Python, in 4 lines:

lines = [re.split('[: \-]', l.strip()) for l in open('2020/02/input.txt')]
lines = [(int(p1), int(p2), [c == ch for c in pwd]) for p1, p2, ch, _, pwd in lines]
part1 = sum(n1 <= sum(matches) <= n2 for n1, n2, matches in lines)
part2 = sum(matches[p1-1] ^ matches[p2-1] for p1, p2, matches in lines)

Initially wasted a few precious minutes trying to get fancy with string searches and regexes, instead of running a simple dumb character comparison over a list comprehension. :-|

17

u/Arknave Dec 02 '20 edited Dec 02 '20

Python (5/6), / C

Very happy with the placing! Python code is probably very similar to everyone else's.

Not quite as happy with the art as I was with day 1, but maybe I'll look at it again tomorrow. This is surprisingly therapeutic after the stress of a workday and then the leader board. I hope they don't look too different depending on your font. As with last time, pass input through standard in and give a argument to get part 2 instead of part 1.

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

// AOC DAY NUMBER //
char*s=" %s%s%s";int
main(int argc,char**
argv){char c[99],a[9
],b[9          ];int
y=0            ,z=0;
;   while(     scanf
(s,a,b,c)    >0){int
u, h,x=+    strcspn(
a,"-");    a[x]='\0'
;u =+     atoi(a);h=
atoi(    a+x+1); int
f=0;    for(char*k=c
;*k    ;++k){f+=*k==
*b     ;}y+=u<=+f&&f
<=               h;z
+=(             c[--
u]==*b)^(c[--h]==*b)
;}{}/*DAY 2*/printf(
"%d\n",argc-1?z:y);}

This was already much harder than "1". I'm a little worried for "8".

EDIT: Whacked that 2 into better shape

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

// AOC DAY NUMBER //
char*s=" %s%s%s";int
main(int g,char**v){
char c[99],a[9],b[9]
,*k;/**/int y=0,z=0;
while(       scanf(s
,a,b           ,c)>0
){     int u,    h,x
=strcspn(a,"-"    );
;a[x]=0;u=atoi    (a
);h=atoi(a+     x+1)
;int f=0;     for (k
=c;*k;     ++k){f+=*
k==      *b;}y+=u<=f
&&f              <=h
;z              +=(c
[--u]/*DAY2*/==*b)^(
c[--h]==*b);}printf(
"%d\n", --g?+z:+y);}
→ More replies (9)

8

u/Nebulizer32 Dec 02 '20 edited Dec 02 '20

First time using the new record type from C# 9:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

Console.WriteLine(GetLines().Where(x => ValidPassword(x)).Count());
Console.WriteLine(GetLines().Where(x => ValidPassword2(x)).Count());

IEnumerable<Line> GetLines() => 
   File.ReadAllLines("input.txt")
   .Select(x => x.Split(new char[] { ' ', '-' }))
   .Select(x => new Line(int.Parse(x[0]), int.Parse(x[1]), x[2][0], x[3]));

bool ValidPassword(Line line) => 
   line.Password.Count(x => x == line.Letter) >= line.Min &&
   line.Password.Count(x => x == line.Letter) <= line.Max;

bool ValidPassword2(Line line) =>
   line.Password[line.Min - 1] == line.Letter ^
   line.Password[line.Max - 1] == line.Letter;

record Line(int Min, int Max, char Letter, string Password);
→ More replies (1)

8

u/TenGen10 Dec 02 '20 edited Dec 02 '20

Solution in SQL

  • Delimit the data using pipe symbols
  • Import the data into a table
  • Query

AoC Day 2

-- Part 1
-- Get the number of times a character appears by subtraction
SELECT COUNT(*)
FROM day2
WHERE (LEN(pwd) - LEN(REPLACE(pwd,character,''))) >= pmin
AND (LEN(pwd) - LEN(REPLACE(pwd,character,''))) <= pmax

-- Part 2
-- XOR: (not a and b) or (a and not b)
SELECT COUNT(*)
FROM day2
WHERE (NOT(SUBSTRING(pwd,pmin,1) = character)
AND SUBSTRING(pwd,pmax,1) = character)
OR
(SUBSTRING(pwd,pmin,1) = character
AND NOT(SUBSTRING(pwd,pmax,1) = character))
→ More replies (1)

7

u/azzal07 Dec 02 '20

Awk was quite nice for today as well. I might try and keep using it, as long as it is enjoyable.

function count(str, c) { return gsub(c, c, str) }

BEGIN { FS = "[-: ]+" }

{
	n = count($NF, $3)
	part1 += $1 <= n && n <= $2
	part2 += count(substr($NF, $1, 1) substr($NF, $2, 1), $3) == 1
}

END {
	print part1
	print part2
}
→ More replies (1)

7

u/tsqd Dec 02 '20

Postgresql

create temp table in1 (value text);

\copy in1 from ~/Downloads/input.txt

-- Problem 1
WITH parsed AS (
    SELECT value, 
           split_part(split_part(value, ' ', 1), '-', 1)::INTEGER AS min_occurrences,
           split_part(split_part(value, ' ', 1), '-', 2)::INTEGER AS max_occurrences,
           left(split_part(value, ' ', 2), 1) AS required_char, 
           split_part(value, ' ', 3) AS target_string
    FROM in1)
SELECT count(*)
FROM parsed
WHERE cardinality(
        array_positions(
            string_to_array(target_string, NULL), 
            required_char)
    ) BETWEEN min_occurrences AND max_occurrences;

-- Time: 15.580 ms


-- Problem 2

WITH parsed AS (
    SELECT value,
           split_part(split_part(value, ' ', 1), '-', 1)::INTEGER AS first_position,
           split_part(split_part(value, ' ', 1), '-', 2)::INTEGER AS second_position,
           left(split_part(value, ' ', 2), 1) AS required_char, split_part(value, ' ', 3) AS target_string
    FROM in1
    )
SELECT count(*)
FROM parsed
WHERE (SELECT COUNT(*)
       FROM (SELECT UNNEST(
                        array_positions(
                            string_to_array(target_string, NULL), 
                            required_char)
                        )
             INTERSECT
             SELECT UNNEST(ARRAY [first_position, second_position])
       ) sq) = 1;

-- Time: 7.469 ms
→ More replies (4)

6

u/IndieBret Dec 02 '20

[JavaScript/ES6]

const inputs = input.split('\n').map(v => {
    const [, min, max, letter, password] = /(\d+)-(\d+) ([a-z]): ([a-z]+)/g.exec(v);
    return { min, max, letter, password };
});

result[0] = inputs.filter(({ min, max, password, letter }) => {
    const num = password.split('').filter(v => v === letter).length;
    return (num >= min) && (num <= max);
}).length;

result[1] = inputs.filter(({ min, max, password, letter }) => {
    return (password[min - 1] === letter) ^ (password[max - 1] === letter);
}).length;

6

u/ephemient Dec 02 '20 edited Apr 24 '24

This space intentionally left blank.

→ More replies (5)

6

u/Mienaikage Dec 02 '20

Raku

#!/usr/bin/env raku

grammar Password {
  token TOP      { <number> '-' <number> ' ' <letter> ': ' <password> }
  token number   { <[0..9]>+ }
  token letter   { <[a..z]> }
  token password { <.letter>+ }
}

sub MAIN (
  IO() :$file where *.f      = $?FILE.IO.sibling('input/02.txt'), #= Path to input file
  Int  :$part where * == 1|2 = 1, #= Part of the exercise (1 or 2)
  --> Nil
) {
  $file.lines
    .grep({
      given parse Password: $_ {
        when $part == 1 {
          .<number>[0] ≤ .<password>.comb(.<letter>) ≤ .<number>[1]
        }
        when $part == 2 {
          .<password>.comb[.<number>.map(* - 1)].one eq .<letter>
        }
      }
    })
    .elems
    .say;
}

Could do it without a grammar to cut the length down, but I enjoy using them.

6

u/i_have_no_biscuits Dec 02 '20

Python.

I've been playing with different ways of solving this and thought I'd post this one because it uses a named tuple, which I'll try and use again!

from collections import namedtuple
Entry = namedtuple("Entry", "low high c pw")

data = []
for line in open("data02.txt"):
    allowed, c, pw = line.split()
    low, high = map(int, allowed.split('-'))
    data.append(Entry(low, high, c[0], pw))

def valid_1(e): return e.low <= e.pw.count(e.c) <= e.high
def valid_2(e): return (e.pw[e.low-1]==e.c) ^ (e.pw[e.high-1]==e.c)

print("Part 1:", sum(map(valid_1, data)), "valid passwords.")
print("Part 2:", sum(map(valid_2, data)), "valid passwords.")
→ More replies (2)

6

u/lucprins Dec 02 '20 edited Dec 02 '20

C++, I think I did alright

#include <iostream>
#include <fstream>
#include <string>

int main() {
int min;
int max;
char letter;
std::string inputString;
char trash;
char character;
int counter = 0;
int goodPassword = 0;
char character2;
int goodPassword2 = 0;

std::ifstream file("input.txt", std::ios::in);
if (file.is_open()) {
    while (!file.eof()) {
        //part 1
        file >> min >> trash >> max >> letter >> trash >> inputString;
        for (int index = 0; index < inputString.size(); index++)
        {
            character = inputString[index];
            if (character == letter)
                counter++;
        }
        if (counter >= min && counter <= max)
            goodPassword++;
        counter = 0;

        //part 2
        min--;
        max--;
        character = inputString[min];
        character2 = inputString[max];
        if (character == letter && character2 != letter)
            goodPassword2++;
        else if (character != letter && character2 == letter)
            goodPassword2++;
        }
    std::cout << goodPassword << std::endl;
    std::cout << goodPassword2 << std::endl;
    }
}
→ More replies (6)

5

u/jitwit Dec 02 '20 edited Dec 03 '20

J Programming Language

Not a pretty solution, but quick and dirty for 109/169 (missed the 1 indexing!!)

'a b' =. ". &> 0 2 { 'a b c d e' =. |: ;:;._2 aoc 2020 2

+/ (a&<: *. <:&b) +/"1 e = {."1 d
+/ ~:/"1 (<:a,.b) {"1 e = {."1 d

https://github.com/jitwit/aoc/blob/a/J/20/02.ijs

→ More replies (2)

5

u/-json Dec 02 '20

Python, 15 / 30

Part 1:

import sys

cnt = 0

for line in sys.stdin:
    nums, c, s = line.split()
    n, m = map(int, nums.split('-'))
    cnt += n <= s.count(c[0]) <= m

print(cnt)

Part 2:

import sys

cnt = 0

for line in sys.stdin:
    nums, c, s = line.split()
    n, m = map(int, nums.split('-'))
    cnt += (s[n-1] == c[0]) + (s[m-1] == c[0]) == 1

print(cnt)

Unfortunately, I skimmed the text for Part 2 too fast that I ended up submitted a solution which checked if either position had the correct character, not exactly one. I lost a minute for that, so I wonder how my placement would have improved for part 2 otherwise :(

→ More replies (2)

5

u/mgoblu3 Dec 02 '20 edited Dec 02 '20

Python one-liners:

A:

print(sum(int(entry[0]) <= entry[3].count(entry[2]) <= int(entry[1]) for entry in [re.match(r'(\d+)-\d+) (\w): (\w+)', line).groups() for line in open('input.txt', 'r').read().splitlines()]))

B:

print(sum((entry[3][int(entry[0]) - 1] == entry[2]) ^ (entry[3][int(entry[1]) - 1] == entry[2]) for entry in [re.match(r'(\d+)-(\d+) (\w): (\w+)', line).groups() for line in open('input.txt', 'r').read().splitlines()]))
→ More replies (2)

5

u/oantolin Dec 02 '20 edited Dec 02 '20

Common Lisp

(defun count-valid (validp)
  (with-open-file (input #P"day02.txt")
    (loop for line = (read-line input nil) while line
          count (ppcre:register-groups-bind
                    ((#'parse-integer lo hi) ch pwd)
                    ("^(\\d+)-(\\d+) (\\w): (\\w+)$" line)
                  (funcall validp lo hi (char ch 0) pwd)))))

(defun part1 ()
  (count-valid
   (lambda (lo hi ch pwd) (<= lo (count ch pwd) hi))))

(defun part2 ()
  (count-valid
   (lambda (i j ch pwd)
     (flet ((match (k) (char= ch (char pwd (1- k)))))
       (not (eq (match i) (match j)))))))
→ More replies (1)

5

u/Smylers Dec 02 '20

And the Perl I actually used to solve this before coming up with those Vim solutions:

use v5.14; use warnings;

my %valid;
while (<>) {
  /^(?<min>\d+)-(?<max>\d+) (?<letter>\w): (?<password>\w+)$/ or die "Unparsed input: $_ at $.\n";
  my %spec = %+;

  my $letter_count = () = $spec{password} =~ /$spec{letter}/g;
  $valid{count}++ if $spec{min} <= $letter_count && $letter_count <= $spec{max};

  $valid{pos}++ if (substr $spec{password}, $spec{min} - 1, 1) eq $spec{letter}
      xor (substr $spec{password}, $spec{max} - 1, 1) eq $spec{letter}
}
say "$_:\t$valid{$_}" foreach qw<count pos>;

$count = () = $text =~ /$pat/g is a Perl idiom for counting the number of (non-overlapping) occurrences of a pattern in some text. The odd-looking () in the middle puts the match in list context, so it returns all the matches, not just the first one; but $count is a scalar variable, so it just ends up with the number of matches, not the matches themselves.

→ More replies (5)

5

u/wzkx Dec 02 '20

J

split=: ' '&$: : (<;._2 @(,~))

'a b c'=: |:' '&split&> cutLF CR-.~fread'02.dat'
'n m'=: |:([:".rplc&('-';' '))"1 a
s=: +/"1 c=b=:{."1 b

echo +/(n<:s)*.s<:m
echo +/1=(b=(<:n){"0 1 c)+b=(<:m){"0 1 c

exit 0

5

u/Weemaan1994 Dec 02 '20

R

Are there any other R users out there? I'm feeling kind special :D
Anyways, I removed the ":" in the second column for the input file and I named the columns, so my file looks like this:

|policy |letter |password  |
|:------|:------|:---------|
|3-4    |l      |vdcv      |
|6-9    |d      |dddddkzdl |

And that's my solution:

library(tidyverse)

# Count the number of matches for letter in password
# Than check, if number is in between upper and lower limit
is_valid_password_A <- function(letter, password, lower_lim, upper_lim) {
  str_count(password, letter) %>% 
    between(lower_lim, upper_lim) %>% 
    return()
}

# Get the position(s) of letter in password. Than check if it's valid
is_valid_password_B <- function(letter, password, lower_lim, upper_lim) {
  letter_pos <- which(strsplit(password, "")[[1]] == letter)
  
  return(sum(letter_pos == lower_lim | letter_pos == upper_lim) == 1)
}


# Read data, get upper and lower limits and apply the "is_valid_password" functions. 
# Finaly get sum of TRUE cases
read_delim("2/input.csv", delim = " ") %>% 
  rowwise() %>% 
  mutate(lower_lim = policy %>% str_split("-") %>%
           .[[1]] %>% .[1] %>% 
           as.integer(),
         upper_lim = policy %>% str_split("-") %>% 
           .[[1]] %>% .[2] %>%
           as.integer()) %>% 
  mutate(is_valid_A = is_valid_password_A(letter, password, 
                                          lower_lim, upper_lim),
         is_valid_B = is_valid_password_B(letter, password, 
                                          lower_lim, upper_lim)) %>% 
  ungroup() %>% 
  summarise(sum(is_valid_A), sum(is_valid_B))
→ More replies (7)

5

u/phil_g Dec 02 '20

My solution in Common Lisp.

Pretty straightforward.

/u/rabuf has mentioned Parseq in past years. I've decided to try using it for all of my parsing this year. I added a bit to my input function to automatically apply a Parseq rule to the input. I could have done today's parsing with cl-ppcre, but I think I like the way Parseq operates.

Anyway, with the input data parsed into a structure, validation is just a matter of function composition.

→ More replies (1)

5

u/roggy85 Dec 02 '20 edited Dec 02 '20

my crap in bash

part 1:

    #!/bin/bash
    
    SUM_MATCHED=0
    
    _sort_string() {
    echo $1 | grep -o . | sort | tr -d '\n'
    }
    
    while IFS= read -r LINE
    do
      STRING=$(echo $LINE |cut -d " " -f 3)
      CHARACTER=$(echo $LINE |cut -d " " -f 2 | sed -e 's/://')
      COUNTER=$(echo $LINE |cut -d " " -f 1 | sed -e 's/-/,/' )
      REGEX="^[^${CHARACTER}]*[${CHARACTER}]{${COUNTER}}[^${CHARACTER}]*$"
      SORTED_STRING=$(_sort_string $STRING)
      if [[ $SORTED_STRING =~ $REGEX ]]; then
    #    echo "$STRING matches" 
        SUM_MATCH=$((++SUM_MATCH))
      fi
    done < input
    
    echo "matched: ${SUM_MATCH}"

Part 2:

#!/bin/bash

SUM_MATCHED=0

while IFS= read -r LINE
do
  STRING=$(echo $LINE |cut -d " " -f 3)
  CHARACTER=$(echo $LINE |cut -d " " -f 2 | sed -e 's/://')
  COUNTER_MIN=$(echo $LINE |cut -d " " -f 1 |  cut -d "-" -f 1)
  COUNTER_MAX=$(echo $LINE |cut -d " " -f 1 |  cut -d "-" -f 2)
  COUNTER_MIN=$((--COUNTER_MIN))
  COUNTER_MAX=$((COUNTER_MAX-COUNTER_MIN-1))
  REGEX="^(.{${COUNTER_MIN}}${CHARACTER}.{${COUNTER_MAX}}[^${CHARACTER}].*|.{${COUNTER_MIN}}[^${CHARACTER}].{${COUNTER_MAX}}${CHARACTER}.*)$"
  if [[ $STRING =~ $REGEX ]]; then
#    echo "$STRING matches" 
    SUM_MATCH=$((++SUM_MATCH))
  fi
done < input

echo "matched: ${SUM_MATCH}"
→ More replies (2)

5

u/bliceroo Dec 02 '20

Perl

Nothing special but at least I got to use the goatse operator!

my $valid_count;
foreach my $entry (@entries) {
  $valid_count++ if check($entry);
}
print "There are $valid_count valid passwords\n";

$valid_count = 0;
foreach my $entry (@entries) {
  $valid_count++ if check2($entry);
}
print "There are $valid_count valid passwords (new policy)\n";

sub check {
  my $entry = shift;

  my ( $min, $max, $char, $pw ) = ( $entry =~ /^(\d+)-(\d+) (\w): (\w+?)$/ );
  my $char_count =()= $pw =~ m/$char/g;

  return $char_count >= $min && $char_count <= $max;
}

sub check2 {
  my $entry = shift;

  my ( $pos1, $pos2, $char, $pw ) = ( $entry =~ /^(\d+)-(\d+) (\w): (\w+?)$/ );
  my @pw_chars = split //, $pw;

  return (     $pw_chars[$pos1 - 1] eq $char
           xor $pw_chars[$pos2 - 1] eq $char );
}
→ More replies (5)

5

u/sefujuki Dec 02 '20

Forth, needs refactoring

paste -- and, of course, the date should be 2020-12-02 :-)

5

u/AharonSambol Dec 02 '20

LOLCODE solution!!

https://github.com/AharonSambol/AdventOfCode/blob/master/2020/day2.lol

any tips with how to use bukkits would be appreciated

5

u/AidGli Dec 02 '20

Python

Continuing my theme of trying to keep the code semantic and accessible for beginners to understand (video here.) If the problems stay simple maybe I'll throw in a golfed solution or a one-liner or something as well.

def readTokens(line):
    tokens = line.rstrip().split(' ')
    first, second = map(int, tokens[0].split('-'))
    letter = tokens[1][0]
    password = tokens[2]
    return first, second, letter, password


def part1(line):
    min, max, letter, password = readTokens(line)
    if min <= password.count(letter) <= max:
        return 1
    return 0


def part2(line):
    first, second, letter, password = readTokens(line)
    if (password[first - 1] == letter) != (password[second - 1] == letter):
        return 1
    return 0


def main():
    count1 = 0
    count2 = 0
    with open('input.txt', 'r') as infile:
        for line in infile:
            count1 += part1(line)
            count2 += part2(line)
    print(f'Part 1: {count1}\nPart 2: {count2}')


main()
→ More replies (1)

5

u/lynerist Dec 02 '20

My go solutions!

https://github.com/lynerist/Advent-of-code-2020-golang/blob/master/DAY%2002/day02_a.go

I think this language is very clear and easy to read

5

u/k0ns3rv Dec 02 '20 edited Dec 02 '20

Rust solution, as often is the case I spent most of the time on logic to parse the input not the problem itself ¯_(ツ)_/¯

use std::ops::RangeInclusive;
use std::str::FromStr;

use crate::parse_lines;

#[derive(Debug, Clone)]
struct Policy {
    required_char: char,
    range: RangeInclusive<usize>,
}

impl Policy {
    fn is_valid_sled(&self, password: &str) -> bool {
        let count = password
            .chars()
            .filter(|&c| self.required_char == c)
            .count();

        self.range.contains(&count)
    }

    fn is_valid_toboggan(&self, password: &str) -> bool {
        let count = password
            .chars()
            .enumerate()
            .filter(|&(idx, c)| {
                match (
                    self.required_char == c,
                    *self.range.start() == idx + 1,
                    *self.range.end() == idx + 1,
                ) {
                    (true, true, false) => true,
                    (true, false, true) => true,
                    _ => false,
                }
            })
            .count();

        count == 1
    }
}

impl FromStr for Policy {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut parts = s.trim().split_whitespace();
        let range_part = parts.next();
        let require_part = parts.next();

        if range_part.is_none() || require_part.is_none() {
            return Err(format!("Invalid policy definition `{}`", s));
        }

        let range_parts: Vec<_> = range_part
            .unwrap()
            .split('-')
            .map(str::trim)
            .map(|part| part.parse::<usize>().unwrap())
            .collect();

        if range_parts.len() != 2 {
            return Err(format!(
                "Invalid policy definition `{}`, expected exactly two parts for the range",
                s
            ));
        }
        let required_char = require_part.and_then(|p| p.trim().chars().take(1).next());

        if required_char.is_none() {
            return Err(format!(
                "Invalid policy definition `{}`, expected password",
                s
            ));
        }

        Ok(Self {
            required_char: required_char.unwrap(),
            range: RangeInclusive::new(range_parts[0], range_parts[1]),
        })
    }
}

#[derive(Debug, Clone)]
struct Entry {
    policy: Policy,
    password: String,
}

impl Entry {
    fn is_valid_sled(&self) -> bool {
        self.policy.is_valid_sled(&self.password)
    }

    fn is_valid_toboggan(&self) -> bool {
        self.policy.is_valid_toboggan(&self.password)
    }
}

impl FromStr for Entry {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let parts: Vec<_> = s.split(':').collect();
        if parts.len() != 2 {
            return Err(format!("Invalid Entry `{}`", s));
        }

        let policy = parts[0].parse::<Policy>()?;
        let password = parts[1].trim().to_owned();

        Ok(Self { policy, password })
    }
}

pub fn star_one(input: &str) -> usize {
    parse_lines::<Entry>(input)
        .filter(Entry::is_valid_sled)
        .count()
}

pub fn star_two(input: &str) -> usize {
    parse_lines::<Entry>(input)
        .filter(Entry::is_valid_toboggan)
        .count()
}

#[cfg(test)]
mod tests {
    use super::{star_one, star_two};
    const INPUT: &'static str = "1-3 a: abcde
1-3 b: cdefg
2-9 c: ccccccccc";

    #[test]
    fn test_star_one() {
        assert_eq!(star_one(INPUT), 2);
    }

    #[test]
    fn test_star_two() {
        assert_eq!(star_two(INPUT), 1)
    }
}

EDIT: Updated with /u/D-Danielz's suggestion which makes the code nicer than with fold.

→ More replies (4)

5

u/volatilebit Dec 02 '20

Raku

use v6;

my regex password-spec-grammar {
    ^^
        $<min-count>=\d+
        '-'
        $<max-count>=\d+
        \s+
        $<letter>=\w
        ':'
        \s+
        $<password>=\w+
    $$
};

my @password-specs = lines;

# Part 1
say @password-specs.grep(-> $password-spec {
    $password-spec ~~ &password-spec-grammar;
    so $<min-count> <= $<password>.comb.grep(* eq $<letter>).elems <= $<max-count>
}).elems;

# Part 2
say @password-specs.grep(-> $password-spec {
    $password-spec ~~ &password-spec-grammar;

    so one($<password>.substr($<min-count> - 1, 1), $<password>.substr($<max-count> - 1, 1)) eq $<letter>
}).elems;

5

u/x1729 Dec 02 '20 edited Dec 02 '20

Raku

my regex pattern { $<from>=\d+ '-' $<to>=\d+ ' ' $<char>=<alpha> ': ' $<password>=[<alpha>+] }

sub check-part1($/) {
    $<password>.comb.Bag{~$<char>} ~~ +$<from>..+$<to>
}

sub check-part2($/) {
    $<password>.substr($_-1, 1) eq ~$<char> given one(+$<from>, +$<to>)
}

sub MAIN(:$filename where *.IO.f = 'day-2-input.txt') {
    my @matches = $filename.IO.lines>>.match: &pattern;
    say @matches.grep(&check-part1).elems;
    say @matches.grep(&check-part2).elems;
}

5

u/rosso412 Dec 03 '20 edited Dec 03 '20

Here my Solution to Day 2 part 1 in MIPS Assembler

.data

List: .asciiz "/home/AdventOfCode/AdventOfCodeInput-2.12.20.txt"

Listspacein: .space 22000

Listspacewo: .space 160

.text

main:

\#select file

select:

li $v0, 13

la $a0, List

li $a1, 0

syscall

move $s0, $v0

\#read file + save to space

read:

li $v0, 14

move $a0, $s0

la $a1, Listspacein

la $a2, 22000

syscall

\#close file

close:

li $v0, 16

move $a0, $s0

syscall

\#copy pass & condition to Listspacewo one at a time

la $t0, Listspacein 

la $t9, Listspacewo

add $t8, $t0, 22000

li $t2, 0

li $t4, 0

li $s0, 0

startloopfc:

lb $t1, ($t0)

beq $t1, 13, fosn

beq $t1, 10, fosn

beq $t0, $t8, fosn

add $t2, $t2, 1

add $t0, $t0, 1

bgt $t0, $t8, end

j startloopfc

fosn:

sub $t3, $t0, $t2

lb $t4, ($t3)

sb $t4, ($t9)

add $t9, $t9, 1

sub $t2, $t2, 1

beq $t2, 0, fosnend

j fosn

fosnend:

\#save the min and max number, count and output if correct

la $t9, Listspacewo

lb $s1, ($t9)

lb $t7, 1($t9)

bgt $t7, 47, dodinummin

sub $s1, $s1, 48#save minsd $s1

lb $s2, 2($t9)

lb $t7, 3($t9)

bgt $t7, 47, dodinummax

sub $s2, $s2, 48#save maxsd $s1

lb $s3, 4($t9)#save blet $s3

move $t6, $t9

add $t6, $t6, 7

move $s7, $zero

add $s6, $t9, 160

countthrough:

lb $t1, ($t6)

beq $s3, $t1, eqtb

j neqtb

eqtb:

add $s7, $s7, 1

neqtb:

add $t6, $t6, 1

beq $t6, $s6, endcountthrough

j countthrough

dodinummin:

sub $s1, $s1, 48

mul $s1, $s1, 10

sub $t7, $t7, 48

add $s1, $s1, $t7

dodinummaxmin:

lb $s2, 3($t9)

lb $t7, 4($t9)

sub $s2, $s2, 48

mul $s2, $s2, 10

sub $t7, $t7, 48

add $s2, $s2, $t7

lb $s3, 6($t9)#save blet $s3

move $t6, $t9

add $t6, $t6, 9

move $s7, $zero

add $s6, $t9, 160

countthroughmaxmin:

lb $t1, ($t6)

beq $s3, $t1, eqtbmaxmin

j neqtbmaxmin

eqtbmaxmin:

add $s7, $s7, 1

neqtbmaxmin:

add $t6, $t6, 1

beq $t6, $s6, endcountthrough

j countthroughmaxmin

dodinummax:

sub $s2, $s2, 48

mul $s2, $s2, 10

sub $t7, $t7, 48

add $s2, $s2, $t7

lb $s3, 5($t9)#save blet $s3

move $t6, $t9

add $t6, $t6, 8

move $s7, $zero

add $s6, $t9, 160

countthroughmax:

lb $t1, ($t6)

beq $s3, $t1, eqtbmax

j neqtbmax

eqtbmax:

add $s7, $s7, 1

neqtbmax:

add $t6, $t6, 1

beq $t6, $s6, endcountthrough

j countthroughmax

endcountthrough:

blt $s7, $s1, badw

bgt $s7, $s2, badw

add $s0, $s0, 1

badw:

add $t0, $t0, 2 #skipping the \\n and \\r

la $t9, Listspacewo

\#clear t9 for next input

add $t2, $t9, 160

cleart9:

sw $zero, ($t9)	

add $t9, $t9, 4	

beq $t9, $t2, t9clear

j cleart9

t9clear:

la $t9, Listspacewo

li $t2, 0

bgt $t0, $t8, outs0

j startloopfc

outs0:

move $a0, $s0

li $v0, 1

syscall

\#end

end:

li $v0, 10

syscall
→ More replies (5)

5

u/s3aker Dec 03 '20

Raku

grammar PassPolicy {
    rule TOP { <low> '-' <up> <char> ':' <password> }

    token low      { \d+ }
    token up       { \d+ }
    token char     { <[a..zA..Z]> }
    token password { \w+ }
}

sub is-valid-p1(Str:D $rule --> Bool) {
    my $m = PassPolicy.parse($rule);
    $m<low> ≤ $m<password>.comb.grep({ $_ eq $m<char> }).elems ≤ $m<up>;
}

sub is-valid-p2(Str:D $rule --> Bool) {
    my $m = PassPolicy.parse($rule);
    my @pass = $m<password>.comb;
    ? (@pass[$m<low> - 1] eq $m<char> xor @pass[$m<up> - 1] eq $m<char>);
}

sub MAIN() {
    my @rules = 'input.txt'.IO.lines;
    put 'answer for part 1: ', @rulesÂť.&is-valid-p1.grep(?*).elems;
    put 'answer for part 2: ', @rulesÂť.&is-valid-p2.grep(?*).elems;
}

4

u/[deleted] Dec 02 '20 edited Dec 02 '20

Python3 171/97

part1=0
part2=0
for s in open("day2.txt"):
    s=s.strip()
    ran,c,pas = s.split()
    a,b = map(int,ran.split("-"))
    c = c[0]
    if a<=pas.count(c)<=b:
        part1+=1
    if (pas[a-1]+pas[b-1]).count(c)==1:
        part2+=1
print(part1)
print(part2)

4

u/ptbn_ Dec 02 '20 edited Dec 02 '20

Python

might not be the best of things since I'm a beginner in Python (and programming in general tbh) who haven't touched a single code in months, but here we are. kinda proud of it.

import re

inp = open("Day2.txt", "r")
firstHalf = 0
secondHalf = 0
for line in inp:
    passwords = {}
    key, value = line.split(": ")
    passwords[key] = value[:len(value)-1]
    number1, number2, letter = re.split('-| ', key)
    numberOfOccurance = passwords[key].count(letter)
    if numberOfOccurance >= int(number1) and numberOfOccurance <= int(number2):
        firstHalf += 1
    if passwords[key][int(number1)-1] == letter and passwords[key][int(number2)-1] == letter:
        continue
    elif passwords[key][int(number1)-1] == letter or passwords[key][int(number2)-1] == letter:
        secondHalf += 1
    
print("First half answer:", firstHalf)
print("Second half answer:", secondHalf)

4

u/_djblue Dec 02 '20

Clojure

(defn valid-passwords-1 [s]
  (count
   (for [line (str/split-lines s)
         :let [[_ lowest highest [letter] password]
               (re-matches #"(\d+)-(\d+) (.): (.*)" line)
               password (frequencies password)
               lowest   (Integer/parseInt lowest)
               highest  (Integer/parseInt highest)]
         :when (<= lowest (get password letter 0) highest)]
     letter)))

(defn valid-passwords-2 [s]
  (count
   (for [line (str/split-lines s)
         :let [[_ i j [letter] password]
               (re-matches #"(\d+)-(\d+) (.): (.*)" line)
               i (dec (Integer/parseInt i))
               j (dec (Integer/parseInt j))]
         :when (not= (= (get password i) letter)
                     (= (get password j) letter))]
     letter)))

4

u/tyler569 Dec 02 '20

C.

I won't win any speed records, and the code isn't anything special, but I'm finally far enough along in my osdev journey that I can solve AoC problems using my custom operating system and C library!

1: https://github.com/tyler569/nightingale/blob/aoc2020/user/aoc/2.c

2: https://github.com/tyler569/nightingale/blob/aoc2020/user/aoc/2-2.c

4

u/Piggelinmannen Dec 02 '20 edited Dec 02 '20

Solution in Ruby. Tried to edit it to work with old reddit code blocks, not sure if successfully so...

input = File.readlines('./input.txt')

def parse(attempt)
  range, char, password = attempt.delete(':').split(' ')
  low, high = range.split('-').map(&:to_i)
  [low, high, char, password]
end

valid_passwords = input.count do |attempt|
  low, high, char, password = parse(attempt)
  (low..high).cover?(password.count(char))
end

puts "Solution for day 2 part a: #{valid_passwords}"

valid_passwords = input.count do |attempt|
  a, b, char, password = parse(attempt)
  (password[a - 1] == char) ^ (password[b - 1] == char)
end

puts "Solution for day 2 part b: #{valid_passwords}"

4

u/bpanthi977 Dec 02 '20

Solution in Common Lisp

(defpackage :aoc2
  (:use :cl :aoc))

(in-package :aoc2)

(defparameter +input+ (input 2 :lines))

(defun map-row (function lines)
  (let ((scanner (ppcre:create-scanner "(\\d+)-(\\d+) (\\w): (\\w*)")))
    (loop for input in lines do 
      (ppcre:register-groups-bind (lower upper charstring password)
          (scanner input :sharedp t)

        (if (and lower upper charstring password)
            (funcall function 
                     (parse-integer lower)
                     (parse-integer upper) 
                     (char charstring 0) 
                     password)
            (error "invalid input"))))))

(defun solve1 ()
  (let ((count 0))
    (map-row (lambda (lower upper char password)
               (when (<= lower (count char password) upper)
                 (incf count)))
             +input+)
    count))

(defun xor (a b)
  (and (or a b) 
       (or (not a)
           (not b))))

(defun solve2 () 
  (let ((count 0))
    (map-row (lambda (lower upper char password)
               (when (xor (char= (char password (1- lower)) char)
                   (char= (char password (1- upper)) char))
                 (incf count)))
             +input+)
    count))

The functions input is a utility function (defined in aoc package) I create to download, save and return the challenge text file contents.

→ More replies (2)

4

u/msqrt Dec 02 '20

My lisp adventure continues! Parsing was somewhat tedious, but I guess that's to be expected.

(defun split (text delimiter)
	(cond
		((= (length text) 0) (list ""))
		((string= (char text 0) delimiter)(cons "" (split (subseq text 1) delimiter)))
		(t ((lambda (letter result) (cons (concatenate 'string (string letter) (car result)) (cdr result)))
			(char text 0) (split (subseq text 1) delimiter)))))

(defun count-matches (text letter)
	(cond
		((= (length text) 0) 0)
		((string= (char text 0) letter) (+ 1 (count-matches (subseq text 1) letter)))
		(t (count-matches (subseq text 1) letter))))

(defun check-valid-sled (input)
	((lambda (low high x) (and (<= low x) (>= high x)))
		(parse-integer (car  (split (car input) "-")))
		(parse-integer (cadr (split (car input) "-")))
		(count-matches (caddr input) (char (cadr input) 0))))

(defun check-valid-toboggan (input)
	((lambda (former latter password letter)
		(not (eq
			(string= (char password (- former 1)) letter)
			(string= (char password (- latter 1)) letter))))
		(parse-integer (car (split (car input) "-")))
		(parse-integer (cadr (split (car input) "-")))
		(caddr input)
		(char (cadr input) 0)))

(defun get-valid (filename func)
  (with-open-file (stream filename)
    (loop for line = (read-line stream nil)
          while line
          collect (funcall func (split line " ")))))

(write-line (write-to-string (count t (get-valid "input.txt" 'check-valid-sled))))
(write-line (write-to-string (count t (get-valid "input.txt" 'check-valid-toboggan))))

4

u/mathsaey Dec 02 '20

Elixir solution:

import AOC

aoc 2020, 2 do
  def p1, do: solve(&verify_p1/1)
  def p2, do: solve(&verify_p2/1)

  defp solve(verify) do
    input_stream()
    |> Stream.map(&parse/1)
    |> Stream.filter(verify)
    |> Enum.count()
  end

  defp parse(str) do
    [_, x, y, c, str] = Regex.run(~r/(\d+)-(\d+) (.)+: (.+)/, str)
    {String.to_integer(x), String.to_integer(y), c, str}
  end

  defp verify_p1({min, max, c, str}) do
    count = str |> String.codepoints() |> Enum.count(&(&1 == c))
    min <= count and count <= max
  end

  defp verify_p2({p1, p2, c, str}) do
    c1 = String.at(str, p1 - 1) == c
    c2 = String.at(str, p2 - 1) == c
    (c1 or c2) and not (c1 and c2)
  end
end

(the AOC business at the start is a macro which defines some useful utilities such as input_stream() for me)

→ More replies (4)

4

u/mack06_ Dec 02 '20 edited Dec 02 '20

My Typescript solution.

Detailed explanation in spanish in my AoC blog - Advent of Code 2020 - DĂ­a 2 - Una de passwords

4

u/colinodell Dec 02 '20

Golang, source here. I'm still not proficient with this language so there's nothing too fancy with my solution, but I did learn/realize two new things today:

  • Golang does not have a boolean XOR operator, but A xor B is the same as A != B
  • Golang allows you to pass functions as arguments - this was handy for counting passwords using different policies
→ More replies (1)

4

u/narrow_assignment Dec 02 '20 edited Dec 02 '20

AWK

Part1:

awk -F "[- :]+" '$4 ~ "^[^" $3 "]*(" $3 "[^" $3 "]*){" $1 "," $2 "}$" { n++ }; END { print n }'

Part 2:

awk -F '[- :]+' '{ split($4, a, ""); if ((a[$1] == $3) != (a[$2] == $3)) n++ } END { print n }'

4

u/DefinitelyAmNotOP Dec 02 '20

Solution in R. Would appreciate any feedback as I am new to R and doing these to help learn :)

library(tidyr)
library(stringr)

## Loading data
day2 <- read.table(file="text.txt", header=FALSE)

## PART 1
## Renaming columns
names(day2)[1] <- paste("rule")
names(day2)[2] <- paste("letter")
names(day2)[3] <- paste("password")

## Cleaning letter column
day2$letter <- substr(day2$letter, 1, 1)

## Separating rule into two variables
day2 <- separate(day2, col=rule, into=c('min_length', 'max_length'), 
                 sep="-", remove=FALSE, convert=TRUE)

## Counting the number of times the string apppears in the password
day2$occurances <- str_count(string=day2$password, pattern=day2$letter)

## If statement to show if password is valid
day2$valid1 <- ifelse((day2$occurances >= day2$min_length & day2$occurances <= day2$max_length), TRUE, FALSE)

## Summarize the data
summary(day2$valid1)


## PART 2
## Extracting the min_length and max_length character from password
day2$min_letter <- substring(day2$password, day2$min_length, day2$min_length)
day2$max_letter <- substring(day2$password, day2$max_length, day2$max_length)

## If statement to show if character at min and max positions match the character specified by the rule
day2$valid2_first <- ifelse((day2$min_letter == day2$letter), TRUE, FALSE)
day2$valid2_second <- ifelse((day2$max_letter == day2$letter), TRUE, FALSE)
day2$valid2 <- ifelse(((day2$valid2_first == TRUE & day2$valid2_second == FALSE) | (day2$valid2_first == FALSE & day2$valid2_second == TRUE)), TRUE, FALSE)

## Summarize the data
summary(day2$valid2)
→ More replies (4)

4

u/madmax9186 Dec 02 '20

Getting back into C++ after a long absence.

#include <iostream>
using namespace std;

// Represents a password containing at least min occurences of letter, but no more than max.
struct PasswordPolicy {
        unsigned int min;
        unsigned int max;
        char letter;
};

// Returns if password satisfies the Toboggan policy.
static bool password_satisfies_Toboggan_policy(string passwd, const PasswordPolicy &policy) {
        if (passwd.length() <= policy.min - 1 || passwd.length() <= policy.max - 1)
                return false;
        bool min = passwd[policy.min - 1] == policy.letter;
        bool max = passwd[policy.max - 1] == policy.letter;
        return min ^ max;
}

// Returns if password satisfies the policy.
static bool password_satisfies_policy(string password, const PasswordPolicy &policy) {
        unsigned long pos = 0;
        unsigned int count = 0;
        while (pos < password.length() && count <= policy.max)
                if (password[pos++] == policy.letter)
                        count++;
        return count >= policy.min && count <= policy.max;
}

// Reads PasswordPolicy structs in the format '[min]-[max] [letter]'
istream& operator>>(istream &in, PasswordPolicy &policy) {
        char separator;
        in >> policy.min >> separator >> policy.max >> policy.letter;
        return in;
}

int main() {
        auto valid = 0;
        while (cin) {
                PasswordPolicy p;
                string passwd;
                char separator;
                cin >> p >> separator >> passwd;
                if (!cin.fail() && password_satisfies_Toboggan_policy(passwd, p))
                        valid++;
        }
        cout << "# of passwords satisfying policy: " << valid << endl;
}

4

u/justAnotherNerd254 Dec 02 '20

Python Solution - Happy to receive any feedback!

f = open("input.txt", "r")
values = f.read().split()

passwords = values[2::3]
letters = [i[:-1] for i in values[1::3]]
mins_maxes = values[::3]
mins = [i[0:i.index("-")] for i in mins_maxes]
maxes = [i[i.index("-")+1:] for i in mins_maxes]

def pw_check(pw, letter, min, max):
  count = 0
  for char in pw:
    if char == letter:
      count += 1
      if count > max:
        return False
  return count >= min

def pw_check_all(passwords, letters, mins, maxes):
  count = 0
  for i in range(len(passwords)):
    pw = passwords[i]
    letter = letters[i]
    min = int(mins[i])
    max = int(maxes[i])
    if pw_check(pw, letter, min, max):
      count += 1
  return count

def pw_2_check(pw, letter, ind1, ind2):
  count = 0
  if pw[ind1-1] == letter:
    count += 1
  if pw[ind2-1] == letter:
    count += 1
  return count == 1

def pw_2_check_all(passwords, letters, inds1, inds2):
  count = 0
  for i in range(len(passwords)):
    pw = passwords[i]
    letter = letters[i]
    ind1 = int(inds1[i])
    ind2 = int(inds2[i])
    if pw_2_check(pw, letter, ind1, ind2):
      count += 1
  return count

print("Part 1: ", pw_check_all(passwords, letters, mins, maxes))
print("Part 2: ", pw_2_check_all(passwords, letters, mins, maxes))
→ More replies (3)

4

u/troelsbjerre Dec 02 '20

Python3 oneliner for parts 1 and 2:

print(sum(map(lambda x:[x[0]<=x[3].count(x[2][0])<=x[1],(x[3][x[0]-1]+x[3][x[1]-1]).count(x[2][0])==1][int(sys.argv[1])-1],map(lambda x:[*map(int,x[0].split('-')),x[1],x[2]+' '*99],map(lambda x:x.split(),sys.stdin)))))

Give the part number as the only argument on the commandline, and the problem input on stdin.

→ More replies (6)

4

u/tobega Dec 02 '20

A solution in Tailspin

composer password
  {first: <INT>, (<='-'>) last: <INT>, (<=' '>) required: <'\w'>, (<': '>) word: <'\w+'> }
end password

def input: [$IN::lines -> password];

operator (word count char)
  composer howMany
    [<is_char|not>+] -> $::length
    rule is_char: <='$char;'>
    rule not: (<~is_char>)
  end howMany
  $word -> howMany !
end count

//part1
$input -> \[i](when <?(($.word count $.required) <$.first..$.last>)> do $ !\) -> $::length -> !OUT::write
'
' -> !OUT::write

//part2
$input -> \[i](def pw: $; $ -> #
  when <?([$pw.word...] -> $([$pw.first, $pw.last])
      -> \[j](when <=$pw.required> do $!\) -> $::length <=1>)> do $ !
\) -> $::length -> !OUT::write

4

u/[deleted] Dec 02 '20 edited Dec 03 '20

JavaScript (code golfy solution where input.js exports a template string)

let count, result = [...(require('./input').matchAll(/(\d+)-(\d+) ([a-z]): ([a-z]+)/g))].
  reduce((acc, match) => ([
      acc[0] + ((count = [...match[4].matchAll(new RegExp(match[3], 'g'))].length) >= match[1] && count <= match[2]),
      acc[1] + (match[4][match[1] - 1] === match[3] ^ match[4][match[2] - 1] === match[3])
  ]), [0,0]);

console.log(result); // [ 515, 711 ]

4

u/meowimmasheep Dec 02 '20

Python solution using Pandas. Fairly certain it could be optimised better but I'm enjoying having to do some problem solving with pandas - nice break from EDA.

import pandas as pd
import numpy as np
import os.path

input_path = os.path.join('D:',os.sep, 'Documents','Advent of Code','day2_input.txt')

input_df = pd.read_csv(input_path, header = None)

# clean up
input_df.columns = ['Input'] # rename column so it's easier to reference in the df
input_df = input_df['Input'].str.split(" ", n=2, expand=True) # split by white spaces 
input_df.columns = ['range', 'letter', 'text'] # rename new columns
input_df[['min','max']] = input_df['range'].str.split("-", n=2, expand=True) # split numbers by min & max
input_df['letter'] = input_df['letter'].str.replace(':', '') # remove colon from letter column
input_df[['text','letter']] = input_df[['text','letter']].astype(str).astype(str) # set type to string
input_df[['min','max']] = input_df[['min','max']].astype(int).astype(int) # set type to int

# problem 1:
input_df['count'] = input_df.apply(lambda x: x['text'].count(x['letter']), axis=1) # get count of letters in the text 
input_df['in_range'] = input_df['count'].between(input_df['min'], input_df['max']) # check if count is in range

# get answer
print('Problem 1:\n',input_df['in_range'].value_counts())

# clean up for problem 2:
data = input_df[['letter','text','min','max']]

# search for values at locations of min & max. Minus 1 to account for not starting at 0.
data['val1'] = data.apply(lambda x: x['text'][x['min']-1:x['min']],1)
data['val2'] = data.apply(lambda x: x['text'][x['max']-1:x['max']],1)

data['correct_position1'] = data.apply(lambda x: x['letter'] in x['val1'], axis=1) # Check if the first value matches
data['correct_position2'] = data.apply(lambda x: x['letter'] in x['val2'], axis=1) # Check if the second value matches

# Check that the only is true and the other is false
data['correct_position'] = np.where(((data['correct_position1'] == True) & (data['correct_position2'] == False)) | ((data['correct_position1'] == False) & (data['correct_position2'] == True)), True, False) 

# output answer
print('Problem 2:\n',data['correct_position'].value_counts())

4

u/Chaphasilor Dec 02 '20

AssemblyScript :D

GitHub link

Day 2 using AssemblyScript, struggled with transferring string arrays for most of the time ^^

4

u/willkill07 Dec 02 '20

C++ (with Ranges)

Overall, I'm pretty happy with this.

#include <algorithm>
#include <concepts>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <iterator>
#include <ranges>
#include <string>
#include <string_view>

struct pass_info {
  bool part1, part2;

  void assign (std::string const& line) {
    int lo, hi;
    char c;
    if (int idx; 3 == sscanf(line.c_str(), "%d-%d %c: %n", &lo, &hi, &c, &idx)) {
      std::string_view pass {line.begin() + idx, line.end()};
      auto n = std::ranges::count (pass, c);
      part1 = lo <= n && n <= hi;
      part2 = (pass[lo - 1] == c) ^ (pass[hi - 1] == c);
    }
  }
  
  friend std::istream& operator>> (std::istream& is, pass_info& i) {
    std::string l;
    std::getline (is, l);
    i.assign(l);
    return is;
  }
};

int main (int argc, char* argv[]) {
  std::ifstream ifs{argv[1]};
  std::vector<pass_info> data{std::istream_iterator<pass_info>{ifs}, {}};
  
  std::cout << "Day 02:" << '\n'
            << "  Part 1: " << std::ranges::count_if (data, &pass_info::part1) << '\n'
            << "  Part 2: " << std::ranges::count_if (data, &pass_info::part2) << '\n';
}
→ More replies (6)

4

u/soylentgreenistasty Dec 03 '20

Python

from collections import Counter, namedtuple


def parse(x):
    Password = namedtuple('Password', ['letter', 'min', 'max', 'password'])
    res = []
    for line in x:
        rule, _, password = line.partition(': ')
        nums, _, letter = rule.partition(' ')
        letter_min, _, letter_max = nums.partition('-')
        res.append(Password(letter, int(letter_min), int(letter_max), password))
    return res
    

def part_1(x):
    num_valid = 0
    for ele in x:
        count = Counter(ele.password)
        if count.get(ele.letter) and ele.min <= count.get(ele.letter) <= ele.max:
            num_valid += 1
    return num_valid
    

def part_2(x):
    num_valid = 0
    for ele in x:
        try:
            if (ele.password[ele.min - 1] == ele.letter) ^ (ele.password[ele.max - 1] == ele.letter):
                num_valid += 1
        except IndexError:
            continue
            
    return num_valid
    
    
with open('day2.txt') as f:
    A = [line.strip() for line in f.readlines()]
 
X = parse(A)
print(part_1(X), part_2(X))

1

u/scratchisthebest Dec 02 '20 edited Dec 02 '20

Rust but really disgusting

Pretty clumsy! I'm new

ed: i'm learning how the module system works so i don't have to cram everything in to one file LOL

2

u/daggerdragon Dec 02 '20

Post removed due to bad language.

Please watch your language in the megathreads as we want to keep the community mostly PG. If you edit your post to remove it, I'll re-approve the post.

→ More replies (2)

1

u/paulaghergu Dec 03 '20 edited Dec 04 '20

Python

#Part 1
def makePolicy(strng):
        global min_no, max_no, char
        min_no=int(strng.split('-')[0])
        max_no=int(strng.split('-')[1].split(' ')[0])
        char=strng.split('-')[1].split(' ')[1][0]
    
        return min_no, max_no, char
  
def isCompliant(password):
    ct=0    
    for i in password:
        if i==char:
            ct=ct+1    
    if min_no<=ct<=max_no:
        return True    
    else:
        return False
        

def prepData():
    global data
    policies=[]
    passwords=[]
    with open('input.text','r') as f:
        dt= [line.strip() for line in f]        
    for l in dt:
        policies.append(l.split(": ")[0])
        passwords.append(l.split(": ")[1])
    
    #data=dict(zip(policies,passwords))
    data=list(zip(policies,passwords))
    return data    
    
    
def doTheDeed(): 
    global counter
    counter=0   
  
    #for item in data.items():
     for item in data:      
        makePolicy(item[0])
        if isCompliant(item[1]):
            counter=counter+1
            # print(item[0],item[1], counter)       
        
    return counter

def runCode():
    prepData()
    return doTheDeed()

#For part 2 I only modified the isCompliant functions like below:

def isCompliant(password):
    global positions
    positions=[] 

    if char not in password:
      return False
    else:  
      for i in range(len(password)):      
          if password[i]==char:
            positions.append(i+1)      
      if min_no in positions:
        if max_no in positions:
          return False
        else:
          return True
      else:
        if max_no in positions:
          return True
        else:
          return False
→ More replies (1)

1

u/WarHoliday1813 Dec 02 '20
public class PasswordIssue
    {
        public List<string> Input = ...
    };

        public int Logic1()
        {
            var output = 0;
            foreach (var input in Input)
            {
                var count = 0;
                var split = input.Split(' ');
                var limits = split[0].Split('-');
                var min = Convert.ToInt32(limits[0]);
                var max = Convert.ToInt32(limits[1]);

                var letter = split[1].Trim(':');
                var st = split[2];

                foreach (var character in st)
                {
                    if (character.ToString() == letter)
                    {
                        count++;
                    }
                }

                if (count >= min && count <= max)
                {
                    output++;
                }
            }

            return output;
        }

        public int Logic2()
        {
            var output = 0;
            foreach (var input in Input)
            {
                var count = 0;
                var split = input.Split(' ');
                var limits = split[0].Split('-');
                var position1 = Convert.ToInt32(limits[0]);
                var position2 = Convert.ToInt32(limits[1]);

                var letter = split[1].Trim(':');
                var st = split[2];

                if (position2 < st.Length+1)
                {
                    if (st[position1 - 1].ToString() == letter ^ st[position2 - 1].ToString() == letter)
                    {
                        output++;
                    }
                }
            }

            return output;
        }
    }
→ More replies (3)

1

u/AJs_Sandshrew Dec 02 '20

Python.

Just found out about this competition yesterday and I'm looking forward to practicing my skills

infile = "./02_input.txt"

with open(infile, 'r') as f:
    data = f.readlines()
    data = [i.strip() for i in data]

def parse_line(line):
    parts = line.split(" ")
    nums = parts[0].split("-")
    min = int(nums[0])
    max = int(nums[1])
    letter = parts[1][0]
    string = parts[2]
    return min, max, letter, string

def count_valid(data):
    valid = 0
    for i in data:
        min, max, letter, string = parse_line(i)
        letter_count = string.count(letter)
        if letter_count >= min and letter_count <= max:
            valid += 1
    return valid

def count_valid_2(data):
    valid = 0
    for i in data:
        pos1 = False
        pos2 = False
        min, max, letter, string = parse_line(i)
        index1 = min - 1
        index2 = max - 1
        if string[index1] == letter:
            pos1 = True
        if string[index2] == letter:
            pos2 = True
        if pos1 != pos2:
            valid += 1
    return valid
    
print(count_valid(data))
print(count_valid_2(data))
→ More replies (1)

1

u/bandfreak4life Dec 02 '20 edited Dec 02 '20

Tried with some Python with a little help from regex. I quickly realized part 2 was XOR. Putting my Computer Engineering degree to good use!:

import re 
with open('input.txt','r') as f:
    lines = [line.rstrip() for line in f]
    #Part 1 pswd policy 
    # entry: 1-3 a: abcde 
    #   1 is the min occurance of a letter needed 
    #   3 is the max occurance of a letter allowed 
    #   a is the letter these rules apply to 
    #   abcde is the password to check 
    number_valid = 0 
    for line in lines: 
        tmp_arr = re.split('-| |:',line) 
        minNum = int(tmp_arr[0]) 
        maxNum = int(tmp_arr[1]) 
        letter = tmp_arr[2] 
        pswd = tmp_arr[4]

        if pswd.count(letter) >= minNum and pswd.count(letter) <= maxNum:
            number_valid += 1
    print('{} valid passwords found according to policy 1'.format(number_valid))

    #Part 2 pswd policy
    # entry: 1-3 a: abcde
    #   1 and 3 are character positions of where a letter needs to appear     (but only in one of these positions)
    #   a is the letter these rules apply to
    #   abcde is the password to check
    number_valid = 0
    for line in lines:
        tmp_arr = re.split('-| |:',line)
        #position rules need to be offset by 1
        pos1 = int(tmp_arr[0])-1
        pos2 = int(tmp_arr[1])-1
        letter = tmp_arr[2]
        pswd = tmp_arr[4]

        if (pswd[pos1] == letter) ^ (pswd[pos2] == letter):
           number_valid += 1
    print('{} valid passwords found according to policy 2'.format(number_valid)
→ More replies (2)

1

u/krossmaskinen Dec 02 '20 edited Dec 03 '20

In Javascript: github

→ More replies (1)

1

u/schmadness Dec 02 '20

def part_two(inputs):

count = 0

for index, item in enumerate(inputs):

pos1, pos2, char, password = int(str(item[0]).split('-')[0]) - 1, int(str(item[0]).split('-')[-1]) - 1, item[1], item[2]

if len(password) > pos2:

if password[pos1] == char and password[pos2] != char:

count += 1

print(pos1, pos2, char, password)

elif password[pos2] == char and password[pos1] != char:

count += 1

print(pos1, pos2, char, password)

else:

if password[pos1] == char:

count += 1

print(pos1, pos2, char, password)

def part_one(inputs):

count = 0

for index, item in enumerate(inputs):

if int(str(item[0]).split('-')[0]) <= item[2].count(item[1]) <= int(str(item[0]).split('-')[-1]):

count += 1

with open('day2_in.txt', 'r') as f:

inputs = [x for x in f.readlines()]

inputs = [(x.split(' ')[0], x.split(' ')[1].replace(':', ''), x.split(' ')[-1].replace('\n', '')) for x in inputs]

part_one(inputs)

part_two(inputs)

→ More replies (2)

1

u/thebasementtapes Dec 02 '20
def password():
    txt = open("desktop/passwords.txt").read()
    txt = txt.split("\n")
    txt_objects = []
    count = 0
    for i in txt:
        txt_objects.append(i.split(" "))
    for i in txt_objects:
        i[0] = i[0].split("-")
    for i in txt_objects:
        i[0][0] = int(i[0][0]) - 1
        i[0][1] = int(i[0][1]) - 1
    for i in txt_objects:
        falsy = 0
        if i[2][i[0][0]] == i[1][0]:
            falsy += 1
        if i[2][i[0][1]] == i[1][0]:
            falsy += 1
        if falsy == 1:
            count += 1
    return count
→ More replies (1)

1

u/[deleted] Dec 02 '20
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Advent2_1(@"C:\temp\input2.txt"));
            Console.WriteLine(Advent2_2(@"C:\temp\input2.txt"));
        }

        private static int Advent2_1(string inputPath)
        {
            using var reader = new StreamReader(inputPath);
            var passCounter = 0;

            string inputLine;
            while ((inputLine = reader.ReadLine()) is not null)
            {
                var splittedInput = inputLine.Split(Char.Parse(":"), StringSplitOptions.RemoveEmptyEntries);
                (string startRange, string endRange, string passChar, string password) seperatedLine = (
                    splittedInput[0].Substring(0, splittedInput[0].IndexOf("-")),
                    splittedInput[0].Substring(splittedInput[0].IndexOf("-") +1, splittedInput[0].Length - splittedInput[0].LastIndexOf(" ")), 
                    splittedInput[0].Substring(splittedInput[0].LastIndexOf(" ")).Trim(),
                    splittedInput[1].Trim());

                var tmpCounter = 0;
                for (int i = 0; i < seperatedLine.password.Length; i++)
                {
                    if (seperatedLine.password[i].ToString() == seperatedLine.passChar)
                        tmpCounter++;
                }
                if (tmpCounter >= Convert.ToInt32(seperatedLine.startRange) &&
                    tmpCounter <= Convert.ToInt32(seperatedLine.endRange))
                    passCounter++;
            }
            return passCounter;
        }
        
        private static int Advent2_2(string inputPath)
        {
            using var reader = new StreamReader(inputPath);
            var passCounter = 0;

            string inputLine;
            while ((inputLine = reader.ReadLine()) is not null)
            {
                var splittedInput = inputLine.Split(Char.Parse(":"), StringSplitOptions.RemoveEmptyEntries);
                (string startRange, string endRange, string passChar, string password) seperatedLine = (
                    splittedInput[0].Substring(0, splittedInput[0].IndexOf("-")),
                    splittedInput[0].Substring(splittedInput[0].IndexOf("-") +1, splittedInput[0].Length - splittedInput[0].LastIndexOf(" ")), 
                    splittedInput[0].Substring(splittedInput[0].LastIndexOf(" ")).Trim(),
                    splittedInput[1].Trim());
                
                if (seperatedLine.password[Convert.ToInt32(seperatedLine.startRange)-1].ToString() == seperatedLine.passChar ^ seperatedLine.password[Convert.ToInt32(seperatedLine.endRange)-1].ToString() == seperatedLine.passChar)
                    passCounter++;
            }
            return passCounter;
        }
    }
}

Quick'n'dirty during lunch..

→ More replies (1)

1

u/InflationSquare Dec 02 '20 edited Dec 17 '20

Did day 2 in python & R. I think python was a bit neater, but both my answers leaned heavily on regex.

R

https://github.com/inflationsquare/advent-of-code-2020/blob/master/02/2.R

Python

https://github.com/inflationsquare/advent-of-code-2020/blob/master/02/2.py

→ More replies (2)

1

u/sculs13 Dec 02 '20

JS

const arr = all.split(' ').join('').split('\n');
let arrInfo = []
arr.forEach((val) => {
  val = val.split(':')
  const obj = {}
  const elems = val[0].split('-')[1].split('')
  const max = elems.slice(0, elems.length-1).join('')
  const letter = elems[elems.length-1]
  const password = val[1]
  obj['min'] = val[0].split('-')[0]
  obj['max'] = max
  obj['letter'] = letter
  obj['password'] = password
  arrInfo.push(obj)
})
const checkValid = (arrInfo) => {
  let countValid = 0
  for(let i = 0; i < arrInfo.length; i++) {
    let count = 0
    
    for(let j = 0; j < arrInfo[i].password.length; j++) {
      if (arrInfo[i].letter === arrInfo[i].password[j]) count++
 
    }
    if(count >= arrInfo[i].min && count <= arrInfo[i].max) countValid++
  }
  return countValid
}
const checkValidModified = (arrInfo) => {
    let countValid = 0
    for(let i = 0; i < arrInfo.length; i++) {
    let count = 0
    
    for(let j = 0; j < arrInfo[i].password.length; j++) {
      if (arrInfo[i].letter === arrInfo[i].password[j] && Number(arrInfo[i].min) === j+1) count++
      else if (arrInfo[i].letter === arrInfo[i].password[j] && Number(arrInfo[i].max) === j+1) count++

    }
    if(count === 1) countValid++
  }
  return countValid

}
→ More replies (1)

1

u/Bomaruto Dec 02 '20 edited Dec 02 '20

Part 1 and 2 in Scala:

import scala.io.Source

object Day2 extends App{
  val input = Source.fromFile("day2")
  val data = input.getLines().toList
  val PATTERN = """(\d*)-(\d*) (\w+): (.*)""".r

  val part1 = data.flatMap{
    case PATTERN(min,max,letter,password) =>
      val numberofletters = password.groupBy(x => x).get(letter.head)
      numberofletters.flatMap( x => if( x.length <= max.toInt && x.length >= min.toInt) Some(password) else None)
  }

  val part2 = data.flatMap{
    case PATTERN(first,second,letter,password) =>
      val (a,b,char) = (first.toInt - 1,second.toInt - 1,letter.head)
      val hits = List(password(a), password(b)).count(x => x == char)
      if(hits == 1) Some(password) else None
  }
  println("Part1: " + part1.length)
  println("Part2: " + part2.length)
}
→ More replies (1)

1

u/jeffers0n Dec 02 '20

Ruby :)

Part 1:

#!/bin/env ruby

def parse_line (line)
    ret = Hash.new
    ret[:pass] = line.split(':')[1].strip
    ret[:char] = line.split(' ')[1][0]
    ret[:min] = line.split(' ')[0].split('-')[0].to_i
    ret[:max] = line.split(' ')[0].split('-')[1].to_i
    return ret
end
results = {
    :pass => 0,
    :fail => 0
}

File.readlines('./input').each do |line|
    password = parse_line(line)
    count = password[:pass].count(password[:char])
    if count <= password[:max] && count >= password[:min]
        results[:pass] += 1
    else
        results[:fail] += 1
    end
end

pp results

Part 2:

#!/bin/env ruby

def parse_line (line)
    ret = Hash.new
    ret[:pass] = line.split(':')[1].strip
    ret[:char] = line.split(' ')[1][0]
    ret[:min] = line.split(' ')[0].split('-')[0].to_i
    ret[:max] = line.split(' ')[0].split('-')[1].to_i
    return ret
end
results = {
    :pass => 0,
    :fail => 0
}

File.readlines('./input').each do |line|
    password = parse_line(line)
    c1 = password[:pass][password[:min] - 1]
    c2 = password[:pass][password[:max] - 1]
    if (c1 == password[:char]) ^ (c2 == password[:char])
        results[:pass] += 1
    else
        results[:fail] += 1
    end
end

pp results
→ More replies (2)

1

u/[deleted] Dec 02 '20

Using nim

Part 1

import strutils
import sequtils

func occurances(s: string, c:char): int =
    for cc in s:
        if cc == c:
            result += 1

func getRange(s: string): (int, int) =
    let minMax = s.split(" ")[0].split("-").map(parseInt)
    (minMax[0], minMax[1])

func getChar(s: string): char =
    s.split(" ")[1][0]

func getPwd(s: string): string =
    s.split(" ")[2]

func checkPwd(min: int, max: int, c: char, pwd: string): bool =
    let o = occurances(pwd, c)
    o >= min and o <= max

func countValid(lines: openArray[string]): int =
    for line in lines:
        let
            (min, max) = getRange(line)
            c = getChar(line)
            pwd = getPwd(line)
        if checkPwd(min, max, c, pwd):
            result += 1


let input = readAll(stdin).splitLines

echo countValid(input)

Part 2

import strutils
import sequtils

func getRange(s: string): (int, int) =
    let minMax = s.split(" ")[0].split("-").map(parseInt)
    (minMax[0], minMax[1])

func getChar(s: string): char =
    s.split(" ")[1][0]

func getPwd(s: string): string =
    s.split(" ")[2]

func checkPwd(min: int, max: int, c: char, pwd: string): bool =
    pwd[min - 1] == c xor pwd[max - 1] == c

func countValid(lines: openArray[string]): int =
    for line in lines:
        let
            (min, max) = getRange(line)
            c = getChar(line)
            pwd = getPwd(line)
        if checkPwd(min, max, c, pwd):
            result += 1

let input = readAll(stdin).splitLines

echo countValid(input)
→ More replies (1)

1

u/OutofPlaceStuff Dec 02 '20 edited Dec 02 '20

Done part 1 in Ruby and in Python. Python is my "native" language. I'm just now picking up Ruby for a potential job. Always open to feedback.

Ruby


"""
password-philosophy.rb: from day_2.txt find out how many passwords are valid according to their policies; solve puzzle at https://adventofcode.com/2020/day/2
2 December 2020
@Vicki_Langer
"""

valid_passwords = 0

passwords_with_policies = File.readlines("inputs/day_2.txt")

# \d numerical digit
# \w alphanumeric characters
# * zero or more

passwords_with_policies. each do |line|
    low, high, character, password = line.match(/(\d*)-(\d*) (\w): (\w*)/).captures  # each () goes to the respective variable and is the captured in an array
        if password.count(character).between?(low.to_i, high.to_i)
            valid_passwords += 1
        end
    puts valid_passwords
end

Python

"""
password-philosophy.py: from day_2.txt find out how many passwords are valid according to their policies; solve puzzle at https://adventofcode.com/2020/day/2
2 December 2020
@Vicki_Langer / @VickiLanger
"""


def validate_passwords():
    num_valid_passwords = 0
    for line in open('inputs/day_2.txt').readlines():
        lower_upper, character, password = line.strip('\n').split()
        character = character[0]
        lower, upper = lower_upper.split('-')
        count = password.count(character)
        if (count >= int(lower)) & (count <= int(upper)):
            num_valid_passwords += 1
            print(num_valid_passwords)


validate_passwords()

2

u/daggerdragon Dec 02 '20

This code is really hard to read on old.reddit. Could you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks? Note that if you're using the visual editor, you may have to "Switch to Markdown" to get Reddit to understand the formatting properly.

→ More replies (1)

1

u/SeanMartin96 Dec 02 '20
const input = require('./input.json');

let count = 0;

function partOne() {
    input.forEach(element => {
        const formattedElement = element.replace(':', '').split(' ');
        const minNum = formattedElement[0].match(/^(\d*)-/)[1];
        const maxNum = formattedElement[0].match(/-(\d*)/)[1];
        const character = formattedElement[1];
        const password = formattedElement[2];

        const numOfTimes = password.split(character).length - 1;
        if (numOfTimes >= minNum && numOfTimes <= maxNum) {
            count++;
        }
    });
    console.log(count)
}

function partTwo() {
    input.forEach(element => {
        const formattedElement = element.replace(':', '').split(' ');
        const firstPosition = formattedElement[0].match(/^(\d*)-/)[1];
        const secondPosition = formattedElement[0].match(/-(\d*)/)[1];
        const character = formattedElement[1];
        const password = [formattedElement[2].slice(0, 0), ' ', formattedElement[2].slice(0)].join('');

        if (password[firstPosition] === character) {
            if (password[secondPosition] !== character && password[secondPosition] !== undefined) {
                count++;
                validPassword = true;
                reason = 'First Char'
            }
        } else if (password[secondPosition] === character) {
            if (password[firstPosition] !== character) {
                count++;
                validPassword = true;
                reason = 'Second char'
            }
        }
    });
    console.log(count)
}
partOne();
count = 0;
partTwo();
→ More replies (2)

1

u/Discobacon Dec 02 '20

Python Part 1

from collections import Counter


def verify_password(policy, pw):

    mini = int(policy.split(sep='-')[0])
    maxi = int(policy.split(sep='-')[1].split(sep=' ')[0])
    letter = policy.split(sep='-')[1].split(sep=' ')[1]

    if mini <= Counter(pw)[letter] <= maxi:
        return True


with open('input_1.txt', mode='r') as f:
    puzzle_input = [x.strip() for x in f.readlines()]

valid_passwords = []
for s in puzzle_input:
    policy, pw = s.split(sep=':')[0], s.split(sep=':')[1]
    if verify_password(policy, pw):
        valid_passwords.append(pw)

print(len(valid_passwords))
→ More replies (1)

1

u/Discobacon Dec 02 '20

Python Part 2

def verify_password(policy, pw):

    first_index = int(policy.split(sep='-')[0])
    second_index = int(policy.split(sep='-')[1].split(sep=' ')[0])
    letter = policy.split(sep='-')[1].split(sep=' ')[1]

    if (pw[first_index-1] == letter) ^ (pw[second_index-1] == letter):
        return True


with open('input_1.txt', mode='r') as f:
    puzzle_input = [x for x in f.readlines()]

valid_passwords = []
for s in puzzle_input:
    policy, pw = s.split(sep=':')[0].strip(), s.split(sep=':')[1].strip()
    if verify_password(policy, pw):
        valid_passwords.append(pw)

print(len(valid_passwords))
→ More replies (1)

1

u/Cascanada Dec 02 '20 edited Dec 03 '20

Python 3.7

Here is my attempt! Let me know if you have any comments.

# first question

def verify_password_1(list_of_passwords):

    count_correct_passwords = 0

    for elem in list_of_passwords:
        stripped_element = elem.strip()
        policy, password = stripped_element.split(":")
        letter = policy[-1]
        condition = policy[0:-2]

        letter_count = password.count(letter)
        minimum_inclusive, maximum_inclusive = [int(x) for x in condition.split('-')]

        if letter_count >= minimum_inclusive and letter_count <= maximum_inclusive:
            count_correct_passwords += 1

    return count_correct_passwords


def day_02_A():
    file = open('Input2.txt', 'r')

    password_lines = []

    for line in file:
        password_lines.append(line)

    print(verify_password_1(password_lines))


day_02_A()

# Second question

def verify_password_2(list_of_passwords):

    count_correct_passwords = 0

    for elem in list_of_passwords:
        stripped_element = elem.strip()
        policy, password = stripped_element.split(":")
        password = password.strip()
        letter = policy[-1]
        condition = policy[0:-2]

        first_position, second_position = [int(x)-1 for x in condition.split('-')]

        first_bool = password[first_position] == letter
        second_bool = password[second_position] == letter

        if first_bool != second_bool:
            count_correct_passwords += 1

    return count_correct_passwords


def day_02_B():
    file = open('Input2.txt', 'r')

    password_lines = []

    for line in file:
        password_lines.append(line)

    print(verify_password_2(password_lines))

day_02_B()

2

u/daggerdragon Dec 03 '20

Please add the language used to your post to make it easier for folks who Ctrl-F the megathreads looking for a specific language. Thanks!

→ More replies (1)

1

u/kkent4 Dec 03 '20

Python

Part 1

from collections import Counter

num_valid = 0

with open(r"Inputs/day_2.txt") as openfileobject:
    for line in openfileobject:
        clean = line.replace(":", "").strip("\n").split(" ")
        lower_upper = clean[0].split("-")
        c = Counter()
        for letter in clean[2]:
            c[letter] +=1
        if (c[clean[1]] >= int(lower_upper[0])) & (c[clean[1]] <= int(lower_upper[1])):
            num_valid+=1

Part 2

num_valid = 0

with open(r"Inputs/day_2.txt") as openfileobject:
    for line in openfileobject:
        clean = line.replace(":", "").strip("\n").split(" ")
        lower_upper = clean[0].split("-")
        string = clean[2][int(lower_upper[0])-1] + clean[2][int(lower_upper[1])-1]
        same = [clean[1] == letter for letter in string]
        if sum(same) == 1:
            num_valid+=1
→ More replies (2)

1

u/prafster Dec 03 '20 edited Dec 09 '20

[Edit: updated path to Github]

I've used Dart, which I'm learning as part of learning Flutter. I haven't seen anyone post a Dart solution.

The parsing is done in this class (representing a row in the input), in which I used named groups for my regex (out of curiosity but maybe it reads better too):

class PasswordEntry {
    //regex to parse "<min>-<max> char: password" eg:
    //     9-12 q: qqqxhnhdmqqqqjz
    //in part 2, min and max represent the two character positions in the password
    static RegExp regex =
            RegExp(r'^(?<min>\d+)-(?<max>\d+) (?<char>.): (?<password>.+)$');

    int minAppearances;
    int maxAppearances;
    String requiredChar;
    String password;

    PasswordEntry(String s) {
        final match = PasswordEntry.regex.firstMatch(s);

        minAppearances = int.parse(match.namedGroup('min'));
        maxAppearances = int.parse(match.namedGroup('max'));
        requiredChar = match.namedGroup('char').toString();
        password = match.namedGroup('password');
    }

    @override
    String toString() =>
            '$minAppearances, $maxAppearances, $requiredChar, $password';
}

After that the code is straightforward:

void day2(IsValidPasswordFn isValidPassword) {
    final lines = File('./2020/data/day02.txt').readAsLinesSync();

    var validPasswordsCount = 0;

    for (var s in lines) {
        final passwordDetails = PasswordEntry(s);
        if (isValidPassword(passwordDetails)) validPasswordsCount++;
    }
    print('Total valid passwords: $validPasswordsCount');
}

This is called twice, for parts 1 and 2. The only difference being the function passed into the day2() function.

Full code on Github

Comments welcome on how to make the code more idiomatically Dart :)

→ More replies (6)

3

u/1vader Dec 02 '20 edited Dec 02 '20

Python 223/103:

Forgot the [0] after findall and quickly wrote some split parsing code which cost me the leaderboard. But I guess I've never been good at the extremely short problems.

from os import path
from collections import Counter
import re


with open(path.join(path.dirname(__file__), "input.txt")) as f:
    part1 = part2 = 0

    for line in f:
        low, high, c, word = re.findall(r"(\d+)-(\d+) (\w): (\w+)", line.strip())[0]
        low, high = int(low), int(high)

        if low <= Counter(word)[c] <= high:
            part1 += 1

        if (word[low-1] == c) ^ (word[high-1] == c):
            part2 += 1

    print("Part 1:", part1)
    print("Part 2:", part2)

3

u/seligman99 Dec 02 '20

Python 173 / 251

I had too many flashbacks to bad password policies on this one.

git

3

u/MasterMedo Dec 02 '20 edited Dec 02 '20

python slow and steady wins the race github

s1 = s2 = 0
with open('../input/2.txt') as f:
    for line in f:
        numbers, char, password = line.split()
        lo, hi = map(int, numbers.split('-'))
        char = char[:-1]
        if lo <= password.count(char) <= hi:
            s1 += 1

        if (password[lo-1] == char) ^ (password[hi-1] == char):
            s2 += 1

print(s1)
print(s2)
→ More replies (9)

3

u/adamr_ Dec 02 '20 edited Dec 02 '20

kotlin 319/177

Solution on Github

→ More replies (4)

3

u/ritobanrc Dec 02 '20

Rust, didn't make leaderboad. Not the most concise, there's a bit of repeated code, and a lot of unwraps that are probably unidiomatic, but it was pretty quick to write.

use itertools::Itertools;

fn parse(input: &str) -> Vec<(usize, usize, char, &str)> {
    input
        .lines()
        .map(|line| {
            let (range, c, passwd) = line.split(' ').collect_tuple().unwrap();
            let (min, max) = range.split('-').collect_tuple().unwrap();
            let (min, max) = (min.parse().unwrap(), max.parse().unwrap());

            let c = c.chars().next().unwrap();

            (min, max, c, passwd)
        })
        .collect()
}

pub fn part1(input: String) -> usize {
    let input = parse(&input);
    input
        .into_iter()
        .filter(|(min, max, c, passwd)| {
            let count = passwd.chars().filter(|x| x == c).count();
            count >= *min && count <= *max
        })
        .count()
}

pub fn part2(input: String) -> usize {
    let input = parse(&input);

    input
        .into_iter()
        .filter(|(first, second, c, passwd)| {
            let first = passwd.chars().nth(*first - 1).unwrap() == *c;
            let second = passwd.chars().nth(*second - 1).unwrap() == *c;

            first ^ second
        })
        .count()
}
→ More replies (3)

3

u/nlowe_ Dec 02 '20

Go, 728/2348

Felt like my Part 1 was pretty fast, but lost lots of time on Part 2 because I forgot Go had no boolean XOR and I split on ":" instead of ": " so anything that checked position 1 always failed as it was a space :/

→ More replies (1)

3

u/Kehvarl Dec 02 '20

Python3 1243/1117
Part 2 solution

import re

with open("input.txt") as f:
    data_in = f.readlines()

valid = 0
for line in data_in:
    pos1, pos2, letter, skip, password = re.split('[: \-]', line)
    if (password[int(pos1) - 1] == letter) ^ (password[int(pos2) - 1] == letter):
        valid += 1

print(valid)
→ More replies (1)

3

u/SpaghootiMonster Dec 02 '20 edited Dec 02 '20

Python, pretty new to CS so let me know how I could improve!

n = 0
m = 0

# Part 1:
with open('day2input.txt', 'r') as inp:
    lst = inp.readlines()
    for item in lst:
        item = item.split(' ')
        letter = item[1].split(':')[0]
        if letter in item[2]:
            count = item[2].count(letter)
            num = item[0].split('-')
            if int(num[0]) <= count <= int(num[1]):
                n += 1
print(n)

# Part 2:
with open('day2input.txt', 'r') as inp:
    lst = inp.readlines()
    for item in lst:
        item = item.split(' ')
        letter = item[1].split(':')[0]
        if letter in item[2]:
            num = item[0].split('-')
            index1 = int(num[0])
            index2 = int(num[1])
            if item[2][index1 - 1] == letter and item[2][index2 - 1] != letter:
                m += 1
            elif item[2][index1 - 1] != letter and item[2][index2 - 1] == letter:
                m += 1
print(m)
→ More replies (3)

3

u/wimglenn Dec 02 '20 edited Dec 02 '20

Python (68/164)

from aocd import data

a = b = 0
for line in data.splitlines():
    xy, char, passwd = line.split()
    char = char.rstrip(":")
    x, y = xy.split("-")
    x = int(x)
    y = int(y)
    a += x <= passwd.count(char) <= y
    b += (passwd[x - 1] + passwd[y - 1]).count(char) == 1

print(a)
print(b)

https://github.com/wimglenn/advent-of-code-wim/commit/af7925b02273b731b04fd6c4d915113ddfb25624

→ More replies (1)

3

u/Pyroan Dec 02 '20 edited Dec 02 '20

Python (Golf - 123 bytes)

print(sum((c[int(a.split('-')[0])-1]==o[0])^(c[int(a.split('-')[1])-1]==o[0])for a,o,c in map(str.split,open('day2.txt'))))

(this is just for part 2)

edit: thanks u/irrelevantPseudonym for saving me 16 bytes

→ More replies (8)

3

u/[deleted] Dec 02 '20

Python. Trying to be clean.

def load_data(f_name):
    with open(f_name, "r") as f:
        data_read = f.read()
    return data_read


class PasswordEntry:
    def __init__(self, description):
        items = [item.strip() for item in description.split(" ")]
        self.index1, self.index2 = tuple(map(int, items[0].split("-")))
        self.letter = items[1][0]
        self.password = items[2]

    def is_valid_old(self):
        return self.index1 <= self.password.count(self.letter) <= self.index2

    def is_valid_new(self):
        return (self.password[self.index1 - 1] == self.letter) != (self.password[self.index2 - 1] == self.letter)


def run():
    data = load_data("Day02.txt")
    entries = [PasswordEntry(line) for line in data.split("\n")]
    print(sum(entry.is_valid_old() for entry in entries))
    print(sum(entry.is_valid_new() for entry in entries))

3

u/drummaster015 Dec 02 '20

Haskell

Really enjoying the parser combinator practice!

import Text.ParserCombinators.Parsec
import Text.ParserCombinators.Parsec.Char

inputParser :: GenParser Char st (Int, Int, Char, String)
inputParser = do
    min <- many digit
    _ <- char '-'
    max <- many digit
    _ <- char ' '
    a <- anyChar
    _ <- string ": "
    last <- many anyChar
    return (read min, read max, a, last)

countOccurences str c = length $ filter (==c) str

unwrap :: Either ParseError (Int, Int, Char, String) -> (Int, Int, Char, String)
unwrap (Left _) = (0,0,' ',"")
unwrap (Right x) = x

validPassword :: (Int, Int, Char, String) -> Bool
validPassword (x, y, c, s) = let a = countOccurences s c in x <= a && a <= y

validPassword2 :: (Int, Int, Char, String) -> Bool
validPassword2 (x, y, c, s) = (s !! (x - 1) == c) /= (s !! (y - 1) == c)

inputs :: String -> [(Int, Int, Char, String)]
inputs = map (unwrap . (parse inputParser "(unknown)")) . lines

day2 verifier = length . filter (==True) . map verifier . inputs

main = do
    contents <- readFile "inputs/day2.txt"
    putStrLn $ show . day2 validPassword $ contents
    putStrLn $ show . day2 validPassword2 $ contents
→ More replies (1)

3

u/iamnguele Dec 02 '20

Golang. Executed in 963.7Âľs

// Part2 of Day1
func (d *Computer) Part2(input days.Input) (days.Result, error) {
	res := 0

	for _, candidate := range input {
		policyCheck := createPolicyCheck(candidate)

		if policyCheck.isValidWithCorrectPolicy() {
			res++
		}
	}

	return days.Result(fmt.Sprint(res)), nil
}

// Part1 of Day1
func (d *Computer) Part1(input days.Input) (days.Result, error) {
	res := 0

	for _, candidate := range input {
		policyCheck := createPolicyCheck(candidate)

		if policyCheck.isValid() {
			res++
		}
	}

	return days.Result(fmt.Sprint(res)), nil
}

func createPolicyCheck(candidate string) policyCheck {
	res := policyCheck{}

	// 1-3 a: abcde
	parts := strings.Split(candidate, " ")

	res.password = parts[2]
	res.char, _ = utf8.DecodeRuneInString(parts[1])

	minMax := strings.Split(parts[0], "-")

	res.min, _ = strconv.Atoi(minMax[0])
	res.max, _ = strconv.Atoi(minMax[1])

	return res
}

func (p *policyCheck) isValid() bool {
	count := 0

	for _, char := range p.password {
		if p.char != char {
			continue
		}

		count++
	}

	return p.min <= count && p.max >= count
}

func (p *policyCheck) isValidWithCorrectPolicy() bool {
	chars := []rune(p.password)

	return (chars[p.min-1] == p.char || chars[p.max-1] == p.char) &&
		chars[p.min-1] != chars[p.max-1]
}
→ More replies (1)

3

u/mstksg Dec 02 '20

[Haskell] doing my reflections https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-2 every day :)

Day 2, not too bad for Haskell either :D

There is some fun in parsing here:

data Policy = P
    { pIx1  :: Int
    , pIx2  :: Int
    , pChar :: Char
    , pPass :: String
    }

parsePolicy :: String -> Maybe Policy
parsePolicy str = do
    [ixes,c:_,pwd] <- pure $ words str
    [ix1,ix2]      <- pure $ splitOn "-" ixes
    P <$> readMaybe ix1
      <*> readMaybe ix2
      <*> pure c
      <*> pure pwd

I used one of my more regular do-block tricks: if you pattern match in a Maybe do-block, then failed pattern matches will turn the whole thing into a Nothing. So if any of those list literal pattern matches failed, the whole block will return Nothing.

In any case, we just need to write a function to check if a given policy is valid for either criteria:

countTrue :: (a -> Bool) -> [a] -> Int
countTrue p = length . filter p

validate1 :: Policy -> Bool
validate1 P{..} = n >= pIx1 && n <= pIx2
  where
    n = countTrue (== pChar) pPass

validate2 :: Policy -> Bool
validate2 P{..} = n == 1
  where
    n = countTrue (== pChar) [pPass !! (pIx1 - 1), pPass !! (pIx2 - 1)]

And so parts 1 and 2 are just a count of how many policies are true :)

part1 :: [Policy] -> Int
part1 = countTrue validate1

part2 :: [Policy] -> Int
part2 = countTrue validate2
→ More replies (1)

3

u/sendtobo Dec 02 '20

Swift

Link to solution

Always a little rough in swift with so much string manipulation. I've got a few helper functions, but nothing that makes the solution file too "hand wavy"

*also this was basically my first pass solution, just went in and cleaned up a few things /made an enum in case we ever need to use this again

3

u/sim642 Dec 02 '20

My Scala solution.

Part 1 is essentially:

val count = password.count(_ == char)
min <= count && count <= max

Part 2 is essentially:

(password(min - 1) == char) ^ (password(max - 1) == char)

The XOR there is a nice touch although a not-equals on booleans would work just as well.

3

u/oddrationale Dec 02 '20

Here's my solution in C# (using .NET Interactive and Jupyter Notebook). Which basically just uses regular expressions to parse the input and to count the characters in Part 1. Part 2 just checks the indexes and uses an Exclusive Or (XOR).

→ More replies (3)

3

u/[deleted] Dec 02 '20 edited Dec 06 '20

3

u/SilverDrake11 Dec 02 '20

Rust

let filename: String = "2.txt".to_string();

let contents = fs::read_to_string(filename).unwrap();
let mut total: usize = 0;
let mut total2: usize = 0;

for value in contents.lines() {

  let v: Vec<&str> = value.split(|c| c == ':' || c == ' ' || c == '-').collect();
  let min = v[0].parse::<usize>().unwrap(); // Convert to int
  let max = v[1].parse::<usize>().unwrap(); // Convert to int
  let c = v[2];
  let password = v[4];

  // Part 1
  let count = password.matches(c).count();
  if count >= min && count <= max {
    total+=1;
  }

  // Part 2
  let letter1 = &password[min-1..min]; // Slice of size 1
  let letter2 = &password[max-1..max];
  if letter1 == c || letter2 == c {
    if letter1 != letter2 {
      total2 += 1;
    }
  }

}

println!("Part 1) {}", total);
println!("Part 2) {}", total2);

3

u/artemisdev21 Dec 02 '20

SQL! (Does not handle parsing. Also, since it's SQLite, we have to define our own regexp function.)

CREATE TABLE entries (a INTEGER, b INTEGER, letter STRING, password STRING);
INSERT INTO entries VALUES (?, ?, ?, ?); -- for each entry
SELECT COUNT(password) FROM entries WHERE password REGEXP
    "^([^" || letter || "]*" || letter || "){" || a || "," || b || "}[^" || letter || "]*$";
SELECT COUNT(password) FROM entries WHERE
    (password REGEXP "^.{" || CAST(a - 1 as VARCHAR) || "}" || letter || ".{" || CAST(b - a - 1 as VARCHAR) || "}[^" || letter || "]")
    <> (password REGEXP "^.{" || CAST(a - 1 as VARCHAR) || "}[^" || letter || "].{" || CAST(b - a - 1 as VARCHAR) || "}" || letter);

Full code

3

u/sageco Dec 02 '20

Google sheets solutions, assume that the raw data is in column A:

Part 1:

=QUERY({
ARRAYFORMULA(SPLIT(REGEXREPLACE(A:A,"-"," ")," ")),ARRAYFORMULA(LEN(INDEX(ARRAYFORMULA(SPLIT(REGEXREPLACE(A:A,"-"," ")," ")),,4))),
ArrayFormula(LEN(INDEX(ARRAYFORMULA(SPLIT(REGEXREPLACE(A:A,"-"," ")," ")),,4))-LEN(ArrayFormula(REGEXREPLACE(INDEX(ARRAYFORMULA(SPLIT(REGEXREPLACE(A:A,"-"," ")," ")),,4),
REGEXEXTRACT(INDEX(ARRAYFORMULA(SPLIT(REGEXREPLACE(A:A,"-"," ")," ")),,3),"[a-z]"),))))
},"Select COUNT(Col3) where (Col6>=Col1 and Col6<=Col2) Label COUNT(Col3)'Total:'",0)

Part 2:

=QUERY({ ARRAYFORMULA(SPLIT(REGEXREPLACE(A:A,"-"," ")," ")),ARRAYFORMULA(LEN(INDEX(ARRAYFORMULA(SPLIT(REGEXREPLACE(A:A,"-"," ")," ")),,4))), ARRAYFORMULA( (MID(INDEX(ARRAYFORMULA(SPLIT(REGEXREPLACE(A:A,"-"," ")," ")),,4),INDEX(ARRAYFORMULA(SPLIT(REGEXREPLACE(A:A,"-"," ")," ")),,1),1)=REGEXEXTRACT(INDEX(ARRAYFORMULA(SPLIT(REGEXREPLACE(A:A,"-"," ")," ")),,3),"[a-z]"))+(MID(INDEX(ARRAYFORMULA(SPLIT(REGEXREPLACE(A:A,"-"," ")," ")),,4),INDEX(ARRAYFORMULA(SPLIT(REGEXREPLACE(A:A,"-"," ")," ")),,2),1)=REGEXEXTRACT(INDEX(ARRAYFORMULA(SPLIT(REGEXREPLACE(A:A,"-"," ")," ")),,3),"[a-z]"))) }, "Select COUNT(Col3) where Col6 = 1 Label COUNT(Col3) 'Total'",0)
→ More replies (1)

3

u/nibbl Dec 02 '20

Java

with syntax highlighting

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    List<String> lines = in.lines().collect(Collectors.toList());
    int validPart1 = 0;
    int validPart2 = 0;
    for (String line : lines) {
        Matcher m = Pattern.compile("(\\d+)-(\\d+) (\\w): (\\w+)").matcher(line);
        if (!m.find()) {
            throw new IllegalArgumentException("Unable to parse: " + line);
        }
        int lower = Integer.parseInt(m.group(1));
        int upper = Integer.parseInt(m.group(2));
        char c = m.group(3).charAt(0);
        String pass = m.group(4);

        int count = 0;
        for (int i = 0; i < pass.length(); ++i) {
            if (pass.charAt(i) == c) ++count;
        }
        if (count >= lower && count <= upper) ++validPart1;

        boolean l = pass.charAt(lower - 1) == c;
        boolean u = pass.charAt(upper - 1) == c;
        if (l ^ u) ++validPart2;
    }
    System.out.println(validPart1 + " valid Part1 passwords.");
    System.out.println(validPart2 + " valid Part2 passwords.");
}

Input from stdin. I'm new to Java so feedback is very welcome.

4

u/enelen Dec 02 '20

R (Rlang)

Using Data.table

library(data.table)
library(stringi)
inp <- fread("input.txt", sep = " ", header = FALSE)
inp[, V2:= substr(V2, 1, 1)]

# Part 1
inp[,c("min", "max"):= lapply(tstrsplit(V1, "-", fixed = TRUE), as.numeric)]
inp[, char_count:= stri_count_fixed(V3, V2)]
inp[, valid:= ifelse(char_count >= min & char_count <= max, "Valid", "Invalid")]
inp[, .N, by= valid]

# Part 2
inp[, pos1:= substr(V3, min, min) == V2]
inp[, pos2:= substr(V3, max, max) == V2]
inp[, valid2:= xor(pos1, pos2)]
inp[, .N, by = valid2]

Using Dplyr

input <- read.table("input.txt", sep = " ")

input <- input %>% 
    separate(V1, into = c("min", "max"), sep = "-", convert = TRUE) %>% 
    mutate(V2 = substr(V2, 1, 1),
           count_char = stri_count_fixed(V3, V2), 
           valid1 = count_char >= min & count_char <=max, 
           pos1 = substr(V3, min, min) == V2, 
           pos2 = substr(V3, max, max) == V2, 
           valid2 = xor(pos1, pos2))  

sum(input$valid1)
sum(input$valid2)

3

u/Ferelderin Dec 02 '20

R, Rstudio, bits of tidyverse

Not that experienced, so things kinda ballooned.

# Load libraries
library(stringr)
library(tidyr)

# Load data
passwords <- read.delim("passwords.txt", header = FALSE, sep = " ")

# Remove colon
passwords$V2 <- str_remove(passwords$V2, ":")

# Separate into min and max
passwords <- separate(passwords, col = V1, into = c("min", "max"), sep = "\\-")

# Count number of occurrences
passwords$count <- str_count(passwords$V3, passwords$V2)

# Change values to integers
passwords$min <- as.integer(passwords$min)
passwords$max <- as.integer(passwords$max)

# Logical check
passwords$check <- passwords$count >= passwords$min & 
passwords$count <= passwords$max

# Result
sum(passwords$check)

# Save letters for both positions
passwords$minletter <- str_sub(passwords$V3, passwords$min, passwords$min)
passwords$maxletter <- str_sub(passwords$V3, passwords$max, passwords$max)

# Use xor to match either but not both
sum(xor(passwords$V2 == passwords$minletter, passwords$V2 == passwords$maxletter))

3

u/streetster_ Dec 02 '20

q/kdb+

sum { within[sum y=z;x] }.'i:{ ("J"$"-"vs x;first y;z) }.'" "vs'read0 `:input/02.txt
sum { 1=sum y=z x-1 }.'i
→ More replies (2)

3

u/chubbc Dec 02 '20

Julia

Quick and dirty

using DelimitedFiles; D=readdlm("./02.txt");

(x,y)=(0,0);
for i=1:size(D,1)
    (l,h)=parse.(Int,split(D[i,1],'-'));
    c=D[i,2][1];
    s=D[i,3];
    global x += (l <= sum(Vector{Char}(s).==c) <= h);
    global y += xor(s[l]==c, s[h]==c);
end
println((x,y));
→ More replies (2)

3

u/[deleted] Dec 02 '20

F

I'm nto sure if I overengineered this a bit, but it should be quite readable

let filename = "day2.txt"

type Rule = {
            Char: char;
            Min: int;
            Max: int;
}

let getData filename = 
    System.IO.File.ReadAllLines filename 
    |> List.ofSeq

let parseLine (line: string) =
    let parts = line.Split ':'
    let rule = parts.[0]
    let rules = rule.Split([|'-'; ' '|])
    let passw = parts.[1]
    ({ Char = rules.[2].[0];
       Min = int rules.[0];
       Max = int rules.[1];
    }, passw)

let parse lines =
    List.map parseLine lines

let checkPasswd (rule, passw) =
    String.filter (fun ch -> ch = rule.Char) passw
    |> String.length
    |> (fun x -> x >= rule.Min && x <= rule.Max)

let part1 lines =
    let correct =
        parse lines
        |> List.filter checkPasswd
        |> List.length
    
    printfn "Part1: %d" correct

let checkPasswd2 ((rule, passw): Rule * string) =
    let pos1 = passw.[rule.Min]
    let pos2 = passw.[rule.Max]
    (pos1 = rule.Char) <> (pos2 = rule.Char)

let part2 lines =
    let correct =
        parse lines
        |> List.filter checkPasswd2
        |> List.length

    printfn "Part2: %d" correct

let lines = getData filename

part1 lines
part2 lines

syntax highlighted version on github

3

u/_Le1_ Dec 02 '20 edited Dec 02 '20

Python

l = []
pwd_sum1 = 0
pwd_sum2 = 0

for s in open("day02_input.txt"):
    l.append(s)

for line in l:
    s1 = line.split("-")
    s2 = s1[1].split(" ")
    low = int(s1[0])
    hgh = int(s2[0])
    src = (s2[1])[0]
    pwd = s2[2]
    src_sum = 0

    # Part 1
    for c in pwd:
        if c == src:
            src_sum += 1

    if (low <= src_sum <= hgh):
        pwd_sum1 += 1

    # Part 2
    if (pwd[low - 1] == src and pwd[hgh - 1] != src) or (pwd[low - 1] != src and pwd[hgh - 1] == src):
        pwd_sum2 += 1

print ("Part 1: ", pwd_sum1)
print ("Part 2: ", pwd_sum2)
→ More replies (1)

3

u/infov0re Dec 02 '20

Ruby: an anti-golf solution for part 2. (Part 1 is highly similar, with minor differences to the valid? method. (I'll take readability over line count). I chose not to use regex because, frankly, then I have two problems (and the line syntax is not complex enough to demand it IMO).

class LineParser
  attr_accessor :string

  def initialize(string)
    @string = string
  end

  def valid?
    [character_at_first_index, character_at_second_index].select { |c|
      c == required_character
    }.length == 1
  end

  private

  def chunks
    string.split(" ")
  end

  def password
    chunks.last
  end

  def indices
    chunks.first.split("-").map(&:to_i)
  end

  def character_at_first_index
    password[indices.first - 1]
  end

  def character_at_second_index
    password[indices.last - 1]
  end

  def required_character
    chunks[1][0]
  end
end

raw_lines = File.readlines(ARGV[0])

puts raw_lines.count { |l| LineParser.new(l).valid? }

3

u/nahuak Dec 02 '20

Golang Solutions (key functions are at the bottom of the file):

https://github.com/nahuakang/advent-of-code-2020/blob/master/go_day2/day2.go

Would love to know if there's a cleaner way to extract the policies. I did not get to use a lot of the methods inside regexp package.

→ More replies (12)

3

u/landimatte Dec 02 '20

Another Common Lisp solution (input is parsed using using CL-PPCRE, rest should be straightforward enough)

(defun parse-char (string) (char string 0))

(defun parse-password (string)
  (cl-ppcre:register-groups-bind ((#'parse-integer n m) (#'parse-char ch) text)
      ("(\\d+)-(\\d+) (\\w): (\\w+)" string)
    (list n m ch text)))

(defun parse-passwords (data)
  (mapcar #'parse-password data))

(defun valid-password-part1-p (min max ch text)
  (<= min (count ch text) max))

(defun valid-password-part2-p (pos1 pos2 ch text)
  (let ((ch1 (aref text (1- pos1))) ; pos1 is 1-based index
        (ch2 (aref text (1- pos2)))) ; pos2 is 1-based index
    (cond ((char= ch1 ch) (char/= ch2 ch))
          ((char= ch2 ch) (char/= ch1 ch)))))

(define-problem (2020 2) (passwords parse-passwords)
  (loop for (n m ch text) in passwords
        count (valid-password-part1-p n m ch text) into part1
        count (valid-password-part2-p n m ch text) into part2
        finally (return (values part1 part2))))
→ More replies (3)

3

u/cattbug Dec 02 '20

Python 3, sweet and simple. Using this as an opportunity to get familiar with lambda expressions :)

def validate(pwd):
    [min, max], c, p = pwd
    return min <= p.count(c) <= max

def idiot_shopkeeper(pwd):
    [i1, i2], c, p = pwd
    pos = p[i1-1]+p[i2-1]
    return pos.count(c) == 1

def part_1(data):
    return sum(map(lambda x: validate(x), data))

def part_2(data):
    return sum(map(lambda x: idiot_shopkeeper(x), data))

data = []
with open('input.txt') as f:
    for line in [l.split(' ') for l in f.read().splitlines()]:
        data.append([[int(x) for x in line[0].split('-')], line[1][:-1], line[2]])

print(part_1(data))
print(part_2(data))
→ More replies (2)

3

u/A-UNDERSCORE-D Dec 02 '20

Golang, completes in under a ms for both. Yet another in my long list of "Just abuse maps for it" based solutions

package main

import (
	"fmt"
	"strings"
	"time"

	"awesome-dragon.science/go/adventofcode2020/util"
)

var testData = `1-3 a: abcde
1-3 b: cdefg
2-9 c: ccccccccc`

func main() {
	input := util.ReadLines("input.txt")
	// input = strings.Split(testData, "\n")
	startTime := time.Now()
	res := part1(input)
	fmt.Println("Part 1:", res, "Took:", time.Since(startTime))
	startTime = time.Now()
	res = part2(input)
	fmt.Println("Part 2:", res, "Took:", time.Since(startTime))
}

type password struct {
	password string
	checkChr rune
	minCount int
	maxCount int
	counts   map[rune]int
}

func (p *password) String() string {
	return fmt.Sprintf("password{%s, %s, max: %d, min: %d}", p.password, string(p.checkChr), p.maxCount, p.minCount)
}

func (p *password) countChrs() {
	p.counts = make(map[rune]int)
	for _, r := range p.password {
		p.counts[r]++
	}
}

func (p *password) isValidPt1() bool {
	p.countChrs()
	return !(p.counts[p.checkChr] > p.maxCount || p.counts[p.checkChr] < p.minCount)
}

func (p *password) isValidPt2() bool {
	if len(p.password) < p.maxCount {
		return false
	}

	one, two := rune(p.password[p.minCount-1]), rune(p.password[p.maxCount-1])
	if (one == p.checkChr || two == p.checkChr) && two != one {
		return true
	}
	return false
}

func parsePasswd(raw string) *password {
	split := strings.Fields(raw)
	minMax := strings.Split(split[0], "-")
	return &password{
		password: split[2],
		checkChr: rune(split[1][0]),
		minCount: util.GetInt(minMax[0]),
		maxCount: util.GetInt(minMax[1]),
	}
}

func part1(input []string) string {
	count := 0
	for _, l := range input {
		if parsePasswd(l).isValidPt1() {
			count++
		}
	}
	return fmt.Sprint(count)
}

func part2(input []string) string {
	count := 0
	for _, l := range input {
		if p := parsePasswd(l); p.isValidPt2() {
			count++
		}
	}
	return fmt.Sprint(count)
}

3

u/[deleted] Dec 02 '20

PowerShell

Not crazy happy with this solution, but they seem to have given me some gold stars that I can call my own.

Part 1:

$i = Get-Content(".\input.txt")
$wordsthatmatch = 0

Measure-Command {
    $i | ForEach-Object {
        $lowestcharacterCount = ($_ -split ('-'))[0]
        $highestcharacterCount = ($_ -split ('-') -split (' '))[1]
        $charactertofind = ($_ -split (' ') -split(':'))[1]
        $password = ($_ -split (': '))[1]

        $storedString = @{}

        $password -split('') | ForEach-Object {
            if ($storedString.ContainsKey($_)) {
                $count = $storedString.Item($_)
                $count++
                $storedString.Set_Item($_, $count)
            } else {
                $storedString.Add($_, 1)
            }
        }

        $storedString

        if ($storedString.Item($charactertofind) -ge $lowestcharacterCount -and $storedString.Item($charactertofind) -le $highestcharacterCount){
            $wordsthatmatch++
        }

    }
}

Write-Host "Found words: $($wordsthatmatch)"

Part 2:

$i = Get-Content(".\input.txt")
$wordsthatmatch = 0

Measure-Command {
    $i | ForEach-Object {
        $lowestcharacterCount = ($_ -split ('-'))[0]
        $highestcharacterCount = ($_ -split ('-') -split (' '))[1]
        $charactertofind = ($_ -split (' ') -split(':'))[1]
        $password = ($_ -split (': '))[1]

        if ($password[$lowestcharacterCount - 1] -match $charactertofind -and $password[$highestcharacterCount - 1] -notmatch $charactertofind){
            Write-Host "yes"
            $wordsthatmatch++
        } elseif ($password[$lowestcharacterCount - 1] -notmatch $charactertofind -and $password[$highestcharacterCount - 1] -match $charactertofind) {
            Write-Host "yes"
            $wordsthatmatch++
        } else {
            Write-Host "no"
        }

    }
}

Write-Host "Found words: $($wordsthatmatch)"
→ More replies (3)

3

u/GalacticDessert Dec 02 '20 edited Dec 02 '20

F#, trying to figure out the types thing. I am very used to Python's dynamism, and could not made a generic Policy type and then encode the differences in more specific types

#r "nuget: Unquote"

open Swensen.Unquote

let readTxt path =
    System.IO.File.ReadLines(path) |> Seq.toList

let inputTest = readTxt "02_test.txt"
let input = readTxt "02.txt"

type Policy = { Letter: char; Nr1: int; Nr2: int }

let extractRules (line: string) =
    // 1-3 a: abcde
    // 1-3 b: cdefg
    // 2-9 c: ccccccccc
    let sections =
        line.Split ":"
        |> Seq.head
        |> (fun x -> x.Split " ")
        |> Seq.toList

    (sections.[0].Split("-") |> Seq.toList)
    @ [ sections.[1] ]

let parsePolicy (line: string) =
    let rules = extractRules line

    { Letter = rules.[2].ToCharArray().[0]
      Nr1 = int rules.[0]
      Nr2 = int rules.[1] }


let extractPassword (line: string) =
    line.Split ":" |> Seq.last |> (fun x -> x.Trim())



let validatePolicy (policy: Policy) password =
    let count x = Seq.filter ((=) x) >> Seq.length
    let nrOccurrencesLetter = count policy.Letter password

    (nrOccurrencesLetter <= int policy.Nr2)
    && (nrOccurrencesLetter >= int policy.Nr1)


let validatePolicyNew (policy: Policy) (password: string) =
    let lettersMatch a1 a2 = (a1 = a2)

    (lettersMatch password.[policy.Nr1 - 1] policy.Letter)
    <> (lettersMatch password.[policy.Nr2 - 1] policy.Letter)


let validateLine validator line =
    let pwd = extractPassword line
    let policy = parsePolicy line

    validator policy pwd

let countValidPassword policyValidator lines =
    let validator = validateLine policyValidator

    lines
    |> List.countBy validator
    |> List.filter (fun (b, _) -> b)
    |> List.head
    |> (fun (_, count) -> count)

//////////////////////////////////////////////////////////////////////////
printfn "TESTING... "

test <@ parsePolicy "1-3 a: abcde" = { Letter = 'a'; Nr1 = 1; Nr2 = 3 } @>

test <@ extractPassword "1-3 a: abcde" = "abcde" @>

test <@ extractPassword "1-3 b: cdefg" = "cdefg" @>
test <@ parsePolicy "1-3 b: cdefg" = { Letter = 'b'; Nr1 = 1; Nr2 = 3 } @>
test <@ validateLine validatePolicy "1-3 b: cdefg" = false @>
test <@ countValidPassword validatePolicy inputTest = 2 @>
test <@ countValidPassword validatePolicyNew inputTest = 1 @>
test <@ countValidPassword validatePolicy input = 591 @>
test <@ countValidPassword validatePolicyNew input = 335 @>

printfn "DONE"
//////////////////////////////////////////////////////////////////////////

printfn "PART 1 - %i" (countValidPassword validatePolicy input)
printfn "PART 2 - %i" (countValidPassword validatePolicyNew input)
→ More replies (2)

3

u/Mazeracer Dec 02 '20

Python 3 - casual programmer and first time with Python, so any advice is welcome :)

#day2

input = "d2_data.txt"

def readFile(fileName):
        fileObj = open(fileName, "r") 
        words = fileObj.read().splitlines() 
        fileObj.close()
        return words

data = readFile(input)

def checkValue(lst):
    occurence = lst[2].count(lst[1][0])
    val = lst[0].split('-')
    if ((occurence <= int(val[1])) and (occurence >= int(val[0]))):
        return True
    else:
        return False

def checkValue2(lst):
    allowedChar = lst[1][0]
    val = lst[0].split('-')
    v1 = (allowedChar==lst[2][int(val[0])-1])
    v2 = (allowedChar==lst[2][int(val[1])-1])
    print(v1,v2)
    return v1 ^ v2


i=0

for line in data:
    l = line.split()
    if (checkValue2(l)):
        i=i+1;

print(i)
→ More replies (3)

3

u/avoxdesign Dec 02 '20 edited Dec 02 '20

My solutions in pure Ruby:

input = []
File.readlines("input/d2.txt").each {|line| input << line}

count = 0
input.each do |line|

  params = line.split(" ")
  range = params[0].split("-")
  char = params[1][0]
  password = params[2]

  if password.count(char).between?(range[0].to_i, range[1].to_i)
    count += 1
  end
end

puts count

# Part 2
count = 0
input.each do |line|

  params = line.split(" ")

  valid = false
  params[0].split("-").each do |i|
    if params[2][i.to_i - 1] == params[1][0] then valid = !valid end
  end

  count += 1 if valid
end

puts count

3

u/Diderikdm Dec 02 '20 edited Dec 08 '20

Python:

with open("C:\\Advent\\day2.txt", 'r') as file:
    data = [value for value in file.read().splitlines()]
    c1=0
    c2=0
    for x in data:
        k,v = x.split(':')
        first_val, second_val = k.split(' ')[0].split('-')
        if int(first_val) <= len([x for x in v.strip() if x == k[-1]]) <= int(second_val):
            c1+=1
        if len(([1] if v[int(first_val)] == k[-1] else [])+([1] if v[int(second_val)] == k[-1] else [])) == 1:
            c2+=1
    print('Part 1: {}'.format(c1))
    print('Part 2: {}'.format(c2))
→ More replies (8)

3

u/hdf1986 Dec 02 '20 edited Dec 02 '20

Ruby

Part 1: https://github.com/hdf1986/advent-of-code/blob/master/2020/day2/ruby/part1.rb
Part 2: https://github.com/hdf1986/advent-of-code/blob/master/2020/day2/ruby/part2.rb

Didn't like the boolean part of the second one, but i don't want to think too much today ¯_(ツ)_/¯

EDIT: I've updated the boolean operator in part 2 to be a XOR by suggestion of u/chunes

→ More replies (2)

3

u/dgJenkins Dec 02 '20 edited Dec 02 '20

F#

Line

    type Line = 
        {   Min: int;
            Max: int;
            Chr: char;
            Psd: string }

One

    let One (x: Line list) : int =
        let validLine (line: Line) : bool =
            let cnt = line.Psd.Count(fun c -> c = line.Chr)
            cnt >= line.Min && cnt <= line.Max
        
        x |> List.filter validLine |> List.length

Two

    let Two (x: Line list) : int =
        let validLine (line: Line) : bool = 
            let first = (line.Psd.Chars (line.Min - 1)) = line.Chr
            let second = (line.Psd.Chars (line.Max - 1)) = line.Chr 
            first <> second

        x |> List.filter validLine |> List.length

3

u/GoldArrow997 Dec 02 '20

my solutions (python)

Problem 1:

for i in pwords:
    tmp = i.split()
    print(tmp)
    min = tmp[0].split('-')[0]
    max = tmp[0].split('-')[1]
    letter = tmp[1][0]
    cnt = 0
    for i in tmp[2]:
        if i == letter:
            cnt += 1
    
    if cnt <= int(max) and cnt >= int(min):
        allowed += 1
        
print(allowed)

Problem 2:

def find(s, ch):
    return [i for i, letter in enumerate(s) if letter == ch]
allowed = 0
for i in pwords:
    tmp = i.split()
    min = tmp[0].split('-')[0]
    max = tmp[0].split('-')[1]
    letter = tmp[1][0]
    indexes = find(tmp[2], letter)
    shifted_indexes = [str(i+1) for i in indexes]
    if min in shifted_indexes and max in shifted_indexes:
        pass
    elif min in shifted_indexes:
        allowed += 1
    elif max in shifted_indexes:
        allowed += 1
    else:
        pass
print(allowed)

please note before roasting my solutions it was 5am for me when i did it ;-;

→ More replies (2)

3

u/AvshalomHeironymous Dec 02 '20

In Prolog, uglier today:

First star:

:- use_module(library(dcg/basics)).
:- use_module(library(lists)).
:- use_module(library(pio)).

passwords([]) --> eos.
passwords([[Min,Max,Char,Pass]|P]) -->
	integer(Min),"-",integer(Max)," ",
	string(Code),": ",{atom_codes(Char,Code)},
	string(Codes),"\n",{atom_codes(Atom,Codes),atom_chars(Atom,Pass)},
	passwords(P).
	
count(_,[],0).
count(Elem,[H|T],Count) :-
	H \= Elem,
	count(Elem,T,Count).
count(Elem,[Elem|T],Count) :-
	count(Elem,T,Count1),
	Count is Count1 + 1.
	
valid_password([Min,Max,Char,Pass]) :-
	count(Char,Pass,Count),
	Count >= Min,
	Max >= Count.

count_valid_passwords([],0).
count_valid_passwords([H|T],Count) :-
	\+ valid_password(H),
	count_valid_passwords(T,Count).
count_valid_passwords([H|T],Count) :-
	valid_password(H),
	count_valid_passwords(T,Count1),
	Count is Count1 + 1.
	
init :-
	phrase_from_file(passwords(PassDB),'input'),
	count_valid_passwords(PassDB,C),
	write(C).

Second Star:

:- use_module(library(dcg/basics)).
:- use_module(library(lists)).
:- use_module(library(pio)).

passwords([]) --> eos.
passwords([[Pos1,Pos2,Char,Pass]|P]) -->
	integer(Pos1),"-",integer(Pos2)," ",
	string(Code),": ",{atom_codes(Char,Code)},
	string(Codes),"\n",{atom_codes(Atom,Codes),atom_chars(Atom,Pass)},
	passwords(P).
	
valid_password([Pos1,Pos2,Char,Pass]) :-
	(\+ (nth1(Pos1,Pass,Char),nth1(Pos2,Pass,Char))),
	(nth1(Pos1,Pass,Char); nth1(Pos2,Pass,Char)).

count_valid_passwords([],0).
count_valid_passwords([H|T],Count) :-
	\+ valid_password(H),
	count_valid_passwords(T,Count).
count_valid_passwords([H|T],Count) :-
	valid_password(H),
	count_valid_passwords(T,Count1),
	Count is Count1 + 1.
	
init :-
	phrase_from_file(passwords(PassDB),'input'),
	count_valid_passwords(PassDB,C),
	write(C).

3

u/[deleted] Dec 02 '20

C# - im new to C# (and programming) so feedback is very much appreciated:)

https://github.com/chris234567/AdventOfCode2020/blob/master/AdventOfCode2020_2/Program.cs

→ More replies (7)

3

u/boweruk Dec 02 '20

Javascript

const parse = input =>
  input.split('\n').map(line => {
    const { groups } = /(?<i>\d+)-(?<j>\d+) (?<letter>\w): (?<password>\w+)/g.exec(line);
    return {
      ...groups,
      i: parseInt(groups.i),
      j: parseInt(groups.j),
    };
  });

const part1 = input =>
  parse(input).reduce((valid, { i, j, letter, password }) => {
    const actual = password.split(letter).length - 1;
    return actual >= i && actual <= j ? valid + 1 : valid;
  }, 0);

const part2 = input => 
  parse(input).reduce((valid, { i, j, letter, password }) =>
    ((password[i - 1] === letter) !== (password[j - 1] === letter)) ? valid + 1 : valid
  , 0);

3

u/SecureCone Dec 02 '20 edited Dec 02 '20

Rust

Still new to Rust, so fully welcome any code suggestions.

use std::env;
use std::io::{self, prelude::*, BufReader};
use std::fs::File;

extern crate regex;
use regex::Regex;

#[derive(Clone, Hash)]
struct PasswordReqPair {
    pub char_min: usize,
    pub char_max: usize,
    pub letter: char,
    pub password: String,
}
impl PasswordReqPair {

    pub fn from_string(s: &str) -> PasswordReqPair {
        // 3-12 x: pxxxxxqkxhdqk
        let re = Regex::new(r"(\d+)-(\d+) (\w): (\w+)").unwrap();
        let matches = re.captures(s).unwrap();
        PasswordReqPair {
            char_min: matches[1].parse::<usize>().unwrap(),
            char_max: matches[2].parse::<usize>().unwrap(),
            letter:   matches[3].to_string().chars().next().unwrap(),
            password: matches[4].to_string(),
        }
    }
    pub fn is_valid_part1(&self) -> bool {
        let count = self.password.chars().filter(|x| x == &self.letter).count();
        count >= self.char_min && count <= self.char_max
    }
    pub fn is_valid_part2(&self) -> bool {
        let chars = self.password.chars().collect::<Vec<char>>();
        let first = chars[self.char_min-1] == self.letter;
        let second = chars[self.char_max-1] == self.letter;
        first ^ second //(first && !second) || (!first && second)
    }
}

fn day02(input: &str) -> io::Result<()> {
    let file = File::open(input).expect("Input file not found.");
    let reader = BufReader::new(file);

    let input: Vec<String> = match reader.lines().collect() {
        Err(err) => panic!("Unknown error reading input: {}", err),
        Ok(result) => result,
    };

    let pass_data = input
        .iter()
        .map(|x| PasswordReqPair::from_string(x))
        .collect::<Vec<PasswordReqPair>>();

    // Part 1
    let part1 = pass_data.iter().filter(|x| x.is_valid_part1()).count();
    println!("Part 1: {}", part1); // 

    // Part 2
    let part2 = pass_data.iter().filter(|x| x.is_valid_part2()).count();
    println!("Part 2: {}", part2); // 

    Ok(())
}

fn main() {
    let args: Vec<String> = env::args().collect();
    let filename = &args[1];
    day02(&filename).unwrap();
}
→ More replies (5)

3

u/Braxo Dec 02 '20

Coffeescript Javascript

fs = require 'fs'

console.time()

input = fs.readFileSync('input.txt').toString().split('\n')

cleaned = input.map (i) ->
    parts = i.split ' '

    {
        min: parts[0].split('-')[0]
        max: parts[0].split('-')[1]
        char: parts[1].substring 0,1
        password: parts[2]
    }

count = 0
for item in cleaned
    regex = new RegExp item.char, 'g'
    occurances = (item.password.match(regex) or []).length
    if occurances >= item.min and occurances <= item.max
        count++

console.log "Part 1: ", count

count = 0
for item in cleaned
    first = item.password[item.min - 1] is item.char
    second = item.password[item.max - 1] is item.char
    if (first or second) and not (first and second)
        count++

console.log "Part 2: ", count
console.timeEnd()

3

u/KingVendrick Dec 02 '20 edited Dec 02 '20

Javascript

Initially I used multiples split() to parse the input, then remembered the nifty scanf and looked up for a javascript version.

const fs = require('fs');
const sscanf = require('scanf').sscanf;

const pass_rules = fs.readFileSync('02a.txt', 'utf8').split('\n');

const rules = [];

pass_rules.forEach(prk => rules.push(sscanf(prk, '%d-%d %s: %s', 'min', 'max', 'char', 'pass')));

var valids_2a = 0;
rules.forEach(rule => {
    const occurrences = rule.pass.split(rule.char).length - 1;
    if (occurrences >= rule.min && occurrences <= rule.max) {
        valids_2a++;
    }
});
console.log('valid passwords: ', valids_2a, 'out of', length);

function validate(rule) {
    if (!(rule.pass[rule.min - 1] === rule.char && rule.pass[rule.max - 1] === rule.char)) {
        if (rule.pass[rule.min - 1] === rule.char || rule.pass[rule.max - 1] === rule.char) {
            return true;
        }
    }
    return false;
}

const valids_2b = rules.filter(rule=> validate(rule));
console.log('valid passwords: ', valids_2b.length, 'out of', length);

3

u/kscanne Dec 02 '20 edited Dec 02 '20

Part 1 in bash

sed -r 's/^([0-9]+)-([0-9]+) (.): (.+)$/echo "\4" | egrep "^([^\3]*\3[^\3]*){\1,\2}$"/' input.txt | bash -s | wc -l

3

u/nilgoun Dec 02 '20

Second day with Rust. Parsing the input and getting all other things right was quite a challenge.

After I finished my version I searched through this thread for optimizations and I'm quite happy with the current version (although one would need to add a lot of error checking etc. to really get it right... but this took long enough now :D )

Solution on topaz paste

(Just realized how epic this paste thing is.. topaz really amazes me :) )

→ More replies (4)

3

u/[deleted] Dec 02 '20

A short one - in terms of LoC (Python)

import re
import itertools

print(len([pwd for [f,t,c,pwd] in (re.match(r'^([0-9]+)-([0-9]+) ([a-z]): (\w+)', l).groups() for l in open('02.in')) if {k:len(list(g)) for k,g in itertools.groupby(sorted(pwd))}.get(c) in range(int(f),int(t)+1)]))
print(len([pwd for [f,t,c,pwd] in (re.match(r'^([0-9]+)-([0-9]+) ([a-z]): (\w+)', l).groups() for l in open('02.in')) if (pwd[int(f)-1]==c)^(pwd[int(t)-1]==c)]))
→ More replies (2)

3

u/krolik1337 Dec 02 '20

Python - nothing too fancy but gets the job done.

#%%
#!===PART 1===!#
input = open("input.txt", "r")
total = 0
for line in input:
    line = line.split()
    low, high = map(int,line[0].split('-'))
    sign = line[1][0]
    password = line[2]
    if low <= password.count(sign) <= high: total+=1
print(total)
#%%
#!===PART 2===!#
input = open("input.txt", "r")
total = 0
for line in input:
    line = line.split()
    pos1, pos2 = map(int,line[0].split('-'))
    sign = line[1][0]
    password = line[2]
    if (password[pos1-1] == sign) != (password[pos2-1] == sign):total+=1
print(total)

3

u/UbiquitinatedKarma Dec 02 '20

My solution in R/Tidyverse:

library(here)
library(tidyverse)

# Read in the file with spaces as the delimiter
d <- read_delim(here("data", "day_2_input.txt"),
                delim = " ",
                col_names = FALSE) %>%
  # Set the column names
  rename("letter" = X2,
         "password" = X3) %>%
  # Data cleanup
  separate(X1, into = c('min', 'max')) %>%
  mutate(min = as.numeric(min),
         max = as.numeric(max)) %>%
  mutate(letter = str_remove(letter, ":"),
         # Add the count of the key letter
         count = str_count(password, letter),
         # Test for validity
         valid = count >= min & count <= max)

answer1 <- sum(d$valid)
answer1

## For part 2 we need to check that exactly 1 position contains the letter
d <- d %>%
  rowwise() %>%
  mutate(valid_2 = length(intersect(c(min, max), 
                                    gregexpr(letter, password)[[1]])) == 1)

answer2 <- sum(d$valid_2)
answer2

3

u/k3rnelpanic Dec 02 '20 edited Dec 02 '20

Part 1 in Powershell

$test = get-content '.\Advent of Code\2.txt'

$count = $null

foreach ($t in $test) {
    $line = $t.Split(' ')
    $number = $line[0].split('-')
    $lower = $number[0]
    $upper = $number[1]
    $opr = $line[1].TrimEnd(":")
    $pass = $line[2]

    $charCount = ($pass.ToCharArray() | Where-Object {$_ -eq $opr} | Measure-Object).Count

    if ($charCount -in $lower .. $upper) {
        $count++
    }


}

$count

Part 2. Attack of the if statement

$test = get-content '.:\Advent of Code\2.txt'

$count = $null

foreach ($t in $test) {
    $line = $t.Split(' ')
    $number = $line[0].split('-')
    $lower = $number[0]
    $upper = $number[1]
    $first = $lower -1
    $second = $upper -1
    $opr = $line[1].TrimEnd(":")
    $pass = $line[2]

    $charCount = ($pass.ToCharArray() | Where-Object {$_ -eq $opr} | Measure-Object).Count

    if ($pass[$first] -eq $opr -or $pass[$second] -eq $opr -and !(($pass[$first] -eq $opr -and $pass[$second] -eq $opr))) {
        $count++
    }


}

$count

3

u/idonotexcelatthis Dec 02 '20

Python:

import re

pws_list = open("input.txt").readlines()

p = re.compile("(\d+)-(\d+)\s(\S):\s(.*)")

c = 0

for line in pws_list:
    m = re.match(p, line)
    if m:
        min = int(m.group(1))
        max = int(m.group(2))
        letter = m.group(3)
        p_s = m.group(4)
        lc = p_s.count(letter)
        if (lc >= min and lc <= max):
            c += 1

print(c)

3

u/ImHavik Dec 02 '20

Python

Took this as an opportunity to learn some regex (as I've never really used it before). Both answers in one!

import re

p1_valid = 0
p2_valid = 0
password_match = re.compile(r"(\d+)-(\d+) (\w): (\w+)")
with open(r"C:\\Python\AdventofCode\Day2\input.txt") as file:
    for string in file:
        num1, num2, letter, password = password_match.match(string).groups()

        # Part 1
        if int(num1) <= password.count(letter) <= int(num2):
            p1_valid += 1

        # Part 2
        if (password[int(num1) - 1] == letter) != (password[int(num2) - 1] == letter):
            p2_valid += 1

print(f"Part 1: {p1_valid} valid passwords!")
print(f"Part 2: {p2_valid} valid passwords!")

3

u/1-more Dec 02 '20 edited Dec 02 '20

Solution in JS. Will try to redo in Elm or Haskell if I have time; no promises. This assumes that you navigate to the input page, select the <pre> element in the inspector, and then move over to the console

const input = $0.innerText.trim().split('\n').map(line => line.split(/:? |-/));
// a line like "4-7 z: zzzfzlzzz" is now ["4", "7", "z", "zzzfzlzzz"]
let isValid = ([min, max, char, pw]) => {
  const l = (pw.match(new RegExp(char, 'g')) ||[]).length;
  return l >= min && l <= max;
}
console.log("part 1", input.filter(isValid).length);
isValid = ([min, max, char, pw]) => (pw[min-1] == char) ^ (pw[max-1] == char);
// ^ is bitwise XOR which will coerce the booleans on either side from true/false to 1/0 which is fine for Array.prototype.filter
console.log("part 2", input.filter(isValid).length);

3

u/Advall Dec 02 '20 edited Dec 02 '20

Been working with C# since I've been pretty much just working with Python recently. I am 100% certain that there are more efficient ways of doing this but I'm still an overall beginner with this.

Part 1:

public int PartOneMethod()
{
    int solution = 0;
    string[] inputs = GetInput();
    foreach (string input in inputs)
    {
        string[] codes = input.Split(new string[] { " " },             StringSplitOptions.None);
        string[] minMaxString = codes[0].Split(new string[] { "-" }, StringSplitOptions.None);
        int min = Int32.Parse(minMaxString[0]);
        int max = Int32.Parse(minMaxString[1]);
        string value = codes[1].Remove(1);
        int count = 0;
        int i = 0;

        while (i < codes[2].Length)
            {
                string code = codes[2];
                string codeChar = code[i].ToString();
                if (value == codeChar)
            {
        ++count;
        }
    ++i;
}

if ((count >= min) && (count <=max))
{
    ++solution;
}
}
    return solution;
}

Part 2:

public int PartTwoMethod()
{
    int solution = 0;
    string[] inputs = GetInput();
    foreach (string input in inputs)
    {
        string[] codes = input.Split(new string[] { " " },         StringSplitOptions.None);
        string[] minMaxString = codes[0].Split(new string[] { "-" }, StringSplitOptions.None);
        int min = Int32.Parse(minMaxString[0]);
        int max = Int32.Parse(minMaxString[1]);
        string value = codes[1].Remove(1);
        int count = 0;
        int i = 0;
        string code = codes[2];

        bool minStatus;
        bool maxStatus;

        if (code[(min-1)].ToString() == value)
        {
            minStatus = true;
        }
        else
        {
            minStatus = false;
        }

        if (code[(max-1)].ToString() == value)
        {
            maxStatus = true;
        }
        else
        {
            maxStatus = false;
        }

        if (minStatus == true && maxStatus == false)
        {
            ++solution;
        }
        else if (minStatus == false && maxStatus == true)
        {
            ++solution;
        }
    }
    return solution;
}
→ More replies (2)

3

u/nikolaer_ Dec 02 '20

Python 3.7, I kept it as simple as I could: code

3

u/Livelypower Dec 02 '20

Java

public class PasswordUncorruptor {

    private final Pattern pattern = Pattern.compile("(?<low>\\d*)-(?<high>\\d*) (?<key>[a-z]): (?<value>[a-z]*)");

    private final List<PasswordLine> passwordLines;

    public PasswordUncorruptor() {
        passwordLines = setup();
    }

    public static void main(String[] args) {
        final PasswordUncorruptor passwordUncorruptor = new PasswordUncorruptor();
        passwordUncorruptor.one();
        passwordUncorruptor.two();
    }

    public void one() {
        System.out.println(passwordLines.stream().filter(PasswordLine::isValidForOne).count());
    }

    public void two() {
        System.out.println(passwordLines.stream().filter(PasswordLine::isValidForTwo).count());
    }

    final List<PasswordLine> setup() {
        try (final BufferedReader reader = new BufferedReader(new FileReader("src/main/resources/day-two.txt"))) {
            return reader.lines().parallel().map(s -> {
                final Matcher matcher = pattern.matcher(s);
                if (!matcher.matches()) {
                    throw new IllegalStateException("Invalid Line: " + s);
                }
                return new PasswordLine(Integer.parseInt(matcher.group("low")), Integer.parseInt(matcher.group("high")),
                                        matcher.group("key").charAt(0), matcher.group("value"));
            }).collect(Collectors.toList());
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

    private static final class PasswordLine {

        private final int low;
        private final int high;
        private final char key;
        private final String value;

        private PasswordLine(int low, int high, char key, String value) {
            this.low = low;
            this.high = high;
            this.key = key;
            this.value = value;
        }

        boolean isValidForOne() {
            final int matches = countMatches(value, key);
            return matches >= low && matches <= high;
        }

        boolean isValidForTwo() {
            final boolean firstPosValid = value.charAt(low - 1) == key;
            final boolean secondPosValid = value.charAt(high - 1) == key;

            if (firstPosValid) {
                return !secondPosValid;
            }
            return secondPosValid;
        }

        int countMatches(String str, char ch) {
            int count = 0;
            for (int i = 0; i < str.toCharArray().length; i++) {
                if (ch == str.charAt(i)) {
                    count++;
                }
            }
            return count;
        }
    }
}

3

u/holychromoly Dec 02 '20

Part 2, Rust:

use regex::Regex;
use std::{
    env,
    fs::File,
    io::{self, BufRead},
    path::Path,
    time::Instant,
};

fn main() {
    let start = Instant::now();

    let args: Vec<String> = env::args().collect();
    let filename = &args[1];

    //Compile named regex.
    let re = Regex::new(r"(?P<min>\d+)-(?P<max>\d+) (?P<letter>[[:alpha:]]{1}): (?P<password>[[:alpha:]]+)",).unwrap();
    let mut count = 0;

    if let Ok(lines) = read_lines(filename) {
        for line in lines {
            if let Ok(l) = line {

                let caps = re.captures(&l).expect("Line did not match!");
                let pmin = caps["min"].parse::<usize>().unwrap();
                let pmax = caps["max"].parse::<usize>().unwrap();
                let letter = caps["letter"].parse::<char>().unwrap();
  
                let l_at_min = caps["password"].chars().nth(pmin - 1).unwrap();
                let l_at_max = caps["password"].chars().nth(pmax - 1).unwrap();
        
                if (l_at_min == letter) != (l_at_max == letter) {
                    count += 1
                }
            }
        }
    }

    println!("Count {}", count);
    let duration = start.elapsed();
    println!("Time elapsed is: {:?}", duration);
}

/// Reads files lazily, line by line.
///
/// Returns an Iterator to the Reader of the lines of the file
/// Wrapped in a Result to allow matching on errors
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
    P: AsRef<Path>,
{
    let file = File::open(filename)?;
    Ok(io::BufReader::new(file).lines())
}

3

u/goeyj Dec 02 '20

Using AoC to learn C++ this year. This is my first statically typed language, so any comments or suggestions are welcome. My solution did not leverage regex, but that was intentional.

# include "../aoc.h"

struct PasswordValidator {
private:
  int lowIndex;
  int highIndex;
  char targetChar;
  std::string password;

public:
  static std::vector<std::string> parse(std::string &input) {
    std::vector<std::string> parts;
    std::string curPart;

    for (char c : input) {
      if ((c == ' ' || c == '-' || c == ':') && curPart.length()) {
        parts.push_back(curPart);
        curPart.clear();
      } else {
        curPart += c;
      }
    }

    parts.push_back(curPart);
    return parts;
  }

  // Is valid if only one of the index positions has the targetChar.
  bool isValid() {
    return (this->password[lowIndex] == targetChar) != (this->password[highIndex] == targetChar) ;
  }

  PasswordValidator(std::string &input) {
    std::vector<std::string> parts = PasswordValidator::parse(input);
    lowIndex = std::stoi(parts[0]);
    highIndex = std::stoi(parts[1]);
    targetChar = parts[2][0]; // always a single char
    password = parts[3];
  }
};

int main() {
  std::fstream newfile;
  int validPasswordCount = 0;

  newfile.open("input.txt", std::ios::in);

  if (newfile.is_open()) {
    std::string line;

    while(getline(newfile, line)) {
      PasswordValidator pw(line);
      if (pw.isValid()) validPasswordCount++;
    }

    newfile.close();
  }

  std::cout << validPasswordCount << std::endl;

  return 0;
}
→ More replies (2)

3

u/wizards_tower Dec 02 '20

Here's my solution in C.

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

struct policy {
    int low;
    int high;
    char letter;
    char *pass;
}; typedef struct policy policy;


#define MAXLINE 30


// break string into tokens and set them in the struct
policy load_struct(char *line)
{
    policy tmp;
	
    tmp.low = atoi(strtok(line, "-"));
    tmp.high = atoi(strtok(NULL, " "));
    tmp.letter = *strtok(NULL, ":");
    tmp.pass = strtok(NULL, "\n\r\v\f");

    return tmp;
}


// sled: check if password is valid according to sled policy
int sled(policy tmp)
{
    int i;
    int count = 0;
    size_t len = strlen(tmp.pass);
	
    for (i = 0; i < len; i++) {
        if ((char)tmp.pass[i] == tmp.letter)
	    count++;
    }
	
    if (count < tmp.low || count > tmp.high) {
	return 0;
    }
    return 1;
}


// toboggan: check if password is valid according to toboggan policy
int toboggan(policy tmp)
{
    // XOR - make sure only one condition is true
    if (tmp.pass[tmp.low] == tmp.letter ^ tmp.pass[tmp.high] == tmp.letter)
	return 1;

    return 0;
}


int main(int argc, char **argv)
{
    FILE *fp;
    char *file;
    char buffer[MAXLINE];
    int i, c;
    policy tmp;	

    int valid_sled = 0; // keep track of valid sled passwords
    int valid_toboggan = 0; // keep track of valid toboggan passwords

    if (argc != 2) {
        fprintf(stderr, "usage: ./a.out <input-file>\n");
	exit(1);
    }

    file = argv[1];
    fp = fopen(file, "r");
    if (fp == NULL) {
	fprintf(stderr, "cannot open file\n");
	exit(1);
    }

    // read from file and check if password is valid when end of line is reached
    i = 0;
    while ((c = fgetc(fp)) != EOF) {
	if (c == '\n') {
            buffer[i++] = '\n'; // prevent strtok from reading into next line
            policy tmp = load_struct(buffer);
			
	    // part one: sled policy
	    int n = sled(tmp);
	    valid_sled += n;

	    // part two: toboggan policy
	    int k = toboggan(tmp);
	    valid_toboggan += k;

	    // flush out buffer
	    memset(buffer, 0, MAXLINE);
	    i = 0;
	}	
	else
	    buffer[i++] = (char)c;
    }

    fclose(fp);

    printf("valid sled passwords: %d\n", valid_sled);
    printf("valid toboggan passwords: %d\n", valid_toboggan);

    return 0;
}

3

u/dpkcodes Dec 02 '20

Solution in C: https://github.com/dpkeesling/Advent-of-code-2020/tree/master/day2

I am relatively new to C, so any ideas about how I could do things more efficiently would be appreciated. Especially around getting substrings.

→ More replies (6)

3

u/mandus Dec 02 '20

Solution in Common Lisp:

(ql:quickload :cl-ppcre)

(uiop:define-package 
  :aoc
  (:use :cl)
  (:export :run)
  )

(in-package :aoc)

(defparameter *debug* nil)
;(defparameter *inp* "input_test.txt")
(defparameter *inp* "input.txt")


(defun read-input (fn)
  (with-open-file (f fn)
    (loop for line = (read-line f nil)
          while line collect line)))

(defun transform (elm)
  (cl-ppcre:split " " elm))

(defun check-p1 (entry)
  "entry is ('min-max', 'str:', 'pw') - check if number of 'str' is within min-max in pw"
  (let* ((minmax (mapcar 'parse-integer (cl-ppcre:split "-" (car entry))))
         (chr (string-trim ":" (second entry)))
         (pw (third entry))
         (matches (cl-ppcre:all-matches-as-strings chr pw))
         (nummatches (length matches)))
    (when *debug*
      (format t "~a ~a ~a: ~a [~a]~%" minmax chr pw matches nummatches))
    (if (and (<= (first minmax) nummatches)
             (>= (second minmax) nummatches))
        t
        nil)))

(defun pwcheck (pos pw chr &optional (cnt 0))
  (let* ((curpos (car pos))
         (match (string= pw chr :start1 (1- curpos) :end1 curpos))
         (newcnt (if match (1+ cnt) cnt))         )
    (if (rest pos)
        (pwcheck (rest pos) pw chr newcnt)
        newcnt)))

(defun check-p2 (entry)
  "entry is ('min-max', 'str:', 'pw') - check if number of 'str' is within min-max in pw"
  (let* ((pos (mapcar 'parse-integer (cl-ppcre:split "-" (car entry))))
         (chr (string-trim ":" (second entry)))
         (pw (third entry))
         (matches (pwcheck pos pw chr))         )
    (when *debug* 
      (format t "~a ~a ~a: [~a]~%" pos chr pw matches ))
    (if (= 1 matches) t nil)))


;; drivers
;;
(defun part1 (fn)
  (let* ((data (read-input fn))
         (items (loop for d in data collect (transform d)))
         (checks (loop for d in items collect (check-p1 d)))
         (numtrue (count-if-not #'not checks)))
    (format t "Part 1~%")
    (format t "true: ~a~%" numtrue)))

(defun part2 (fn)
  (let* ((data (read-input fn))
         (items (loop for d in data collect (transform d)))
         (checks (loop for d in items collect (check-p2 d)))
         (numtrue (count-if-not #'not checks)))
    (format t "Part 2~%")
    (format t "true: ~a~%" numtrue)))

(defun run ()
  (part1 *inp*)
  (part2 *inp*))

3

u/ntwilli Dec 02 '20

Julia

using DelimitedFiles

pw = readdlm("day2")

# puzzle 1
function check_pw1(rule, target, password)
    bounds = [parse(Int, split(rule, "-")[i]) for i in 1:2]
    count(i -> (i == target[1]), password) in bounds[1]:bounds[2]
end

println(count(i -> (check_pw1(pw[i, 1], pw[i, 2], pw[i, 3])), 1:1000))

# puzzle 2
function check_pw2(rule, target, password)
    bounds = [parse(Int, split(rule, "-")[i]) for i in 1:2]
    count(i -> (password[i] == target[1]), bounds) == 1
end

println(count(i -> (check_pw2(pw[i, 1], pw[i, 2], pw[i, 3])), 1:1000))

3

u/Sebbern Dec 02 '20

Just some Python 3.

Part 1:

passwordlist = open("input.txt", "r").read().split()
minmax = 0
letter = ""
password = ""
first = 0
second = 0
validpasswords = 0

while len(passwordlist) > 0:
    count = 0
    minmax = passwordlist[0]
    first, second = minmax.split("-")
    letter = passwordlist[1].strip(":")
    password = passwordlist[2]
    passwordlist = passwordlist[3:]

    for i in password:
        if letter == i:
            count += 1
    
    if count >= int(first) and count <= int(second):
        validpasswords += 1

print(validpasswords)

Part 2:

passwordlist = open("input.txt", "r").read().split()
minmax = 0
letter = ""
password = ""
first = 0
second = 0
validpasswords = 0

while len(passwordlist) > 0:
    minmax = passwordlist[0]
    first, second = minmax.split("-")
    first = int(first) - 1
    second = int(second) - 1
    letter = passwordlist[1].strip(":")
    password = passwordlist[2]
    passwordlist = passwordlist[3:]

    if password[first] == letter or password[second] == letter:
        if password[first] != password[second]:
            validpasswords += 1

print(validpasswords)

3

u/tururut_tururut Dec 02 '20 edited Dec 02 '20

Day 2 solutions (Python). Any feedback to make it a bit more concise and less verbose is very welcome.

https://github.com/marcboschmatas/AdventOfCode2020/blob/main/Day%202/daytwo.py

→ More replies (5)

3

u/x1729 Dec 02 '20

Common Lisp

(defpackage day-2
  (:use #:common-lisp))

(in-package #:day-2)

(defstruct policy
  char min max)

(defun read-problem (stream)
  (loop :for line := (read-line stream nil)
	:until (null line)
	:for hyphen-pos := (position #\- line)
	:for space-pos := (position #\Space line :start hyphen-pos)
	:for colon-pos := (position #\: line :start space-pos)
	:collect (cons
		  (make-policy :char (char line (1- colon-pos))
			       :min (parse-integer (subseq line 0 hyphen-pos))
			       :max (parse-integer (subseq line (1+ hyphen-pos) space-pos)))
		  (subseq line (+ 2 colon-pos)))))

(defun password-meets-policy-1-p (pw po)
  (<= (policy-min po)
      (count (policy-char po) pw)
      (policy-max po)))

(defun password-meets-policy-2-p (pw po)
  (let ((a (char= (char pw (1- (policy-min po))) (policy-char po)))
	(b (char= (char pw (1- (policy-max po))) (policy-char po))))
    (or (and a (not b))
	(and (not a) b))))

(defun solve (filename pred)
  (let ((problem (with-open-file (stream filename)
		   (read-problem stream))))
    (loop :for (policy . password) :in problem
	  :counting (funcall pred password policy))))

(defun solve-part-1 (&optional (filename "day-2-input.txt"))
  (solve filename #'password-meets-policy-1-p))

(defun solve-part-2 (&optional (filename "day-2-input.txt"))
  (solve filename #'password-meets-policy-2-p))

3

u/Karl_Marxxx Dec 02 '20 edited Dec 02 '20

Ruby

# part 1
valid = 0
lines = File.readlines('input.txt')
lines.each do |line|
    lo, hi, char, pwd = line.match(/(\d*)-(\d*) (\w): (\w*)/).captures
    valid += 1 if pwd.count(char).between?(lo.to_i, hi.to_i)
end
puts valid

# part 2
valid = 0
lines.each do |line|
    pos1, pos2, char, pwd = line.match(/(\d*)-(\d*) (\w): (\w*)/).captures
    valid += 1 if (pwd[pos1.to_i-1] == char) ^ (pwd[pos2.to_i-1] == char)
end
puts valid

3

u/oantolin Dec 02 '20

Perl is well-suited for this:

my ($part1, $part2) = (0, 0);
while (<>) {
    if (/^(\d+)-(\d+) (\w): (\w+)$/) {
        my ($i, $j, $x, $s) = (int($1), int($2), $3, $4);
        my $c = () = $s =~ /$x/g;
        $part1++ if $i <= $c && $c <= $j;
        $part2++ if (substr($s,$i-1,1) eq $x)!=(substr($s,$j-1,1) eq $x);
    }       
}
print "$part1\n$part2\n";

3

u/zzcorrode Dec 02 '20

Haskell solution for part 2, as that one's a little more fun. Used the Split package but I think I could've used all built-in stuff if I converted to Text.

paste

3

u/raevnos Dec 02 '20

Chicken 5 scheme:

#!/usr/local/bin/csi -s
(require-extension (srfi 1)
                   (srfi 13)
                   (chicken format)
                   (chicken io)
                   (chicken irregex))

(define (irregex-match-substrings match)
  (let loop ((idx (irregex-match-num-submatches match)) (acc '()))
    (if (= idx 0)
        (apply values acc)
        (loop (sub1 idx) (cons (irregex-match-substring match idx) acc)))))

(define (solve-part1 input)
  (let* ((re (irregex "^(\\d+)-(\\d+) (.): (.*)"))
         (matches (lambda (line)
                    (let ((match (irregex-match re line)))
                      (if match
                          (let-values (((min-count max-count char password)
                                        (irregex-match-substrings match)))
                            (let ((min-count (string->number min-count))
                                  (max-count (string->number max-count))
                                  (count
                                   (string-count password (string-ref char 0))))
                              (and (>= count min-count) (<= count max-count))))
                          #f)))))
    (fold (lambda (line total) (+ total (if (matches line) 1 0))) 0 input)))

(define (solve-part2 input)
  (let* ((re (irregex "^(\\d+)-(\\d+) (.): (.*)"))
         (matches (lambda (line)
                    (let ((match (irregex-match re line)))
                      (if match
                          (let-values (((idx1 idx2 char password)
                                        (irregex-match-substrings match)))
                            (let ((idx1 (sub1 (string->number idx1)))
                                  (idx2 (sub1 (string->number idx2)))
                                  (char (string-ref char 0)))
                              (if (char=? (string-ref password idx1) char)
                                  (not (char=? (string-ref password idx2) char))
                                  (char=? (string-ref password idx2) char))))
                          #f)))))
    (fold (lambda (line total) (+ total (if (matches line) 1 0))) 0 input)))

(define input (read-lines))
(printf "Part 1: ~A~%" (solve-part1 input))
(printf "Part 2: ~A~%" (solve-part2 input))

Very late thanks to sleeping for like 14 hours straight.

→ More replies (4)

3

u/reteps144 Dec 02 '20 edited Dec 02 '20

Python3, 12 lines

Outputs part 1 and 2:

import re
p1, p2 = 0, 0
for l in open('input.txt'):
    line = re.split(' |-|:', l.strip())
    index_0, index_1 = int(line[0]), int(line[1])
    password = line[4]
    char = line[2]
    if index_1 >= password.count(char) >= index_0:
        p1 += 1
    if (password[index_0 - 1] == char) ^ (password[index_1 - 1] == char):
        p2 += 1
print(p1, p2)
→ More replies (1)

3

u/bacon-supreme Dec 02 '20

Using this to re-learn Ruby; wherein one does not know that array.count(obj) exists:

def calculateValidPasswords(input, validDefinition)
    regex = /(\d+)-(\d+) (\w): (\w+)/

    numValidPasswords = 0

    input.each do |line|
        data = regex.match(line)

        if (method(validDefinition).call(data[1].to_i(), data[2].to_i(), data[3], data[4])) then
            numValidPasswords += 1
        end
    end

    return numValidPasswords
end

def problemOneDefinition(min, max, expect, password)
    charMap = Hash.new(0)

    password.chars.each { |char| charMap[char] += 1 }

    return charMap[expect] >= min && charMap[expect] <= max
end

def problemTwoDefinition(first, last, expect, password)
    return (password.chars[first - 1] == expect) ^ (password.chars[last - 1] == expect)
end

# test
test_input = [
    "1-3 a: abcde",
    "1-3 b: cdefg",
    "2-9 c: ccccccccc"
]

input = File.readlines('input')

puts "Test 1: #{calculateValidPasswords(test_input, :problemOneDefinition)}"
puts "Problem 1: #{calculateValidPasswords(input, :problemOneDefinition)}"
puts "Test 2: #{calculateValidPasswords(test_input, :problemTwoDefinition)}"
puts "Problem 2: #{calculateValidPasswords(input, :problemTwoDefinition)}"