Created
December 1, 2024 09:44
-
-
Save bjorn-einar-bjartnes-4ss/f7aabc229b90f08f48d04469cfd306e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
import builtins | |
import sys | |
from collections.abc import Callable | |
from typing import Any | |
from fable_modules.fable_library.array_ import map | |
from fable_modules.fable_library.int32 import parse | |
from fable_modules.fable_library.list import (of_array, FSharpList, sort, map as map_1, sum_by, zip, fold) | |
from fable_modules.fable_library.map import (of_list, try_find) | |
from fable_modules.fable_library.option import default_arg | |
from fable_modules.fable_library.seq2 import List_countBy | |
from fable_modules.fable_library.string_ import split | |
from fable_modules.fable_library.types import Array | |
from fable_modules.fable_library.util import (compare_primitives, number_hash) | |
from fable_modules.fable_python.stdlib.builtins_ import (TextIOWrapper, print) | |
def Input_readInit(file_path: str) -> FSharpList[tuple[int, int]]: | |
file: TextIOWrapper = builtins.open(file_path) | |
lines: Array[str] = split(file.read(), ["\n"], None, 0) | |
def mapping_1(strings: Array[str], file_path: Any=file_path) -> tuple[int, int]: | |
return (parse(strings[0], 511, False, 32), parse(strings[1], 511, False, 32)) | |
def mapping(line: str, file_path: Any=file_path) -> Array[str]: | |
return split(line, [" "], None, 0) | |
return of_array(map(mapping_1, map(mapping, lines, None), None)) | |
def Input_splitAndSortList(pair_list: FSharpList[tuple[int, int]]) -> tuple[FSharpList[int], FSharpList[int]]: | |
def mapping(tuple: tuple[int, int], pair_list: Any=pair_list) -> int: | |
return tuple[0] | |
class ObjectExpr89: | |
@property | |
def Compare(self) -> Callable[[int, int], int]: | |
return compare_primitives | |
def mapping_1(tuple_1: tuple[int, int], pair_list: Any=pair_list) -> int: | |
return tuple_1[1] | |
class ObjectExpr90: | |
@property | |
def Compare(self) -> Callable[[int, int], int]: | |
return compare_primitives | |
return (sort(map_1(mapping, pair_list), ObjectExpr89()), sort(map_1(mapping_1, pair_list), ObjectExpr90())) | |
def Input_findDistance(l1: FSharpList[int], l2: FSharpList[int]) -> int: | |
def projection(tupled_arg: tuple[int, int], l1: Any=l1, l2: Any=l2) -> int: | |
n1: int = tupled_arg[0] or 0 | |
n2: int = tupled_arg[1] or 0 | |
return abs(n1 - n2) | |
class ObjectExpr93: | |
@property | |
def GetZero(self) -> Callable[[], int]: | |
def _arrow91(__unit: None=None) -> int: | |
return 0 | |
return _arrow91 | |
@property | |
def Add(self) -> Callable[[int, int], int]: | |
def _arrow92(x: int, y: int) -> int: | |
return x + y | |
return _arrow92 | |
return sum_by(projection, zip(l1, l2), ObjectExpr93()) | |
def Input_countOccurrances(l1: FSharpList[int], l2: FSharpList[int]) -> FSharpList[tuple[int, int]]: | |
def projection(x: int, l1: Any=l1, l2: Any=l2) -> int: | |
return x | |
class ObjectExpr95: | |
@property | |
def Equals(self) -> Callable[[int, int], bool]: | |
def _arrow94(x_1: int, y: int) -> bool: | |
return x_1 == y | |
return _arrow94 | |
@property | |
def GetHashCode(self) -> Callable[[int], int]: | |
return number_hash | |
class ObjectExpr96: | |
@property | |
def Compare(self) -> Callable[[int, int], int]: | |
return compare_primitives | |
counts: Any = of_list(List_countBy(projection, l2, ObjectExpr95()), ObjectExpr96()) | |
def mapping(num1: int, l1: Any=l1, l2: Any=l2) -> tuple[int, int]: | |
return (num1, default_arg(try_find(num1, counts), 0)) | |
return map_1(mapping, l1) | |
def Input_productOfOccurances(counts: FSharpList[tuple[int, int]]) -> int: | |
def folder(acc: int, tupled_arg: tuple[int, int], counts: Any=counts) -> int: | |
num: int = tupled_arg[0] or 0 | |
count: int = tupled_arg[1] or 0 | |
return acc + (num * count) | |
return fold(folder, 0, counts) | |
def Program_main(_arg: Array[str]) -> int: | |
def _arrow97(__unit: None=None, _arg: Any=_arg) -> FSharpList[tuple[int, int]]: | |
tupled_arg: tuple[FSharpList[int], FSharpList[int]] = Input_splitAndSortList(Input_readInit("input2.txt")) | |
return Input_countOccurrances(tupled_arg[0], tupled_arg[1]) | |
print(("Answer from python is %d", Input_productOfOccurances(_arrow97()))) | |
return 0 | |
if __name__ == "__main__": | |
Program_main(sys.argv[1:]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment