diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7a4c68b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +template/ diff --git a/README.md b/README.md index 23fa45b..d6d8ed4 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ My (attempted) solutions to [Advent of Code 2020](https://adventofcode.com/2020) in Elixir. -image +image ## Strategy diff --git a/day3/README b/day3/README new file mode 100644 index 0000000..18ab134 --- /dev/null +++ b/day3/README @@ -0,0 +1,34 @@ +Day 3 Notes + ++--------+ +| Part 1 | ++--------| + +$ elixir day3part1.exs +Travelled (969, 323) and encountereed 270 trees + +Thoughts: + +Input repeats, so use Stream.cycle/2. +Need to keep track of the x-offset, pass it to Enum.at/2. + + ++--------+ +| Part 2 | ++--------| + +$ elixir day3part2.exs +2122848000 + +Thoughts: + +Modify original answer to take x and y velocity. +Can just pass x velocity in as before. +Use Enum.take_every/2 to skip unneeded rows for y velocity. + ++------------------+ +| Overall Thoughts | ++------------------+ + +Pretty simple, because Stream.cycle/2 takes care of the tricky bit. +Thankfully I answered part 1 in a way that made part 2 just a small change to add Enum.take_every/2. diff --git a/day3/day3part1.exs b/day3/day3part1.exs new file mode 100644 index 0000000..68732fa --- /dev/null +++ b/day3/day3part1.exs @@ -0,0 +1,22 @@ +defmodule Day3Part1 do + def run do + {x, y, trees} = + File.read!("input") + |> String.trim() + |> String.split("\n") + |> Enum.map(&String.to_charlist/1) + |> Enum.reduce({0, 0, 0}, fn row, {x, y, trees} -> + trees = + case Stream.cycle(row) |> Enum.at(x) do + ?# -> trees + 1 + ?. -> trees + end + + {x + 3, y + 1, trees} + end) + + IO.puts("Travelled (#{x}, #{y}) and encountereed #{trees} trees") + end +end + +Day3Part1.run() diff --git a/day3/day3part2.exs b/day3/day3part2.exs new file mode 100644 index 0000000..608c894 --- /dev/null +++ b/day3/day3part2.exs @@ -0,0 +1,34 @@ +defmodule Day3Part2 do + def run do + answer = + slope(1, 1) * + slope(3, 1) * + slope(5, 1) * + slope(7, 1) * + slope(1, 2) + + IO.puts(answer) + end + + def slope(v_x, v_y) do + {_, trees} = + File.read!("input") + |> String.trim() + |> String.split("\n") + |> Enum.take_every(v_y) + |> Enum.map(&String.to_charlist/1) + |> Enum.reduce({0, 0}, fn row, {x, trees} -> + trees = + case Stream.cycle(row) |> Enum.at(x) do + ?# -> trees + 1 + ?. -> trees + end + + {x + v_x, trees} + end) + + trees + end +end + +Day3Part2.run() diff --git a/day3/input b/day3/input new file mode 100644 index 0000000..9256a36 --- /dev/null +++ b/day3/input @@ -0,0 +1,323 @@ +...............#.#............. +##..#....................#...## +......#..#.#.....#..#.#.##..... +.........#...#..............#.# +............#.......##......... +...#.....#.....#...#.....#..#.. +..............#..##.#..#......# +.##.....#.....#......##.#...... +.#..........###....#...##....#. +.....#....#.#.......#......##.. +.#....#......#.......#........# +..#.#.......#..##.....##....... +...#.#....#.......#.......#...# +##.##...##..#......#.#.....#..# +.#.#.......#..#.#......#...#.#. +#.......##.......#...#......... +.....#......#.#.#.....#....##.. +.#.#........#....#..#..#....... +...#....#..###.........#.....#. +........#........#........#.... +..##..............#.....#.#..#. +.#...##.............#.#........ +....#..#...........#.......#... +..#....#.....................#. +#.#..................##......## +.#.##....#......#........#..... +.........##.....#....#...##..#. +#..........#..#.#.............# +.........#...#.#.#.#..##..##... +#...#.....#..#..#....#...#..... +..##.....#..................#.. +#..###.....#....#.......#..#... +...##.##..#............#......# +........###.........###......#. +#..##....#.........#.........#. +....#.....................#.... +#..#..##..#..####.##..#.....##. +..#...#.#....#....##.....#..... +...#.#.........#.....#.#....... +....#................#..#...##. +....#..#..........#...#.#.##... +........#..##............#....# +...#......##..........#.##...#. +.......##...................... +.......##..........#....#.#...# +......###.##..##..#....#...#..# +#.#...........##.....#........# +..#...........#..###....#.#.#.. +........#...........#......##.. +.........#...##.###...###..#... +.....#.....#..##.........##.... +...##..............#.....#...## +.##....#.......###.....#....... +.#...........##.............##. +......#..#..##.##......#......# +........###........#......#.#.. +#.#....#.....#........#......#. +.##..#.........##...##....#.... +.....#.........#...##.....#.... +.............#........###....#. +......#.......#.#........#.#... +..#....#.#...#....#...#.#...##. +#...#......##..##......#.##.### +...##.#....#...#....#.........# +...#..####.....##.#..#.#...##.. +##.#..#....##......#......##... +###.........#.#..#.#.....#..... +...#........#..##...#.#.#..#.#. +...###..#.###.#...#............ +....................###........ +...........#...........#....... +#..............#.#.........###. +....................##.....#..# +#.#.....#.......#...#.......... +.#...#......#....##...#...#.... +.....#.##..................###. +.........#.#..#.#......#....... +.......#.....##..#.##.#........ +..#..........#.###.....#....#.. +......#.............#.#........ +........##....#........#....... +...#.............#....#.#...... +#........#..####.....#.....#.#. +.##......##...#........#..#.#.. +....##....#...#...#..##...#.#.. +#.##...###..#....##.#.......... +....#.#...#.#...#..##.###...#.. +#.....##..#..#....#.#.....##... +.#..#..........##.#.....##..... +.#..#........#.#.#.#........... +.#..#.....#...........#...#.... +...#......##..........##..#.... +...#..#....#.##...#..#.....###. +#.#....#.....##................ +#..#......#.#.#.......#........ +......#....#.#....#..##....#..# +.#.....#.#....###.##.........#. +.###..#.....#........#.#....... +.#...#......#..#.#......#.....# +#...............####...#.....#. +.......#..........##.#........# +#........##....##.....###..##.. +#..#.....#..##.....#....#..#... +#.....#.......##......#.#.....# +#.##..#......##..#............. +##...#.....#........##......... +....#..##....#...#.......#.#... +....#...#...##..#....#..#...#.. +..............#.#...#....###... +...#....#..##...##..#....##.... +#.##.#..#..#......#.#.#.#...#.. +.......#..#..##........#......# +##.#....#....##.#......##.#.... +.#...#..............#........#. +.#.#....#.........#............ +.#..#..###.............#....#.. +#......#...#.#..##..#...#....#. +.......................#...#.#. +.............#..#...##......... +..#.#..#....#....#........#.... +#......#.##..#...#.#........... +.....#....#...........##.#..#.. +..#.#.....#..............#.#... +#.......#.....#................ +#..............#...#....#...#.. +...#...##..#..#............#... +......###.....................# +.........#.......##..#....#.... +........#...#.##..#.##......#.. +....###..#.#...#...#..#.#...### +##...#...##.#...#.#...#.#....#. +.........#...#.....###......... +...#........##..#.......##..... +.#.......##.........#.....##..# +.#..................#...#...... +.##..#..#.#.....#.###.......... +...#.....##..#.........#...#... +.#......#.#.......#.#.......... +.........#.#...#..........#.#.. +#..........#.##..#.##....#..... +.#.#....#.....#..##.....#...#.. +..#........##...##..#..#....#.. +#...........##....#..###....#.. +...........##.........####...#. +..#........###...#.#.........#. +.#...............#.##.#.#...#.. +.#.##..#.....#.#.....##..#..... +...#...#..#.##.##...#.......##. +..#...#...#......##.##.##...#.. +##....#...#...#...............# +...##...........#......#..#.#.. +#.........#......#.#.##.....#.. +........#..#.........##........ +..#.#....###.....##..#...#..... +.........#...#.......#.....##.. +##.....................#...##.. +.#.#..#......#................. +.....###..#......#..###..#..... +...#.....##.........#......#..# +......##.....#...#........#.#.. +..#.#...#......#...#.##.##..... +...#..........#...#.......#..## +.###........#........##........ +..#.#.#..........#.#...##...... +.........#........#......###..# +....##..#.........#...........# +..####..#............##.......# +.....##.#..##.........#...#.#.. +...#.........#.....#.....#..... +.......#...#..#...##.........#. +...#...#..#...#....#..#........ +#............##.##...#......... +.#.#.....#.......####.....#.... +..............#......#.#....... +..............#...........#...# +#...#........###....#.#....#.#. +##.#..#..#......#......#.#.#... +.#..#.....#..#.#..#.#.......##. +......##.#...#...#......#...#.. +#...........##....#.#.......... +....#.......###.#...#.......... +.......................#.....#. +........#...#..#...#.#.#.#.#... +.#.#...........#......##...#... +.........................#..... +.................#.##.#...##... +...#...##.....#.....##....#.#.. +...#...#...................#... +...#..#..#...#...#....#........ +#....#...#.....#............... +.......#...........#...#....... +....#....#.....##.......#...... +.......#..........##........... +.#.#........#..##....#......#.. +.....#.......#.#.........#...#. +.#..####.#.#...............#..# +.....###..#..#..........#.#..## +..#.......#...#.....##..#..#.#. +#....#......#.................. +........#.##.#....#...........# +....#.#....##..#.#.....##...... +...#..#.......#....#.....#.#.#. +#...#......#.....#.#..........# +....#....#...............#..... +..###......................###. +.##....#..#.......###.....#..#. +..###............#........#.##. +.#........#......#.....#..#.... +....#..##...#...#.###.......#.# +.......#.##...........#.#..#... +.....#...##.................... +....#....#...##......#......... +..#............##....###.#...#. +.#........#...............#.... +#..#.#.##.........#..##....##.. +#.#....#..#.##....##...#.#..... +.....#.....##....#.#........#.. +#..#...#...#....#....#......... +...#........#..#.#.....##...... +..#...#...#................##.. +#........#.#.##.......#.#...#.. +#......#..####.##.....#.#..#.#. +............#..#.#....#......## +..#.....##....#...#.#.......... +...#...#.........#...#.#....... +.###..#.......##.##.....#.#.#.. +...#....#...............##.#... +....##..#..#..#.#......##.....# +#.#..............##...##...#### +.....#.##...#.#...............# +.##.....#.........#.......#.#.# +#.#..#.....#.......#.......#..# +...#.#.....#.....#......#...... +.......#....#..#.#..........#.. +......#......#.##...#.......... +.....#.......###...#...#.#..... +#..#.#.........#.....#.##....#. +..#.#.........#..#..#..#.....#. +.#..##..#..#....#......#.##..#. +...##......###.....#.##.##..... +.#.....#...#..#...#............ +##..##..#.##....#..#........... +...#..##..#..#.............#.## +...............##............#. +..#.....##........##.#...#....# +.#.#...#.#.#..#.#.....#....#... +.#....#...............#..#..... +....#.##..#....#......#...###.. +#................###...#.#..... +...#...#......##..#.#....#..... +.#....#....#.#...##............ +....#...##..#..#........#.##... +..##.....#..#..##.............. +..#..##..#.#..##....#....#....# +...##.............#............ +#....#....#.#........#.....##.# +.....#..#.#.....####...###..... +................#......#....... +.....#.#.#.#.#....#..#........# +.##.#...#.#.......##....#....#. +.....#........#................ +..#.....#..#...#..#...........# +.#.....#...##.....##..#.#....## +......#.......#..#......##.#... +#.#..........#.##.#........#... +...#..#.............#.......... +#..#..#..........#..##.#....... +.#..#...............####..#.... +.......#.....#......#.....#.#.. +.#...............#...#......... +.#..#..........#..#.#..##..#..# +......##..#.....#..#......###.. +..........#...#..#.......#..... +.#.#.#..#.....#.##.#...#..#.... +........#.......#.....#.#...... +......#.....##.....#....##.#... +...............#......#.......# +..#.#...#.....#.#...##......#.. +#.#.........#.#...#........#### +#..........##..#..#........##.. +.............#..#.......##.#..# +..#........#.#....#........#.#. +.#......####..#..#............. +............###.......#.#..#... +#.##......##...#...#.........#. +....##.#.#.#......#....#..#...# +.#..#.#....#...#.........#..... +#...#.....##............#...#.. +#.#...#..#.................#... +............#.#..#.....#.#.#..# +...................#....#.##... +.....#...#.#....#....#.#......# +.......##.#.#......##.......... +.#..#...##.#...#..#......#..... +......#.#..#..###..##..##...... +.#.#.#.#.....#...###.....#..#.. +.#....#.....#.......#.......#.. +..........##.........####...... +.#.#.............#..#.#...#.... +........#........##...#.#....#. +........#...................... +..#.#....#...............#...## +.......#.#...#..#.....##......# +.#...#....#..........##........ +.#.........#.#............##... +.....#......##...#.......#..#.. +#.#..#.............#...#...#... +......#.......#............#... +...........##....#......##..... +.#.#..#.....................#.. +##..##.....###..##.#........... +...##......##....#...##.....#.. +#...#.##.............#......... +......#..#.........###.#......# +#.#.....#.....................# +....#####.....##........#.#..#. +...........##..##.###.......... +..........##.....#........#...# +.......#..#......#.....##..##.# +.....##.#........#.........#... +......##......................# +.#.......#.#.#............#..#. +.....##.#.......#.#........#...