diff --git a/2020/day03/input.txt b/2020/day03/input.txt new file mode 100755 index 0000000..e1bd0f6 --- /dev/null +++ b/2020/day03/input.txt @@ -0,0 +1,323 @@ +.#.......#...........#......... +..##.......#.#.#.....##...#.... +.......#..#.....#...#.......... +...#..........###...#........## +#.#..#.#.##.#........#.#.....#. +#..#....#..#....#.............. +#..#........#.................. +..#.#...#.#...#....#.#.#..#.... +..............#..#............. +.##....#...................#... +........#..........#......#...# +.##..#..#...##..........#...#.. +.#...#....#.........#...#.....# +.#........##............#.#.... +...........#..............##... +.#..#......#..#..............#. +..#.#.#...........#........#... +..###..........#....#.#......#. +.......#...##..........#....... +........#...#.................. +....#....#..#.......#........#. +.......##.#......#.....#...##.. +..#.#........................#. +.#.....#.##..............#.#... +..#.#...#.#..#....#....#....... +.#....##.....#....#........#... +..#...........#.##....#...#.... +..#.##...#....#.#.....##...#... +.......#...####...#...#.......# +.#...#......................... +.......................#....... +.....#.#.........#..........#.# +#.........#............###..#.. +.....#.#.............###....... +...#..#........#.#.......#..... +...................#....#...... +...#..#...#............#..##... +...#.....#....#.......##......# +.....#....#...##..#..#...#...#. +..........#...........#.#.#.... +..#.......#...#.....#......#... +.........#.......##......#..#.# +..#.....#..#.###...#.#......#.. +#....#...#..#...#.....#........ +..#......#..#.......#.#.....#.. +#......#...#......#.....##.#... +........##.......#.......#..... +.#.#...............#........... +..............#...#.#....#..... +....#......#.#..#......#....... +...##....#....#...#............ +.#...............#...........#. +.#.#...#.#.....#.....#...#.#... +...##...........#.....#..#...#. +.#.#...##.#.#......#......#.... +.##.....#.......##....#.#.#.... +.......#...........#....#....#. +....#...........#......#.####.. +......#....#...#...##.......#.. +......................#.#####.. +..#...#.#...#..#..#......#..... +....#........##.......##....#.. +#.#......##.........##.#..#...# +.#.#....#...#..#...#...##....#. +.....##...#....#....#.#........ +......#..#....#.#...#.......... +.........#...................#. +............#.###....#.#....... +...#.#.....#......#....#.#..#.. +..............#..#.#.#.#....... +#..##...................##..... +..#.......#..#.........##..#... +.........##...#......#........# +..#.........#........##.###.#.. +...........#.#....#.....###.... +..#....##.#..#.##....#.....##.. +..#.....#.##..................# +#....#......................... +..............#..#...#.#....... +......#..#.#.##....#..........# +..#.........#.####.....#....... +......#..#.#..........#...#.... +......#.................#..#.#. +.....#..........#.............. +....#.....#............#....##. +.....#.....#........#.......... +............#.....#...#........ +........#....#.#............... +#.....#.........#......#..#.#.. +...#..#......#......#.......#.. +.....#......#.#....#..#...#...# +......................#..##.... +.............#.........###....# +#..............#.#..........##. +...#.#.................##...... +...........#.#.....#........... +.........#.................#.#. +........#........#...#..##...#. +........#......##.......###.... +..............#.#.#............ +.#.....###...##.#......#.....#. +.............#......#.#.#...#.# +..#.........#.......#.....#.... +......#........#...##......#... +.##..........##......#.#.....#. +..#.##....#....#............... +......#...#..#.....#.....#...#. +.......##..##..#............##. +..............#...##........#.. +#....#................#..#..... +........#.......#.#.#...#...... +......#.......#..............#. +#.#..#...#........#....#..####. +..#........#...........#.....#. +.##...........................# +.............#...........#..... +.#.....#.#...#.........#....... +..........#...#....#....#...... +.#..#........##....#........... +.......###......##...#......... +..........#.#.#..#.#....#...... +........##..#.........#....#... +........#.#......#.#...#.#..#.. +....#....................#.##.. +##....#..#...........#.....#.#. +...#..............##...##..#.#. +......#.##.#.......#..#...#.... +....#..#..##.....#.....#.#....# +.......#....##.##.............. +#..##....#.....#.#............. +..................#......#..#.. +..#......#...#..#.......#...#.. +...........#....#.#.....#...... +#..#...##.........###..#......# +.......#......................# +#.......#....................#. +..#..#..........#..#..#....#... +.##..#..#.....#.#..##.......... +#..###.......#..##..#...#..#.#. +.....##......###.....#.#.##...# +..............#...#....#.#..... +#...........#.................. +..............#....#..##..#..#. +.........#.............#....... +.#.#....#....#...............## +.##.##.#.....###.....#......... +....#..............##......#... +....#........##................ +....#.....#....#....##....##... +.#........#......#......#...... +....#..........#............... +##..........#......#.....#..... +........#.#..#.#..#.....##..... +..##......#.#.......#.#..#..... +.#.......#......#...........#.. +..#.#..#.#..................#.. +...#...#...#...##......#....... +.#...##....#...#...#...#....... +.......#.#.......#............. +.#.##.#.....#...........#.##.#. +.#.##.#........#...##.......... +.#.....#.....#....#..#......... +...##.............##........... +.#........##.....#.......#...#. +...........#..#..##........##.. +.....#..#...................... +..#.......#.................... +.....#......#....#....#.......# +........#..#.#.....#......#.... +..........#..#.....#......#.... +..........#####.....#........#. +........#..#...#.#....#......#. +.........#...#....#.#.......... +......#....##..........#...#... +#..............###.#.#......... +.#.#............##......#.#..#. +......#........................ +...#..#......#.......#....#...# +.......#....##.....#.#......##. +...........#..........#..#..... +...........#..#.....###......#. +.......#....#..##......#....... +.........#.#.#.......#..#...#.. +.......#.......##.....##...#... +..............#....#.....#..... +...#....#.....#.#..........##.. +###.........#.............#.... +...##......#.#........#....#..# +#....###.......#...#.#......##. +....#...##.......#......#.....# +.....#......#.................. +#........##....#....#.#........ +........#.......###...#........ +........#..#.......###......... +..............#......#..#...... +#......#.....#....#.#.......... +.#......##.#.#.....#...#.#....# +.##...........#..#.##.....#.... +.....#.....................#... +.#..#...#...##.#...#........... +.......#.......##..#.#..#...... +.......##.....#.....#.......... +.................#............. +#........#..#.......##......... +#...#..###.#..#....#.#.###..... +..#.......#.......#.......#.... +..............#............##.. +.#...#..#...##.........#....#.. +#...........#...#.............. +.......#.....#......#..#.....#. +..........#......#............. +##.........###..##.#....#..#.#. +..............###.............. +#..##.............##.....#..... +....##...................#..#.. +....#.....#..............#..#.# +........#........##...#.....##. +#...........#.##..........##... +#......##.....#...............# +..##..#....#.................#. +#.......##..................... +...............#.##..##......#. +..#.##..#.#....#.......##...... +......##....#............##.... +.#..#..##.....#.##....#........ +#.........#..........#...#....# +...#.......#.............#.#.#. +..##............#...##........# +.......#.#.#........#.......... +.....#.............#.....#..... +.........#.........#.........#. +#.....#....#.......#........... +.........#....#.............#.# +.##..#.......#...#......#...... +....#....#....#........#....#.. +............#.......#..#......# +.#............#.##........##... +..#...##...#....#...#.#...#..#. +#...#..........#..##.........#. +..#.........................#.# +...........#.........#..#.##... +.#..................#..#....... +......#......#...........#..#.. +...##.....#.....#..#.......#... +.........#.#.......#......#.... +...........#................#.. +.....#...#..#............##.... +.#.......#..#....#..........#.. +#.....#..#.....#..##.......##.. +...#.......#...#....#...#.#..## +...#...##......#....#....#..... +.......###.#..#.......#......#. +........#.#...#..#..#...#....#. +....#.........##.#.....#....... +....#.........#..##........#... +..#...........#......#....#.##. +.....................#......... +...................##......#..# +......#.#.....##..##..........# +..#.##........#.#.#..........#. +.#.......#...##.#....#....#.... +#.#......#..#..#.......#....... +.............#........#.......# +....#...#.....#........#...#... +..#..............##..#......... +..#.................#..#...##.. +....#..#...#................... +......#.........##.#..#..#...## +........#..#....#.......#.#.##. +.#...#...........#..........#.. +##.....#...#............##...#. +.##.....#...#.................. +.#.......####.#..##.##.#......# +.............#...#..#..#....... +...#.##.........#.#....#....... +...........##...##....#....##.. +........#......#...#........... +...........#..#...#....#.##.... +..##....#..........#....#...#.. +#....#.#.#.......#.#........... +......#............##.......... +#.#.###..#....#.......#...#.... +.#......##..#..#.#.........#..# +..#.........#........#....#.... +......##.#.......##....#..#..## +.............#...#............# +......#......#...#.#.#.##.#.... +#.#...#.##.....#..............# +..........#.............##.##.. +#......#....#...#.#.#.#..#....# +........#........#...#.#......# +.....#...........#............. +...........#....#..........#... +....####...#..##....#.#........ +.#......#...#..#...........#... +#......###..#.##.###........... +..#...........#.........#....#. +................#.#....#..#.##. +...................#......#.... +....#.#.....#.......#...###.##. +.#........#.#....#...#..#...#.. +....#..###.................#..# +.....#.#..#........#......#..#. +....#.....#...............#...# +............##.#.........#..#.. +.......#..#..##.#.#...##....... +..#..........##..#..#........#. +..............#..#...#......... +......#.#....#........##....... +.#.....##....#..#...#.......##. +..............#.##............. +#..#..#...##....##.#.....##.#.. +..#...###..#.........##........ +........##......#.....#..###... +.....#......##.###............. +....#.....#.#..#.#..#.......... +....#..#.......#...........#... +.#.............#..#......##.... +..#.#......#.#................. +.......#.#.#............#..#... +......###....##............#..# +.........#....#......#......... +..........#...............#..#. diff --git a/2020/day03/main.go b/2020/day03/main.go index 6058282..b67a668 100644 --- a/2020/day03/main.go +++ b/2020/day03/main.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "strings" "github.com/alexchao26/advent-of-code-go/util" ) @@ -15,17 +16,73 @@ func main() { if part == 1 { ans := part1(util.ReadFile("./input.txt")) + util.CopyToClipboard(fmt.Sprintf("%v", ans)) fmt.Println("Output:", ans) } else { ans := part2(util.ReadFile("./input.txt")) + util.CopyToClipboard(fmt.Sprintf("%v", ans)) fmt.Println("Output:", ans) } } func part1(input string) int { - return 0 + grid := parseInput(input) + return rideSlopes(grid, 3, 1) } func part2(input string) int { - return 0 + grid := parseInput(input) + + slopes := [][2]int{ + // right, down + {1, 1}, + {3, 1}, + {5, 1}, + {7, 1}, + {1, 2}, + } + + ans := 1 + for _, slope := range slopes { + ans *= rideSlopes(grid, slope[0], slope[1]) + } + + return ans +} + +func parseInput(input string) (grid [][]bool) { + lines := strings.Split(input, "\n") + + grid = make([][]bool, len(lines)) + for i, l := range lines { + grid[i] = make([]bool, len(l)) + for j, v := range l { + if v == '#' { + grid[i][j] = true + } + } + } + + return grid +} + +func rideSlopes(grid [][]bool, right, down int) int { + var row, col int + var ans int + + for { + row += down + col += right + col %= len(grid[0]) + + if row < len(grid) { + if grid[row][col] { + ans++ + } + } else { + break + } + } + + return ans } diff --git a/2020/day03/main_test.go b/2020/day03/main_test.go index e267bfa..585779b 100644 --- a/2020/day03/main_test.go +++ b/2020/day03/main_test.go @@ -1,19 +1,33 @@ package main -import "testing" +import ( + "testing" + + "github.com/alexchao26/advent-of-code-go/util" +) var tests1 = []struct { name string want int input string - // add extra args if needed }{ - // {"actual", ACTUAL_ANSWER, util.ReadFile("input.txt")}, + {"example", 7, `..##....... +#...#...#.. +.#....#..#. +..#.#...#.# +.#...##..#. +..#.##..... +.#.#.#....# +.#........# +#.##...#... +#...##....# +.#..#...#.#`}, + {"actual", 209, util.ReadFile("input.txt")}, } func TestPart1(t *testing.T) { for _, test := range tests1 { - t.Run(test.name, func(*testing.T) { + t.Run(test.name, func(t *testing.T) { got := part1(test.input) if got != test.want { t.Errorf("got %v, want %v", got, test.want) @@ -26,14 +40,24 @@ var tests2 = []struct { name string want int input string - // add extra args if needed }{ - // {"actual", ACTUAL_ANSWER, util.ReadFile("input.txt")}, + {"example", 336, `..##....... +#...#...#.. +.#....#..#. +..#.#...#.# +.#...##..#. +..#.##..... +.#.#.#....# +.#........# +#.##...#... +#...##....# +.#..#...#.#`}, + {"actual", 1574890240, util.ReadFile("input.txt")}, } func TestPart2(t *testing.T) { for _, test := range tests2 { - t.Run(test.name, func(*testing.T) { + t.Run(test.name, func(t *testing.T) { got := part2(test.input) if got != test.want { t.Errorf("got %v, want %v", got, test.want) diff --git a/util/CopyToClipboard.go b/util/CopyToClipboard.go new file mode 100644 index 0000000..2e402d4 --- /dev/null +++ b/util/CopyToClipboard.go @@ -0,0 +1,24 @@ +package util + +import ( + "bytes" + "fmt" + "os/exec" +) + +// CopyToClipboard is for macOS +func CopyToClipboard(text string) error { + command := exec.Command("pbcopy") + command.Stdin = bytes.NewReader([]byte(text)) + + if err := command.Start(); err != nil { + return fmt.Errorf("error starting pbcopy command: %w", err) + } + + err := command.Wait() + if err != nil { + return fmt.Errorf("error running pbcopy %w", err) + } + + return nil +} diff --git a/util/CopyToClipboard_test.go b/util/CopyToClipboard_test.go new file mode 100644 index 0000000..1a3267b --- /dev/null +++ b/util/CopyToClipboard_test.go @@ -0,0 +1,11 @@ +package util + +import "testing" + +// not a real test... used o run copy to clipboard without a main package +func TestCopyToClipboard(t *testing.T) { + err := CopyToClipboard("asdfqwert") + if err != nil { + t.Errorf("Unexpected error while running CopyToClipboard: %w", err) + } +}