Skip to content

Commit 575f389

Browse files
Minor bugs
1 parent 68ef972 commit 575f389

File tree

9 files changed

+117
-115
lines changed

9 files changed

+117
-115
lines changed

β€Žday01/day01.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package day01
22

33
import (
4-
"github.com/alokmenghrajani/adventofcode2020/utils"
54
"regexp"
65
"strings"
6+
7+
"github.com/alokmenghrajani/adventofcode2020/utils"
78
)
89

910
type number struct {
@@ -16,7 +17,7 @@ func Part1(input string) int {
1617
var numbers []number
1718
for _, line := range lines {
1819
var n number
19-
re := regexp.MustCompile(`\d+`)
20+
re := regexp.MustCompile(`(\d+)`)
2021
if utils.ParseToStruct(re, line, &n) {
2122
numbers = append(numbers, n)
2223
}
@@ -38,7 +39,7 @@ func Part2(input string) int {
3839
var numbers []number
3940
for _, line := range lines {
4041
var n number
41-
re := regexp.MustCompile(`\d+`)
42+
re := regexp.MustCompile(`(\d+)`)
4243
if utils.ParseToStruct(re, line, &n) {
4344
numbers = append(numbers, n)
4445
}

β€Žday03/day03.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
)
77

88
func Part1(input string) uint {
9-
grid := inputs.ToGrid(input)
9+
grid := inputs.ToGrid(input, '.')
1010
return collisions(grid, 3, 1)
1111
}
1212

1313
func Part2(input string) uint {
14-
grid := inputs.ToGrid(input)
14+
grid := inputs.ToGrid(input, '.')
1515
deltas := [][]int{
1616
{1, 1},
1717
{3, 1},

β€Žday11/day11.go

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import (
77
)
88

99
func Part1(input string) int {
10-
grid := inputs.ToGrid(input)
10+
grid := inputs.ToGrid(input, 'L')
1111
_, maxX := grid.SizeX()
1212
_, maxY := grid.SizeY()
1313

1414
for done := false; !done; {
1515
done = true
16-
nextGrid := grids.NewGrid(0)
16+
nextGrid := grids.NewGrid('L')
1717

1818
for i := 0; i <= maxX; i++ {
1919
for j := 0; j <= maxY; j++ {
@@ -60,13 +60,13 @@ func countPart1(grid *grids.Grid, x, y int) int {
6060
}
6161

6262
func Part2(input string) int {
63-
grid := inputs.ToGrid(input)
63+
grid := inputs.ToGrid(input, 'L')
6464
_, maxX := grid.SizeX()
6565
_, maxY := grid.SizeY()
6666

6767
for done := false; !done; {
6868
done = true
69-
nextGrid := grids.NewGrid(0)
69+
nextGrid := grids.NewGrid('L')
7070

7171
for i := 0; i <= maxX; i++ {
7272
for j := 0; j <= maxY; j++ {
@@ -111,40 +111,11 @@ func countPart2(grid *grids.Grid, x, y int) int {
111111
if v == '#' {
112112
r++
113113
break
114-
} else if v != '.' {
114+
} else if v == 'L' {
115115
break
116116
}
117117
}
118118
}
119119
}
120120
return r
121121
}
122-
123-
//
124-
// for _, line := range strings.Split(input, "\n") {
125-
// fmt.Println(line)
126-
// }
127-
// // d := 1
128-
// // for {
129-
// // ty := y + i*d
130-
// // tx := x + j*d
131-
// // if ty < 0 || ty >= len(grid) || tx < 0 || tx >= len(grid[0]) {
132-
// // break
133-
// // }
134-
// // if grid[ty][tx] == '#' {
135-
// // r++
136-
// // break
137-
// // }
138-
// // if grid[ty][tx] == 'L' {
139-
// // break
140-
// // }
141-
// // d++
142-
// // }
143-
// //}
144-
// //}
145-
// //return r
146-
// //numbers := inputs.ToInts(input)
147-
// //sort.Slice(numbers, func(i, j int) bool { return numbers[i] < numbers[j] })
148-
//
149-
// return 0
150-
//}

β€Žday13/day13.go

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ package day13
33
import (
44
"fmt"
55
"math"
6+
"math/big"
67
"strings"
78

89
"github.com/alokmenghrajani/adventofcode2020/utils"
910
)
1011

1112
func Part1(input string) int {
1213
lines := strings.Split(input, "\n")
13-
//soonest := utils.MustAtoi(lines[0])
14+
soonest := utils.MustAtoi(lines[0])
1415
scheduleString := strings.Split(lines[1], ",")
1516
var schedule []int
1617
for _, s := range scheduleString {
@@ -21,41 +22,55 @@ func Part1(input string) int {
2122
schedule = append(schedule, utils.MustAtoi(s))
2223
}
2324

24-
//// find id and at what time
25-
//when := map[int]int{}
26-
//for _, v := range schedule {
27-
// t := int(math.Ceil(float64(soonest)/float64(v))) * v
28-
// when[v] = t
29-
//}
30-
//
31-
32-
last := schedule[len(schedule)-1]
33-
i := int(math.Ceil(float64(100000000000000)/float64(last))) * last
34-
j := 0
35-
for {
36-
ok := true
37-
j = i - (len(schedule) - 1)
38-
for offset, v := range schedule {
39-
if v == 0 {
40-
continue
41-
}
42-
t := int(math.Ceil(float64(j+offset)/float64(v))) * v
43-
if t != j+offset {
44-
ok = false
45-
break
46-
}
47-
}
48-
if ok {
49-
break
25+
// find id and at what time
26+
when := map[int]int{}
27+
for _, v := range schedule {
28+
if v == 0 {
29+
continue
5030
}
51-
i += last
31+
t := int(math.Ceil(float64(soonest)/float64(v))) * v
32+
when[v] = t
5233
}
53-
return j
34+
35+
k := utils.MapFindMin(when).(int)
36+
return k * (when[k] - soonest)
5437
}
5538

5639
func Part2(input string) int {
57-
for _, line := range strings.Split(input, "\n") {
58-
fmt.Printf("%s\n", line)
40+
lines := strings.Split(input, "\n")
41+
42+
scheduleString := strings.Split(lines[1], ",")
43+
var crtA []*big.Int
44+
var crtN []*big.Int
45+
for offset, s := range scheduleString {
46+
if s == "x" {
47+
continue
48+
}
49+
v := utils.MustAtoi(s)
50+
crtA = append(crtA, big.NewInt(int64(v-offset%v)))
51+
crtN = append(crtN, big.NewInt(int64(v)))
52+
}
53+
54+
r, err := crt(crtA, crtN)
55+
utils.PanicOnErr(err)
56+
return int(r.Int64())
57+
}
58+
59+
var one = big.NewInt(1)
60+
61+
func crt(a, n []*big.Int) (*big.Int, error) {
62+
p := new(big.Int).Set(n[0])
63+
for _, n1 := range n[1:] {
64+
p.Mul(p, n1)
65+
}
66+
var x, q, s, z big.Int
67+
for i, n1 := range n {
68+
q.Div(p, n1)
69+
z.GCD(nil, &s, n1, &q)
70+
if z.Cmp(one) != 0 {
71+
return nil, fmt.Errorf("%d not coprime", n1)
72+
}
73+
x.Add(&x, s.Mul(a[i], s.Mul(&s, &q)))
5974
}
60-
return 0
75+
return x.Mod(&x, p), nil
6176
}

β€Žday13/day13_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import (
99
func TestPart1(t *testing.T) {
1010
r := Part1(`939
1111
7,13,x,x,59,x,31,19`)
12-
require.Equal(t, 1068781, r)
12+
require.Equal(t, 295, r)
1313
}
1414

1515
func TestPart2(t *testing.T) {
16-
r := Part2(``)
17-
require.Equal(t, 0, r)
16+
r := Part2(`0
17+
7,13,x,x,59,x,31,19`)
18+
require.Equal(t, 1068781, r)
1819
}

β€Žday14/day14.go

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,50 @@ import (
88
)
99

1010
func Part1(input string) int {
11+
mask := ""
12+
memory := map[int]int{}
13+
for _, line := range strings.Split(input, "\n") {
14+
re1 := regexp.MustCompile(`mask = (.*)$`)
15+
match := re1.FindStringSubmatch(line)
16+
if len(match) > 0 {
17+
mask = match[1]
18+
continue
19+
}
20+
21+
re2 := regexp.MustCompile(`mem\[(\d+)\] = (\d+)$`)
22+
match = re2.FindStringSubmatch(line)
23+
if len(match) > 0 {
24+
memory[utils.MustAtoi(match[1])] = compute(mask, utils.MustAtoi(match[2]))
25+
} else {
26+
panic("here")
27+
}
28+
}
29+
30+
sum := 0
31+
for _, v := range memory {
32+
sum += v
33+
}
34+
return sum
35+
}
36+
37+
func compute(mask string, v int) int {
38+
for i := 0; i < len(mask); i++ {
39+
if mask[i] == 'X' {
40+
continue
41+
}
42+
if mask[i] == '0' {
43+
m := ^(1 << (35 - i))
44+
v = v & m
45+
}
46+
if mask[i] == '1' {
47+
m := 1 << (35 - i)
48+
v = v | m
49+
}
50+
}
51+
return v
52+
}
53+
54+
func Part2(input string) int {
1155
mask := ""
1256
memory := map[int]int{}
1357
for _, line := range strings.Split(input, "\n") {
@@ -63,36 +107,3 @@ func compute2(mask string, v int, offset int) []int {
63107
}
64108
panic("unreach")
65109
}
66-
67-
func compute(mask string, v int) int {
68-
for i := 0; i < len(mask); i++ {
69-
if mask[i] == 'X' {
70-
continue
71-
}
72-
if mask[i] == '0' {
73-
m := ^(1 << (35 - i))
74-
v = v & m
75-
}
76-
if mask[i] == '1' {
77-
m := 1 << (35 - i)
78-
v = v | m
79-
}
80-
}
81-
return v
82-
}
83-
84-
// // mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
85-
// //mem[8] = 11
86-
// //mem[7] = 101
87-
// //mem[8] = 0
88-
// fmt.Println(line)
89-
// }
90-
// return 0
91-
//}
92-
//
93-
////func Part2(input string) int {
94-
//// for _, line := range strings.Split(input, "\n") {
95-
//// fmt.Printf("%s\n", line)
96-
//// }
97-
//// return 0
98-
////}

β€Žday14/day14_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ mem[8] = 0`)
1414
require.Equal(t, 165, r)
1515
}
1616

17-
//func TestPart2(t *testing.T) {
18-
// r := Part2(``)
19-
// require.Equal(t, 0, r)
20-
//}
17+
func TestPart2(t *testing.T) {
18+
r := Part2(`mask = 000000000000000000000000000000X1001X
19+
mem[42] = 100
20+
mask = 00000000000000000000000000000000X0XX
21+
mem[26] = 1`)
22+
require.Equal(t, 208, r)
23+
}

β€Žmain.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ func main() {
7171
fmt.Printf("part 2: %d\n", day12.Part2(utils.Readfile(d)))
7272
case 13:
7373
fmt.Printf("part 1: %d\n", day13.Part1(utils.Readfile(d)))
74-
//fmt.Printf("part 2: %d\n", day13.Part2(utils.Readfile(d)))
74+
fmt.Printf("part 2: %d\n", day13.Part2(utils.Readfile(d)))
7575
case 14:
7676
fmt.Printf("part 1: %d\n", day14.Part1(utils.Readfile(d)))
77-
//fmt.Printf("part 2: %d\n", day14.Part2(utils.Readfile(d)))
77+
fmt.Printf("part 2: %d\n", day14.Part2(utils.Readfile(d)))
7878
case 15:
7979
fmt.Printf("part 1: %d\n", day15.Part1(utils.Readfile(d)))
8080
fmt.Printf("part 2: %d\n", day15.Part2(utils.Readfile(d)))

β€Žutils/inputs/inputs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ func ToInts(input string) []int {
1616
return r
1717
}
1818

19-
func ToGrid(input string) *grids.Grid {
20-
grid := grids.NewGrid(0)
19+
func ToGrid(input string, empty interface{}) *grids.Grid {
20+
grid := grids.NewGrid(empty)
2121

2222
for y, line := range strings.Split(input, "\n") {
2323
for x, rune := range line {

0 commit comments

Comments
Β (0)