Skip to content

Commit 32b3e0f

Browse files
day 04
1 parent ad49d2a commit 32b3e0f

File tree

11 files changed

+1240
-89
lines changed

11 files changed

+1240
-89
lines changed

day01/day01.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package day01
22

33
import (
44
"github.com/alokmenghrajani/adventofcode2020/utils"
5+
"strings"
56
)
67

78
// Inefficiently loop over entire dataset twice
8-
func Part1(input []string) int {
9-
numbers := utils.StringsToInts(input)
9+
func Part1(input string) int {
10+
lines := strings.Split(input, "\n")
11+
numbers := utils.StringsToInts(lines)
1012

1113
for i := 0; i < len(numbers); i++ {
1214
for j := i + 1; j < len(numbers); j++ {
@@ -19,8 +21,9 @@ func Part1(input []string) int {
1921
}
2022

2123
// Inefficiently loop over entire dataset thrice
22-
func Part2(input []string) int {
23-
numbers := utils.StringsToInts(input)
24+
func Part2(input string) int {
25+
lines := strings.Split(input, "\n")
26+
numbers := utils.StringsToInts(lines)
2427

2528
for i := 0; i < len(numbers); i++ {
2629
for j := i + 1; j < len(numbers); j++ {

day01/day01_test.go

+23-31
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,40 @@ import (
66
)
77

88
func TestPart1(t *testing.T) {
9-
r := Part1([]string{
10-
"1721",
11-
"979",
12-
"366",
13-
"299",
14-
"675",
15-
"1456",
16-
})
9+
r := Part1(`1721
10+
979
11+
366
12+
299
13+
675
14+
1456`)
1715
assert.Equal(t, 514579, r)
1816
}
1917

2018
func TestPart1_edgecase(t *testing.T) {
21-
r := Part1([]string{
22-
"1010",
23-
"2019",
24-
"1",
25-
})
19+
r := Part1(`1010
20+
2019
21+
1`)
2622
assert.Equal(t, 2019, r)
2723
}
2824

2925
func TestPart2(t *testing.T) {
30-
r := Part2([]string{
31-
"1721",
32-
"979",
33-
"366",
34-
"299",
35-
"675",
36-
"1456",
37-
})
26+
r := Part2(`1721
27+
979
28+
366
29+
299
30+
675
31+
1456`)
3832
assert.Equal(t, 241861950, r)
3933
}
4034

4135
func TestPart2_edgecase(t *testing.T) {
42-
r := Part2([]string{
43-
"1009",
44-
"2",
45-
"1721",
46-
"979",
47-
"366",
48-
"299",
49-
"675",
50-
"1456",
51-
})
36+
r := Part2(`1009
37+
2
38+
1721
39+
979
40+
366
41+
299
42+
675
43+
1456`)
5244
assert.Equal(t, 241861950, r)
5345
}

day02/day02.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"strings"
77
)
88

9-
func Part1(input []string) int {
9+
func Part1(input string) int {
10+
lines := strings.Split(input, "\n")
11+
1012
type line struct {
1113
Min int
1214
Max int
@@ -17,7 +19,7 @@ func Part1(input []string) int {
1719

1820
valid := 0
1921

20-
for _, l := range input {
22+
for _, l := range lines {
2123
var match line
2224
utils.ParseToStruct(re, l, &match)
2325

@@ -29,7 +31,9 @@ func Part1(input []string) int {
2931
return valid
3032
}
3133

32-
func Part2(input []string) int {
34+
func Part2(input string) int {
35+
lines := strings.Split(input, "\n")
36+
3337
type line struct {
3438
Offset1 int
3539
Offset2 int
@@ -39,7 +43,7 @@ func Part2(input []string) int {
3943
re := regexp.MustCompile(`(\d+)-(\d+) (.): (.*)`)
4044

4145
valid := 0
42-
for _, l := range input {
46+
for _, l := range lines {
4347
var match line
4448
utils.ParseToStruct(re, l, &match)
4549

day02/day02_test.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,15 @@ import (
66
)
77

88
func TestPart1(t *testing.T) {
9-
r := Part1([]string{
10-
"1-3 a: abcde",
11-
"1-3 b: cdefg",
12-
"2-9 c: ccccccccc",
13-
})
9+
r := Part1(`1-3 a: abcde
10+
1-3 b: cdefg
11+
2-9 c: ccccccccc`)
1412
assert.Equal(t, 2, r)
1513
}
1614

1715
func TestPart2(t *testing.T) {
18-
r := Part2([]string{
19-
"1-3 a: abcde",
20-
"1-3 b: cdefg",
21-
"2-9 c: ccccccccc",
22-
})
16+
r := Part2(`1-3 a: abcde
17+
1-3 b: cdefg
18+
2-9 c: ccccccccc`)
2319
assert.Equal(t, 1, r)
2420
}

day03/day03.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package day03
22

3-
import "github.com/alokmenghrajani/adventofcode2020/utils"
3+
import (
4+
"github.com/alokmenghrajani/adventofcode2020/utils"
5+
"strings"
6+
)
47

5-
func Part1(input []string) uint {
6-
grid := utils.Grid(input)
8+
func Part1(input string) uint {
9+
lines := strings.Split(input, "\n")
10+
grid := utils.Grid(lines)
711
return collisions(grid, 3, 1)
812
}
913

10-
func Part2(input []string) uint {
11-
grid := utils.Grid(input)
14+
func Part2(input string) uint {
15+
lines := strings.Split(input, "\n")
16+
grid := utils.Grid(lines)
1217
deltas := [][]int{
1318
{1, 1},
1419
{3, 1},

day03/day03_test.go

+22-26
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,31 @@ import (
66
)
77

88
func TestPart1(t *testing.T) {
9-
r := Part1([]string{
10-
"..##.......",
11-
"#...#...#..",
12-
".#....#..#.",
13-
"..#.#...#.#",
14-
".#...##..#.",
15-
"..#.##.....",
16-
".#.#.#....#",
17-
".#........#",
18-
"#.##...#...",
19-
"#...##....#",
20-
".#..#...#.#",
21-
})
9+
r := Part1(`..##.......
10+
#...#...#..
11+
.#....#..#.
12+
..#.#...#.#
13+
.#...##..#.
14+
..#.##.....
15+
.#.#.#....#
16+
.#........#
17+
#.##...#...
18+
#...##....#
19+
.#..#...#.#`)
2220
assert.Equal(t, uint(7), r)
2321
}
2422

2523
func TestPart2(t *testing.T) {
26-
r := Part2([]string{
27-
"..##.......",
28-
"#...#...#..",
29-
".#....#..#.",
30-
"..#.#...#.#",
31-
".#...##..#.",
32-
"..#.##.....",
33-
".#.#.#....#",
34-
".#........#",
35-
"#.##...#...",
36-
"#...##....#",
37-
".#..#...#.#",
38-
})
24+
r := Part2(`..##.......
25+
#...#...#..
26+
.#....#..#.
27+
..#.#...#.#
28+
.#...##..#.
29+
..#.##.....
30+
.#.#.#....#
31+
.#........#
32+
#.##...#...
33+
#...##....#
34+
.#..#...#.#`)
3935
assert.Equal(t, uint(336), r)
4036
}

day04/day04.go

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package day04
2+
3+
import (
4+
"regexp"
5+
"strconv"
6+
"strings"
7+
)
8+
9+
func Part1(input string) uint {
10+
passports := strings.Split(input, "\n\n")
11+
valid := uint(0)
12+
for _, passport := range passports {
13+
// easier to remove newlines than deal with multiline regexp :P
14+
passport = strings.ReplaceAll(passport, "\n", " ")
15+
current := map[string]bool{}
16+
re := regexp.MustCompile(`(...):([^ ]+)`)
17+
matches := re.FindAllStringSubmatch(passport, -1)
18+
for _, match := range matches {
19+
if match[1] == "cid" {
20+
continue
21+
}
22+
current[match[1]] = true
23+
}
24+
if len(current) == 7 {
25+
valid++
26+
}
27+
}
28+
return valid
29+
}
30+
31+
func Part2(input string) uint {
32+
passports := strings.Split(input, "\n\n")
33+
valid := uint(0)
34+
for _, passport := range passports {
35+
// easier to remove newlines than deal with multiline regexp :P
36+
passport = strings.ReplaceAll(passport, "\n", " ")
37+
current := map[string]bool{}
38+
re := regexp.MustCompile(`(...):([^ ]+)`)
39+
matches := re.FindAllStringSubmatch(passport, -1)
40+
for _, match := range matches {
41+
key := match[1]
42+
val := match[2]
43+
if key == "cid" {
44+
continue
45+
}
46+
if key == "byr" {
47+
t, err := strconv.Atoi(val)
48+
if err != nil || t < 1920 || t > 2002 {
49+
continue
50+
}
51+
}
52+
if key == "iyr" {
53+
t, err := strconv.Atoi(val)
54+
if err != nil || t < 2010 || t > 2020 {
55+
continue
56+
}
57+
}
58+
if key == "eyr" {
59+
t, err := strconv.Atoi(val)
60+
if err != nil || t < 2020 || t > 2030 {
61+
continue
62+
}
63+
}
64+
if key == "hgt" {
65+
if strings.HasSuffix(val, "cm") {
66+
t, err := strconv.Atoi(val[:len(val)-2])
67+
if err != nil || t < 150 || t > 193 {
68+
continue
69+
}
70+
} else if strings.HasSuffix(val, "in") {
71+
t, err := strconv.Atoi(val[:len(val)-2])
72+
if err != nil || t < 59 || t > 76 {
73+
continue
74+
}
75+
} else {
76+
continue
77+
}
78+
}
79+
if key == "hcl" {
80+
re2 := regexp.MustCompile(`^#[0-9a-f]{6}$`)
81+
if !re2.MatchString(val) {
82+
continue
83+
}
84+
}
85+
if key == "ecl" {
86+
switch val {
87+
case "amb",
88+
"blu",
89+
"brn",
90+
"gry",
91+
"grn",
92+
"hzl",
93+
"oth":
94+
default:
95+
continue
96+
}
97+
}
98+
if key == "pid" {
99+
re2 := regexp.MustCompile(`^[0-9]{9}$`)
100+
if !re2.MatchString(val) {
101+
continue
102+
}
103+
}
104+
105+
current[key] = true
106+
}
107+
if len(current) == 7 {
108+
valid++
109+
}
110+
}
111+
return valid
112+
}

0 commit comments

Comments
 (0)