b_van_b

joined 1 year ago
[–] b_van_b@programming.dev 10 points 1 month ago

I only know about the developers of Slay the Spire switching to Godot. Not the biggest name, but still well-known.

https://www.pcgamer.com/games/card-games/slay-the-spire-2-ditched-unity-for-open-source-engine-godot-after-2-years-of-development/

[–] b_van_b@programming.dev 4 points 2 months ago

I assume that left joystick up/down is axis 1, correct?

[–] b_van_b@programming.dev 4 points 4 months ago (2 children)

What would you suggest as an alternative?

[–] b_van_b@programming.dev 13 points 4 months ago (4 children)

I assume it's because many people outside the USA are accustomed to taking off their shoes when entering a house or apartment.

[–] b_van_b@programming.dev 1 points 5 months ago (1 children)

According to GitHub, development of DevToys predates it-tools by a year. If anything, I'd say they're both inspired by CyberChef.

[–] b_van_b@programming.dev 7 points 5 months ago (1 children)

The Godot engine GUI is also made in Godot.

[–] b_van_b@programming.dev 5 points 5 months ago (3 children)

What's the image? I just get an error message.

[–] b_van_b@programming.dev 1 points 5 months ago (1 children)

How big is the context window for the free version? I haven't found any information about it.

[–] b_van_b@programming.dev 3 points 6 months ago (4 children)

Is there anyone that you would recommend instead?

[–] b_van_b@programming.dev 2 points 11 months ago

Python 3

I'm trying to practice writing clear, commented, testable functions, so I added some things that are strictly unnecessary for the challenge (docstrings, error raising, type hints, tests...), but I think it's a necessary exercise for me. If anyone has comments or criticism about my attempt at "best practices," please let me know!

Also, I thought it was odd that the correct answer to part 2 requires that you allow for overlapping letters such as "threeight", but that doesn't occur in the sample input. I imagine that many people will hit a wall wondering why their answer is rejected.

day01.py

import re
from pathlib import Path


DIGITS = [
    "zero",
    "one",
    "two",
    "three",
    "four",
    "five",
    "six",
    "seven",
    "eight",
    "nine",
    r"\d",
]

PATTERN_PART_1 = r"\d"
PATTERN_PART_2 = f"(?=({'|'.join(DIGITS)}))"


def get_digit(s: str) -> int:
    """Return the digit in the input

    Args:
        s (str): one string containing a single digit represented by a single arabic numeral or spelled out in lower-case English

    Returns:
        int: the digit as an integer value
    """

    try:
        return int(s)
    except ValueError:
        return DIGITS.index(s)


def calibration_value(line: str, pattern: str) -> int:
    """Return the calibration value in the input

    Args:
        line (str): one line containing a calibration value
        pattern (str): the regular expression pattern to match

    Raises:
        ValueError: if no digits are found in the line

    Returns:
        int: the calibration value
    """

    digits = re.findall(pattern, line)

    if digits:
        return get_digit(digits[0]) * 10 + get_digit(digits[-1])

    raise ValueError(f"No digits found in: '{line}'")


def calibration_sum(lines: str, pattern: str) -> int:
    """Return the sum of the calibration values in the input

    Args:
        lines (str): one or more lines containing calibration values

    Returns:
        int: the sum of the calibration values
    """

    sum = 0

    for line in lines.split("\n"):
        sum += calibration_value(line, pattern)

    return sum


if __name__ == "__main__":
    path = Path(__file__).resolve().parent / "input" / "day01.txt"

    lines = path.read_text().strip()

    print("Sum of calibration values:")
    print(f"• Part 1: {calibration_sum(lines, PATTERN_PART_1)}")
    print(f"• Part 2: {calibration_sum(lines, PATTERN_PART_2)}")

test_day01.py

import pytest
from advent_2023_python.day01 import (
    calibration_value,
    calibration_sum,
    PATTERN_PART_1,
    PATTERN_PART_2,
)


LINES_PART_1 = [
    ("1abc2", 12),
    ("pqr3stu8vwx", 38),
    ("a1b2c3d4e5f", 15),
    ("treb7uchet", 77),
]
BLOCK_PART_1 = (
    "\n".join([line[0] for line in LINES_PART_1]),
    sum(line[1] for line in LINES_PART_1),
)

LINES_PART_2 = [
    ("two1nine", 29),
    ("eightwothree", 83),
    ("abcone2threexyz", 13),
    ("xtwone3four", 24),
    ("4nineeightseven2", 42),
    ("zoneight234", 14),
    ("7pqrstsixteen", 76),
]
BLOCK_PART_2 = (
    "\n".join([line[0] for line in LINES_PART_2]),
    sum(line[1] for line in LINES_PART_2),
)


def test_part_1():
    for line in LINES_PART_1:
        assert calibration_value(line[0], PATTERN_PART_1) == line[1]

    assert calibration_sum(BLOCK_PART_1[0], PATTERN_PART_1) == BLOCK_PART_1[1]


def test_part_2_with_part_1_values():
    for line in LINES_PART_1:
        assert calibration_value(line[0], PATTERN_PART_2) == line[1]

    assert calibration_sum(BLOCK_PART_1[0], PATTERN_PART_2) == BLOCK_PART_1[1]


def test_part_2_with_part_2_values():
    for line in LINES_PART_2:
        assert calibration_value(line[0], PATTERN_PART_2) == line[1]

    assert calibration_sum(BLOCK_PART_2[0], PATTERN_PART_2) == BLOCK_PART_2[1]


def test_no_digits():
    with pytest.raises(ValueError):
        calibration_value("abc", PATTERN_PART_1)

    with pytest.raises(ValueError):
        calibration_value("abc", PATTERN_PART_2)

view more: next ›