Skip to content

Instantly share code, notes, and snippets.

@GoldsteinE
Created December 5, 2021 12:33
Show Gist options
  • Save GoldsteinE/d0a324844871f7d70687bf932aaf2662 to your computer and use it in GitHub Desktop.
Save GoldsteinE/d0a324844871f7d70687bf932aaf2662 to your computer and use it in GitHub Desktop.
open Hashtbl
open Scanf
open Printf
open Stdlib
let table = Hashtbl.create 100000;;
let continue = ref true;;
while !continue do
try
let (x1, y1, x2, y2) = scanf "%d,%d -> %d,%d\n" (fun x1 y1 x2 y2 -> (x1, y1, x2, y2)) in (
if x1 == x2 then
for y = (min y1 y2) to (max y1 y2) do
match Hashtbl.find_opt table (x1, y) with
| Some count -> Hashtbl.add table (x1, y) (count + 1)
| None -> Hashtbl.add table (x1, y) 1
done
else if y1 == y2 then
for x = (min x1 x2) to (max x1 x2) do
match Hashtbl.find_opt table (x, y1) with
| Some count -> Hashtbl.add table (x, y1) (count + 1)
| None -> Hashtbl.add table (x, y1) 1
done
);
with
End_of_file -> continue := false;
done;;
let ans = Hashtbl.fold (fun (_, _) c a -> if c > 1 then a + 1 else a) table 0;;
Printf.printf "%d\n" ans;;
import sys
points = dict()
for line in sys.stdin:
p1, p2 = line.split(' -> ')
x1, y1 = map(int, p1.split(','))
x2, y2 = map(int, p2.split(','))
if x1 == x2:
for y in range(min(y1, y2), max(y1, y2) + 1):
points.setdefault((x1, y), 0)
points[x1, y] += 1
elif y1 == y2:
for x in range(min(x1, x2), max(x1, x2) + 1):
points.setdefault((x, y1), 0)
points[x, y1] += 1
counter = 0
for c in points.values():
if c > 1:
counter += 1
print(counter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment