Day 4: Perilous Pier
Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465


Python
Didn’t really expect to reuse part one solution as is for part two, but it worked way better that I expected.
Is it good – maybe not; did the snake language solve it in tens of milliseconds – it most certainly did.
The code snippet only includes solution code, for the full repo (plumbing code) see the link in the header.
from collections.abc import Iterable from enum import StrEnum from common import Mode, read_file class Color(StrEnum): Missing = '?' Red = 'r' Orange = 'o' Yellow = 'y' Green = 'g' Blue = 'b' Violet = 'v' _barcode = list[Color] _barcodes = Iterable[_barcode] def part_1(barcodes: _barcodes) -> None: print(sum(_find_minimum(b) for b in barcodes)) def part_2(barcodes: _barcodes) -> None: print(_find_minimum([*b for b in barcodes])) def read(mode: Mode) -> _barcodes: for line in read_file(4, mode): if not line: continue yield [Color(c) for c in line] def solve(mode: Mode, second: bool) -> None: data = read(mode) if second: part_2(data) else: part_1(data) def _check_length(barcode: _barcode, length: int) -> bool: candidate = barcode[:length] start = 0 for step in range(len(barcode) // length): start = step * length for index_candidate in range(length): index_barcode = start + index_candidate if barcode[index_barcode] == Color.Missing: continue if candidate[index_candidate] == Color.Missing: candidate[index_candidate] = barcode[index_barcode] continue if candidate[index_candidate] != barcode[index_barcode]: return False start += length for index_candidate in range(len(barcode) % length): index_barcode = start + index_candidate if barcode[index_barcode] == Color.Missing: continue if candidate[index_candidate] == Color.Missing: candidate[index_candidate] = barcode[index_barcode] continue if candidate[index_candidate] != barcode[index_barcode]: return False return True def _find_minimum(barcode: _barcode) -> int: minimum_length = max(1, len({c for c in barcode if c != Color.Missing})) for length in range(minimum_length, len(barcode)): if _check_length(barcode, length): return length return len(barcode)