AoC Day 1 – Fix Your Expense Report

This is a solution to Advent of Code 2020 day 1, written in Raku and Python.

https://adventofcode.com/2020/day/1

Part One

Before you leave, the Elves in accounting just need you to fix your expense report (your puzzle input); apparently, something isn't quite adding up. Specifically, they need you to find the two entries that sum to 2020 and then multiply those two numbers together.

This problem can be solved nicely using combinations and the reduce meta operator.

Raku

my @values = '1-input.txt'.IO.lines;

say 'Part One';
say [*] @values.combinations(2).grep({([+] $_) == 2020 }).head;
Part One
181044

Python

from itertools import combinations

input = map(lambda s: int(s), open('1-input.txt', 'r').read().splitlines())

combs = list(filter(lambda p: sum(p) == 2020, combinations(input, 2)))[0]

print('Part One')
print(combs[0] * combs[1])
Part One
181044

Part Two

In your expense report, what is the product of the three entries that sum to 2020?

The solution to part two is the same as part one, using combinations of 3 values instead of 2.

Raku

my @values = '1-input.txt'.IO.lines;

say 'Part Two';
say [*] @values.combinations(3).grep({([+] $_) == 2020 }).head;
Part Two
82660352

Python

from itertools import combinations
from math import prod

input = map(lambda s: int(s), open('1-input.txt', 'r').read().splitlines())

combs = list(filter(lambda p: sum(p) == 2020, combinations(input,3)))[0]

print('Part One')
print(prod(combs))
Part One
82660352
raku 
comments powered by Disqus