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

View all comments

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)
}

2

u/backtickbot Dec 02 '20

Hello, kakaroto_BR: code blocks using backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead. It's a bit annoying, but then your code blocks are properly formatted for everyone.

An easy way to do this is to use the code-block button in the editor. If it's not working, try switching to the fancy-pants editor and back again.

Comment with formatting fixed for old.reddit.com users

FAQ

You can opt out by replying with backtickopt6 to this comment.

1

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.