From ac9c3551f97552d0c1d9c3c1667353f42a441ad1 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Sat, 19 Dec 2020 12:15:13 +0100 Subject: [PATCH] day11: solve part 1 & 2 --- day11/part1/input | 98 ++++++++++++++++++++++ day11/part1/main.d | 204 +++++++++++++++++++++++++++++++++++++++++++++ day11/part2/input | 98 ++++++++++++++++++++++ day11/part2/main.d | 148 ++++++++++++++++++++++++++++++++ 4 files changed, 548 insertions(+) create mode 100644 day11/part1/input create mode 100644 day11/part1/main.d create mode 100644 day11/part2/input create mode 100644 day11/part2/main.d diff --git a/day11/part1/input b/day11/part1/input new file mode 100644 index 0000000..ff36bab --- /dev/null +++ b/day11/part1/input @@ -0,0 +1,98 @@ +LLLLL.LLLLLLL.LLLLLL.L.LLLL..LLLL.LLLLLLLLL.LLLLLLLL.LLLLLLLLLLLLL.L.LLLLLLLLLLLLLL.LLLLLL +LLLLLLLLLLLLLLL.LLLL..LLLLLLLLLLLL.LLLLLLLL.L..LLLLLLLLLLLL.LLLL.LLL.LLLLL.LLL.LLLLLLLLLLL +LLLLL.LLLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLL.LLL.LLLLLLLL.LLLLL.LL.LLLLL.LLLLLLLL.LLLLLL +LLLLL.LLLLLLLLL.LLLL.LLLLLL.LLLLLL.LLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLL.LLL.LLLLLLLL.LLLLLL +LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLL.LLLLLL.LLLLLLLLL.LLLLLLLLLLLLL.LLLLLL +LLLLLLLLLLLLLLL.LLLL.LLLLLLLLLLLLLLLLLLLLLL.L.LLLLLL.LLLLLL..LLLLLL..LLLLLLLLLLLLLL.LLLLLL +.L..L.L.L.LL..LL.........L....L.L..LL.LLLL.L..L.LLLL.L.L..L.......LL...LL..LL.L..L.L.LL..L +LLL.L.LLL.LLLLL..LLL.LLLLLL.LL.L.L.LLLLL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LLLLLL..LLLLL..LLLLLLL.LLLLLLLL..LLLLL.LLLLLLLL.LLLLLLLLLLL.LL.LLLLLL +LLLLL.LL.LLL.LL..LLLL.LLLLL.LL.LL..LLL.LLLL.LLLLLLL..LLLLLLLL.LLLLLL.LLLL..LLLLLLLL.LLLLLL +LLLLL.LLLLLLLLLLLLLLLLLLLLL.LLLL.L.LLLLLLLL..LLLLLLLLLLLLLLLLLLLLLLL.LLLLL.LLLLLLLLLLLLLLL +LLLLL.LLLLLLLLL.LLLL.LLLLLL.LLLLLLL.LLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLL.LLLL..LLLLLLLL.LLLLLL +LLLLLLLLLLL.LLL.LLLL.LLLLLL.L.LLLLLLLLLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLLLLLLLLLL +LL...L.LLL.....L....LL.....L...L.L....L.L.....L.LL...L..L.....L.L..LLL.L.LL.LLLL.......... +LLLLL.LLLLLLLLL.LLLL.LLLLL.LLLLLLLLLLL.LLL..LLLL.LLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL +LLLLL.LLLLLLLLL.LLLL.LLLLLL..LLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLL.LLLLL..LLLLLLL.LLLLLL +LLLLLLLLLLLLLLL.LLL..LLLLLL.LLLLL.LL.LLLLLL.LLLLLL.LLLLLLLL.LLLLLLLLLLLLLL.LLLLLLLL.LLLLLL +LLLLL.LLL.LLLLL.LLLLLLLLLLLLLLLLLL.LLLLLLL..LLLLL.LLLLLLLLL.LLLLLLLL.LLLLL..LL.LLLL.LLLLLL +L..LL.L.....L...LL.....L.L........L..LL...L.....L....L..L.L...L...LL.......L.L....L....... +LLLLL..LLLLLLLL.LLLL.LLLLLL.LLLLLLLL.LLLL.LLLLLLL.LLLLLLLLLLLLLLLLLL..LLLL.LLLLLLLL.LLLLLL +LLLLLLLLLLLLLLL.LLLL.LLLLLL.LLLLLLLLLLLLLLL.LL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LLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLL..LLLLLL.LLLLLLLL.LLLLL.LLLLLLLL.LLLLLL +LLLL..LLLLLLLLL..LLL.LLLLLL.L.LLLLLL.LLLLLLLLLLLLLLLLLLLLLL.LLLLLLLL.L..LL.LLLLLLLL.LLLLLL +LLLLL.LLLLLLLLLLLLLL.LLLLLL.LLLLLL.LLLLLLLL.LLLLLLL..LLLLLLLLLLLLLLL.LLLLL.LLLLLLLL.LLLLLL +LLLLL.LLLLLLLLL.LLLLLLLLLLL.LLL.LLLLLLLLLLL.LLLLLLLL.LLLLLL.LLLLLLLL.LLLLLLLLLLLLL..LLLLLL +LLLLL.LLLLL.LLL..LLL.LLLLLLLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLL.LL +.L.......LL...L...L.LL.LL......L..L...L...L...L....LL..L...L..LLL.....L.....L.....LLL.LL.. +LLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLL.LLLLL..LLLLL.LL.LLLLL.LLLLLLL..L.LLLL +LLLLL.LLLLLLLLLLLLLLLLLLLLL.L.LLLLLLLLLLL.L.LLL.LLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLL +LLLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLL.LLLLLL.LLLLL.LLLLLLLL.LLLLLLLLLLLLLLL +LLLLL.LLLLLLLLL.LLLL.LLLLLL.LLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLLLLLLL.L.LLLLL.LL.LLLLL.LLLLLL +LLLL..LLLLLLLLL.LLLLLLLLLLL.LL.LLL.LLLLLLLL.LLLLLLLLLLLL.LL.LLLLLLLL.LLLLL.LLLLLLLL.LLLLLL +.......L......L....L....L......L...L.L..L...L.LL.....L......L.......LL.........L.L.L...LLL +L.LLL.LLLLLLLLL.LLLL.LLLLL..LLLLLL.LLLLLLL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LLL.LLLLLLLLLL.LLLLLLL.LLL.LLLLLLLLLL.L.LLLLLLLLLLLLLL.L.LLLL +...L.L.L.L.....LL...L...LL.L........L...LL.L...........L..L.L.L.L..L...LL.LL.L.L.L....L..L +LLL.L.LLLLLLLLL.LLLL.L.LLLLLLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLL..LLLL.LLLLL.LL.LLLLLL +LLLLL.LLLLLLLLL.LLLL.LLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLL.LLL.LLLLLLLLLL.LLLLLLLLLLLLLLL +LLLLL.LLLLLLLLL.LLLL.LLLLLL.LLLLLL.LLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLL.LLLL..LLLLLLLL.LLLLLL +LLLLL.LLLLLLLLLLLLLL.L.LLLL.LLLL.L.LLLLLLLL.LLLLLLLL.LLL.LL.LLLLLLLL.LLLLL.LLLLLLLL.LLLLLL +LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLL.LLLLL.LLLLLLLL.LLLLLL diff --git a/day11/part1/main.d b/day11/part1/main.d new file mode 100644 index 0000000..83068b6 --- /dev/null +++ b/day11/part1/main.d @@ -0,0 +1,204 @@ +import std; + +void main() +{ + File("input", "r").byLineCopy.array.calculateNumberOfOccupiedSeatsStable.writeln; +} + +auto calculateValidNeighbouringSeatPositions(size_t i, size_t j, size_t height, size_t width) +{ + return [ + tuple(i - 1, j - 1), tuple(i - 1, j), tuple(i - 1, j + 1), tuple(i, + j - 1), tuple(i, j + 1), tuple(i + 1, j - 1), tuple(i + 1, j), + tuple(i + 1, j + 1) + ].filter!(position => position[0] >= 0 && position[0] < height + && position[1] >= 0 && position[1] < width); +} + +auto calculateNewSeatState(const char seat, ulong numberOfOccupiedNeighbouringSeats) +{ + if (seat == '#' && numberOfOccupiedNeighbouringSeats >= 4) + { + return 'L'; + } + else if (seat == 'L' && numberOfOccupiedNeighbouringSeats == 0) + { + return '#'; + } + else + { + return seat; + } +} + +auto calculateNextSeatOccupation(const char[][] seats) +{ + if (seats.empty || seats.front.empty) + { + return seats; + } + immutable height = seats.length; + immutable width = seats.front.length; + + return seats.enumerate.map!((indexedRow) { + immutable i = indexedRow.index; + const row = indexedRow.value; + return row.byChar.enumerate.map!((indexedSeat) { + immutable j = indexedSeat.index; + immutable seat = indexedSeat.value; + immutable numberOfOccupiedNeighbouringSeats = calculateValidNeighbouringSeatPositions(i, + j, height, width).map!(position => seats[position[0]][position[1]]) + .filter!(it => it == '#') + .count; + return seat.calculateNewSeatState(numberOfOccupiedNeighbouringSeats); + }).array; + }).array; +} + +unittest +{ + auto input = `L.LL.LL.LL +LLLLLLL.LL +L.L.L..L.. +LLLL.LL.LL +L.LL.LL.LL +L.LLLLL.LL +..L.L..... +LLLLLLLLLL +L.LLLLLL.L +L.LLLLL.LL`.dup.split("\n"); + assert(input.calculateNextSeatOccupation.join("\n") == `#.##.##.## +#######.## +#.#.#..#.. +####.##.## +#.##.##.## +#.#####.## +..#.#..... +########## +#.######.# +#.#####.##`); +} + +unittest +{ + auto input = `#.##.##.## +#######.## +#.#.#..#.. +####.##.## +#.##.##.## +#.#####.## +..#.#..... +########## +#.######.# +#.#####.##`.dup.split("\n"); + assert(input.calculateNextSeatOccupation.join("\n") == `#.LL.L#.## +#LLLLLL.L# +L.L.L..L.. +#LLL.LL.L# +#.LL.LL.LL +#.LLLL#.## +..L.L..... +#LLLLLLLL# +#.LLLLLL.L +#.#LLLL.##`); +} + +unittest +{ + auto input = `#.LL.L#.## +#LLLLLL.L# +L.L.L..L.. +#LLL.LL.L# +#.LL.LL.LL +#.LLLL#.## +..L.L..... +#LLLLLLLL# +#.LLLLLL.L +#.#LLLL.##`.dup.split("\n"); + assert(input.calculateNextSeatOccupation.join("\n") == `#.##.L#.## +#L###LL.L# +L.#.#..#.. +#L##.##.L# +#.##.LL.LL +#.###L#.## +..#.#..... +#L######L# +#.LL###L.L +#.#L###.##`); +} + +unittest +{ + auto input = `#.##.L#.## +#L###LL.L# +L.#.#..#.. +#L##.##.L# +#.##.LL.LL +#.###L#.## +..#.#..... +#L######L# +#.LL###L.L +#.#L###.##`.dup.split("\n"); + assert(input.calculateNextSeatOccupation.join("\n") == `#.#L.L#.## +#LLL#LL.L# +L.L.L..#.. +#LLL.##.L# +#.LL.LL.LL +#.LL#L#.## +..L.L..... +#L#LLLL#L# +#.LLLLLL.L +#.#L#L#.##`); +} + +unittest +{ + auto input = `#.#L.L#.## +#LLL#LL.L# +L.L.L..#.. +#LLL.##.L# +#.LL.LL.LL +#.LL#L#.## +..L.L..... +#L#LLLL#L# +#.LLLLLL.L +#.#L#L#.##`.dup.split("\n"); + assert(input.calculateNextSeatOccupation.join("\n") == `#.#L.L#.## +#LLL#LL.L# +L.#.L..#.. +#L##.##.L# +#.#L.LL.LL +#.#L#L#.## +..L.L..... +#L#L##L#L# +#.LLLLLL.L +#.#L#L#.##`); +} + +auto calculateNumberOfOccupiedSeatsStable(const char[][] seats) +{ + return recurrence!((seats, n) => seats[n - 1].calculateNextSeatOccupation)(seats).slide(2) + .map!array + .find!((pair) => pair[0] == pair[1]) + .front + .front + .join + .filter!(seat => seat == '#') + .count; + +} + +unittest +{ + auto input = `L.LL.LL.LL +LLLLLLL.LL +L.L.L..L.. +LLLL.LL.LL +L.LL.LL.LL +L.LLLLL.LL +..L.L..... +LLLLLLLLLL +L.LLLLLL.L +L.LLLLL.LL`.dup.split("\n"); + assert(input.calculateNumberOfOccupiedSeatsStable == 37); +} diff --git a/day11/part2/input b/day11/part2/input new file mode 100644 index 0000000..ff36bab --- /dev/null +++ b/day11/part2/input @@ -0,0 +1,98 @@ +LLLLL.LLLLLLL.LLLLLL.L.LLLL..LLLL.LLLLLLLLL.LLLLLLLL.LLLLLLLLLLLLL.L.LLLLLLLLLLLLLL.LLLLLL +LLLLLLLLLLLLLLL.LLLL..LLLLLLLLLLLL.LLLLLLLL.L..LLLLLLLLLLLL.LLLL.LLL.L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LLLLLLLL.LLLLLL.LLLLLLLL.LL.LL..LLLLLLL.LLLLLL +LLLLL.LLLL..LLLLL.LL.LLLLLL.LLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLL.LLLLL.LLLL.LLL.LLLLLL +....L........L..L...........L.L............L.....LL..L.L.L.........LLLLLL.LL..L..L.L..L... +LL.LLLLLLLLLLLL.LLLLLLLLLLL.LLLLLL.LLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLLLLLL.L..LLLL..LLLLLL +LLLLL.LLLLLLLLL.LLLL.LLLLLLLLLLL.L.LLLLLLLL.LLLLLLLL.LLL.LL.LLLLLLLL..LLLL.LLLL.LLLLLLLLLL +LLLL.LLLLLLLLLL.LLLL.LLLLLL.LLLLLLLLLLLLL.L.LLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLL.LLLLLL +LLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLL.LLL.LLLLLLLL.LLLLLLLLLLL.LLL.LLLLL.LLLLLLL..LLLLLL +LLLLL.LLLLLLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLL..LL.LLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLL +LLLLL.LLLLLLLLLLLLLL.LLLLLL.LLLLLL..LLLLLLL.LLLLL.LL.LLLLLL.LLLLLLLL.LLLLLLLLLLLLLL.LLLLLL +LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL...LLLLLL.LLLL.L.LLLLL.LL.LLLLL.LLLLLLLL.L.LLLL +LLLLLLLLLLLLLLL.LLLL.L.LLLL.LLLLLL.LLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLLLLLL.L.LLLLLL.LLLLLL +L.LLL......LLL....L.......L..L..L.....LL......L.LLLL...L.....L.L............L.LL....LL.... +LLLLLLLLLLLLLLL.LLLLLLLLLLL.LLL.LL.LLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLL.LLLLL.LL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LLLL.LLLLLLL.LLLLL.LLLLLLLL.LLLLLL diff --git a/day11/part2/main.d b/day11/part2/main.d new file mode 100644 index 0000000..18b613b --- /dev/null +++ b/day11/part2/main.d @@ -0,0 +1,148 @@ +import std; + +void main() +{ + File("input", "r").byLineCopy.array.calculateNumberOfOccupiedSeatsStable.writeln; +} + +auto calculateNewSeatState(const char seat, ulong numberOfSeenOccupiedSeats) +{ + if (seat == '#' && numberOfSeenOccupiedSeats >= 5) + { + return 'L'; + } + else if (seat == 'L' && numberOfSeenOccupiedSeats == 0) + { + return '#'; + } + else + { + return seat; + } +} + +auto generateIndexes(ptrdiff_t posY, ptrdiff_t posX, ptrdiff_t y, ptrdiff_t x) +{ + return recurrence!((state, n) => tuple(state[n - 1][0] + y, state[n - 1][1] + x))( + tuple(posY, posX)).drop(1); +} + +auto isValidPosition(Tuple!(ptrdiff_t, ptrdiff_t) pos, size_t height, size_t width) +{ + return pos[0] >= 0 && pos[0] < height && pos[1] >= 0 && pos[1] < width; +} + +auto findFirstSeatInDirection(const char[][] seats, const Tuple!(ptrdiff_t, + ptrdiff_t) direction, const Tuple!(ptrdiff_t, + ptrdiff_t) initialPosition, const size_t height, const size_t width) +{ + return generateIndexes(initialPosition[0], initialPosition[1], direction[0], direction[1]).until!( + pos => !pos.isValidPosition(height, width)) + .map!(pos => seats[pos[0]][pos[1]]) + .find!(seat => seat != '.'); +} + +auto calculateNextSeatOccupation(const char[][] seats) +{ + if (seats.empty || seats.front.empty) + { + return seats; + } + auto height = seats.length; + auto width = seats.front.length; + + return seats.enumerate.map!((indexedRow) { + ptrdiff_t i = indexedRow.index; + auto row = indexedRow.value; + return row.byChar.enumerate.map!((indexedSeat) { + ptrdiff_t j = indexedSeat.index; + auto seat = indexedSeat.value; + auto numberOfSeenOccupiedSeats = [ + tuple(0L, 1L), tuple(-1L, 1L), tuple(-1L, 0L), tuple(-1L, + -1L), tuple(0L, -1L), tuple(1L, -1L), tuple(1L, 0L), tuple(1L, 1L) + ].map!(direction => findFirstSeatInDirection(seats, direction, + tuple(i, j), height, width)) + .filter!(lineOfSight => !lineOfSight.empty) + .map!(lineOfSeight => lineOfSeight.front) + .filter!(it => it == '#') + .count; + return seat.calculateNewSeatState(numberOfSeenOccupiedSeats); + }).array; + }).array; +} + +unittest +{ + auto input = `L.LL.LL.LL +LLLLLLL.LL +L.L.L..L.. +LLLL.LL.LL +L.LL.LL.LL +L.LLLLL.LL +..L.L..... +LLLLLLLLLL +L.LLLLLL.L +L.LLLLL.LL`.dup.split("\n"); + assert(input.calculateNextSeatOccupation.join("\n") == `#.##.##.## +#######.## +#.#.#..#.. +####.##.## +#.##.##.## +#.#####.## +..#.#..... +########## +#.######.# +#.#####.##`); +} + +unittest +{ + auto input = `#.##.##.## +#######.## +#.#.#..#.. +####.##.## +#.##.##.## +#.#####.## +..#.#..... +########## +#.######.# +#.#####.##`.dup.split("\n"); + assert(input.calculateNextSeatOccupation.join("\n") == `#.LL.LL.L# +#LLLLLL.LL +L.L.L..L.. +LLLL.LL.LL +L.LL.LL.LL +L.LLLLL.LL +..L.L..... +LLLLLLLLL# +#.LLLLLL.L +#.LLLLL.L#`); +} + +auto calculateNumberOfOccupiedSeatsStable(const char[][] seats) +{ + return recurrence!((seats, n) => seats[n - 1].calculateNextSeatOccupation)(seats).slide(2) + .map!array + .find!((pair) => pair[0] == pair[1]) + .front + .front + .join + .filter!(seat => seat == '#') + .count; + +} + +unittest +{ + auto input = `L.LL.LL.LL +LLLLLLL.LL +L.L.L..L.. +LLLL.LL.LL +L.LL.LL.LL +L.LLLLL.LL +..L.L..... +LLLLLLLLLL +L.LLLLLL.L +L.LLLLL.LL`.dup.split("\n"); + assert(input.calculateNumberOfOccupiedSeatsStable == 26); +}