Python Solution for 2021 Advent of Code - Day 1 - Sonar Sweep

preview_player
Показать описание
Solution to both parts of 2021's Advent of Code's Day 1's problem. You might have different data to deal with. However, hopefully the code still works the same for you as it does for me.

Bradley Sward is currently an Associate Professor at the College of DuPage in suburban Chicago, Illinois. He has earned a Masters degree in Software Engineering from DePaul University, a Masters degree in Computer Science from the University of Illinois at Springfield, and two Bachelors degrees in Computer Science and Molecular Biology from Benedictine University. Prior to teaching, Bradley worked for five years in the field of casino gaming on a variety of video slot machine and poker games. The Village People have been permanently etched into his brain.
Рекомендации по теме
Комментарии
Автор

Hi @Bradley,
thanks for your solutions to AoC.

For both parts, one could use generators:
Part 1:
increments = sum(1 for idx, val in enumerate(lst) if idx > 0 and lst[idx] > lst[idx - 1])

Part 2*:
increments = sum(1 for idx, val in enumerate(lst) if sum(lst[idx + 1 : idx + 4]) > sum(lst[idx : idx + 3]))

*While using "enumerate" on the initial list, one could ask for indexes that are not in the list and get a valid (although empty) result :)

doblerone