diff options
| author | nekineki <nekineki@nekineki.net> | 2022-12-11 07:32:01 +0100 |
|---|---|---|
| committer | nekineki <nekineki@nekineki.net> | 2022-12-11 07:32:35 +0100 |
| commit | a74d2dc54aef546664bcc8c81eb8e01a93e94391 (patch) | |
| tree | 2d019e2ce893315e50971c420c0aeb8a61758c1b /2022 | |
| parent | fe9cdd83b907a2ff9ce1fd6b1522808ca1015a4a (diff) | |
day11
Diffstat (limited to '2022')
| -rwxr-xr-x | 2022/day11/asd.py | 82 | ||||
| -rw-r--r-- | 2022/day11/pzl.txt | 55 | ||||
| -rw-r--r-- | 2022/day11/ref.txt | 27 |
3 files changed, 164 insertions, 0 deletions
diff --git a/2022/day11/asd.py b/2022/day11/asd.py new file mode 100755 index 0000000..f89179c --- /dev/null +++ b/2022/day11/asd.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python3 + +# import numpy as np +from functools import reduce +from re import findall +from copy import deepcopy +import sys + +# filename = "ref.txt" +filename = "pzl.txt" +data = open(filename).read() + +num_mon = len([None for _ in data.split('\n\n')]) + +mons = list() +itmss = list() +for m in data.split('\n\n'): + lines = [line for line in m.rstrip('\n').split('\n')] + mon = dict() + # mon['n'] = int(lines[0][-2]) + # mon['items'] = [int(i.strip()) for i in lines[1].split(':')[-1].split(',')] + itmss.append([int(i.strip()) for i in lines[1].split(':')[-1].split(',')]) + mon['op'] = lines[2].split('=')[-1].strip() + mon['test'] = int(lines[3].split(' ')[-1]) + mon['true'] = int(lines[4].split(' ')[-1]) + mon['false'] = int(lines[5].split(' ')[-1]) + mons.append(mon) + +# for mon,itms in zip(mons,itmss): +# print(mon) +# print(itms) + +def get_next_one_ape(n, old_timss, sums, div=0, mod=0): + new_itmss = [list() for _ in old_timss] + + for ape,itms in enumerate(old_timss): + for wl in itms: + if n == ape: + old = wl + wl = eval(mons[ape]['op']) + if div != 0: + wl = int(wl/div) + if mod != 0: + wl = wl % mod + sums[ape] += 1 + if wl % mons[ape]['test'] == 0: + new_itmss[mons[ape]['true']].append(wl) + else: + new_itmss[mons[ape]['false']].append(wl) + else: + new_itmss[ape].append(wl) + return new_itmss, sums + + +def get_next(itmss, sums, div=0, mod=0): + for ape,itms in enumerate(itmss): + itmss, sums = get_next_one_ape(ape, itmss, sums, div=div, mod=mod) + return itmss, sums + + +res1 = 0 +div1 = 3 +sums1 = [0 for _ in itmss] +itmss1 = itmss +for i in range(20): + itmss1, sums1 = get_next(itmss1, sums1, div=div1) +res1 = reduce(lambda x,y:x*y, sorted(sums1, reverse=True)[:2]) + + +res2 = 0 +mod2 = reduce(lambda x,y:x*y, map(lambda a:a['test'],mons)) +sums2 = [0 for _ in itmss] +itmss2 = itmss +for i in range(1, 10001): + itmss2, sums2 = get_next(itmss2, sums2, mod=mod2) + # if i in [1, 20, 100, 200, 300, 400, 500, 1000, 2000, 10000]: + # print(i, sums2) +res2 = reduce(lambda x,y:x*y, sorted(sums2, reverse=True)[:2]) + +print('res1:', res1) +print('res2:', res2) + diff --git a/2022/day11/pzl.txt b/2022/day11/pzl.txt new file mode 100644 index 0000000..f6d24aa --- /dev/null +++ b/2022/day11/pzl.txt @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 76, 88, 96, 97, 58, 61, 67 + Operation: new = old * 19 + Test: divisible by 3 + If true: throw to monkey 2 + If false: throw to monkey 3 + +Monkey 1: + Starting items: 93, 71, 79, 83, 69, 70, 94, 98 + Operation: new = old + 8 + Test: divisible by 11 + If true: throw to monkey 5 + If false: throw to monkey 6 + +Monkey 2: + Starting items: 50, 74, 67, 92, 61, 76 + Operation: new = old * 13 + Test: divisible by 19 + If true: throw to monkey 3 + If false: throw to monkey 1 + +Monkey 3: + Starting items: 76, 92 + Operation: new = old + 6 + Test: divisible by 5 + If true: throw to monkey 1 + If false: throw to monkey 6 + +Monkey 4: + Starting items: 74, 94, 55, 87, 62 + Operation: new = old + 5 + Test: divisible by 2 + If true: throw to monkey 2 + If false: throw to monkey 0 + +Monkey 5: + Starting items: 59, 62, 53, 62 + Operation: new = old * old + Test: divisible by 7 + If true: throw to monkey 4 + If false: throw to monkey 7 + +Monkey 6: + Starting items: 62 + Operation: new = old + 2 + Test: divisible by 17 + If true: throw to monkey 5 + If false: throw to monkey 7 + +Monkey 7: + Starting items: 85, 54, 53 + Operation: new = old + 3 + Test: divisible by 13 + If true: throw to monkey 4 + If false: throw to monkey 0 diff --git a/2022/day11/ref.txt b/2022/day11/ref.txt new file mode 100644 index 0000000..30e09e5 --- /dev/null +++ b/2022/day11/ref.txt @@ -0,0 +1,27 @@ +Monkey 0: + Starting items: 79, 98 + Operation: new = old * 19 + Test: divisible by 23 + If true: throw to monkey 2 + If false: throw to monkey 3 + +Monkey 1: + Starting items: 54, 65, 75, 74 + Operation: new = old + 6 + Test: divisible by 19 + If true: throw to monkey 2 + If false: throw to monkey 0 + +Monkey 2: + Starting items: 79, 60, 97 + Operation: new = old * old + Test: divisible by 13 + If true: throw to monkey 1 + If false: throw to monkey 3 + +Monkey 3: + Starting items: 74 + Operation: new = old + 3 + Test: divisible by 17 + If true: throw to monkey 0 + If false: throw to monkey 1 |
