From 0a09b8dab09bb2dc52aded0793922f4a0456fdfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Grygierzec?= Date: Sat, 13 Mar 2021 12:15:33 +0000 Subject: [PATCH 1/6] Init day09 --- 2020/README.md | 14 ++++++++++++++ 2020/day09/.gitignore | 1 + 2020/day09/build.sh | 1 + 2020/day09/day09.chpl | 5 +++++ 2020/day09/sample.txt | 20 ++++++++++++++++++++ 5 files changed, 41 insertions(+) create mode 100644 2020/day09/.gitignore create mode 100755 2020/day09/build.sh create mode 100644 2020/day09/day09.chpl create mode 100644 2020/day09/sample.txt diff --git a/2020/README.md b/2020/README.md index 24f21f2..f75ce6c 100644 --- a/2020/README.md +++ b/2020/README.md @@ -185,3 +185,17 @@ Inside after changes: ### Time `bash -c 'time sbcl --script day08.lisp'` + +## Day 09 - Chapel + +### Build + +`chpl -o day09 day09.chpl` + +### Run + +`./day09` + +### Time + +`bash -c 'time ./day09'` diff --git a/2020/day09/.gitignore b/2020/day09/.gitignore new file mode 100644 index 0000000..768f546 --- /dev/null +++ b/2020/day09/.gitignore @@ -0,0 +1 @@ +day09 diff --git a/2020/day09/build.sh b/2020/day09/build.sh new file mode 100755 index 0000000..449bb07 --- /dev/null +++ b/2020/day09/build.sh @@ -0,0 +1 @@ +chpl -o day09 day09.chpl diff --git a/2020/day09/day09.chpl b/2020/day09/day09.chpl new file mode 100644 index 0000000..1576394 --- /dev/null +++ b/2020/day09/day09.chpl @@ -0,0 +1,5 @@ +module Day09 { + proc main() { + writeln("Hello, world!"); + } +} diff --git a/2020/day09/sample.txt b/2020/day09/sample.txt new file mode 100644 index 0000000..28d66e4 --- /dev/null +++ b/2020/day09/sample.txt @@ -0,0 +1,20 @@ +35 +20 +15 +25 +47 +40 +62 +55 +65 +95 +102 +117 +150 +182 +127 +219 +299 +277 +309 +576 From 1c6a254700b22d005061ecaad38b168ef4a21f04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Grygierzec?= Date: Sat, 13 Mar 2021 12:26:03 +0000 Subject: [PATCH 2/6] Read whether to run sample or input --- 2020/day09/day09.chpl | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/2020/day09/day09.chpl b/2020/day09/day09.chpl index 1576394..e013fcd 100644 --- a/2020/day09/day09.chpl +++ b/2020/day09/day09.chpl @@ -1,5 +1,21 @@ +// TODO config param to run sample or not +// TODO read file to list +// TODO change list to array? +// TODO solveA +// TODO solveB + + module Day09 { + config const sample: bool = false; + const fileName: string = if sample then "sample.txt" else "input.txt"; + proc main() { - writeln("Hello, world!"); + writeln("Solving Day09A..."); + const resultA = solveA(); + writeln(resultA); + } + + proc solveA(): int { + return 0; } } From 65b2c87dbb444a1d7774ee8d3a5cafb9cf99adea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Grygierzec?= Date: Sat, 13 Mar 2021 12:54:49 +0000 Subject: [PATCH 3/6] Read input file --- 2020/day09/day09.chpl | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/2020/day09/day09.chpl b/2020/day09/day09.chpl index e013fcd..0a94aa6 100644 --- a/2020/day09/day09.chpl +++ b/2020/day09/day09.chpl @@ -1,4 +1,3 @@ -// TODO config param to run sample or not // TODO read file to list // TODO change list to array? // TODO solveA @@ -6,16 +5,31 @@ module Day09 { + private use IO; + private use List; + config const sample: bool = false; const fileName: string = if sample then "sample.txt" else "input.txt"; - proc main() { + proc main() throws { + const input: list(int) = readInput(fileName); writeln("Solving Day09A..."); - const resultA = solveA(); + const resultA = solveA(input); writeln(resultA); } - proc solveA(): int { + proc solveA(input: list(int)): int { + writeln(input); return 0; } + + proc readInput(fname: string): list(int) throws { + var input: list(int); + var f = open(fname, iomode.r); + for line in f.lines() { + input.append(line:int); + } + f.close(); + return input; + } } From 999db5a9e0d63cd4d5075f07e19f632e710000e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Grygierzec?= Date: Sat, 13 Mar 2021 14:54:37 +0000 Subject: [PATCH 4/6] Day09A done --- 2020/day09/day09.chpl | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/2020/day09/day09.chpl b/2020/day09/day09.chpl index 0a94aa6..1f1c5ad 100644 --- a/2020/day09/day09.chpl +++ b/2020/day09/day09.chpl @@ -1,8 +1,7 @@ -// TODO read file to list -// TODO change list to array? // TODO solveA // TODO solveB - +// TODO change list to array? +// TODO make it parallel? module Day09 { private use IO; @@ -10,6 +9,7 @@ module Day09 { config const sample: bool = false; const fileName: string = if sample then "sample.txt" else "input.txt"; + const preambleSize: int = if sample then 5 else 25; proc main() throws { const input: list(int) = readInput(fileName); @@ -19,8 +19,22 @@ module Day09 { } proc solveA(input: list(int)): int { - writeln(input); - return 0; + for i in preambleSize .. input.size-1 { + const current = input[i]; + const preamble: domain(int) = input[i-preambleSize .. #preambleSize]; + //writeln(preamble); + //writeln("i ", i, " ", input[i]); + var valid: bool = false; + for p in preamble { + //writeln("p ", p); + if preamble.contains(current - p) && 2*p != current { + valid = true; + break; + } + } + if !valid then return current; + } + return -1; } proc readInput(fname: string): list(int) throws { From d8358200b9e095029c0d7c7c0e921415f8ed9fac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Grygierzec?= Date: Sat, 13 Mar 2021 15:36:43 +0000 Subject: [PATCH 5/6] Day09B done --- 2020/day09/day09.chpl | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/2020/day09/day09.chpl b/2020/day09/day09.chpl index 1f1c5ad..866af1b 100644 --- a/2020/day09/day09.chpl +++ b/2020/day09/day09.chpl @@ -1,5 +1,3 @@ -// TODO solveA -// TODO solveB // TODO change list to array? // TODO make it parallel? @@ -13,9 +11,16 @@ module Day09 { proc main() throws { const input: list(int) = readInput(fileName); + writeln("Solving Day09A..."); + // 15353384 const resultA = solveA(input); writeln(resultA); + + writeln("Solving Day09B..."); + // 2466556 + const resultB = solveB(input, resultA); + writeln(resultB); } proc solveA(input: list(int)): int { @@ -37,6 +42,28 @@ module Day09 { return -1; } + proc solveB(input: list(int), n: int): int { + for i in 0 .. #(input.size - 1) { + //writeln(i, " ", input[i]); + var currNum: int = input[i]; + var contSum: int = currNum; + var contSumList: list(int); + contSumList.append(currNum); + for sumIndex in i+1 .. input.size-1 { + if contSum >= n then break; + currNum = input[sumIndex]; + contSum += currNum; + contSumList.append(currNum); + } + //writeln(contSumList); + if contSum == n && contSumList.size >= 2 { + contSumList.sort(); + return contSumList.first() + contSumList.last(); + } + } + return -1; + } + proc readInput(fname: string): list(int) throws { var input: list(int); var f = open(fname, iomode.r); From 1c189d01120d2e1fcc74ecf4350c36f1af4bf739 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Grygierzec?= Date: Sun, 14 Mar 2021 10:39:08 +0000 Subject: [PATCH 6/6] Day09 parallel Unfortunately without shortcircuiting computation once the solution is found. --- 2020/day09/day09.chpl | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/2020/day09/day09.chpl b/2020/day09/day09.chpl index 866af1b..217ecd3 100644 --- a/2020/day09/day09.chpl +++ b/2020/day09/day09.chpl @@ -1,5 +1,5 @@ // TODO change list to array? -// TODO make it parallel? +// TODO make it parallel? using forall module Day09 { private use IO; @@ -24,7 +24,8 @@ module Day09 { } proc solveA(input: list(int)): int { - for i in preambleSize .. input.size-1 { + var result$: single int; + forall i in preambleSize .. input.size-1 { const current = input[i]; const preamble: domain(int) = input[i-preambleSize .. #preambleSize]; //writeln(preamble); @@ -37,13 +38,16 @@ module Day09 { break; } } - if !valid then return current; + if !valid { + result$ = current; + } } - return -1; + return result$; } proc solveB(input: list(int), n: int): int { - for i in 0 .. #(input.size - 1) { + var result$: single int; + forall i in 0 .. #(input.size - 1) { //writeln(i, " ", input[i]); var currNum: int = input[i]; var contSum: int = currNum; @@ -58,14 +62,14 @@ module Day09 { //writeln(contSumList); if contSum == n && contSumList.size >= 2 { contSumList.sort(); - return contSumList.first() + contSumList.last(); + result$ = contSumList.first() + contSumList.last(); } } - return -1; + return result$; } proc readInput(fname: string): list(int) throws { - var input: list(int); + var input: list(int, parSafe=true); var f = open(fname, iomode.r); for line in f.lines() { input.append(line:int);