2020 day2 - string parsing & simple validation

This commit is contained in:
alexchao26
2020-12-02 00:15:37 -05:00
parent f363235bc9
commit 79925f6507
7 changed files with 1253 additions and 5 deletions
+32
View File
@@ -0,0 +1,32 @@
################################
################.#.#..##########
################.#...G##########
################...#############
######..##########.#..##########
####.G...#########.G...#########
###.........######....##########
##..#.##.....#....#....#########
#G.#GG..................##.#####
##.##..##..G........G.........##
#######......G.G...............#
#######........................#
########.G....#####..E#...E.G..#
#########G...#######...........#
#########...#########.........##
#####.......#########....G...###
###.........#########.....E..###
#...........#########.........##
#..#....G..G#########........###
#..#.........#######.........###
#G.##G......E.#####...E..E..####
##......E...............########
#.....#G.G..............E..#####
#....#####....E........###.#####
#...#########.........####.#####
#.###########......#.#####.#####
#....##########.##...###########
#....#############....##########
##.##############E....##########
##.##############..#############
##....##########################
################################
+59
View File
@@ -0,0 +1,59 @@
package main
import (
"flag"
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/util"
)
func main() {
var part int
flag.IntVar(&part, "part", 1, "part 1 or 2")
flag.Parse()
fmt.Println("Running part", part)
if part == 1 {
ans := part1(util.ReadFile("./input.txt"))
fmt.Println("Output:", ans)
} else {
ans := part2(util.ReadFile("./input.txt"))
fmt.Println("Output:", ans)
}
}
func part1(input string) int {
strGrid := parseInputs(input)
_ = strGrid
return 0
}
func part2(input string) int {
return 0
}
func parseInputs(input string) [][]string {
lines := strings.Split(input, "\n")
grid := [][]string{}
for r, row := range lines {
grid = append(grid, []string{})
for _, v := range row {
grid[r] = append(grid[r], string(v))
}
}
return grid
}
type Board struct {
grid [][]string
goblins []*Character
elves []*Character
}
type Character struct {
HP int
Type string
}
+43
View File
@@ -0,0 +1,43 @@
package main
import "testing"
var tests1 = []struct {
name string
want int
input string
// add extra args if needed
}{
// {"actual", ACTUAL_ANSWER, util.ReadFile("input.txt")},
}
func TestPart1(t *testing.T) {
for _, test := range tests1 {
t.Run(test.name, func(*testing.T) {
got := part1(test.input)
if got != test.want {
t.Errorf("got %v, want %v", got, test.want)
}
})
}
}
var tests2 = []struct {
name string
want int
input string
// add extra args if needed
}{
// {"actual", ACTUAL_ANSWER, util.ReadFile("input.txt")},
}
func TestPart2(t *testing.T) {
for _, test := range tests2 {
t.Run(test.name, func(*testing.T) {
got := part2(test.input)
if got != test.want {
t.Errorf("got %v, want %v", got, test.want)
}
})
}
}
+1000
View File
File diff suppressed because it is too large Load Diff
+52 -2
View File
@@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -23,9 +24,58 @@ func main() {
}
func part1(input string) int {
return 0
lines := strings.Split(input, "\n")
var ans int
for _, l := range lines {
var lower, upper int
var char, pw string
_, err := fmt.Sscanf(l, "%d-%d %1s: %s", &lower, &upper, &char, &pw)
if err != nil {
panic("scanning line " + err.Error())
}
count := 0
for _, v := range pw {
if string(v) == char {
count++
}
}
if count <= upper && count >= lower {
ans++
}
}
return ans
}
func part2(input string) int {
return 0
lines := strings.Split(input, "\n")
var ans int
for _, l := range lines {
var lower, upper int
var char, pw string
_, err := fmt.Sscanf(l, "%d-%d %1s: %s", &lower, &upper, &char, &pw)
if err != nil {
panic("scanning line " + err.Error())
}
count := 0
if string(pw[lower-1]) == char {
count++
}
if string(pw[upper-1]) == char {
count++
}
if count == 1 {
ans++
}
}
return ans
}
+13 -3
View File
@@ -1,6 +1,10 @@
package main
import "testing"
import (
"testing"
"github.com/alexchao26/advent-of-code-go/util"
)
var tests1 = []struct {
name string
@@ -8,7 +12,10 @@ var tests1 = []struct {
input string
// add extra args if needed
}{
// {"actual", ACTUAL_ANSWER, util.ReadFile("input.txt")},
{"example", 2, `1-3 a: abcde
1-3 b: cdefg
2-9 c: ccccccccc`},
{"actual", 645, util.ReadFile("input.txt")},
}
func TestPart1(t *testing.T) {
@@ -28,7 +35,10 @@ var tests2 = []struct {
input string
// add extra args if needed
}{
// {"actual", ACTUAL_ANSWER, util.ReadFile("input.txt")},
{"example", 1, `1-3 a: abcde
1-3 b: cdefg
2-9 c: ccccccccc`},
{"actual", 737, util.ReadFile("input.txt")},
}
func TestPart2(t *testing.T) {
+54
View File
@@ -0,0 +1,54 @@
--- Day 2: Password Philosophy ---
Your flight departs in a few days from the coastal airport; the easiest way down to the coast from here is via toboggan.
The shopkeeper at the North Pole Toboggan Rental Shop is having a bad day. "Something's wrong with our computers; we can't log in!" You ask if you can take a look.
Their password database seems to be a little corrupted: some of the passwords wouldn't have been allowed by the Official Toboggan Corporate Policy that was in effect when they were chosen.
To try to debug the problem, they have created a list (your puzzle input) of passwords (according to the corrupted database) and the corporate policy when that password was set.
For example, suppose you have the following list:
1-3 a: abcde
1-3 b: cdefg
2-9 c: ccccccccc
Each line gives the password policy and then the password. The password policy indicates the lowest and highest number of times a given letter must appear for the password to be valid. For example, 1-3 a means that the password must contain a at least 1 time and at most 3 times.
In the above example, 2 passwords are valid. The middle password, cdefg, is not; it contains no instances of b, but needs at least 1. The first and third passwords are valid: they contain one a or nine c, both within the limits of their respective policies.
How many passwords are valid according to their policies?
--- Part Two ---
While it appears you validated the passwords correctly, they don't seem to be what the Official Toboggan Corporate Authentication System is expecting.
The shopkeeper suddenly realizes that he just accidentally explained the password policy rules from his old job at the sled rental place down the street! The Official Toboggan Corporate Policy actually works a little differently.
Each policy actually describes two positions in the password, where 1 means the first character, 2 means the second character, and so on. (Be careful; Toboggan Corporate Policies have no concept of "index zero"!) Exactly one of these positions must contain the given letter. Other occurrences of the letter are irrelevant for the purposes of policy enforcement.
Given the same example list from above:
1-3 a: abcde is valid: position 1 contains a and position 3 does not.
1-3 b: cdefg is invalid: neither position 1 nor position 3 contains b.
2-9 c: ccccccccc is invalid: both position 2 and position 9 contain c.
How many passwords are valid according to the new interpretation of the policies?