<commit_before>#!/usr/local/bin/python3

from functools import reduce
import operator
import pathlib

input_file = pathlib.Path(__file__).parent.parent.joinpath('day15_input.txt')


def parse_input(text):
    parsed = dict()
    for line in text.splitlines():
        ingredient, qualities = line.split(': ', maxsplit=1)
        parsed[ingredient] = dict()
        for qual_pair in qualities.split(', '):
            quality, magnitude = qual_pair.split()
            parsed[ingredient][quality] = int(magnitude)
    return parsed


def test_parse():
    sample_input = '''\
Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8
Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3'''
    expected = dict(
        Butterscotch=dict(
            capacity=-1, durability=-2, flavor=6, texture=3, calories=8),
        Cinnamon=dict(
            capacity=2, durability=3, flavor=-2, texture=-1, calories=3))
    assert parse_input(sample_input) == expected


def cookie_score(recipe, ingredient_dict, ignore_calories=True):
    recipe = filter(lambda t: t[1] > 0, recipe)
    qual_scores = []
    for ingredient, amount in recipe:
        ing_score = [
            amount * magnitude
            for quality, magnitude in ingredient_dict[ingredient].items()
            if quality != 'calories']
        qual_scores.append(ing_score)
    qual_totals = map(sum, zip(*qual_scores))
    # Set negative numbers to zero
    qual_totals = map(lambda x: x if x > 0 else 0, qual_totals)
    cookie_score = reduce(operator.mul, qual_totals)
    return cookie_score


def test_score():
    sample_data = dict(
        Butterscotch=dict(
            capacity=-1, durability=-2, flavor=6, texture=3, calories=8),
        Cinnamon=dict(
            capacity=2, durability=3, flavor=-2, texture=-1, calories=3))
    recipe = [('Butterscotch', 44), ('Cinnamon', 56)]
    assert cookie_score(recipe, sample_data) == 62842880


def test_score_zero():
    """Score is zero when any ingredients have negative total"""
    sample_data = dict(
        Butterscotch=dict(
            capacity=-1, durability=-2, flavor=6, texture=3, calories=8),
        Cinnamon=dict(
            capacity=2, durability=3, flavor=-2, texture=-1, calories=3))
    recipe = [('Butterscotch', 100)]
    assert cookie_score(recipe, sample_data) == 0
<commit_msg>Day 15: clean up scoring function a little<commit_after>#!/usr/local/bin/python3

from functools import reduce
import operator
import pathlib

input_file = pathlib.Path(__file__).parent.parent.joinpath('day15_input.txt')


def parse_input(text):
    parsed = dict()
    for line in text.splitlines():
        ingredient, qualities = line.split(': ', maxsplit=1)
        parsed[ingredient] = dict()
        for qual_pair in qualities.split(', '):
            quality, magnitude = qual_pair.split()
            parsed[ingredient][quality] = int(magnitude)
    return parsed


def test_parse():
    sample_input = '''\
Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8
Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3'''
    expected = dict(
        Butterscotch=dict(
            capacity=-1, durability=-2, flavor=6, texture=3, calories=8),
        Cinnamon=dict(
            capacity=2, durability=3, flavor=-2, texture=-1, calories=3))
    assert parse_input(sample_input) == expected


def cookie_score(recipe, ingredient_dict, ignore_calories=True):
    qual_scores = [
        [amount * magnitude
            for quality, magnitude in ingredient_dict[ingredient].items()
            if quality != 'calories']
        for ingredient, amount in recipe]

    qual_totals = map(sum, zip(*qual_scores))
    # Set negative numbers to zero
    qual_totals = map(lambda x: x if x > 0 else 0, qual_totals)
    cookie_score = reduce(operator.mul, qual_totals)
    return cookie_score


def test_score():
    sample_data = dict(
        Butterscotch=dict(
            capacity=-1, durability=-2, flavor=6, texture=3, calories=8),
        Cinnamon=dict(
            capacity=2, durability=3, flavor=-2, texture=-1, calories=3))
    recipe = [('Butterscotch', 44), ('Cinnamon', 56)]
    assert cookie_score(recipe, sample_data) == 62842880


def test_score_zero():
    """Score is zero when any ingredients have negative total"""
    sample_data = dict(
        Butterscotch=dict(
            capacity=-1, durability=-2, flavor=6, texture=3, calories=8),
        Cinnamon=dict(
            capacity=2, durability=3, flavor=-2, texture=-1, calories=3))
    recipe = [('Butterscotch', 100)]
    assert cookie_score(recipe, sample_data) == 0

{'fraction_non_alphanumeric': 0.1222990492653414, 'fraction_numerical': 0.027657735522904063, 'mean_word_length': 3.177797833935018, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3898042', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1415, 'n_words': 371}